/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

Use bzr-foreign function names for converting between git and bzr revids.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
"""An adapter between a Git Repository and a Bazaar Branch"""
18
18
 
 
19
import git
19
20
import os
20
21
import time
21
22
 
31
32
    urlutils,
32
33
    versionedfile,
33
34
    )
34
 
from bzrlib.foreign import (
35
 
        ForeignRevision,
36
 
        )
37
35
from bzrlib.transport import get_transport
38
36
 
39
 
from bzrlib.plugins.git.foreign import (
40
 
    ForeignRepository,
41
 
    versionedfiles,
 
37
from bzrlib.plugins.git import (
 
38
    versionedfiles
42
39
    )
43
40
from bzrlib.plugins.git.mapping import default_mapping
44
41
 
45
 
from bzrlib.plugins.git import git
46
 
 
47
 
 
48
 
class GitTags(object):
49
 
 
50
 
    def __init__(self, tags):
51
 
        self._tags = tags
52
 
 
53
 
    def __iter__(self):
54
 
        return iter(self._tags)
55
 
 
56
 
 
57
 
class GitRepository(ForeignRepository):
 
42
 
 
43
class GitRepository(repository.Repository):
58
44
    """An adapter to git repositories for bzr."""
59
45
 
60
46
    _serializer = None
69
55
        self.revisions = versionedfiles.VirtualRevisionTexts(self)
70
56
        self._format = GitFormat()
71
57
        self._fallback_repositories = []
72
 
        self.tags = GitTags(self._git.get_tags())
73
58
 
74
59
    def _all_revision_ids(self):
75
60
        if self._git.heads == []:
81
66
        while cms != []:
82
67
            cms = self._git.commits("--all", max_count=max_count, skip=skip)
83
68
            skip += max_count
84
 
            ret.update([self.get_mapping().revision_id_foreign_to_bzr(cm.id) for cm in cms])
 
69
            ret.update([default_mapping.revision_id_foreign_to_bzr(cm.id) for cm in cms])
85
70
        return ret
86
71
 
87
72
    def is_shared(self):
103
88
            max_count = 1000
104
89
            cms = None
105
90
            while cms != []:
106
 
                cms = self._git.commits(self.lookup_git_revid(revision_id, self.get_mapping()), max_count=max_count, skip=skip)
 
91
                cms = self._git.commits(default_mapping.revision_id_bzr_to_foreign(revision_id), max_count=max_count, skip=skip)
107
92
                skip += max_count
108
 
                ret += [self.get_mapping().revision_id_foreign_to_bzr(cm.id) for cm in cms]
 
93
                ret += [default_mapping.revision_id_foreign_to_bzr(cm.id) for cm in cms]
109
94
        return [None] + ret
110
95
 
111
96
    def get_signature_text(self, revision_id):
112
97
        raise errors.NoSuchRevision(self, revision_id)
113
98
 
114
 
    def lookup_revision_id(self, revid):
115
 
        """Lookup a revision id.
116
 
        
117
 
        :param revid: Bazaar revision id.
118
 
        :return: Tuple with git revisionid and mapping.
119
 
        """
120
 
        # Yes, this doesn't really work, but good enough as a stub
121
 
        return osutils.sha(rev_id).hexdigest(), self.get_mapping()
122
 
 
123
99
    def has_signature_for_revision_id(self, revision_id):
124
100
        return False
125
101
 
126
 
    def get_mapping(self):
127
 
        return default_mapping
128
 
 
129
102
    def get_parent_map(self, revision_ids):
130
103
        ret = {}
131
104
        for revid in revision_ids:
132
105
            if revid == revision.NULL_REVISION:
133
106
                ret[revid] = ()
134
107
            else:
135
 
                git_commit_id = self.lookup_git_revid(revid, self.get_mapping())
136
 
                commit = self._git.commit(git_commit_id)
137
 
                ret[revid] = tuple([self.get_mapping().revision_id_foreign_to_bzr(p.id) for p in commit.parents])
 
108
                commit = self._git.commit(default_mapping.revision_id_bzr_to_foreign(revid))
 
109
                ret[revid] = tuple([default_mapping.revision_id_foreign_to_bzr(p.id) for p in commit.parents])
138
110
        return ret
139
111
 
140
 
    def lookup_git_revid(self, bzr_revid, mapping):
141
 
        try:
142
 
            return mapping.revision_id_bzr_to_foreign(bzr_revid)
143
 
        except errors.InvalidRevisionId:
144
 
            raise errors.NoSuchRevision(bzr_revid, self)
145
 
 
146
112
    def get_revision(self, revision_id):
147
 
        git_commit_id = self.lookup_git_revid(revision_id, self.get_mapping())
 
113
        git_commit_id = default_mapping.revision_id_bzr_to_foreign(revision_id)
148
114
        commit = self._git.commit(git_commit_id)
149
115
        # print "fetched revision:", git_commit_id
150
 
        revision = self._parse_rev(commit, self.get_mapping())
 
116
        revision = self._parse_rev(commit)
151
117
        return revision
152
118
 
153
119
    def has_revision(self, revision_id):
162
128
        return [self.get_revision(r) for r in revisions]
163
129
 
164
130
    @classmethod
165
 
    def _parse_rev(klass, commit, mapping):
 
131
    def _parse_rev(klass, commit):
166
132
        """Convert a git commit to a bzr revision.
167
133
 
168
134
        :return: a `bzrlib.revision.Revision` object.
169
135
        """
170
 
        if commit is None:
171
 
            raise AssertionError("Commit object can't be None")
172
 
        rev = ForeignRevision(commit.id, mapping, mapping.revision_id_foreign_to_bzr(commit.id))
173
 
        rev.parent_ids = tuple([mapping.revision_id_foreign_to_bzr(p.id) for p in commit.parents])
 
136
        rev = revision.Revision(default_mapping.revision_id_foreign_to_bzr(commit.id))
 
137
        rev.parent_ids = tuple([default_mapping.revision_id_foreign_to_bzr(p.id) for p in commit.parents])
 
138
        rev.inventory_sha1 = ""
174
139
        rev.message = commit.message.decode("utf-8", "replace")
175
 
        rev.committer = str(commit.committer).decode("utf-8", "replace")
176
 
        rev.properties['author'] = str(commit.author).decode("utf-8", "replace")
 
140
        rev.committer = str(commit.committer)
 
141
        rev.properties['author'] = str(commit.author)
177
142
        rev.timestamp = time.mktime(commit.committed_date)
178
143
        rev.timezone = 0
179
144
        return rev
196
161
        assert revision_id != None
197
162
        return self.revision_tree(revision_id).inventory
198
163
 
199
 
    def set_make_working_trees(self, trees):
200
 
        pass
201
 
 
202
164
 
203
165
def escape_file_id(file_id):
204
166
    return file_id.replace('_', '__').replace(' ', '_s')
213
175
    def __init__(self, repository, revision_id):
214
176
        self._repository = repository
215
177
        self.revision_id = revision_id
216
 
        git_id = repository.lookup_git_revid(revision_id, repository.get_mapping())
 
178
        git_id = default_mapping.revision_id_bzr_to_foreign(revision_id)
217
179
        self.tree = repository._git.commit(git_id).tree
218
180
        self._inventory = inventory.Inventory(revision_id=revision_id)
219
181
        self._inventory.root.revision = revision_id
229
191
 
230
192
    def _build_inventory(self, tree, ie, path):
231
193
        assert isinstance(path, str)
232
 
        for name, mode, hexsha in tree.entries():
233
 
            basename = name.decode("utf-8")
 
194
        for b in tree.contents:
 
195
            basename = b.name.decode("utf-8")
234
196
            if path == "":
235
 
                child_path = name
 
197
                child_path = b.name
236
198
            else:
237
 
                child_path = urlutils.join(path, name)
 
199
                child_path = urlutils.join(path, b.name)
238
200
            file_id = escape_file_id(child_path.encode('utf-8'))
239
 
            if mode[0] == '0':
 
201
            if b.mode[0] == '0':
240
202
                child_ie = inventory.InventoryDirectory(file_id, basename, ie.file_id)
241
 
            elif mode[0] == '1':
242
 
                if mode[1] == '0':
 
203
            elif b.mode[0] == '1':
 
204
                if b.mode[1] == '0':
243
205
                    child_ie = inventory.InventoryFile(file_id, basename, ie.file_id)
244
206
                    child_ie.text_sha1 = osutils.sha_string(b.data)
245
 
                elif mode[1] == '2':
 
207
                elif b.mode[1] == '2':
246
208
                    child_ie = inventory.InventoryLink(file_id, basename, ie.file_id)
247
209
                    child_ie.text_sha1 = osutils.sha_string("")
248
210
                else:
249
211
                    raise AssertionError(
250
 
                        "Unknown file kind, perms=%r." % (mode,))
 
212
                        "Unknown file kind, perms=%r." % (b.mode,))
251
213
                child_ie.text_id = b.id
252
214
                child_ie.text_size = b.size
253
215
            else:
254
216
                raise AssertionError(
255
 
                    "Unknown blob kind, perms=%r." % (mode,))
256
 
            child_ie.executable = bool(int(mode[3:], 8) & 0111)
 
217
                    "Unknown blob kind, perms=%r." % (b.mode,))
 
218
            child_ie.executable = bool(int(b.mode[3:], 8) & 0111)
257
219
            child_ie.revision = self.revision_id
258
220
            self._inventory.add(child_ie)
259
 
            if mode[0] == '0':
 
221
            if b.mode[0] == '0':
260
222
                self._build_inventory(b, child_ie, child_path)
261
223
 
262
224