/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

Merge new dulwich.

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
20
19
import os
21
20
import time
22
21
 
32
31
    urlutils,
33
32
    versionedfile,
34
33
    )
 
34
from bzrlib.foreign import (
 
35
        ForeignRevision,
 
36
        )
35
37
from bzrlib.transport import get_transport
36
38
 
37
39
from bzrlib.plugins.git.foreign import (
38
 
    versionedfiles
 
40
    ForeignRepository,
 
41
    versionedfiles,
39
42
    )
40
43
from bzrlib.plugins.git.mapping import default_mapping
41
44
 
42
 
 
43
 
class GitRepository(repository.Repository):
 
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):
44
58
    """An adapter to git repositories for bzr."""
45
59
 
46
60
    _serializer = None
55
69
        self.revisions = versionedfiles.VirtualRevisionTexts(self)
56
70
        self._format = GitFormat()
57
71
        self._fallback_repositories = []
 
72
        self.tags = GitTags(self._git.get_tags())
58
73
 
59
74
    def _all_revision_ids(self):
60
75
        if self._git.heads == []:
66
81
        while cms != []:
67
82
            cms = self._git.commits("--all", max_count=max_count, skip=skip)
68
83
            skip += max_count
69
 
            ret.update([default_mapping.revision_id_foreign_to_bzr(cm.id) for cm in cms])
 
84
            ret.update([self.get_mapping().revision_id_foreign_to_bzr(cm.id) for cm in cms])
70
85
        return ret
71
86
 
72
87
    def is_shared(self):
88
103
            max_count = 1000
89
104
            cms = None
90
105
            while cms != []:
91
 
                cms = self._git.commits(self.lookup_git_revid(revision_id, default_mapping), max_count=max_count, skip=skip)
 
106
                cms = self._git.commits(self.lookup_git_revid(revision_id, self.get_mapping()), max_count=max_count, skip=skip)
92
107
                skip += max_count
93
 
                ret += [default_mapping.revision_id_foreign_to_bzr(cm.id) for cm in cms]
 
108
                ret += [self.get_mapping().revision_id_foreign_to_bzr(cm.id) for cm in cms]
94
109
        return [None] + ret
95
110
 
96
111
    def get_signature_text(self, revision_id):
97
112
        raise errors.NoSuchRevision(self, revision_id)
98
113
 
 
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
 
99
123
    def has_signature_for_revision_id(self, revision_id):
100
124
        return False
101
125
 
 
126
    def get_mapping(self):
 
127
        return default_mapping
 
128
 
102
129
    def get_parent_map(self, revision_ids):
103
130
        ret = {}
104
131
        for revid in revision_ids:
105
132
            if revid == revision.NULL_REVISION:
106
133
                ret[revid] = ()
107
134
            else:
108
 
                commit = self._git.commit(self.lookup_git_revid(revid, default_mapping))
109
 
                ret[revid] = tuple([default_mapping.revision_id_foreign_to_bzr(p.id) for p in commit.parents])
 
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])
110
138
        return ret
111
139
 
112
140
    def lookup_git_revid(self, bzr_revid, mapping):
116
144
            raise errors.NoSuchRevision(bzr_revid, self)
117
145
 
118
146
    def get_revision(self, revision_id):
119
 
        git_commit_id = self.lookup_git_revid(revision_id, default_mapping)
 
147
        git_commit_id = self.lookup_git_revid(revision_id, self.get_mapping())
120
148
        commit = self._git.commit(git_commit_id)
121
149
        # print "fetched revision:", git_commit_id
122
 
        revision = self._parse_rev(commit)
 
150
        revision = self._parse_rev(commit, self.get_mapping())
123
151
        return revision
124
152
 
125
153
    def has_revision(self, revision_id):
134
162
        return [self.get_revision(r) for r in revisions]
135
163
 
136
164
    @classmethod
137
 
    def _parse_rev(klass, commit):
 
165
    def _parse_rev(klass, commit, mapping):
138
166
        """Convert a git commit to a bzr revision.
139
167
 
140
168
        :return: a `bzrlib.revision.Revision` object.
141
169
        """
142
 
        rev = revision.Revision(default_mapping.revision_id_foreign_to_bzr(commit.id))
143
 
        rev.parent_ids = tuple([default_mapping.revision_id_foreign_to_bzr(p.id) for p in commit.parents])
144
 
        rev.inventory_sha1 = ""
 
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])
145
174
        rev.message = commit.message.decode("utf-8", "replace")
146
 
        rev.committer = str(commit.committer)
147
 
        rev.properties['author'] = str(commit.author)
 
175
        rev.committer = str(commit.committer).decode("utf-8", "replace")
 
176
        rev.properties['author'] = str(commit.author).decode("utf-8", "replace")
148
177
        rev.timestamp = time.mktime(commit.committed_date)
149
178
        rev.timezone = 0
150
179
        return rev
184
213
    def __init__(self, repository, revision_id):
185
214
        self._repository = repository
186
215
        self.revision_id = revision_id
187
 
        git_id = repository.lookup_git_revid(revision_id, default_mapping)
 
216
        git_id = repository.lookup_git_revid(revision_id, repository.get_mapping())
188
217
        self.tree = repository._git.commit(git_id).tree
189
218
        self._inventory = inventory.Inventory(revision_id=revision_id)
190
219
        self._inventory.root.revision = revision_id
200
229
 
201
230
    def _build_inventory(self, tree, ie, path):
202
231
        assert isinstance(path, str)
203
 
        for b in tree.contents:
204
 
            basename = b.name.decode("utf-8")
 
232
        for name, mode, hexsha in tree.entries():
 
233
            basename = name.decode("utf-8")
205
234
            if path == "":
206
 
                child_path = b.name
 
235
                child_path = name
207
236
            else:
208
 
                child_path = urlutils.join(path, b.name)
 
237
                child_path = urlutils.join(path, name)
209
238
            file_id = escape_file_id(child_path.encode('utf-8'))
210
 
            if b.mode[0] == '0':
 
239
            if mode[0] == '0':
211
240
                child_ie = inventory.InventoryDirectory(file_id, basename, ie.file_id)
212
 
            elif b.mode[0] == '1':
213
 
                if b.mode[1] == '0':
 
241
            elif mode[0] == '1':
 
242
                if mode[1] == '0':
214
243
                    child_ie = inventory.InventoryFile(file_id, basename, ie.file_id)
215
244
                    child_ie.text_sha1 = osutils.sha_string(b.data)
216
 
                elif b.mode[1] == '2':
 
245
                elif mode[1] == '2':
217
246
                    child_ie = inventory.InventoryLink(file_id, basename, ie.file_id)
218
247
                    child_ie.text_sha1 = osutils.sha_string("")
219
248
                else:
220
249
                    raise AssertionError(
221
 
                        "Unknown file kind, perms=%r." % (b.mode,))
 
250
                        "Unknown file kind, perms=%r." % (mode,))
222
251
                child_ie.text_id = b.id
223
252
                child_ie.text_size = b.size
224
253
            else:
225
254
                raise AssertionError(
226
 
                    "Unknown blob kind, perms=%r." % (b.mode,))
227
 
            child_ie.executable = bool(int(b.mode[3:], 8) & 0111)
 
255
                    "Unknown blob kind, perms=%r." % (mode,))
 
256
            child_ie.executable = bool(int(mode[3:], 8) & 0111)
228
257
            child_ie.revision = self.revision_id
229
258
            self._inventory.add(child_ie)
230
 
            if b.mode[0] == '0':
 
259
            if mode[0] == '0':
231
260
                self._build_inventory(b, child_ie, child_path)
232
261
 
233
262