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

Fix two mistakes in 'bzr help git'.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2007 Canonical Ltd
 
2
# Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org>
 
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
 
 
18
"""An adapter between a Git Repository and a Bazaar Branch"""
 
19
 
 
20
from bzrlib import (
 
21
    check,
 
22
    errors,
 
23
    inventory,
 
24
    repository,
 
25
    revision,
 
26
    )
 
27
try:
 
28
    from bzrlib.revisiontree import InventoryRevisionTree
 
29
except ImportError: # bzr < 2.4
 
30
    from bzrlib.revisiontree import RevisionTree as InventoryRevisionTree
 
31
from bzrlib.foreign import (
 
32
    ForeignRepository,
 
33
    )
 
34
 
 
35
from bzrlib.plugins.git.commit import (
 
36
    GitCommitBuilder,
 
37
    )
 
38
from bzrlib.plugins.git.mapping import (
 
39
    default_mapping,
 
40
    foreign_vcs_git,
 
41
    mapping_registry,
 
42
    )
 
43
from bzrlib.plugins.git.tree import (
 
44
    GitRevisionTree,
 
45
    )
 
46
 
 
47
 
 
48
from dulwich.objects import (
 
49
    Commit,
 
50
    Tag,
 
51
    ZERO_SHA,
 
52
    )
 
53
from dulwich.object_store import (
 
54
    tree_lookup_path,
 
55
    )
 
56
 
 
57
 
 
58
class RepoReconciler(object):
 
59
    """Reconciler that reconciles a repository.
 
60
 
 
61
    """
 
62
 
 
63
    def __init__(self, repo, other=None, thorough=False):
 
64
        """Construct a RepoReconciler.
 
65
 
 
66
        :param thorough: perform a thorough check which may take longer but
 
67
                         will correct non-data loss issues such as incorrect
 
68
                         cached data.
 
69
        """
 
70
        self.repo = repo
 
71
 
 
72
    def reconcile(self):
 
73
        """Perform reconciliation.
 
74
 
 
75
        After reconciliation the following attributes document found issues:
 
76
        inconsistent_parents: The number of revisions in the repository whose
 
77
                              ancestry was being reported incorrectly.
 
78
        garbage_inventories: The number of inventory objects without revisions
 
79
                             that were garbage collected.
 
80
        """
 
81
 
 
82
 
 
83
class GitCheck(check.Check):
 
84
 
 
85
    def __init__(self, repository, check_repo=True):
 
86
        self.repository = repository
 
87
        self.checked_rev_cnt = 0
 
88
 
 
89
    def check(self, callback_refs=None, check_repo=True):
 
90
        if callback_refs is None:
 
91
            callback_refs = {}
 
92
        self.repository.lock_read()
 
93
        self.repository.unlock()
 
94
 
 
95
    def report_results(self, verbose):
 
96
        pass
 
97
 
 
98
 
 
99
class GitRepository(ForeignRepository):
 
100
    """An adapter to git repositories for bzr."""
 
101
 
 
102
    _serializer = None
 
103
    vcs = foreign_vcs_git
 
104
    chk_bytes = None
 
105
 
 
106
    def __init__(self, gitdir, lockfiles):
 
107
        ForeignRepository.__init__(self, GitRepositoryFormat(), gitdir, lockfiles)
 
108
        from bzrlib.plugins.git import fetch, push
 
109
        for optimiser in [fetch.InterRemoteGitNonGitRepository,
 
110
                          fetch.InterLocalGitNonGitRepository,
 
111
                          fetch.InterGitGitRepository,
 
112
                          push.InterToLocalGitRepository,
 
113
                          push.InterToRemoteGitRepository]:
 
114
            repository.InterRepository.register_optimiser(optimiser)
 
115
 
 
116
    def add_fallback_repository(self, basis_url):
 
117
        raise errors.UnstackableRepositoryFormat(self._format, self.control_transport.base)
 
118
 
 
119
    def is_shared(self):
 
120
        return False
 
121
 
 
122
    def reconcile(self, other=None, thorough=False):
 
123
        """Reconcile this repository."""
 
124
        reconciler = RepoReconciler(self, thorough=thorough)
 
125
        reconciler.reconcile()
 
126
        return reconciler
 
127
 
 
128
    def supports_rich_root(self):
 
129
        return True
 
130
 
 
131
    def _warn_if_deprecated(self, branch=None): # for bzr < 2.4
 
132
        # This class isn't deprecated
 
133
        pass
 
134
 
 
135
    def get_mapping(self):
 
136
        return default_mapping
 
137
 
 
138
    def make_working_trees(self):
 
139
        return not self._git.bare
 
140
 
 
141
    def revision_graph_can_have_wrong_parents(self):
 
142
        return False
 
143
 
 
144
    def dfetch(self, source, stop_revision):
 
145
        interrepo = repository.InterRepository.get(source, self)
 
146
        return interrepo.dfetch(stop_revision)
 
147
 
 
148
    def add_signature_text(self, revid, signature):
 
149
        raise errors.UnsupportedOperation(self.add_signature_text, self)
 
150
 
 
151
 
 
152
class LocalGitRepository(GitRepository):
 
153
    """Git repository on the file system."""
 
154
 
 
155
    def __init__(self, gitdir, lockfiles):
 
156
        GitRepository.__init__(self, gitdir, lockfiles)
 
157
        self.base = gitdir.root_transport.base
 
158
        self._git = gitdir._git
 
159
        self.signatures = None
 
160
        self.revisions = None
 
161
        self.inventories = None
 
162
        self.texts = None
 
163
 
 
164
    def get_commit_builder(self, branch, parents, config, timestamp=None,
 
165
                           timezone=None, committer=None, revprops=None,
 
166
                           revision_id=None, lossy=False):
 
167
        """Obtain a CommitBuilder for this repository.
 
168
 
 
169
        :param branch: Branch to commit to.
 
170
        :param parents: Revision ids of the parents of the new revision.
 
171
        :param config: Configuration to use.
 
172
        :param timestamp: Optional timestamp recorded for commit.
 
173
        :param timezone: Optional timezone for timestamp.
 
174
        :param committer: Optional committer to set for commit.
 
175
        :param revprops: Optional dictionary of revision properties.
 
176
        :param revision_id: Optional revision id.
 
177
        :param lossy: Whether to discard data that can not be natively
 
178
            represented, when pushing to a foreign VCS
 
179
        """
 
180
        self.start_write_group()
 
181
        return GitCommitBuilder(self, parents, config,
 
182
            timestamp, timezone, committer, revprops, revision_id,
 
183
            lossy)
 
184
 
 
185
    def iter_files_bytes(self, desired_files):
 
186
        """Iterate through file versions.
 
187
 
 
188
        Files will not necessarily be returned in the order they occur in
 
189
        desired_files.  No specific order is guaranteed.
 
190
 
 
191
        Yields pairs of identifier, bytes_iterator.  identifier is an opaque
 
192
        value supplied by the caller as part of desired_files.  It should
 
193
        uniquely identify the file version in the caller's context.  (Examples:
 
194
        an index number or a TreeTransform trans_id.)
 
195
 
 
196
        bytes_iterator is an iterable of bytestrings for the file.  The
 
197
        kind of iterable and length of the bytestrings are unspecified, but for
 
198
        this implementation, it is a list of bytes produced by
 
199
        VersionedFile.get_record_stream().
 
200
 
 
201
        :param desired_files: a list of (file_id, revision_id, identifier)
 
202
            triples
 
203
        """
 
204
        per_revision = {}
 
205
        for (file_id, revision_id, identifier) in desired_files:
 
206
            per_revision.setdefault(revision_id, []).append((file_id, identifier))
 
207
        for revid, files in per_revision.iteritems():
 
208
            (commit_id, mapping) = self.lookup_bzr_revision_id(revid)
 
209
            try:
 
210
                commit = self._git.object_store[commit_id]
 
211
            except KeyError:
 
212
                raise errors.RevisionNotPresent(revid, self)
 
213
            root_tree = commit.tree
 
214
            for fileid, identifier in files:
 
215
                path = mapping.parse_file_id(fileid)
 
216
                try:
 
217
                    obj = tree_lookup_path(
 
218
                        self._git.object_store.__getitem__, root_tree, path)
 
219
                    if isinstance(obj, tuple):
 
220
                        (mode, item_id) = obj
 
221
                        obj = self._git.object_store[item_id]
 
222
                except KeyError:
 
223
                    raise errors.RevisionNotPresent((fileid, revid), self)
 
224
                else:
 
225
                    if obj.type_name == "tree":
 
226
                        yield (identifier, [])
 
227
                    elif obj.type_name == "blob":
 
228
                        yield (identifier, obj.chunked)
 
229
                    else:
 
230
                        raise AssertionError("file text resolved to %r" % obj)
 
231
 
 
232
 
 
233
    def _iter_revision_ids(self):
 
234
        mapping = self.get_mapping()
 
235
        for sha in self._git.object_store:
 
236
            o = self._git.object_store[sha]
 
237
            if not isinstance(o, Commit):
 
238
                continue
 
239
            rev, roundtrip_revid, verifiers = mapping.import_commit(o,
 
240
                mapping.revision_id_foreign_to_bzr)
 
241
            yield o.id, rev.revision_id, roundtrip_revid
 
242
 
 
243
    def all_revision_ids(self):
 
244
        ret = set([])
 
245
        for git_sha, revid, roundtrip_revid in self._iter_revision_ids():
 
246
            ret.add(revid)
 
247
            if roundtrip_revid:
 
248
                ret.add(roundtrip_revid)
 
249
        return ret
 
250
 
 
251
    def get_parent_map(self, revids):
 
252
        parent_map = {}
 
253
        for revision_id in revids:
 
254
            assert isinstance(revision_id, str)
 
255
            if revision_id == revision.NULL_REVISION:
 
256
                parent_map[revision_id] = ()
 
257
                continue
 
258
            hexsha, mapping = self.lookup_bzr_revision_id(revision_id)
 
259
            try:
 
260
                commit = self._git[hexsha]
 
261
            except KeyError:
 
262
                continue
 
263
            parents = [
 
264
                self.lookup_foreign_revision_id(p, mapping)
 
265
                for p in commit.parents]
 
266
            if parents == []:
 
267
                parents = [revision.NULL_REVISION]
 
268
            parent_map[revision_id] = tuple(parents)
 
269
        return parent_map
 
270
 
 
271
    def get_ancestry(self, revision_id, topo_sorted=True):
 
272
        """See Repository.get_ancestry().
 
273
        """
 
274
        if revision_id is None:
 
275
            return [None, revision.NULL_REVISION] + self._all_revision_ids()
 
276
        assert isinstance(revision_id, str)
 
277
        ancestry = []
 
278
        graph = self.get_graph()
 
279
        for rev, parents in graph.iter_ancestry([revision_id]):
 
280
            ancestry.append(rev)
 
281
        if revision.NULL_REVISION in ancestry:
 
282
            ancestry.remove(revision.NULL_REVISION)
 
283
        ancestry.reverse()
 
284
        return [None] + ancestry
 
285
 
 
286
    def get_signature_text(self, revision_id):
 
287
        raise errors.NoSuchRevision(self, revision_id)
 
288
 
 
289
    def check(self, revision_ids=None, callback_refs=None, check_repo=True):
 
290
        result = GitCheck(self, check_repo=check_repo)
 
291
        result.check(callback_refs)
 
292
        return result
 
293
 
 
294
    def pack(self, hint=None, clean_obsolete_packs=False):
 
295
        self._git.object_store.pack_loose_objects()
 
296
 
 
297
    def lookup_foreign_revision_id(self, foreign_revid, mapping=None):
 
298
        """Lookup a revision id.
 
299
 
 
300
        """
 
301
        assert type(foreign_revid) is str
 
302
        if mapping is None:
 
303
            mapping = self.get_mapping()
 
304
        if foreign_revid == ZERO_SHA:
 
305
            return revision.NULL_REVISION
 
306
        commit = self._git.object_store[foreign_revid]
 
307
        while isinstance(commit, Tag):
 
308
            commit = self._git[commit.object[1]]
 
309
        rev, roundtrip_revid, verifiers = mapping.import_commit(commit,
 
310
            mapping.revision_id_foreign_to_bzr)
 
311
        # FIXME: check testament before doing this?
 
312
        if roundtrip_revid:
 
313
            return roundtrip_revid
 
314
        else:
 
315
            return rev.revision_id
 
316
 
 
317
    def has_signature_for_revision_id(self, revision_id):
 
318
        return False
 
319
 
 
320
    def lookup_bzr_revision_id(self, bzr_revid, mapping=None):
 
321
        try:
 
322
            return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
 
323
        except errors.InvalidRevisionId:
 
324
            if mapping is None:
 
325
                mapping = self.get_mapping()
 
326
            try:
 
327
                return (self._git.refs[mapping.revid_as_refname(bzr_revid)], mapping)
 
328
            except KeyError:
 
329
                # Update refs from Git commit objects
 
330
                # FIXME: Hitting this a lot will be very inefficient...
 
331
                for git_sha, revid, roundtrip_revid in self._iter_revision_ids():
 
332
                    if not roundtrip_revid:
 
333
                        continue
 
334
                    refname = mapping.revid_as_refname(roundtrip_revid)
 
335
                    self._git.refs[refname] = git_sha
 
336
                    if roundtrip_revid == bzr_revid:
 
337
                        return git_sha, mapping
 
338
                raise errors.NoSuchRevision(self, bzr_revid)
 
339
 
 
340
    def get_revision(self, revision_id):
 
341
        if not isinstance(revision_id, str):
 
342
            raise errors.InvalidRevisionId(revision_id, self)
 
343
        git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
 
344
        try:
 
345
            commit = self._git[git_commit_id]
 
346
        except KeyError:
 
347
            raise errors.NoSuchRevision(self, revision_id)
 
348
        revision, roundtrip_revid, verifiers = mapping.import_commit(
 
349
            commit, self.lookup_foreign_revision_id)
 
350
        assert revision is not None
 
351
        # FIXME: check verifiers ?
 
352
        if roundtrip_revid:
 
353
            revision.revision_id = roundtrip_revid
 
354
        return revision
 
355
 
 
356
    def has_revision(self, revision_id):
 
357
        """See Repository.has_revision."""
 
358
        if revision_id == revision.NULL_REVISION:
 
359
            return True
 
360
        try:
 
361
            git_commit_id, mapping = self.lookup_bzr_revision_id(revision_id)
 
362
        except errors.NoSuchRevision:
 
363
            return False
 
364
        return (git_commit_id in self._git)
 
365
 
 
366
    def has_revisions(self, revision_ids):
 
367
        """See Repository.has_revisions."""
 
368
        return set(filter(self.has_revision, revision_ids))
 
369
 
 
370
    def get_revisions(self, revids):
 
371
        """See Repository.get_revisions."""
 
372
        return [self.get_revision(r) for r in revids]
 
373
 
 
374
    def revision_trees(self, revids):
 
375
        """See Repository.revision_trees."""
 
376
        for revid in revids:
 
377
            yield self.revision_tree(revid)
 
378
 
 
379
    def revision_tree(self, revision_id):
 
380
        """See Repository.revision_tree."""
 
381
        revision_id = revision.ensure_null(revision_id)
 
382
        if revision_id == revision.NULL_REVISION:
 
383
            inv = inventory.Inventory(root_id=None)
 
384
            inv.revision_id = revision_id
 
385
            return InventoryRevisionTree(self, inv, revision_id)
 
386
        return GitRevisionTree(self, revision_id)
 
387
 
 
388
    def get_inventory(self, revision_id):
 
389
        raise NotImplementedError(self.get_inventory)
 
390
 
 
391
    def set_make_working_trees(self, trees):
 
392
        # TODO: Set bare= in the configuration bug=777065
 
393
        raise NotImplementedError(self.set_make_working_trees)
 
394
 
 
395
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
 
396
        progress=None):
 
397
        return self._git.fetch_objects(determine_wants, graph_walker, progress)
 
398
 
 
399
 
 
400
class GitRepositoryFormat(repository.RepositoryFormat):
 
401
    """Git repository format."""
 
402
 
 
403
    supports_tree_reference = False
 
404
    rich_root_data = True
 
405
    supports_leaving_lock = False
 
406
    fast_deltas = True
 
407
    supports_funky_characters = True
 
408
    supports_external_lookups = False
 
409
    supports_full_versioned_files = False
 
410
    supports_revision_signatures = False
 
411
    revision_graph_can_have_wrong_parents = False
 
412
 
 
413
    @property
 
414
    def _matchingbzrdir(self):
 
415
        from bzrlib.plugins.git.dir import LocalGitControlDirFormat
 
416
        return LocalGitControlDirFormat()
 
417
 
 
418
    def get_format_description(self):
 
419
        return "Git Repository"
 
420
 
 
421
    def initialize(self, controldir, shared=False, _internal=False):
 
422
        from bzrlib.plugins.git.dir import GitDir
 
423
        if not isinstance(controldir, GitDir):
 
424
            raise errors.UninitializableFormat(self)
 
425
        return controldir.open_repository()
 
426
 
 
427
    def check_conversion_target(self, target_repo_format):
 
428
        return target_repo_format.rich_root_data
 
429
 
 
430
    def get_foreign_tests_repository_factory(self):
 
431
        from bzrlib.plugins.git.tests.test_repository import (
 
432
            ForeignTestsRepositoryFactory,
 
433
            )
 
434
        return ForeignTestsRepositoryFactory()
 
435
 
 
436
    def network_name(self):
 
437
        return "git"