/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
22
21
 
23
22
import bzrlib
24
23
from bzrlib import (
 
24
    deprecated_graph,
25
25
    errors,
26
26
    graph,
27
27
    inventory,
29
29
    repository,
30
30
    revision,
31
31
    revisiontree,
32
 
    ui,
33
32
    urlutils,
 
33
    versionedfile,
34
34
    )
35
35
from bzrlib.foreign import (
36
36
        ForeignRepository,
41
41
from bzrlib.plugins.git.foreign import (
42
42
    versionedfiles,
43
43
    )
44
 
from bzrlib.plugins.git.mapping import default_mapping, mapping_registry, inventory_to_tree_and_blobs, revision_to_commit
45
 
from bzrlib.plugins.git.versionedfiles import GitTexts
 
44
from bzrlib.plugins.git.mapping import default_mapping
46
45
 
47
 
import dulwich as git
 
46
from bzrlib.plugins.git import git
48
47
 
49
48
 
50
49
class GitTags(object):
65
64
        ForeignRepository.__init__(self, GitFormat(), gitdir, lockfiles)
66
65
        from bzrlib.plugins.git import fetch
67
66
        repository.InterRepository.register_optimiser(fetch.InterGitRepository)
68
 
        repository.InterRepository.register_optimiser(fetch.InterGitNonGitRepository)
69
67
 
70
68
    def is_shared(self):
71
69
        return True
96
94
        self.texts = None
97
95
        self.signatures = versionedfiles.VirtualSignatureTexts(self)
98
96
        self.revisions = versionedfiles.VirtualRevisionTexts(self)
99
 
        self.inventories = versionedfiles.VirtualInventoryTexts(self)
100
 
        self.texts = GitTexts(self)
101
97
        self.tags = GitTags(self._git.get_tags())
102
98
 
103
99
    def all_revision_ids(self):
104
100
        ret = set([revision.NULL_REVISION])
105
 
        heads = self._git.heads()
106
 
        if heads == {}:
 
101
        if self._git.heads() == []:
107
102
            return ret
108
 
        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()]
109
104
        ret = set(bzr_heads)
110
105
        graph = self.get_graph()
111
106
        for rev, parents in graph.iter_ancestry(bzr_heads):
129
124
            if revision_id == revision.NULL_REVISION:
130
125
                parent_map[revision_id] = ()
131
126
                continue
132
 
            hexsha, mapping = self.lookup_git_revid(revision_id)
 
127
            hexsha = self.lookup_git_revid(revision_id, self.get_mapping())
133
128
            commit  = self._git.commit(hexsha)
134
129
            if commit is None:
135
130
                continue
136
131
            else:
137
 
                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]
138
133
        return parent_map
139
134
 
140
135
    def get_ancestry(self, revision_id, topo_sorted=True):
141
136
        """See Repository.get_ancestry().
142
137
        """
143
138
        if revision_id is None:
144
 
            return [None, revision.NULL_REVISION] + self._all_revision_ids()
 
139
            return self._all_revision_ids()
145
140
        assert isinstance(revision_id, str)
146
141
        ancestry = []
147
142
        graph = self.get_graph()
148
143
        for rev, parents in graph.iter_ancestry([revision_id]):
 
144
            if rev == revision.NULL_REVISION:
 
145
                rev = None
149
146
            ancestry.append(rev)
150
147
        ancestry.reverse()
151
 
        return [None] + ancestry
152
 
 
153
 
    def import_revision_gist(self, source, revid, parent_lookup):
154
 
        """Impor the gist of another revision into this Git repository.
155
 
 
156
 
        """
157
 
        objects = []
158
 
        rev = source.get_revision(revid)
159
 
        for sha, object, path in inventory_to_tree_and_blobs(source, None, revid):
160
 
            if path == "":
161
 
                tree_sha = sha
162
 
            objects.append((object, path))
163
 
        commit = revision_to_commit(rev, tree_sha, parent_lookup)
164
 
        objects.append((commit, None))
165
 
        self._git.object_store.add_objects(objects)
166
 
        return commit.sha().hexdigest()
167
 
 
168
 
    def dfetch(self, source, stop_revision):
169
 
        if stop_revision is None:
170
 
            raise NotImplementedError
171
 
        revidmap = {}
172
 
        gitidmap = {}
173
 
        todo = []
174
 
        source.lock_write()
175
 
        try:
176
 
            graph = source.get_graph()
177
 
            ancestry = [x for x in source.get_ancestry(stop_revision) if x is not None]
178
 
            for revid in graph.iter_topo_order(ancestry):
179
 
                if not self.has_revision(revid):
180
 
                    todo.append(revid)
181
 
            pb = ui.ui_factory.nested_progress_bar()
182
 
            try:
183
 
                for i, revid in enumerate(todo):
184
 
                    pb.update("pushing revisions", i, len(todo))
185
 
                    git_commit = self.import_revision_gist(source, revid, gitidmap.__getitem__)
186
 
                    gitidmap[revid] = git_commit
187
 
                    git_revid = self.get_mapping().revision_id_foreign_to_bzr(git_commit)
188
 
                    revidmap[revid] = git_revid
189
 
            finally:
190
 
                pb.finished()
191
 
            source.fetch(self, revision_id=revidmap[stop_revision])
192
 
        finally:
193
 
            source.unlock()
194
 
        return revidmap
 
148
        return ancestry
195
149
 
196
150
    def get_signature_text(self, revision_id):
197
151
        raise errors.NoSuchRevision(self, revision_id)
208
162
    def has_signature_for_revision_id(self, revision_id):
209
163
        return False
210
164
 
211
 
    def lookup_git_revid(self, bzr_revid):
 
165
    def lookup_git_revid(self, bzr_revid, mapping):
212
166
        try:
213
 
            return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
 
167
            return mapping.revision_id_bzr_to_foreign(bzr_revid)
214
168
        except errors.InvalidRevisionId:
215
169
            raise errors.NoSuchRevision(self, bzr_revid)
216
170
 
217
171
    def get_revision(self, revision_id):
218
 
        git_commit_id, mapping = self.lookup_git_revid(revision_id)
 
172
        git_commit_id = self.lookup_git_revid(revision_id, self.get_mapping())
219
173
        try:
220
174
            commit = self._git.commit(git_commit_id)
221
175
        except KeyError:
222
176
            raise errors.NoSuchRevision(self, revision_id)
223
177
        # print "fetched revision:", git_commit_id
224
 
        revision = mapping.import_commit(commit)
 
178
        revision = self.get_mapping().import_commit(commit)
225
179
        assert revision is not None
226
180
        return revision
227
181
 
248
202
            inv.revision_id = revision_id
249
203
            return revisiontree.RevisionTree(self, inv, revision_id)
250
204
 
251
 
        return GitRevisionTree(self, revision_id)
 
205
        return GitRevisionTree(self, self.get_mapping(), revision_id)
252
206
 
253
207
    def get_inventory(self, revision_id):
254
208
        assert revision_id != None
257
211
    def set_make_working_trees(self, trees):
258
212
        pass
259
213
 
260
 
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref, progress=None):
 
214
    def fetch_objects(self, determine_wants, graph_walker, progress=None):
261
215
        return self._git.fetch_objects(determine_wants, graph_walker, progress)
262
216
 
263
217
 
264
218
class GitRevisionTree(revisiontree.RevisionTree):
265
219
 
266
 
    def __init__(self, repository, revision_id):
 
220
    def __init__(self, repository, mapping, revision_id):
267
221
        self._repository = repository
268
222
        self.revision_id = revision_id
269
223
        assert isinstance(revision_id, str)
270
 
        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)
271
226
        try:
272
227
            commit = repository._git.commit(git_id)
273
228
        except KeyError, r: