/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

Reoncile InterGitRepository objects.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
import bzrlib
23
23
from bzrlib import (
 
24
    deprecated_graph,
24
25
    errors,
25
26
    graph,
26
27
    inventory,
28
29
    repository,
29
30
    revision,
30
31
    revisiontree,
31
 
    ui,
32
32
    urlutils,
 
33
    versionedfile,
33
34
    )
34
35
from bzrlib.foreign import (
35
36
        ForeignRepository,
 
37
        ForeignRevision,
36
38
        )
37
39
from bzrlib.trace import mutter
38
40
from bzrlib.transport import get_transport
40
42
from bzrlib.plugins.git.foreign import (
41
43
    versionedfiles,
42
44
    )
43
 
from bzrlib.plugins.git.mapping import default_mapping, mapping_registry, inventory_to_tree_and_blobs, revision_to_commit
44
 
from bzrlib.plugins.git.versionedfiles import GitTexts
 
45
from bzrlib.plugins.git.mapping import default_mapping
45
46
 
46
 
import dulwich as git
 
47
from bzrlib.plugins.git import git
47
48
 
48
49
 
49
50
class GitTags(object):
64
65
        ForeignRepository.__init__(self, GitFormat(), gitdir, lockfiles)
65
66
        from bzrlib.plugins.git import fetch
66
67
        repository.InterRepository.register_optimiser(fetch.InterGitRepository)
67
 
        repository.InterRepository.register_optimiser(fetch.InterGitNonGitRepository)
68
68
 
69
69
    def is_shared(self):
70
70
        return True
79
79
    def get_mapping(self):
80
80
        return default_mapping
81
81
 
82
 
    def make_working_trees(self):
83
 
        return True
84
82
 
85
83
 
86
84
class LocalGitRepository(GitRepository):
95
93
        self.texts = None
96
94
        self.signatures = versionedfiles.VirtualSignatureTexts(self)
97
95
        self.revisions = versionedfiles.VirtualRevisionTexts(self)
98
 
        self.inventories = versionedfiles.VirtualInventoryTexts(self)
99
 
        self.texts = GitTexts(self)
100
96
        self.tags = GitTags(self._git.get_tags())
101
97
 
102
98
    def all_revision_ids(self):
127
123
            if revision_id == revision.NULL_REVISION:
128
124
                parent_map[revision_id] = ()
129
125
                continue
130
 
            hexsha, mapping = self.lookup_git_revid(revision_id)
 
126
            hexsha = self.lookup_git_revid(revision_id, self.get_mapping())
131
127
            commit  = self._git.commit(hexsha)
132
128
            if commit is None:
133
129
                continue
134
130
            else:
135
 
                parent_map[revision_id] = [mapping.revision_id_foreign_to_bzr(p) for p in commit.parents]
 
131
                parent_map[revision_id] = [self.get_mapping().revision_id_foreign_to_bzr(p) for p in commit.parents]
136
132
        return parent_map
137
133
 
138
134
    def get_ancestry(self, revision_id, topo_sorted=True):
150
146
        ancestry.reverse()
151
147
        return ancestry
152
148
 
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
195
 
 
196
149
    def get_signature_text(self, revision_id):
197
150
        raise errors.NoSuchRevision(self, revision_id)
198
151
 
208
161
    def has_signature_for_revision_id(self, revision_id):
209
162
        return False
210
163
 
211
 
    def lookup_git_revid(self, bzr_revid):
 
164
    def lookup_git_revid(self, bzr_revid, mapping):
212
165
        try:
213
 
            return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
 
166
            return mapping.revision_id_bzr_to_foreign(bzr_revid)
214
167
        except errors.InvalidRevisionId:
215
168
            raise errors.NoSuchRevision(self, bzr_revid)
216
169
 
217
170
    def get_revision(self, revision_id):
218
 
        git_commit_id, mapping = self.lookup_git_revid(revision_id)
219
 
        try:
220
 
            commit = self._git.commit(git_commit_id)
221
 
        except KeyError:
 
171
        git_commit_id = self.lookup_git_revid(revision_id, self.get_mapping())
 
172
        commit = self._git.commit(git_commit_id)
 
173
        # print "fetched revision:", git_commit_id
 
174
        if commit is None:
222
175
            raise errors.NoSuchRevision(self, revision_id)
223
 
        # print "fetched revision:", git_commit_id
224
 
        revision = mapping.import_commit(commit)
 
176
        revision = self._parse_rev(commit, self.get_mapping())
225
177
        assert revision is not None
226
178
        return revision
227
179
 
236
188
    def get_revisions(self, revids):
237
189
        return [self.get_revision(r) for r in revids]
238
190
 
 
191
    @classmethod
 
192
    def _parse_rev(klass, commit, mapping):
 
193
        """Convert a git commit to a bzr revision.
 
194
 
 
195
        :return: a `bzrlib.revision.Revision` object.
 
196
        """
 
197
        if commit is None:
 
198
            raise AssertionError("Commit object can't be None")
 
199
        rev = ForeignRevision(commit.id, mapping, mapping.revision_id_foreign_to_bzr(commit.id))
 
200
        rev.parent_ids = tuple([mapping.revision_id_foreign_to_bzr(p) for p in commit.parents])
 
201
        rev.message = commit.message.decode("utf-8", "replace")
 
202
        rev.committer = str(commit.committer).decode("utf-8", "replace")
 
203
        if commit.committer != commit.author:
 
204
            rev.properties['author'] = str(commit.author).decode("utf-8", "replace")
 
205
        rev.timestamp = commit.commit_time
 
206
        rev.timezone = 0
 
207
        return rev
 
208
 
239
209
    def revision_trees(self, revids):
240
210
        for revid in revids:
241
211
            yield self.revision_tree(revid)
257
227
    def set_make_working_trees(self, trees):
258
228
        pass
259
229
 
260
 
    def fetch_objects(self, determine_wants, graph_walker, resolve_ext_ref, progress=None):
261
 
        return self._git.fetch_objects(determine_wants, graph_walker, progress)
 
230
    def fetch_objects(self, determine_wants, graph_walker, pack_data):
 
231
        raise NotImplementedError(self.fetch_objects)
 
232
 
 
233
 
 
234
def escape_file_id(file_id):
 
235
    return file_id.replace('_', '__').replace(' ', '_s')
 
236
 
 
237
 
 
238
def unescape_file_id(file_id):
 
239
    return file_id.replace("_s", " ").replace("__", "_")
262
240
 
263
241
 
264
242
class GitRevisionTree(revisiontree.RevisionTree):
266
244
    def __init__(self, repository, revision_id):
267
245
        self._repository = repository
268
246
        self.revision_id = revision_id
269
 
        assert isinstance(revision_id, str)
270
 
        git_id, self.mapping = repository.lookup_git_revid(revision_id)
271
 
        try:
272
 
            commit = repository._git.commit(git_id)
273
 
        except KeyError, r:
274
 
            raise errors.NoSuchRevision(repository, revision_id)
275
 
        self.tree = commit.tree
 
247
        git_id = repository.lookup_git_revid(revision_id, repository.get_mapping())
 
248
        self.tree = repository._git.commit(git_id).tree
276
249
        self._inventory = inventory.Inventory(revision_id=revision_id)
277
250
        self._inventory.root.revision = revision_id
278
251
        self._build_inventory(self.tree, self._inventory.root, "")
294
267
                child_path = name
295
268
            else:
296
269
                child_path = urlutils.join(path, name)
297
 
            file_id = self.mapping.generate_file_id(child_path)
 
270
            file_id = escape_file_id(child_path.encode('utf-8'))
298
271
            entry_kind = (mode & 0700000) / 0100000
299
272
            if entry_kind == 0:
300
273
                child_ie = inventory.InventoryDirectory(file_id, basename, ie.file_id)