/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 in 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
19
20
import os
20
21
import time
21
22
 
42
43
    )
43
44
from bzrlib.plugins.git.mapping import default_mapping
44
45
 
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
46
 
57
47
class GitRepository(ForeignRepository):
58
48
    """An adapter to git repositories for bzr."""
69
59
        self.revisions = versionedfiles.VirtualRevisionTexts(self)
70
60
        self._format = GitFormat()
71
61
        self._fallback_repositories = []
72
 
        self.tags = GitTags(self._git.get_tags())
73
62
 
74
63
    def _all_revision_ids(self):
75
64
        if self._git.heads == []:
81
70
        while cms != []:
82
71
            cms = self._git.commits("--all", max_count=max_count, skip=skip)
83
72
            skip += max_count
84
 
            ret.update([self.get_mapping().revision_id_foreign_to_bzr(cm.id) for cm in cms])
 
73
            ret.update([default_mapping.revision_id_foreign_to_bzr(cm.id) for cm in cms])
85
74
        return ret
86
75
 
87
76
    def is_shared(self):
103
92
            max_count = 1000
104
93
            cms = None
105
94
            while cms != []:
106
 
                cms = self._git.commits(self.lookup_git_revid(revision_id, self.get_mapping()), max_count=max_count, skip=skip)
 
95
                cms = self._git.commits(self.lookup_git_revid(revision_id, default_mapping), max_count=max_count, skip=skip)
107
96
                skip += max_count
108
 
                ret += [self.get_mapping().revision_id_foreign_to_bzr(cm.id) for cm in cms]
 
97
                ret += [default_mapping.revision_id_foreign_to_bzr(cm.id) for cm in cms]
109
98
        return [None] + ret
110
99
 
111
100
    def get_signature_text(self, revision_id):
112
101
        raise errors.NoSuchRevision(self, revision_id)
113
102
 
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
103
    def has_signature_for_revision_id(self, revision_id):
124
104
        return False
125
105
 
126
 
    def get_mapping(self):
127
 
        return default_mapping
128
 
 
129
106
    def get_parent_map(self, revision_ids):
130
107
        ret = {}
131
108
        for revid in revision_ids:
132
109
            if revid == revision.NULL_REVISION:
133
110
                ret[revid] = ()
134
111
            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])
 
112
                commit = self._git.commit(self.lookup_git_revid(revid, default_mapping))
 
113
                ret[revid] = tuple([default_mapping.revision_id_foreign_to_bzr(p.id) for p in commit.parents])
138
114
        return ret
139
115
 
140
116
    def lookup_git_revid(self, bzr_revid, mapping):
144
120
            raise errors.NoSuchRevision(bzr_revid, self)
145
121
 
146
122
    def get_revision(self, revision_id):
147
 
        git_commit_id = self.lookup_git_revid(revision_id, self.get_mapping())
 
123
        git_commit_id = self.lookup_git_revid(revision_id, default_mapping)
148
124
        commit = self._git.commit(git_commit_id)
149
125
        # print "fetched revision:", git_commit_id
150
 
        revision = self._parse_rev(commit, self.get_mapping())
 
126
        revision = self._parse_rev(commit, default_mapping)
151
127
        return revision
152
128
 
153
129
    def has_revision(self, revision_id):
167
143
 
168
144
        :return: a `bzrlib.revision.Revision` object.
169
145
        """
170
 
        if commit is None:
171
 
            raise AssertionError("Commit object can't be None")
172
146
        rev = ForeignRevision(commit.id, mapping, mapping.revision_id_foreign_to_bzr(commit.id))
173
147
        rev.parent_ids = tuple([mapping.revision_id_foreign_to_bzr(p.id) for p in commit.parents])
174
148
        rev.message = commit.message.decode("utf-8", "replace")
213
187
    def __init__(self, repository, revision_id):
214
188
        self._repository = repository
215
189
        self.revision_id = revision_id
216
 
        git_id = repository.lookup_git_revid(revision_id, repository.get_mapping())
 
190
        git_id = repository.lookup_git_revid(revision_id, default_mapping)
217
191
        self.tree = repository._git.commit(git_id).tree
218
192
        self._inventory = inventory.Inventory(revision_id=revision_id)
219
193
        self._inventory.root.revision = revision_id
229
203
 
230
204
    def _build_inventory(self, tree, ie, path):
231
205
        assert isinstance(path, str)
232
 
        for name, mode, hexsha in tree.entries():
233
 
            basename = name.decode("utf-8")
 
206
        for b in tree.contents:
 
207
            basename = b.name.decode("utf-8")
234
208
            if path == "":
235
 
                child_path = name
 
209
                child_path = b.name
236
210
            else:
237
 
                child_path = urlutils.join(path, name)
 
211
                child_path = urlutils.join(path, b.name)
238
212
            file_id = escape_file_id(child_path.encode('utf-8'))
239
 
            if mode[0] == '0':
 
213
            if b.mode[0] == '0':
240
214
                child_ie = inventory.InventoryDirectory(file_id, basename, ie.file_id)
241
 
            elif mode[0] == '1':
242
 
                if mode[1] == '0':
 
215
            elif b.mode[0] == '1':
 
216
                if b.mode[1] == '0':
243
217
                    child_ie = inventory.InventoryFile(file_id, basename, ie.file_id)
244
218
                    child_ie.text_sha1 = osutils.sha_string(b.data)
245
 
                elif mode[1] == '2':
 
219
                elif b.mode[1] == '2':
246
220
                    child_ie = inventory.InventoryLink(file_id, basename, ie.file_id)
247
221
                    child_ie.text_sha1 = osutils.sha_string("")
248
222
                else:
249
223
                    raise AssertionError(
250
 
                        "Unknown file kind, perms=%r." % (mode,))
 
224
                        "Unknown file kind, perms=%r." % (b.mode,))
251
225
                child_ie.text_id = b.id
252
226
                child_ie.text_size = b.size
253
227
            else:
254
228
                raise AssertionError(
255
 
                    "Unknown blob kind, perms=%r." % (mode,))
256
 
            child_ie.executable = bool(int(mode[3:], 8) & 0111)
 
229
                    "Unknown blob kind, perms=%r." % (b.mode,))
 
230
            child_ie.executable = bool(int(b.mode[3:], 8) & 0111)
257
231
            child_ie.revision = self.revision_id
258
232
            self._inventory.add(child_ie)
259
 
            if mode[0] == '0':
 
233
            if b.mode[0] == '0':
260
234
                self._build_inventory(b, child_ie, child_path)
261
235
 
262
236