/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/repository.py

  • Committer: Jelmer Vernooij
  • Date: 2018-11-18 18:23:32 UTC
  • mto: This revision was merged to the branch mainline in revision 7197.
  • Revision ID: jelmer@jelmer.uk-20181118182332-viz1qvqese2mo9i6
Fix some more Bazaar references.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2008-2018 Jelmer Vernooij <jelmer@jelmer.uk>
 
2
# Copyright (C) 2007 Canonical Ltd
 
3
#
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
#
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License
 
15
# along with this program; if not, write to the Free Software
 
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
17
 
 
18
"""An adapter between a Git Repository and a Bazaar Branch"""
 
19
 
 
20
from __future__ import absolute_import
 
21
 
 
22
from .. import (
 
23
    check,
 
24
    errors,
 
25
    graph as _mod_graph,
 
26
    lock,
 
27
    repository,
 
28
    revision as _mod_revision,
 
29
    trace,
 
30
    transactions,
 
31
    ui,
 
32
    )
 
33
from ..decorators import only_raises
 
34
from ..foreign import (
 
35
    ForeignRepository,
 
36
    )
 
37
from ..sixish import (
 
38
    viewitems,
 
39
    viewvalues,
 
40
    )
 
41
 
 
42
from .commit import (
 
43
    GitCommitBuilder,
 
44
    )
 
45
from .filegraph import (
 
46
    GitFileLastChangeScanner,
 
47
    GitFileParentProvider,
 
48
    )
 
49
from .mapping import (
 
50
    default_mapping,
 
51
    foreign_vcs_git,
 
52
    mapping_registry,
 
53
    )
 
54
from .tree import (
 
55
    GitRevisionTree,
 
56
    )
 
57
 
 
58
 
 
59
from dulwich.errors import (
 
60
    NotCommitError,
 
61
    )
 
62
from dulwich.objects import (
 
63
    Commit,
 
64
    ZERO_SHA,
 
65
    )
 
66
from dulwich.object_store import (
 
67
    tree_lookup_path,
 
68
    )
 
69
 
 
70
 
 
71
class RepoReconciler(object):
 
72
    """Reconciler that reconciles a repository.
 
73
 
 
74
    """
 
75
 
 
76
    def __init__(self, repo, other=None, thorough=False):
 
77
        """Construct a RepoReconciler.
 
78
 
 
79
        :param thorough: perform a thorough check which may take longer but
 
80
                         will correct non-data loss issues such as incorrect
 
81
                         cached data.
 
82
        """
 
83
        self.repo = repo
 
84
 
 
85
    def reconcile(self):
 
86
        """Perform reconciliation.
 
87
 
 
88
        After reconciliation the following attributes document found issues:
 
89
        inconsistent_parents: The number of revisions in the repository whose
 
90
                              ancestry was being reported incorrectly.
 
91
        garbage_inventories: The number of inventory objects without revisions
 
92
                             that were garbage collected.
 
93
        """
 
94
 
 
95
 
 
96
class GitCheck(check.Check):
 
97
 
 
98
    def __init__(self, repository, check_repo=True):
 
99
        self.repository = repository
 
100
        self.check_repo = check_repo
 
101
        self.checked_rev_cnt = 0
 
102
        self.object_count = None
 
103
        self.problems = []
 
104
 
 
105
    def check(self, callback_refs=None, check_repo=True):
 
106
        if callback_refs is None:
 
107
            callback_refs = {}
 
108
        with self.repository.lock_read(), \
 
109
                ui.ui_factory.nested_progress_bar() as self.progress:
 
110
            shas = set(self.repository._git.object_store)
 
111
            self.object_count = len(shas)
 
112
            # TODO(jelmer): Check more things
 
113
            for i, sha in enumerate(shas):
 
114
                self.progress.update('checking objects', i, self.object_count)
 
115
                o = self.repository._git.object_store[sha]
 
116
                try:
 
117
                    o.check()
 
118
                except Exception as e:
 
119
                    self.problems.append((sha, e))
 
120
 
 
121
    def _report_repo_results(self, verbose):
 
122
        trace.note('checked repository {0} format {1}'.format(
 
123
            self.repository.user_url,
 
124
            self.repository._format))
 
125
        trace.note('%6d objects', self.object_count)
 
126
        for sha, problem in self.problems:
 
127
            trace.note('%s: %s', sha, problem)
 
128
 
 
129
    def report_results(self, verbose):
 
130
        if self.check_repo:
 
131
            self._report_repo_results(verbose)
 
132
 
 
133
 
 
134
_optimisers_loaded = False
 
135
 
 
136
 
 
137
def lazy_load_optimisers():
 
138
    global _optimisers_loaded
 
139
    if _optimisers_loaded:
 
140
        return
 
141
    from . import interrepo
 
142
    for optimiser in [interrepo.InterRemoteGitNonGitRepository,
 
143
                      interrepo.InterLocalGitNonGitRepository,
 
144
                      interrepo.InterLocalGitLocalGitRepository,
 
145
                      interrepo.InterRemoteGitLocalGitRepository,
 
146
                      interrepo.InterToLocalGitRepository,
 
147
                      interrepo.InterToRemoteGitRepository,
 
148
                      ]:
 
149
        repository.InterRepository.register_optimiser(optimiser)
 
150
    _optimisers_loaded = True
 
151
 
 
152
 
 
153
class GitRepository(ForeignRepository):
 
154
    """An adapter to git repositories for bzr."""
 
155
 
 
156
    _serializer = None
 
157
    vcs = foreign_vcs_git
 
158
    chk_bytes = None
 
159
 
 
160
    def __init__(self, gitdir):
 
161
        self._transport = gitdir.root_transport
 
162
        super(GitRepository, self).__init__(GitRepositoryFormat(),
 
163
                                            gitdir, control_files=None)
 
164
        self.base = gitdir.root_transport.base
 
165
        lazy_load_optimisers()
 
166
        self._lock_mode = None
 
167
        self._lock_count = 0
 
168
 
 
169
    def add_fallback_repository(self, basis_url):
 
170
        raise errors.UnstackableRepositoryFormat(self._format,
 
171
                                                 self.control_transport.base)
 
172
 
 
173
    def is_shared(self):
 
174
        return False
 
175
 
 
176
    def get_physical_lock_status(self):
 
177
        return False
 
178
 
 
179
    def lock_write(self):
 
180
        """See Branch.lock_write()."""
 
181
        if self._lock_mode:
 
182
            if self._lock_mode != 'w':
 
183
                raise errors.ReadOnlyError(self)
 
184
            self._lock_count += 1
 
185
        else:
 
186
            self._lock_mode = 'w'
 
187
            self._lock_count = 1
 
188
            self._transaction = transactions.WriteTransaction()
 
189
        return repository.RepositoryWriteLockResult(self.unlock, None)
 
190
 
 
191
    def break_lock(self):
 
192
        raise NotImplementedError(self.break_lock)
 
193
 
 
194
    def dont_leave_lock_in_place(self):
 
195
        raise NotImplementedError(self.dont_leave_lock_in_place)
 
196
 
 
197
    def leave_lock_in_place(self):
 
198
        raise NotImplementedError(self.leave_lock_in_place)
 
199
 
 
200
    def lock_read(self):
 
201
        if self._lock_mode:
 
202
            if self._lock_mode not in ('r', 'w'):
 
203
                raise AssertionError
 
204
            self._lock_count += 1
 
205
        else:
 
206
            self._lock_mode = 'r'
 
207
            self._lock_count = 1
 
208
            self._transaction = transactions.ReadOnlyTransaction()
 
209
        return lock.LogicalLockResult(self.unlock)
 
210
 
 
211
    @only_raises(errors.LockNotHeld, errors.LockBroken)
 
212
    def unlock(self):
 
213
        if self._lock_count == 0:
 
214
            raise errors.LockNotHeld(self)
 
215
        if self._lock_count == 1 and self._lock_mode == 'w':
 
216
            if self._write_group is not None:
 
217
                self.abort_write_group()
 
218
                self._lock_count -= 1
 
219
                self._lock_mode = None
 
220
                raise errors.BzrError(
 
221
                    'Must end write groups before releasing write locks.')
 
222
        self._lock_count -= 1
 
223
        if self._lock_count == 0:
 
224
            self._lock_mode = None
 
225
            transaction = self._transaction
 
226
            self._transaction = None
 
227
            transaction.finish()
 
228
 
 
229
    def is_write_locked(self):
 
230
        return (self._lock_mode == 'w')
 
231
 
 
232
    def is_locked(self):
 
233
        return (self._lock_mode is not None)
 
234
 
 
235
    def get_transaction(self):
 
236
        """See Repository.get_transaction()."""
 
237
        if self._transaction is None:
 
238
            return transactions.PassThroughTransaction()
 
239
        else:
 
240
            return self._transaction
 
241
 
 
242
    def reconcile(self, other=None, thorough=False):
 
243
        """Reconcile this repository."""
 
244
        reconciler = RepoReconciler(self, thorough=thorough)
 
245
        reconciler.reconcile()
 
246
        return reconciler
 
247
 
 
248
    def supports_rich_root(self):
 
249
        return True
 
250
 
 
251
    def get_mapping(self):
 
252
        return default_mapping
 
253
 
 
254
    def make_working_trees(self):
 
255
        return not self._git.get_config().get_boolean(("core", ), "bare")
 
256
 
 
257
    def revision_graph_can_have_wrong_parents(self):
 
258
        return False
 
259
 
 
260
    def add_signature_text(self, revid, signature):
 
261
        raise errors.UnsupportedOperation(self.add_signature_text, self)
 
262
 
 
263
    def sign_revision(self, revision_id, gpg_strategy):
 
264
        raise errors.UnsupportedOperation(self.add_signature_text, self)
 
265
 
 
266
 
 
267
class LocalGitRepository(GitRepository):
 
268
    """Git repository on the file system."""
 
269
 
 
270
    def __init__(self, gitdir):
 
271
        GitRepository.__init__(self, gitdir)
 
272
        self._git = gitdir._git
 
273
        self._file_change_scanner = GitFileLastChangeScanner(self)
 
274
        self._transaction = None
 
275
 
 
276
    def get_commit_builder(self, branch, parents, config, timestamp=None,
 
277
                           timezone=None, committer=None, revprops=None,
 
278
                           revision_id=None, lossy=False):
 
279
        """Obtain a CommitBuilder for this repository.
 
280
 
 
281
        :param branch: Branch to commit to.
 
282
        :param parents: Revision ids of the parents of the new revision.
 
283
        :param config: Configuration to use.
 
284
        :param timestamp: Optional timestamp recorded for commit.
 
285
        :param timezone: Optional timezone for timestamp.
 
286
        :param committer: Optional committer to set for commit.
 
287
        :param revprops: Optional dictionary of revision properties.
 
288
        :param revision_id: Optional revision id.
 
289
        :param lossy: Whether to discard data that can not be natively
 
290
            represented, when pushing to a foreign VCS
 
291
        """
 
292
        builder = GitCommitBuilder(
 
293
            self, parents, config, timestamp, timezone, committer, revprops,
 
294
            revision_id, lossy)
 
295
        self.start_write_group()
 
296
        return builder
 
297
 
 
298
    def get_file_graph(self):
 
299
        return _mod_graph.Graph(GitFileParentProvider(
 
300
            self._file_change_scanner))
 
301
 
 
302
    def iter_files_bytes(self, desired_files):
 
303
        """Iterate through file versions.
 
304
 
 
305
        Files will not necessarily be returned in the order they occur in
 
306
        desired_files.  No specific order is guaranteed.
 
307
 
 
308
        Yields pairs of identifier, bytes_iterator.  identifier is an opaque
 
309
        value supplied by the caller as part of desired_files.  It should
 
310
        uniquely identify the file version in the caller's context.  (Examples:
 
311
        an index number or a TreeTransform trans_id.)
 
312
 
 
313
        bytes_iterator is an iterable of bytestrings for the file.  The
 
314
        kind of iterable and length of the bytestrings are unspecified, but for
 
315
        this implementation, it is a list of bytes produced by
 
316
        VersionedFile.get_record_stream().
 
317
 
 
318
        :param desired_files: a list of (file_id, revision_id, identifier)
 
319
            triples
 
320
        """
 
321
        per_revision = {}
 
322
        for (file_id, revision_id, identifier) in desired_files:
 
323
            per_revision.setdefault(revision_id, []).append(
 
324
                (file_id, identifier))
 
325
        for revid, files in viewitems(per_revision):
 
326
            try:
 
327
                (commit_id, mapping) = self.lookup_bzr_revision_id(revid)
 
328
            except errors.NoSuchRevision:
 
329
                raise errors.RevisionNotPresent(revid, self)
 
330
            try:
 
331
                commit = self._git.object_store[commit_id]
 
332
            except KeyError:
 
333
                raise errors.RevisionNotPresent(revid, self)
 
334
            root_tree = commit.tree
 
335
            for fileid, identifier in files:
 
336
                try:
 
337
                    path = mapping.parse_file_id(fileid)
 
338
                except ValueError:
 
339
                    raise errors.RevisionNotPresent((fileid, revid), self)
 
340
                try:
 
341
                    obj = tree_lookup_path(
 
342
                        self._git.object_store.__getitem__, root_tree,
 
343
                        path.encode('utf-8'))
 
344
                    if isinstance(obj, tuple):
 
345
                        (mode, item_id) = obj
 
346
                        obj = self._git.object_store[item_id]
 
347
                except KeyError:
 
348
                    raise errors.RevisionNotPresent((fileid, revid), self)
 
349
                else:
 
350
                    if obj.type_name == b"tree":
 
351
                        yield (identifier, [])
 
352
                    elif obj.type_name == b"blob":
 
353
                        yield (identifier, obj.chunked)
 
354
                    else:
 
355
                        raise AssertionError("file text resolved to %r" % obj)
 
356
 
 
357
    def gather_stats(self, revid=None, committers=None):
 
358
        """See Repository.gather_stats()."""
 
359
        result = super(LocalGitRepository, self).gather_stats(
 
360
            revid, committers)
 
361
        revs = []
 
362
        for sha in self._git.object_store:
 
363
            o = self._git.object_store[sha]
 
364
            if o.type_name == b"commit":
 
365
                revs.append(o.id)
 
366
        result['revisions'] = len(revs)
 
367
        return result
 
368
 
 
369
    def _iter_revision_ids(self):
 
370
        mapping = self.get_mapping()
 
371
        for sha in self._git.object_store:
 
372
            o = self._git.object_store[sha]
 
373
            if not isinstance(o, Commit):
 
374
                continue
 
375
            rev, roundtrip_revid, verifiers = mapping.import_commit(
 
376
                o, mapping.revision_id_foreign_to_bzr)
 
377
            yield o.id, rev.revision_id, roundtrip_revid
 
378
 
 
379
    def all_revision_ids(self):
 
380
        ret = set()
 
381
        for git_sha, revid, roundtrip_revid in self._iter_revision_ids():
 
382
            if roundtrip_revid:
 
383
                ret.add(roundtrip_revid)
 
384
            else:
 
385
                ret.add(revid)
 
386
        return list(ret)
 
387
 
 
388
    def _get_parents(self, revid, no_alternates=False):
 
389
        if type(revid) != bytes:
 
390
            raise ValueError
 
391
        try:
 
392
            (hexsha, mapping) = self.lookup_bzr_revision_id(revid)
 
393
        except errors.NoSuchRevision:
 
394
            return None
 
395
        # FIXME: Honor no_alternates setting
 
396
        try:
 
397
            commit = self._git.object_store[hexsha]
 
398
        except KeyError:
 
399
            return None
 
400
        ret = []
 
401
        for p in commit.parents:
 
402
            try:
 
403
                ret.append(self.lookup_foreign_revision_id(p, mapping))
 
404
            except KeyError:
 
405
                ret.append(mapping.revision_id_foreign_to_bzr(p))
 
406
        return ret
 
407
 
 
408
    def _get_parent_map_no_fallbacks(self, revids):
 
409
        return self.get_parent_map(revids, no_alternates=True)
 
410
 
 
411
    def get_parent_map(self, revids, no_alternates=False):
 
412
        parent_map = {}
 
413
        for revision_id in revids:
 
414
            parents = self._get_parents(
 
415
                revision_id, no_alternates=no_alternates)
 
416
            if revision_id == _mod_revision.NULL_REVISION:
 
417
                parent_map[revision_id] = ()
 
418
                continue
 
419
            if parents is None:
 
420
                continue
 
421
            if len(parents) == 0:
 
422
                parents = [_mod_revision.NULL_REVISION]
 
423
            parent_map[revision_id] = tuple(parents)
 
424
        return parent_map
 
425
 
 
426
    def get_known_graph_ancestry(self, revision_ids):
 
427
        """Return the known graph for a set of revision ids and their ancestors.
 
428
        """
 
429
        pending = set(revision_ids)
 
430
        parent_map = {}
 
431
        while pending:
 
432
            this_parent_map = {}
 
433
            for revid in pending:
 
434
                if revid == _mod_revision.NULL_REVISION:
 
435
                    continue
 
436
                parents = self._get_parents(revid)
 
437
                if parents is not None:
 
438
                    this_parent_map[revid] = parents
 
439
            parent_map.update(this_parent_map)
 
440
            pending = set()
 
441
            for values in viewvalues(this_parent_map):
 
442
                pending.update(values)
 
443
            pending = pending.difference(parent_map)
 
444
        return _mod_graph.KnownGraph(parent_map)
 
445
 
 
446
    def get_signature_text(self, revision_id):
 
447
        git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
 
448
        try:
 
449
            commit = self._git.object_store[git_commit_id]
 
450
        except KeyError:
 
451
            raise errors.NoSuchRevision(self, revision_id)
 
452
        if commit.gpgsig is None:
 
453
            raise errors.NoSuchRevision(self, revision_id)
 
454
        return commit.gpgsig
 
455
 
 
456
    def check(self, revision_ids=None, callback_refs=None, check_repo=True):
 
457
        result = GitCheck(self, check_repo=check_repo)
 
458
        result.check(callback_refs)
 
459
        return result
 
460
 
 
461
    def pack(self, hint=None, clean_obsolete_packs=False):
 
462
        self._git.object_store.pack_loose_objects()
 
463
 
 
464
    def lookup_foreign_revision_id(self, foreign_revid, mapping=None):
 
465
        """Lookup a revision id.
 
466
 
 
467
        :param foreign_revid: Foreign revision id to look up
 
468
        :param mapping: Mapping to use (use default mapping if not specified)
 
469
        :raise KeyError: If foreign revision was not found
 
470
        :return: bzr revision id
 
471
        """
 
472
        if not isinstance(foreign_revid, bytes):
 
473
            raise TypeError(foreign_revid)
 
474
        if mapping is None:
 
475
            mapping = self.get_mapping()
 
476
        if foreign_revid == ZERO_SHA:
 
477
            return _mod_revision.NULL_REVISION
 
478
        commit = self._git.object_store.peel_sha(foreign_revid)
 
479
        if not isinstance(commit, Commit):
 
480
            raise NotCommitError(commit.id)
 
481
        rev, roundtrip_revid, verifiers = mapping.import_commit(
 
482
            commit, mapping.revision_id_foreign_to_bzr)
 
483
        # FIXME: check testament before doing this?
 
484
        if roundtrip_revid:
 
485
            return roundtrip_revid
 
486
        else:
 
487
            return rev.revision_id
 
488
 
 
489
    def has_signature_for_revision_id(self, revision_id):
 
490
        """Check whether a GPG signature is present for this revision.
 
491
 
 
492
        This is never the case for Git repositories.
 
493
        """
 
494
        try:
 
495
            self.get_signature_text(revision_id)
 
496
        except errors.NoSuchRevision:
 
497
            return False
 
498
        else:
 
499
            return True
 
500
 
 
501
    def verify_revision_signature(self, revision_id, gpg_strategy):
 
502
        """Verify the signature on a revision.
 
503
 
 
504
        :param revision_id: the revision to verify
 
505
        :gpg_strategy: the GPGStrategy object to used
 
506
 
 
507
        :return: gpg.SIGNATURE_VALID or a failed SIGNATURE_ value
 
508
        """
 
509
        from breezy import gpg
 
510
        with self.lock_read():
 
511
            git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
 
512
            try:
 
513
                commit = self._git.object_store[git_commit_id]
 
514
            except KeyError:
 
515
                raise errors.NoSuchRevision(self, revision_id)
 
516
 
 
517
            if commit.gpgsig is None:
 
518
                return gpg.SIGNATURE_NOT_SIGNED, None
 
519
 
 
520
            without_sig = Commit.from_string(commit.as_raw_string())
 
521
            without_sig.gpgsig = None
 
522
 
 
523
            (result, key, plain_text) = gpg_strategy.verify(
 
524
                without_sig.as_raw_string(), commit.gpgsig)
 
525
            return (result, key)
 
526
 
 
527
    def lookup_bzr_revision_id(self, bzr_revid, mapping=None):
 
528
        """Lookup a bzr revision id in a Git repository.
 
529
 
 
530
        :param bzr_revid: Bazaar revision id
 
531
        :param mapping: Optional mapping to use
 
532
        :return: Tuple with git commit id, mapping that was used and supplement
 
533
            details
 
534
        """
 
535
        try:
 
536
            (git_sha, mapping) = mapping_registry.revision_id_bzr_to_foreign(
 
537
                bzr_revid)
 
538
        except errors.InvalidRevisionId:
 
539
            if mapping is None:
 
540
                mapping = self.get_mapping()
 
541
            try:
 
542
                return (self._git.refs[mapping.revid_as_refname(bzr_revid)],
 
543
                        mapping)
 
544
            except KeyError:
 
545
                # Update refs from Git commit objects
 
546
                # FIXME: Hitting this a lot will be very inefficient...
 
547
                pb = ui.ui_factory.nested_progress_bar()
 
548
                try:
 
549
                    for i, (git_sha, revid, roundtrip_revid) in enumerate(
 
550
                            self._iter_revision_ids()):
 
551
                        if not roundtrip_revid:
 
552
                            continue
 
553
                        pb.update("resolving revision id", i)
 
554
                        refname = mapping.revid_as_refname(roundtrip_revid)
 
555
                        self._git.refs[refname] = git_sha
 
556
                        if roundtrip_revid == bzr_revid:
 
557
                            return git_sha, mapping
 
558
                finally:
 
559
                    pb.finished()
 
560
                raise errors.NoSuchRevision(self, bzr_revid)
 
561
        else:
 
562
            return (git_sha, mapping)
 
563
 
 
564
    def get_revision(self, revision_id):
 
565
        if not isinstance(revision_id, bytes):
 
566
            raise errors.InvalidRevisionId(revision_id, self)
 
567
        git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
 
568
        try:
 
569
            commit = self._git.object_store[git_commit_id]
 
570
        except KeyError:
 
571
            raise errors.NoSuchRevision(self, revision_id)
 
572
        revision, roundtrip_revid, verifiers = mapping.import_commit(
 
573
            commit, self.lookup_foreign_revision_id)
 
574
        if revision is None:
 
575
            raise AssertionError
 
576
        # FIXME: check verifiers ?
 
577
        if roundtrip_revid:
 
578
            revision.revision_id = roundtrip_revid
 
579
        return revision
 
580
 
 
581
    def has_revision(self, revision_id):
 
582
        """See Repository.has_revision."""
 
583
        if revision_id == _mod_revision.NULL_REVISION:
 
584
            return True
 
585
        try:
 
586
            git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
 
587
        except errors.NoSuchRevision:
 
588
            return False
 
589
        return (git_commit_id in self._git)
 
590
 
 
591
    def has_revisions(self, revision_ids):
 
592
        """See Repository.has_revisions."""
 
593
        return set(filter(self.has_revision, revision_ids))
 
594
 
 
595
    def iter_revisions(self, revision_ids):
 
596
        """See Repository.get_revisions."""
 
597
        for revid in revision_ids:
 
598
            try:
 
599
                rev = self.get_revision(revid)
 
600
            except errors.NoSuchRevision:
 
601
                rev = None
 
602
            yield (revid, rev)
 
603
 
 
604
    def revision_trees(self, revids):
 
605
        """See Repository.revision_trees."""
 
606
        for revid in revids:
 
607
            yield self.revision_tree(revid)
 
608
 
 
609
    def revision_tree(self, revision_id):
 
610
        """See Repository.revision_tree."""
 
611
        if revision_id is None:
 
612
            raise ValueError('invalid revision id %s' % revision_id)
 
613
        return GitRevisionTree(self, revision_id)
 
614
 
 
615
    def get_deltas_for_revisions(self, revisions, specific_fileids=None):
 
616
        """Produce a generator of revision deltas.
 
617
 
 
618
        Note that the input is a sequence of REVISIONS, not revision_ids.
 
619
        Trees will be held in memory until the generator exits.
 
620
        Each delta is relative to the revision's lefthand predecessor.
 
621
 
 
622
        :param specific_fileids: if not None, the result is filtered
 
623
          so that only those file-ids, their parents and their
 
624
          children are included.
 
625
        """
 
626
        # Get the revision-ids of interest
 
627
        required_trees = set()
 
628
        for revision in revisions:
 
629
            required_trees.add(revision.revision_id)
 
630
            required_trees.update(revision.parent_ids[:1])
 
631
 
 
632
        trees = dict((t.get_revision_id(), t) for
 
633
                     t in self.revision_trees(required_trees))
 
634
 
 
635
        # Calculate the deltas
 
636
        for revision in revisions:
 
637
            if not revision.parent_ids:
 
638
                old_tree = self.revision_tree(_mod_revision.NULL_REVISION)
 
639
            else:
 
640
                old_tree = trees[revision.parent_ids[0]]
 
641
            new_tree = trees[revision.revision_id]
 
642
            if specific_fileids is not None:
 
643
                specific_files = [new_tree.id2path(
 
644
                    fid) for fid in specific_fileids]
 
645
            else:
 
646
                specific_files = None
 
647
            yield new_tree.changes_from(
 
648
                old_tree, specific_files=specific_files)
 
649
 
 
650
    def set_make_working_trees(self, trees):
 
651
        raise errors.UnsupportedOperation(self.set_make_working_trees, self)
 
652
 
 
653
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
 
654
                      progress=None, limit=None):
 
655
        return self._git.fetch_objects(determine_wants, graph_walker, progress,
 
656
                                       limit=limit)
 
657
 
 
658
 
 
659
class GitRepositoryFormat(repository.RepositoryFormat):
 
660
    """Git repository format."""
 
661
 
 
662
    supports_versioned_directories = False
 
663
    supports_tree_reference = True
 
664
    rich_root_data = True
 
665
    supports_leaving_lock = False
 
666
    fast_deltas = True
 
667
    supports_funky_characters = True
 
668
    supports_external_lookups = False
 
669
    supports_full_versioned_files = False
 
670
    supports_revision_signatures = False
 
671
    supports_nesting_repositories = False
 
672
    revision_graph_can_have_wrong_parents = False
 
673
    supports_unreferenced_revisions = True
 
674
    supports_setting_revision_ids = False
 
675
    supports_storing_branch_nick = False
 
676
    supports_overriding_transport = False
 
677
    supports_custom_revision_properties = False
 
678
    records_per_file_revision = False
 
679
 
 
680
    @property
 
681
    def _matchingcontroldir(self):
 
682
        from .dir import LocalGitControlDirFormat
 
683
        return LocalGitControlDirFormat()
 
684
 
 
685
    def get_format_description(self):
 
686
        return "Git Repository"
 
687
 
 
688
    def initialize(self, controldir, shared=False, _internal=False):
 
689
        from .dir import GitDir
 
690
        if not isinstance(controldir, GitDir):
 
691
            raise errors.UninitializableFormat(self)
 
692
        return controldir.open_repository()
 
693
 
 
694
    def check_conversion_target(self, target_repo_format):
 
695
        return target_repo_format.rich_root_data
 
696
 
 
697
    def get_foreign_tests_repository_factory(self):
 
698
        from .tests.test_repository import (
 
699
            ForeignTestsRepositoryFactory,
 
700
            )
 
701
        return ForeignTestsRepositoryFactory()
 
702
 
 
703
    def network_name(self):
 
704
        return b"git"
 
705
 
 
706
 
 
707
def get_extra_interrepo_test_combinations():
 
708
    from ..bzr.groupcompress_repo import RepositoryFormat2a
 
709
    from . import interrepo
 
710
    return [
 
711
        (interrepo.InterLocalGitNonGitRepository,
 
712
         GitRepositoryFormat(), RepositoryFormat2a()),
 
713
        (interrepo.InterLocalGitLocalGitRepository,
 
714
         GitRepositoryFormat(), GitRepositoryFormat()),
 
715
        (interrepo.InterToLocalGitRepository,
 
716
         RepositoryFormat2a(), GitRepositoryFormat()),
 
717
        ]