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