/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/git/interrepo.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-21 12:41:27 UTC
  • mto: This revision was merged to the branch mainline in revision 6623.
  • Revision ID: jelmer@jelmer.uk-20170521124127-iv8etg0vwymyai6y
s/bzr/brz/ in apport config.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009-2018 Jelmer Vernooij <jelmer@jelmer.uk>
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 2 of the License, or
6
 
# (at your option) any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
 
 
17
 
"""InterRepository operations."""
18
 
 
19
 
from io import BytesIO
20
 
import itertools
21
 
 
22
 
from dulwich.errors import (
23
 
    NotCommitError,
24
 
    )
25
 
from dulwich.object_store import (
26
 
    ObjectStoreGraphWalker,
27
 
    )
28
 
from dulwich.protocol import (
29
 
    CAPABILITY_THIN_PACK,
30
 
    ZERO_SHA,
31
 
    )
32
 
from dulwich.refs import (
33
 
    ANNOTATED_TAG_SUFFIX,
34
 
    SYMREF,
35
 
    )
36
 
from dulwich.walk import Walker
37
 
 
38
 
from ..errors import (
39
 
    DivergedBranches,
40
 
    FetchLimitUnsupported,
41
 
    InvalidRevisionId,
42
 
    LossyPushToSameVCS,
43
 
    NoRoundtrippingSupport,
44
 
    NoSuchRevision,
45
 
    )
46
 
from ..repository import (
47
 
    FetchResult,
48
 
    InterRepository,
49
 
    )
50
 
from ..revision import (
51
 
    NULL_REVISION,
52
 
    )
53
 
from .. import (
54
 
    config,
55
 
    trace,
56
 
    ui,
57
 
    )
58
 
 
59
 
from .errors import (
60
 
    NoPushSupport,
61
 
    )
62
 
from .fetch import (
63
 
    import_git_objects,
64
 
    DetermineWantsRecorder,
65
 
    )
66
 
from .mapping import (
67
 
    needs_roundtripping,
68
 
    )
69
 
from .object_store import (
70
 
    get_object_store,
71
 
    )
72
 
from .push import (
73
 
    MissingObjectsIterator,
74
 
    remote_divergence,
75
 
    )
76
 
from .refs import (
77
 
    is_tag,
78
 
    )
79
 
from .repository import (
80
 
    GitRepository,
81
 
    LocalGitRepository,
82
 
    GitRepositoryFormat,
83
 
    )
84
 
from .remote import (
85
 
    RemoteGitRepository,
86
 
    )
87
 
from .unpeel_map import (
88
 
    UnpeelMap,
89
 
    )
90
 
 
91
 
 
92
 
class InterToGitRepository(InterRepository):
93
 
    """InterRepository that copies into a Git repository."""
94
 
 
95
 
    _matching_repo_format = GitRepositoryFormat()
96
 
 
97
 
    def __init__(self, source, target):
98
 
        super(InterToGitRepository, self).__init__(source, target)
99
 
        self.mapping = self.target.get_mapping()
100
 
        self.source_store = get_object_store(self.source, self.mapping)
101
 
 
102
 
    @staticmethod
103
 
    def _get_repo_format_to_test():
104
 
        return None
105
 
 
106
 
    def copy_content(self, revision_id=None, pb=None):
107
 
        """See InterRepository.copy_content."""
108
 
        self.fetch(revision_id, pb, find_ghosts=False)
109
 
 
110
 
    def fetch_refs(self, update_refs, lossy, overwrite=False):
111
 
        """Fetch possibly roundtripped revisions into the target repository
112
 
        and update refs.
113
 
 
114
 
        :param update_refs: Generate refs to fetch. Receives dictionary
115
 
            with old refs (git shas), returns dictionary of new names to
116
 
            git shas.
117
 
        :param lossy: Whether to roundtrip
118
 
        :return: old refs, new refs
119
 
        """
120
 
        raise NotImplementedError(self.fetch_refs)
121
 
 
122
 
    def search_missing_revision_ids(self,
123
 
                                    find_ghosts=True, revision_ids=None,
124
 
                                    if_present_ids=None, limit=None):
125
 
        if limit is not None:
126
 
            raise FetchLimitUnsupported(self)
127
 
        git_shas = []
128
 
        todo = []
129
 
        if revision_ids:
130
 
            todo.extend(revision_ids)
131
 
        if if_present_ids:
132
 
            todo.extend(revision_ids)
133
 
        with self.source_store.lock_read():
134
 
            for revid in revision_ids:
135
 
                if revid == NULL_REVISION:
136
 
                    continue
137
 
                try:
138
 
                    git_sha = self.source_store._lookup_revision_sha1(revid)
139
 
                except KeyError:
140
 
                    raise NoSuchRevision(revid, self.source)
141
 
                git_shas.append(git_sha)
142
 
            walker = Walker(
143
 
                self.source_store,
144
 
                include=git_shas,
145
 
                exclude=[
146
 
                    sha for sha in self.target.controldir.get_refs_container().as_dict().values()
147
 
                    if sha != ZERO_SHA])
148
 
            missing_revids = set()
149
 
            for entry in walker:
150
 
                for (kind, type_data) in self.source_store.lookup_git_sha(
151
 
                        entry.commit.id):
152
 
                    if kind == "commit":
153
 
                        missing_revids.add(type_data[0])
154
 
            return self.source.revision_ids_to_search_result(missing_revids)
155
 
 
156
 
    def _warn_slow(self):
157
 
        if not config.GlobalConfig().suppress_warning('slow_intervcs_push'):
158
 
            trace.warning(
159
 
                'Pushing from a Bazaar to a Git repository. '
160
 
                'For better performance, push into a Bazaar repository.')
161
 
 
162
 
 
163
 
class InterToLocalGitRepository(InterToGitRepository):
164
 
    """InterBranch implementation between a Bazaar and a Git repository."""
165
 
 
166
 
    def __init__(self, source, target):
167
 
        super(InterToLocalGitRepository, self).__init__(source, target)
168
 
        self.target_store = self.target.controldir._git.object_store
169
 
        self.target_refs = self.target.controldir._git.refs
170
 
 
171
 
    def _commit_needs_fetching(self, sha_id):
172
 
        try:
173
 
            return (sha_id not in self.target_store)
174
 
        except NoSuchRevision:
175
 
            # Ghost, can't push
176
 
            return False
177
 
 
178
 
    def _revision_needs_fetching(self, sha_id, revid):
179
 
        if revid == NULL_REVISION:
180
 
            return False
181
 
        if sha_id is None:
182
 
            try:
183
 
                sha_id = self.source_store._lookup_revision_sha1(revid)
184
 
            except KeyError:
185
 
                return False
186
 
        return self._commit_needs_fetching(sha_id)
187
 
 
188
 
    def missing_revisions(self, stop_revisions):
189
 
        """Find the revisions that are missing from the target repository.
190
 
 
191
 
        :param stop_revisions: Revisions to check for (tuples with
192
 
            Git SHA1, bzr revid)
193
 
        :return: sequence of missing revisions, in topological order
194
 
        :raise: NoSuchRevision if the stop_revisions are not present in
195
 
            the source
196
 
        """
197
 
        revid_sha_map = {}
198
 
        stop_revids = []
199
 
        for (sha1, revid) in stop_revisions:
200
 
            if sha1 is not None and revid is not None:
201
 
                revid_sha_map[revid] = sha1
202
 
                stop_revids.append(revid)
203
 
            elif sha1 is not None:
204
 
                if self._commit_needs_fetching(sha1):
205
 
                    for (kind, (revid, tree_sha, verifiers)) in self.source_store.lookup_git_sha(sha1):
206
 
                        revid_sha_map[revid] = sha1
207
 
                        stop_revids.append(revid)
208
 
            else:
209
 
                if revid is None:
210
 
                    raise AssertionError
211
 
                stop_revids.append(revid)
212
 
        missing = set()
213
 
        graph = self.source.get_graph()
214
 
        with ui.ui_factory.nested_progress_bar() as pb:
215
 
            while stop_revids:
216
 
                new_stop_revids = []
217
 
                for revid in stop_revids:
218
 
                    sha1 = revid_sha_map.get(revid)
219
 
                    if (revid not in missing and
220
 
                            self._revision_needs_fetching(sha1, revid)):
221
 
                        missing.add(revid)
222
 
                        new_stop_revids.append(revid)
223
 
                stop_revids = set()
224
 
                parent_map = graph.get_parent_map(new_stop_revids)
225
 
                for parent_revids in parent_map.values():
226
 
                    stop_revids.update(parent_revids)
227
 
                pb.update("determining revisions to fetch", len(missing))
228
 
        return graph.iter_topo_order(missing)
229
 
 
230
 
    def _get_target_bzr_refs(self):
231
 
        """Return a dictionary with references.
232
 
 
233
 
        :return: Dictionary with reference names as keys and tuples
234
 
            with Git SHA, Bazaar revid as values.
235
 
        """
236
 
        bzr_refs = {}
237
 
        for k in self.target._git.refs.allkeys():
238
 
            try:
239
 
                v = self.target._git.refs.read_ref(k)
240
 
            except KeyError:
241
 
                # broken symref?
242
 
                continue
243
 
            revid = None
244
 
            if not v.startswith(SYMREF):
245
 
                try:
246
 
                    for (kind, type_data) in self.source_store.lookup_git_sha(
247
 
                            v):
248
 
                        if kind == "commit" and self.source.has_revision(
249
 
                                type_data[0]):
250
 
                            revid = type_data[0]
251
 
                            break
252
 
                except KeyError:
253
 
                    pass
254
 
            bzr_refs[k] = (v, revid)
255
 
        return bzr_refs
256
 
 
257
 
    def fetch_refs(self, update_refs, lossy, overwrite=False):
258
 
        self._warn_slow()
259
 
        result_refs = {}
260
 
        with self.source_store.lock_read():
261
 
            old_refs = self._get_target_bzr_refs()
262
 
            new_refs = update_refs(old_refs)
263
 
            revidmap = self.fetch_objects(
264
 
                [(git_sha, bzr_revid)
265
 
                 for (git_sha, bzr_revid) in new_refs.values()
266
 
                 if git_sha is None or not git_sha.startswith(SYMREF)],
267
 
                lossy=lossy)
268
 
            for name, (gitid, revid) in new_refs.items():
269
 
                if gitid is None:
270
 
                    try:
271
 
                        gitid = revidmap[revid][0]
272
 
                    except KeyError:
273
 
                        gitid = self.source_store._lookup_revision_sha1(revid)
274
 
                if gitid.startswith(SYMREF):
275
 
                    self.target_refs.set_symbolic_ref(
276
 
                        name, gitid[len(SYMREF):])
277
 
                else:
278
 
                    try:
279
 
                        old_git_id = old_refs[name][0]
280
 
                    except KeyError:
281
 
                        self.target_refs.add_if_new(name, gitid)
282
 
                    else:
283
 
                        self.target_refs.set_if_equals(name, old_git_id, gitid)
284
 
                    result_refs[name] = (gitid, revid if not lossy else self.mapping.revision_id_foreign_to_bzr(gitid))
285
 
        return revidmap, old_refs, result_refs
286
 
 
287
 
    def fetch_objects(self, revs, lossy, limit=None):
288
 
        if not lossy and not self.mapping.roundtripping:
289
 
            for git_sha, bzr_revid in revs:
290
 
                if (bzr_revid is not None and
291
 
                        needs_roundtripping(self.source, bzr_revid)):
292
 
                    raise NoPushSupport(self.source, self.target, self.mapping,
293
 
                                        bzr_revid)
294
 
        with self.source_store.lock_read():
295
 
            todo = list(self.missing_revisions(revs))[:limit]
296
 
            revidmap = {}
297
 
            with ui.ui_factory.nested_progress_bar() as pb:
298
 
                object_generator = MissingObjectsIterator(
299
 
                    self.source_store, self.source, pb)
300
 
                for (old_revid, git_sha) in object_generator.import_revisions(
301
 
                        todo, lossy=lossy):
302
 
                    if lossy:
303
 
                        new_revid = self.mapping.revision_id_foreign_to_bzr(
304
 
                            git_sha)
305
 
                    else:
306
 
                        new_revid = old_revid
307
 
                        try:
308
 
                            self.mapping.revision_id_bzr_to_foreign(old_revid)
309
 
                        except InvalidRevisionId:
310
 
                            refname = self.mapping.revid_as_refname(old_revid)
311
 
                            self.target_refs[refname] = git_sha
312
 
                    revidmap[old_revid] = (git_sha, new_revid)
313
 
                self.target_store.add_objects(object_generator)
314
 
                return revidmap
315
 
 
316
 
    def fetch(self, revision_id=None, pb=None, find_ghosts=False,
317
 
              fetch_spec=None, mapped_refs=None, lossy=False):
318
 
        if mapped_refs is not None:
319
 
            stop_revisions = mapped_refs
320
 
        elif revision_id is not None:
321
 
            stop_revisions = [(None, revision_id)]
322
 
        elif fetch_spec is not None:
323
 
            recipe = fetch_spec.get_recipe()
324
 
            if recipe[0] in ("search", "proxy-search"):
325
 
                stop_revisions = [(None, revid) for revid in recipe[1]]
326
 
            else:
327
 
                raise AssertionError(
328
 
                    "Unsupported search result type %s" % recipe[0])
329
 
        else:
330
 
            stop_revisions = [(None, revid)
331
 
                              for revid in self.source.all_revision_ids()]
332
 
        self._warn_slow()
333
 
        try:
334
 
            revidmap = self.fetch_objects(stop_revisions, lossy=lossy)
335
 
        except NoPushSupport:
336
 
            raise NoRoundtrippingSupport(self.source, self.target)
337
 
        return FetchResult(revidmap)
338
 
 
339
 
    @staticmethod
340
 
    def is_compatible(source, target):
341
 
        """Be compatible with GitRepository."""
342
 
        return (not isinstance(source, GitRepository) and
343
 
                isinstance(target, LocalGitRepository))
344
 
 
345
 
 
346
 
class InterToRemoteGitRepository(InterToGitRepository):
347
 
 
348
 
    def fetch_refs(self, update_refs, lossy, overwrite=False):
349
 
        """Import the gist of the ancestry of a particular revision."""
350
 
        if not lossy and not self.mapping.roundtripping:
351
 
            raise NoPushSupport(self.source, self.target, self.mapping)
352
 
        unpeel_map = UnpeelMap.from_repository(self.source)
353
 
        revidmap = {}
354
 
 
355
 
        def git_update_refs(old_refs):
356
 
            ret = {}
357
 
            self.old_refs = {
358
 
                k: (v, None) for (k, v) in old_refs.items()}
359
 
            new_refs = update_refs(self.old_refs)
360
 
            for name, (gitid, revid) in new_refs.items():
361
 
                if gitid is None:
362
 
                    git_sha = self.source_store._lookup_revision_sha1(revid)
363
 
                    gitid = unpeel_map.re_unpeel_tag(
364
 
                        git_sha, old_refs.get(name))
365
 
                if not overwrite:
366
 
                    if remote_divergence(
367
 
                            old_refs.get(name), gitid, self.source_store):
368
 
                        raise DivergedBranches(self.source, self.target)
369
 
                ret[name] = gitid
370
 
            return ret
371
 
        self._warn_slow()
372
 
        with self.source_store.lock_read():
373
 
            new_refs = self.target.send_pack(
374
 
                git_update_refs, self.source_store.generate_lossy_pack_data)
375
 
        # FIXME: revidmap?
376
 
        return revidmap, self.old_refs, new_refs
377
 
 
378
 
    @staticmethod
379
 
    def is_compatible(source, target):
380
 
        """Be compatible with GitRepository."""
381
 
        return (not isinstance(source, GitRepository) and
382
 
                isinstance(target, RemoteGitRepository))
383
 
 
384
 
 
385
 
class GitSearchResult(object):
386
 
 
387
 
    def __init__(self, start, exclude, keys):
388
 
        self._start = start
389
 
        self._exclude = exclude
390
 
        self._keys = keys
391
 
 
392
 
    def get_keys(self):
393
 
        return self._keys
394
 
 
395
 
    def get_recipe(self):
396
 
        return ('search', self._start, self._exclude, len(self._keys))
397
 
 
398
 
 
399
 
class InterFromGitRepository(InterRepository):
400
 
 
401
 
    _matching_repo_format = GitRepositoryFormat()
402
 
 
403
 
    def _target_has_shas(self, shas):
404
 
        raise NotImplementedError(self._target_has_shas)
405
 
 
406
 
    def get_determine_wants_heads(self, wants, include_tags=False):
407
 
        wants = set(wants)
408
 
 
409
 
        def determine_wants(refs):
410
 
            unpeel_lookup = {}
411
 
            for k, v in refs.items():
412
 
                if k.endswith(ANNOTATED_TAG_SUFFIX):
413
 
                    unpeel_lookup[v] = refs[k[:-len(ANNOTATED_TAG_SUFFIX)]]
414
 
            potential = set([unpeel_lookup.get(w, w) for w in wants])
415
 
            if include_tags:
416
 
                for k, sha in refs.items():
417
 
                    if k.endswith(ANNOTATED_TAG_SUFFIX):
418
 
                        continue
419
 
                    if not is_tag(k):
420
 
                        continue
421
 
                    if sha == ZERO_SHA:
422
 
                        continue
423
 
                    potential.add(sha)
424
 
            return list(potential - self._target_has_shas(potential))
425
 
        return determine_wants
426
 
 
427
 
    def determine_wants_all(self, refs):
428
 
        raise NotImplementedError(self.determine_wants_all)
429
 
 
430
 
    @staticmethod
431
 
    def _get_repo_format_to_test():
432
 
        return None
433
 
 
434
 
    def copy_content(self, revision_id=None):
435
 
        """See InterRepository.copy_content."""
436
 
        self.fetch(revision_id, find_ghosts=False)
437
 
 
438
 
    def search_missing_revision_ids(self,
439
 
                                    find_ghosts=True, revision_ids=None,
440
 
                                    if_present_ids=None, limit=None):
441
 
        if limit is not None:
442
 
            raise FetchLimitUnsupported(self)
443
 
        if revision_ids is None and if_present_ids is None:
444
 
            todo = set(self.source.all_revision_ids())
445
 
        else:
446
 
            todo = set()
447
 
            if revision_ids is not None:
448
 
                for revid in revision_ids:
449
 
                    if not self.source.has_revision(revid):
450
 
                        raise NoSuchRevision(revid, self.source)
451
 
                todo.update(revision_ids)
452
 
            if if_present_ids is not None:
453
 
                todo.update(if_present_ids)
454
 
        result_set = todo.difference(self.target.all_revision_ids())
455
 
        result_parents = set(itertools.chain.from_iterable(
456
 
            self.source.get_graph().get_parent_map(result_set).values()))
457
 
        included_keys = result_set.intersection(result_parents)
458
 
        start_keys = result_set.difference(included_keys)
459
 
        exclude_keys = result_parents.difference(result_set)
460
 
        return GitSearchResult(start_keys, exclude_keys, result_set)
461
 
 
462
 
 
463
 
class InterGitNonGitRepository(InterFromGitRepository):
464
 
    """Base InterRepository that copies revisions from a Git into a non-Git
465
 
    repository."""
466
 
 
467
 
    def _target_has_shas(self, shas):
468
 
        revids = {}
469
 
        for sha in shas:
470
 
            try:
471
 
                revid = self.source.lookup_foreign_revision_id(sha)
472
 
            except NotCommitError:
473
 
                # Commit is definitely not present
474
 
                continue
475
 
            else:
476
 
                revids[revid] = sha
477
 
        return set([revids[r] for r in self.target.has_revisions(revids)])
478
 
 
479
 
    def determine_wants_all(self, refs):
480
 
        potential = set()
481
 
        for k, v in refs.items():
482
 
            # For non-git target repositories, only worry about peeled
483
 
            if v == ZERO_SHA:
484
 
                continue
485
 
            potential.add(self.source.controldir.get_peeled(k) or v)
486
 
        return list(potential - self._target_has_shas(potential))
487
 
 
488
 
    def _warn_slow(self):
489
 
        if not config.GlobalConfig().suppress_warning('slow_intervcs_push'):
490
 
            trace.warning(
491
 
                'Fetching from Git to Bazaar repository. '
492
 
                'For better performance, fetch into a Git repository.')
493
 
 
494
 
    def fetch_objects(self, determine_wants, mapping, limit=None, lossy=False):
495
 
        """Fetch objects from a remote server.
496
 
 
497
 
        :param determine_wants: determine_wants callback
498
 
        :param mapping: BzrGitMapping to use
499
 
        :param limit: Maximum number of commits to import.
500
 
        :return: Tuple with pack hint, last imported revision id and remote
501
 
            refs
502
 
        """
503
 
        raise NotImplementedError(self.fetch_objects)
504
 
 
505
 
    def get_determine_wants_revids(self, revids, include_tags=False):
506
 
        wants = set()
507
 
        for revid in set(revids):
508
 
            if self.target.has_revision(revid):
509
 
                continue
510
 
            git_sha, mapping = self.source.lookup_bzr_revision_id(revid)
511
 
            wants.add(git_sha)
512
 
        return self.get_determine_wants_heads(wants, include_tags=include_tags)
513
 
 
514
 
    def fetch(self, revision_id=None, find_ghosts=False,
515
 
              mapping=None, fetch_spec=None, include_tags=False, lossy=False):
516
 
        if mapping is None:
517
 
            mapping = self.source.get_mapping()
518
 
        if revision_id is not None:
519
 
            interesting_heads = [revision_id]
520
 
        elif fetch_spec is not None:
521
 
            recipe = fetch_spec.get_recipe()
522
 
            if recipe[0] in ("search", "proxy-search"):
523
 
                interesting_heads = recipe[1]
524
 
            else:
525
 
                raise AssertionError("Unsupported search result type %s" %
526
 
                                     recipe[0])
527
 
        else:
528
 
            interesting_heads = None
529
 
 
530
 
        if interesting_heads is not None:
531
 
            determine_wants = self.get_determine_wants_revids(
532
 
                interesting_heads, include_tags=include_tags)
533
 
        else:
534
 
            determine_wants = self.determine_wants_all
535
 
 
536
 
        (pack_hint, _, remote_refs) = self.fetch_objects(
537
 
            determine_wants, mapping, lossy=lossy)
538
 
        if pack_hint is not None and self.target._format.pack_compresses:
539
 
            self.target.pack(hint=pack_hint)
540
 
        result = FetchResult()
541
 
        result.refs = remote_refs
542
 
        return result
543
 
 
544
 
 
545
 
class InterRemoteGitNonGitRepository(InterGitNonGitRepository):
546
 
    """InterRepository that copies revisions from a remote Git into a non-Git
547
 
    repository."""
548
 
 
549
 
    def get_target_heads(self):
550
 
        # FIXME: This should be more efficient
551
 
        all_revs = self.target.all_revision_ids()
552
 
        parent_map = self.target.get_parent_map(all_revs)
553
 
        all_parents = set()
554
 
        for values in parent_map.values():
555
 
            all_parents.update(values)
556
 
        return set(all_revs) - all_parents
557
 
 
558
 
    def fetch_objects(self, determine_wants, mapping, limit=None, lossy=False):
559
 
        """See `InterGitNonGitRepository`."""
560
 
        self._warn_slow()
561
 
        store = get_object_store(self.target, mapping)
562
 
        with store.lock_write():
563
 
            heads = self.get_target_heads()
564
 
            graph_walker = ObjectStoreGraphWalker(
565
 
                [store._lookup_revision_sha1(head) for head in heads],
566
 
                lambda sha: store[sha].parents)
567
 
            wants_recorder = DetermineWantsRecorder(determine_wants)
568
 
 
569
 
            with ui.ui_factory.nested_progress_bar() as pb:
570
 
                objects_iter = self.source.fetch_objects(
571
 
                    wants_recorder, graph_walker, store.get_raw)
572
 
                trace.mutter("Importing %d new revisions",
573
 
                             len(wants_recorder.wants))
574
 
                (pack_hint, last_rev) = import_git_objects(
575
 
                    self.target, mapping, objects_iter, store,
576
 
                    wants_recorder.wants, pb, limit)
577
 
                return (pack_hint, last_rev, wants_recorder.remote_refs)
578
 
 
579
 
    @staticmethod
580
 
    def is_compatible(source, target):
581
 
        """Be compatible with GitRepository."""
582
 
        if not isinstance(source, RemoteGitRepository):
583
 
            return False
584
 
        if not target.supports_rich_root():
585
 
            return False
586
 
        if isinstance(target, GitRepository):
587
 
            return False
588
 
        if not getattr(target._format, "supports_full_versioned_files", True):
589
 
            return False
590
 
        return True
591
 
 
592
 
 
593
 
class InterLocalGitNonGitRepository(InterGitNonGitRepository):
594
 
    """InterRepository that copies revisions from a local Git into a non-Git
595
 
    repository."""
596
 
 
597
 
    def fetch_objects(self, determine_wants, mapping, limit=None, lossy=False):
598
 
        """See `InterGitNonGitRepository`."""
599
 
        self._warn_slow()
600
 
        remote_refs = self.source.controldir.get_refs_container().as_dict()
601
 
        wants = determine_wants(remote_refs)
602
 
        target_git_object_retriever = get_object_store(self.target, mapping)
603
 
        with ui.ui_factory.nested_progress_bar() as pb:
604
 
            target_git_object_retriever.lock_write()
605
 
            try:
606
 
                (pack_hint, last_rev) = import_git_objects(
607
 
                    self.target, mapping, self.source._git.object_store,
608
 
                    target_git_object_retriever, wants, pb, limit)
609
 
                return (pack_hint, last_rev, remote_refs)
610
 
            finally:
611
 
                target_git_object_retriever.unlock()
612
 
 
613
 
    @staticmethod
614
 
    def is_compatible(source, target):
615
 
        """Be compatible with GitRepository."""
616
 
        if not isinstance(source, LocalGitRepository):
617
 
            return False
618
 
        if not target.supports_rich_root():
619
 
            return False
620
 
        if isinstance(target, GitRepository):
621
 
            return False
622
 
        if not getattr(target._format, "supports_full_versioned_files", True):
623
 
            return False
624
 
        return True
625
 
 
626
 
 
627
 
class InterGitGitRepository(InterFromGitRepository):
628
 
    """InterRepository that copies between Git repositories."""
629
 
 
630
 
    def fetch_refs(self, update_refs, lossy, overwrite=False):
631
 
        if lossy:
632
 
            raise LossyPushToSameVCS(self.source, self.target)
633
 
        old_refs = self.target.controldir.get_refs_container()
634
 
        ref_changes = {}
635
 
 
636
 
        def determine_wants(heads):
637
 
            old_refs = dict([(k, (v, None))
638
 
                             for (k, v) in heads.as_dict().items()])
639
 
            new_refs = update_refs(old_refs)
640
 
            ref_changes.update(new_refs)
641
 
            return [sha1 for (sha1, bzr_revid) in new_refs.values()]
642
 
        self.fetch_objects(determine_wants, lossy=lossy)
643
 
        for k, (git_sha, bzr_revid) in ref_changes.items():
644
 
            self.target._git.refs[k] = git_sha
645
 
        new_refs = self.target.controldir.get_refs_container()
646
 
        return None, old_refs, new_refs
647
 
 
648
 
    def fetch_objects(self, determine_wants, mapping=None, limit=None,
649
 
                      lossy=False):
650
 
        raise NotImplementedError(self.fetch_objects)
651
 
 
652
 
    def _target_has_shas(self, shas):
653
 
        return set(
654
 
            [sha for sha in shas if sha in self.target._git.object_store])
655
 
 
656
 
    def fetch(self, revision_id=None, find_ghosts=False,
657
 
              mapping=None, fetch_spec=None, branches=None, limit=None,
658
 
              include_tags=False, lossy=False):
659
 
        if mapping is None:
660
 
            mapping = self.source.get_mapping()
661
 
        if revision_id is not None:
662
 
            args = [revision_id]
663
 
        elif fetch_spec is not None:
664
 
            recipe = fetch_spec.get_recipe()
665
 
            if recipe[0] in ("search", "proxy-search"):
666
 
                heads = recipe[1]
667
 
            else:
668
 
                raise AssertionError(
669
 
                    "Unsupported search result type %s" % recipe[0])
670
 
            args = heads
671
 
        if branches is not None:
672
 
            determine_wants = self.get_determine_wants_branches(
673
 
                branches, include_tags=include_tags)
674
 
        elif fetch_spec is None and revision_id is None:
675
 
            determine_wants = self.determine_wants_all
676
 
        else:
677
 
            determine_wants = self.get_determine_wants_revids(
678
 
                args, include_tags=include_tags)
679
 
        wants_recorder = DetermineWantsRecorder(determine_wants)
680
 
        self.fetch_objects(wants_recorder, mapping, limit=limit, lossy=lossy)
681
 
        result = FetchResult()
682
 
        result.refs = wants_recorder.remote_refs
683
 
        return result
684
 
 
685
 
    def get_determine_wants_revids(self, revids, include_tags=False):
686
 
        wants = set()
687
 
        for revid in set(revids):
688
 
            if revid == NULL_REVISION:
689
 
                continue
690
 
            git_sha, mapping = self.source.lookup_bzr_revision_id(revid)
691
 
            wants.add(git_sha)
692
 
        return self.get_determine_wants_heads(wants, include_tags=include_tags)
693
 
 
694
 
    def get_determine_wants_branches(self, branches, include_tags=False):
695
 
        def determine_wants(refs):
696
 
            ret = []
697
 
            for name, value in refs.items():
698
 
                if value == ZERO_SHA:
699
 
                    continue
700
 
 
701
 
                if name.endswith(ANNOTATED_TAG_SUFFIX):
702
 
                    continue
703
 
 
704
 
                if name in branches or (include_tags and is_tag(name)):
705
 
                    ret.append(value)
706
 
            return ret
707
 
        return determine_wants
708
 
 
709
 
    def determine_wants_all(self, refs):
710
 
        potential = set([
711
 
            v for k, v in refs.items()
712
 
            if not v == ZERO_SHA and not k.endswith(ANNOTATED_TAG_SUFFIX)])
713
 
        return list(potential - self._target_has_shas(potential))
714
 
 
715
 
 
716
 
class InterLocalGitLocalGitRepository(InterGitGitRepository):
717
 
 
718
 
    def fetch_objects(self, determine_wants, mapping=None, limit=None,
719
 
                      lossy=False):
720
 
        if lossy:
721
 
            raise LossyPushToSameVCS(self.source, self.target)
722
 
        if limit is not None:
723
 
            raise FetchLimitUnsupported(self)
724
 
        from .remote import DefaultProgressReporter
725
 
        with ui.ui_factory.nested_progress_bar() as pb:
726
 
            progress = DefaultProgressReporter(pb).progress
727
 
            refs = self.source._git.fetch(
728
 
                self.target._git, determine_wants,
729
 
                progress=progress)
730
 
        return (None, None, refs)
731
 
 
732
 
    @staticmethod
733
 
    def is_compatible(source, target):
734
 
        """Be compatible with GitRepository."""
735
 
        return (isinstance(source, LocalGitRepository) and
736
 
                isinstance(target, LocalGitRepository))
737
 
 
738
 
 
739
 
class InterRemoteGitLocalGitRepository(InterGitGitRepository):
740
 
 
741
 
    def fetch_objects(self, determine_wants, mapping=None, limit=None,
742
 
                      lossy=False):
743
 
        if lossy:
744
 
            raise LossyPushToSameVCS(self.source, self.target)
745
 
        if limit is not None:
746
 
            raise FetchLimitUnsupported(self)
747
 
        graphwalker = self.target._git.get_graph_walker()
748
 
        if (CAPABILITY_THIN_PACK in
749
 
                self.source.controldir._client._fetch_capabilities):
750
 
            # TODO(jelmer): Avoid reading entire file into memory and
751
 
            # only processing it after the whole file has been fetched.
752
 
            f = BytesIO()
753
 
 
754
 
            def commit():
755
 
                if f.tell():
756
 
                    f.seek(0)
757
 
                    self.target._git.object_store.move_in_thin_pack(f)
758
 
 
759
 
            def abort():
760
 
                pass
761
 
        else:
762
 
            f, commit, abort = self.target._git.object_store.add_pack()
763
 
        try:
764
 
            refs = self.source.controldir.fetch_pack(
765
 
                determine_wants, graphwalker, f.write)
766
 
            commit()
767
 
            return (None, None, refs)
768
 
        except BaseException:
769
 
            abort()
770
 
            raise
771
 
 
772
 
    @staticmethod
773
 
    def is_compatible(source, target):
774
 
        """Be compatible with GitRepository."""
775
 
        return (isinstance(source, RemoteGitRepository) and
776
 
                isinstance(target, LocalGitRepository))