/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

Share more code between local and remote classes, support opening remote branches.

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
    )
35
35
from bzrlib.foreign import (
36
36
        ForeignRepository,
 
37
        ForeignRevision,
37
38
        )
38
39
from bzrlib.trace import mutter
39
40
from bzrlib.transport import get_transport
41
42
from bzrlib.plugins.git.foreign import (
42
43
    versionedfiles,
43
44
    )
44
 
from bzrlib.plugins.git.mapping import default_mapping, mapping_registry
 
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):
63
64
    def __init__(self, gitdir, lockfiles):
64
65
        ForeignRepository.__init__(self, GitFormat(), gitdir, lockfiles)
65
66
        from bzrlib.plugins.git import fetch
66
 
        repository.InterRepository.register_optimiser(fetch.InterGitRepository)
67
 
        repository.InterRepository.register_optimiser(fetch.InterGitNonGitRepository)
 
67
        repository.InterRepository.register_optimiser(fetch.InterFromGitRepository)
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):
125
123
            if revision_id == revision.NULL_REVISION:
126
124
                parent_map[revision_id] = ()
127
125
                continue
128
 
            hexsha, mapping = self.lookup_git_revid(revision_id)
 
126
            hexsha = self.lookup_git_revid(revision_id, self.get_mapping())
129
127
            commit  = self._git.commit(hexsha)
130
128
            if commit is None:
131
129
                continue
132
130
            else:
133
 
                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]
134
132
        return parent_map
135
133
 
136
134
    def get_ancestry(self, revision_id, topo_sorted=True):
163
161
    def has_signature_for_revision_id(self, revision_id):
164
162
        return False
165
163
 
166
 
    def lookup_git_revid(self, bzr_revid):
 
164
    def lookup_git_revid(self, bzr_revid, mapping):
167
165
        try:
168
 
            return mapping_registry.revision_id_bzr_to_foreign(bzr_revid)
 
166
            return mapping.revision_id_bzr_to_foreign(bzr_revid)
169
167
        except errors.InvalidRevisionId:
170
168
            raise errors.NoSuchRevision(self, bzr_revid)
171
169
 
172
170
    def get_revision(self, revision_id):
173
 
        git_commit_id, mapping = self.lookup_git_revid(revision_id)
174
 
        try:
175
 
            commit = self._git.commit(git_commit_id)
176
 
        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:
177
175
            raise errors.NoSuchRevision(self, revision_id)
178
 
        # print "fetched revision:", git_commit_id
179
 
        revision = mapping.import_commit(commit)
 
176
        revision = self._parse_rev(commit, self.get_mapping())
180
177
        assert revision is not None
181
178
        return revision
182
179
 
191
188
    def get_revisions(self, revids):
192
189
        return [self.get_revision(r) for r in revids]
193
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
 
194
209
    def revision_trees(self, revids):
195
210
        for revid in revids:
196
211
            yield self.revision_tree(revid)
212
227
    def set_make_working_trees(self, trees):
213
228
        pass
214
229
 
215
 
    def fetch_objects(self, determine_wants, graph_walker, progress=None):
216
 
        return self._git.fetch_objects(determine_wants, graph_walker, progress)
 
230
    def fetch_pack(self, determine_wants, graph_walker, pack_data):
 
231
        raise NotImplementedError(self.fetch_pack)
 
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("__", "_")
217
240
 
218
241
 
219
242
class GitRevisionTree(revisiontree.RevisionTree):
221
244
    def __init__(self, repository, revision_id):
222
245
        self._repository = repository
223
246
        self.revision_id = revision_id
224
 
        assert isinstance(revision_id, str)
225
 
        git_id, self.mapping = repository.lookup_git_revid(revision_id)
226
 
        try:
227
 
            commit = repository._git.commit(git_id)
228
 
        except KeyError, r:
229
 
            raise errors.NoSuchRevision(repository, revision_id)
230
 
        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
231
249
        self._inventory = inventory.Inventory(revision_id=revision_id)
232
250
        self._inventory.root.revision = revision_id
233
251
        self._build_inventory(self.tree, self._inventory.root, "")
249
267
                child_path = name
250
268
            else:
251
269
                child_path = urlutils.join(path, name)
252
 
            file_id = self.mapping.generate_file_id(child_path)
 
270
            file_id = escape_file_id(child_path.encode('utf-8'))
253
271
            entry_kind = (mode & 0700000) / 0100000
254
272
            if entry_kind == 0:
255
273
                child_ie = inventory.InventoryDirectory(file_id, basename, ie.file_id)