/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

Cope with tags during fetch.

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