/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

Fix reporting of git commits in 'bzr log'.

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