/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 branch cloning.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2007 Canonical Ltd
2
 
# Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org>
3
2
#
4
3
# This program is free software; you can redistribute it and/or modify
5
4
# it under the terms of the GNU General Public License as published by
17
16
 
18
17
"""An adapter between a Git Repository and a Bazaar Branch"""
19
18
 
 
19
import os
 
20
import time
 
21
 
20
22
import bzrlib
21
23
from bzrlib import (
 
24
    deprecated_graph,
22
25
    errors,
23
26
    graph,
24
27
    inventory,
26
29
    repository,
27
30
    revision,
28
31
    revisiontree,
29
 
    ui,
30
32
    urlutils,
 
33
    versionedfile,
31
34
    )
32
35
from bzrlib.foreign import (
33
 
    ForeignRepository,
34
 
    )
35
 
from bzrlib.trace import (
36
 
    mutter,
37
 
    )
38
 
from bzrlib.transport import (
39
 
    get_transport,
40
 
    )
 
36
        ForeignRepository,
 
37
        )
 
38
from bzrlib.trace import mutter
 
39
from bzrlib.transport import get_transport
41
40
 
42
41
from bzrlib.plugins.git.foreign import (
43
42
    versionedfiles,
44
43
    )
45
 
from bzrlib.plugins.git.mapping import (
46
 
    default_mapping,
47
 
    inventory_to_tree_and_blobs,
48
 
    mapping_registry,
49
 
    revision_to_commit,
50
 
    )
51
 
from bzrlib.plugins.git.versionedfiles import (
52
 
    GitTexts,
53
 
    )
 
44
from bzrlib.plugins.git.mapping import default_mapping
54
45
 
55
 
import dulwich as git
56
 
import os
57
 
import time
 
46
from bzrlib.plugins.git import git
58
47
 
59
48
 
60
49
class GitTags(object):
72
61
    _serializer = None
73
62
 
74
63
    def __init__(self, gitdir, lockfiles):
75
 
        ForeignRepository.__init__(self, GitRepositoryFormat(), gitdir, 
76
 
            lockfiles)
77
 
        from bzrlib.plugins.git import fetch, push
78
 
        for optimiser in [fetch.InterGitRepository, 
79
 
                          fetch.InterGitNonGitRepository,
80
 
                          push.InterToGitRepository]:
81
 
            repository.InterRepository.register_optimiser(optimiser)
 
64
        ForeignRepository.__init__(self, GitFormat(), gitdir, lockfiles)
 
65
        from bzrlib.plugins.git import fetch
 
66
        repository.InterRepository.register_optimiser(fetch.InterGitRepository)
82
67
 
83
68
    def is_shared(self):
84
69
        return True
98
83
 
99
84
 
100
85
class LocalGitRepository(GitRepository):
101
 
    """Git repository on the file system."""
102
86
 
103
87
    def __init__(self, gitdir, lockfiles):
104
88
        # FIXME: This also caches negatives. Need to be more careful 
110
94
        self.texts = None
111
95
        self.signatures = versionedfiles.VirtualSignatureTexts(self)
112
96
        self.revisions = versionedfiles.VirtualRevisionTexts(self)
113
 
        self.inventories = versionedfiles.VirtualInventoryTexts(self)
114
 
        self.texts = GitTexts(self)
115
97
        self.tags = GitTags(self._git.get_tags())
116
98
 
117
99
    def all_revision_ids(self):
118
100
        ret = set([revision.NULL_REVISION])
119
 
        heads = self._git.heads()
120
 
        if heads == {}:
 
101
        if self._git.heads() == []:
121
102
            return ret
122
 
        bzr_heads = [self.get_mapping().revision_id_foreign_to_bzr(h) for h in heads.itervalues()]
 
103
        bzr_heads = [self.get_mapping().revision_id_foreign_to_bzr(h) for h in self._git.heads()]
123
104
        ret = set(bzr_heads)
124
105
        graph = self.get_graph()
125
106
        for rev, parents in graph.iter_ancestry(bzr_heads):
143
124
            if revision_id == revision.NULL_REVISION:
144
125
                parent_map[revision_id] = ()
145
126
                continue
146
 
            hexsha, mapping = self.lookup_git_revid(revision_id)
 
127
            hexsha = self.lookup_git_revid(revision_id, self.get_mapping())
147
128
            commit  = self._git.commit(hexsha)
148
129
            if commit is None:
149
130
                continue
150
131
            else:
151
 
                parent_map[revision_id] = [mapping.revision_id_foreign_to_bzr(p) for p in commit.parents]
 
132
                parent_map[revision_id] = [self.get_mapping().revision_id_foreign_to_bzr(p) for p in commit.parents]
152
133
        return parent_map
153
134
 
154
135
    def get_ancestry(self, revision_id, topo_sorted=True):
155
136
        """See Repository.get_ancestry().
156
137
        """
157
138
        if revision_id is None:
158
 
            return [None, revision.NULL_REVISION] + self._all_revision_ids()
 
139
            return self._all_revision_ids()
159
140
        assert isinstance(revision_id, str)
160
141
        ancestry = []
161
142
        graph = self.get_graph()
162
143
        for rev, parents in graph.iter_ancestry([revision_id]):
 
144
            if rev == revision.NULL_REVISION:
 
145
                rev = None
163
146
            ancestry.append(rev)
164
147
        ancestry.reverse()
165
 
        return [None] + ancestry
166
 
 
167
 
    def import_revision_gist(self, source, revid, parent_lookup):
168
 
        """Import the gist of a revision into this Git repository.
169
 
 
170
 
        """
171
 
        objects = []
172
 
        rev = source.get_revision(revid)
173
 
        for sha, object, path in inventory_to_tree_and_blobs(source, None, revid):
174
 
            if path == "":
175
 
                tree_sha = sha
176
 
            objects.append((object, path))
177
 
        commit = revision_to_commit(rev, tree_sha, parent_lookup)
178
 
        objects.append((commit, None))
179
 
        self._git.object_store.add_objects(objects)
180
 
        return commit.sha().hexdigest()
181
 
 
182
 
    def dfetch(self, source, stop_revision):
183
 
        """Import the gist of the ancestry of a particular revision."""
184
 
        if stop_revision is None:
185
 
            raise NotImplementedError
186
 
        revidmap = {}
187
 
        gitidmap = {}
188
 
        def parent_lookup(revid):
189
 
            try:
190
 
                return gitidmap[revid]
191
 
            except KeyError:
192
 
                return self.lookup_git_revid(revid)[0]
193
 
        todo = []
194
 
        source.lock_write()
195
 
        try:
196
 
            graph = source.get_graph()
197
 
            ancestry = [x for x in source.get_ancestry(stop_revision) if x is not None]
198
 
            for revid in graph.iter_topo_order(ancestry):
199
 
                if not self.has_revision(revid):
200
 
                    todo.append(revid)
201
 
            pb = ui.ui_factory.nested_progress_bar()
202
 
            try:
203
 
                for i, revid in enumerate(todo):
204
 
                    pb.update("pushing revisions", i, len(todo))
205
 
                    git_commit = self.import_revision_gist(source, revid,
206
 
                        parent_lookup)
207
 
                    gitidmap[revid] = git_commit
208
 
                    git_revid = self.get_mapping().revision_id_foreign_to_bzr(
209
 
                        git_commit)
210
 
                    revidmap[revid] = git_revid
211
 
            finally:
212
 
                pb.finished()
213
 
            source.fetch(self, revision_id=revidmap[stop_revision])
214
 
        finally:
215
 
            source.unlock()
216
 
        return revidmap
 
148
        return ancestry
217
149
 
218
150
    def get_signature_text(self, revision_id):
219
151
        raise errors.NoSuchRevision(self, revision_id)
230
162
    def has_signature_for_revision_id(self, revision_id):
231
163
        return False
232
164
 
233
 
    def lookup_git_revid(self, bzr_revid):
 
165
    def lookup_git_revid(self, bzr_revid, mapping):
234
166
        try:
235
 
            return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
 
167
            return mapping.revision_id_bzr_to_foreign(bzr_revid)
236
168
        except errors.InvalidRevisionId:
237
169
            raise errors.NoSuchRevision(self, bzr_revid)
238
170
 
239
171
    def get_revision(self, revision_id):
240
 
        git_commit_id, mapping = self.lookup_git_revid(revision_id)
 
172
        git_commit_id = self.lookup_git_revid(revision_id, self.get_mapping())
241
173
        try:
242
174
            commit = self._git.commit(git_commit_id)
243
175
        except KeyError:
244
176
            raise errors.NoSuchRevision(self, revision_id)
245
177
        # print "fetched revision:", git_commit_id
246
 
        revision = mapping.import_commit(commit)
 
178
        revision = self.get_mapping().import_commit(commit)
247
179
        assert revision is not None
248
180
        return revision
249
181
 
264
196
 
265
197
    def revision_tree(self, revision_id):
266
198
        revision_id = revision.ensure_null(revision_id)
 
199
 
267
200
        if revision_id == revision.NULL_REVISION:
268
201
            inv = inventory.Inventory(root_id=None)
269
202
            inv.revision_id = revision_id
270
203
            return revisiontree.RevisionTree(self, inv, revision_id)
271
 
        return GitRevisionTree(self, revision_id)
 
204
 
 
205
        return GitRevisionTree(self, self.get_mapping(), revision_id)
272
206
 
273
207
    def get_inventory(self, revision_id):
274
208
        assert revision_id != None
277
211
    def set_make_working_trees(self, trees):
278
212
        pass
279
213
 
280
 
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref,
281
 
        progress=None):
 
214
    def fetch_objects(self, determine_wants, graph_walker, progress=None):
282
215
        return self._git.fetch_objects(determine_wants, graph_walker, progress)
283
216
 
284
217
 
285
218
class GitRevisionTree(revisiontree.RevisionTree):
286
219
 
287
 
    def __init__(self, repository, revision_id):
 
220
    def __init__(self, repository, mapping, revision_id):
288
221
        self._repository = repository
289
222
        self.revision_id = revision_id
290
223
        assert isinstance(revision_id, str)
291
 
        git_id, self.mapping = repository.lookup_git_revid(revision_id)
 
224
        self.mapping = mapping
 
225
        git_id = repository.lookup_git_revid(revision_id, self.mapping)
292
226
        try:
293
227
            commit = repository._git.commit(git_id)
294
228
        except KeyError, r:
344
278
                self._build_inventory(hexsha, child_ie, child_path)
345
279
 
346
280
 
347
 
class GitRepositoryFormat(repository.RepositoryFormat):
 
281
class GitFormat(object):
348
282
 
349
283
    supports_tree_reference = False
350
284
    rich_root_data = True