/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 breezy/git/mapping.py

  • Committer: Jelmer Vernooij
  • Date: 2018-06-14 17:59:16 UTC
  • mto: This revision was merged to the branch mainline in revision 7065.
  • Revision ID: jelmer@jelmer.uk-20180614175916-a2e2xh5k533guq1x
Move breezy.plugins.git to breezy.git.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 
19
19
"""Converters, etc for going between Bazaar and Git ids."""
20
20
 
 
21
from __future__ import absolute_import
 
22
 
21
23
import base64
22
24
import stat
23
25
 
26
28
    errors,
27
29
    foreign,
28
30
    trace,
29
 
    urlutils,
 
31
    )
 
32
from ..bzr.inventory import (
 
33
    ROOT_ID,
30
34
    )
31
35
from ..foreign import (
32
36
    ForeignVcs,
35
39
    )
36
40
from ..revision import (
37
41
    NULL_REVISION,
38
 
    Revision,
39
42
    )
40
43
from .errors import (
41
44
    NoPushSupport,
 
45
    UnknownCommitExtra,
 
46
    UnknownMercurialCommitExtra,
42
47
    )
43
48
from .hg import (
44
49
    format_hg_metadata,
48
53
    extract_bzr_metadata,
49
54
    inject_bzr_metadata,
50
55
    CommitSupplement,
 
56
    deserialize_fileid_map,
 
57
    serialize_fileid_map,
51
58
    )
52
59
 
 
60
try:
 
61
    from urllib.parse import quote
 
62
except ImportError:
 
63
    from urllib import quote
53
64
 
54
65
DEFAULT_FILE_MODE = stat.S_IFREG | 0o644
55
 
HG_RENAME_SOURCE = b"HG:rename-source"
56
 
HG_EXTRA = b"HG:extra"
 
66
HG_RENAME_SOURCE = "HG:rename-source"
 
67
HG_EXTRA = "HG:extra"
57
68
 
58
69
# This HG extra is used to indicate the commit that this commit was based on.
59
 
HG_EXTRA_AMEND_SOURCE = b"amend_source"
 
70
HG_EXTRA_AMEND_SOURCE = "amend_source"
60
71
 
61
72
FILE_ID_PREFIX = b'git:'
62
73
 
63
 
# Always the same.
64
 
ROOT_ID = b"TREE_ROOT"
65
 
 
66
 
 
67
 
class UnknownCommitExtra(errors.BzrError):
68
 
    _fmt = "Unknown extra fields in %(object)r: %(fields)r."
69
 
 
70
 
    def __init__(self, object, fields):
71
 
        errors.BzrError.__init__(self)
72
 
        self.object = object
73
 
        self.fields = ",".join(fields)
74
 
 
75
 
 
76
 
class UnknownMercurialCommitExtra(errors.BzrError):
77
 
    _fmt = "Unknown mercurial extra fields in %(object)r: %(fields)r."
78
 
 
79
 
    def __init__(self, object, fields):
80
 
        errors.BzrError.__init__(self)
81
 
        self.object = object
82
 
        self.fields = b",".join(fields)
83
 
 
84
74
 
85
75
def escape_file_id(file_id):
86
 
    file_id = file_id.replace(b'_', b'__')
87
 
    file_id = file_id.replace(b' ', b'_s')
88
 
    file_id = file_id.replace(b'\x0c', b'_c')
89
 
    return file_id
 
76
    return file_id.replace('_', '__').replace(' ', '_s').replace('\x0c', '_c')
90
77
 
91
78
 
92
79
def unescape_file_id(file_id):
93
 
    ret = bytearray()
 
80
    ret = []
94
81
    i = 0
95
82
    while i < len(file_id):
96
 
        if file_id[i:i + 1] != b'_':
 
83
        if file_id[i] != '_':
97
84
            ret.append(file_id[i])
98
85
        else:
99
 
            if file_id[i + 1:i + 2] == b'_':
100
 
                ret.append(b"_"[0])
101
 
            elif file_id[i + 1:i + 2] == b's':
102
 
                ret.append(b" "[0])
103
 
            elif file_id[i + 1:i + 2] == b'c':
104
 
                ret.append(b"\x0c"[0])
 
86
            if file_id[i+1] == '_':
 
87
                ret.append("_")
 
88
            elif file_id[i+1] == 's':
 
89
                ret.append(" ")
 
90
            elif file_id[i+1] == 'c':
 
91
                ret.append("\x0c")
105
92
            else:
106
93
                raise ValueError("unknown escape character %s" %
107
 
                                 file_id[i + 1:i + 2])
 
94
                    file_id[i+1])
108
95
            i += 1
109
96
        i += 1
110
 
    return bytes(ret)
 
97
    return "".join(ret)
111
98
 
112
99
 
113
100
def fix_person_identifier(text):
114
 
    if b"<" not in text and b">" not in text:
 
101
    if not "<" in text and not ">" in text:
115
102
        username = text
116
103
        email = text
117
 
    elif b">" not in text:
118
 
        return text + b">"
119
104
    else:
120
 
        if text.rindex(b">") < text.rindex(b"<"):
 
105
        if text.rindex(">") < text.rindex("<"):
121
106
            raise ValueError(text)
122
 
        username, email = text.split(b"<", 2)[-2:]
123
 
        email = email.split(b">", 1)[0]
124
 
        if username.endswith(b" "):
 
107
        username, email = text.split("<", 2)[-2:]
 
108
        email = email.split(">", 1)[0]
 
109
        if username.endswith(" "):
125
110
            username = username[:-1]
126
 
    return b"%s <%s>" % (username, email)
127
 
 
128
 
 
129
 
def decode_git_path(path):
130
 
    """Take a git path and decode it."""
131
 
    try:
132
 
        return path.decode('utf-8')
133
 
    except UnicodeDecodeError:
134
 
        if PY3:
135
 
            return path.decode('utf-8', 'surrogateescape')
136
 
        raise
137
 
 
138
 
 
139
 
def encode_git_path(path):
140
 
    """Take a regular path and encode it for git."""
141
 
    try:
142
 
        return path.encode('utf-8')
143
 
    except UnicodeEncodeError:
144
 
        if PY3:
145
 
            return path.encode('utf-8', 'surrogateescape')
146
 
        raise
 
111
    return "%s <%s>" % (username, email)
147
112
 
148
113
 
149
114
def warn_escaped(commit, num_escaped):
160
125
    """Class that maps between Git and Bazaar semantics."""
161
126
    experimental = False
162
127
 
 
128
    BZR_FILE_IDS_FILE = None
 
129
 
163
130
    BZR_DUMMY_FILE = None
164
131
 
165
132
    def is_special_file(self, filename):
166
 
        return (filename in (self.BZR_DUMMY_FILE, ))
 
133
        return (filename in (self.BZR_FILE_IDS_FILE, self.BZR_DUMMY_FILE))
167
134
 
168
135
    def __init__(self):
169
136
        super(BzrGitMapping, self).__init__(foreign_vcs_git)
170
137
 
171
138
    def __eq__(self, other):
172
 
        return (type(self) == type(other)
173
 
                and self.revid_prefix == other.revid_prefix)
 
139
        return (type(self) == type(other) and
 
140
                self.revid_prefix == other.revid_prefix)
174
141
 
175
142
    @classmethod
176
143
    def revision_id_foreign_to_bzr(cls, git_rev_id):
178
145
        from dulwich.protocol import ZERO_SHA
179
146
        if git_rev_id == ZERO_SHA:
180
147
            return NULL_REVISION
181
 
        return b"%s:%s" % (cls.revid_prefix, git_rev_id)
 
148
        return "%s:%s" % (cls.revid_prefix, git_rev_id)
182
149
 
183
150
    @classmethod
184
151
    def revision_id_bzr_to_foreign(cls, bzr_rev_id):
185
152
        """Convert a Bazaar revision id to a git revision id handle."""
186
 
        if not bzr_rev_id.startswith(b"%s:" % cls.revid_prefix):
 
153
        if not bzr_rev_id.startswith("%s:" % cls.revid_prefix):
187
154
            raise errors.InvalidRevisionId(bzr_rev_id, cls)
188
 
        return bzr_rev_id[len(cls.revid_prefix) + 1:], cls()
 
155
        return bzr_rev_id[len(cls.revid_prefix)+1:], cls()
189
156
 
190
157
    def generate_file_id(self, path):
191
158
        # Git paths are just bytestrings
192
159
        # We must just hope they are valid UTF-8..
193
 
        if isinstance(path, str):
 
160
        if path == "":
 
161
            return ROOT_ID
 
162
        if type(path) is unicode:
194
163
            path = path.encode("utf-8")
195
 
        if path == b"":
196
 
            return ROOT_ID
197
164
        return FILE_ID_PREFIX + escape_file_id(path)
198
165
 
199
166
    def parse_file_id(self, file_id):
200
167
        if file_id == ROOT_ID:
201
 
            return u""
 
168
            return ""
202
169
        if not file_id.startswith(FILE_ID_PREFIX):
203
170
            raise ValueError
204
 
        return decode_git_path(unescape_file_id(file_id[len(FILE_ID_PREFIX):]))
 
171
        return unescape_file_id(file_id[len(FILE_ID_PREFIX):])
 
172
 
 
173
    def revid_as_refname(self, revid):
 
174
        return "refs/bzr/%s" % quote(revid)
205
175
 
206
176
    def import_unusual_file_modes(self, rev, unusual_file_modes):
207
177
        if unusual_file_modes:
208
178
            ret = [(path, unusual_file_modes[path])
209
179
                   for path in sorted(unusual_file_modes.keys())]
210
 
            rev.properties[u'file-modes'] = bencode.bencode(ret)
 
180
            rev.properties['file-modes'] = bencode.bencode(ret)
211
181
 
212
182
    def export_unusual_file_modes(self, rev):
213
183
        try:
214
 
            file_modes = rev.properties[u'file-modes']
 
184
            file_modes = rev.properties['file-modes']
215
185
        except KeyError:
216
186
            return {}
217
187
        else:
219
189
 
220
190
    def _generate_git_svn_metadata(self, rev, encoding):
221
191
        try:
222
 
            git_svn_id = rev.properties[u"git-svn-id"]
 
192
            git_svn_id = rev.properties["git-svn-id"]
223
193
        except KeyError:
224
194
            return ""
225
195
        else:
230
200
        renames = []
231
201
        branch = 'default'
232
202
        for name in rev.properties:
233
 
            if name == u'hg:extra:branch':
234
 
                branch = rev.properties[u'hg:extra:branch']
235
 
            elif name.startswith(u'hg:extra'):
236
 
                extra[name[len(u'hg:extra:'):]] = base64.b64decode(
 
203
            if name == 'hg:extra:branch':
 
204
                branch = rev.properties['hg:extra:branch']
 
205
            elif name.startswith('hg:extra'):
 
206
                extra[name[len('hg:extra:'):]] = base64.b64decode(
237
207
                    rev.properties[name])
238
 
            elif name == u'hg:renames':
 
208
            elif name == 'hg:renames':
239
209
                renames = bencode.bdecode(base64.b64decode(
240
 
                    rev.properties[u'hg:renames']))
 
210
                    rev.properties['hg:renames']))
241
211
            # TODO: Export other properties as 'bzr:' extras?
242
212
        ret = format_hg_metadata(renames, branch, extra)
243
 
        if not isinstance(ret, bytes):
 
213
        if type(ret) is not str:
244
214
            raise TypeError(ret)
245
215
        return ret
246
216
 
247
217
    def _extract_git_svn_metadata(self, rev, message):
248
218
        lines = message.split("\n")
249
 
        if not (lines[-1] == "" and len(lines) >= 2 and
250
 
                lines[-2].startswith("git-svn-id:")):
 
219
        if not (lines[-1] == "" and len(lines) >= 2 and lines[-2].startswith("git-svn-id:")):
251
220
            return message
252
221
        git_svn_id = lines[-2].split(": ", 1)[1]
253
 
        rev.properties[u'git-svn-id'] = git_svn_id
 
222
        rev.properties['git-svn-id'] = git_svn_id
254
223
        (url, rev, uuid) = parse_git_svn_id(git_svn_id)
255
224
        # FIXME: Convert this to converted-from property somehow..
256
225
        return "\n".join(lines[:-2])
258
227
    def _extract_hg_metadata(self, rev, message):
259
228
        (message, renames, branch, extra) = extract_hg_metadata(message)
260
229
        if branch is not None:
261
 
            rev.properties[u'hg:extra:branch'] = branch
262
 
        for name, value in extra.items():
263
 
            rev.properties[u'hg:extra:' + name] = base64.b64encode(value)
 
230
            rev.properties['hg:extra:branch'] = branch
 
231
        for name, value in extra.iteritems():
 
232
            rev.properties['hg:extra:' + name] = base64.b64encode(value)
264
233
        if renames:
265
 
            rev.properties[u'hg:renames'] = base64.b64encode(bencode.bencode(
266
 
                [(new, old) for (old, new) in renames.items()]))
 
234
            rev.properties['hg:renames'] = base64.b64encode(bencode.bencode(
 
235
                [(new, old) for (old, new) in renames.iteritems()]))
267
236
        return message
268
237
 
269
238
    def _extract_bzr_metadata(self, rev, message):
276
245
    def _encode_commit_message(self, rev, message, encoding):
277
246
        return message.encode(encoding)
278
247
 
 
248
    def export_fileid_map(self, fileid_map):
 
249
        """Export a file id map to a fileid map.
 
250
 
 
251
        :param fileid_map: File id map, mapping paths to file ids
 
252
        :return: A Git blob object (or None if there are no entries)
 
253
        """
 
254
        from dulwich.objects import Blob
 
255
        b = Blob()
 
256
        b.set_raw_chunks(serialize_fileid_map(fileid_map))
 
257
        return b
 
258
 
279
259
    def export_commit(self, rev, tree_sha, parent_lookup, lossy,
280
260
                      verifiers):
281
261
        """Turn a Bazaar revision in to a Git commit
309
289
                parents.append(git_p)
310
290
        commit.parents = parents
311
291
        try:
312
 
            encoding = rev.properties[u'git-explicit-encoding']
 
292
            encoding = rev.properties['git-explicit-encoding']
313
293
        except KeyError:
314
 
            encoding = rev.properties.get(u'git-implicit-encoding', 'utf-8')
 
294
            encoding = rev.properties.get('git-implicit-encoding', 'utf-8')
315
295
        try:
316
 
            commit.encoding = rev.properties[u'git-explicit-encoding'].encode(
317
 
                'ascii')
 
296
            commit.encoding = rev.properties['git-explicit-encoding'].encode('ascii')
318
297
        except KeyError:
319
298
            pass
320
299
        commit.committer = fix_person_identifier(rev.committer.encode(
321
300
            encoding))
322
301
        commit.author = fix_person_identifier(
323
302
            rev.get_apparent_authors()[0].encode(encoding))
324
 
        # TODO(jelmer): Don't use this hack.
325
 
        long = getattr(__builtins__, 'long', int)
326
303
        commit.commit_time = long(rev.timestamp)
327
 
        if u'author-timestamp' in rev.properties:
328
 
            commit.author_time = long(rev.properties[u'author-timestamp'])
 
304
        if 'author-timestamp' in rev.properties:
 
305
            commit.author_time = long(rev.properties['author-timestamp'])
329
306
        else:
330
307
            commit.author_time = commit.commit_time
331
 
        commit._commit_timezone_neg_utc = (
332
 
            u"commit-timezone-neg-utc" in rev.properties)
 
308
        commit._commit_timezone_neg_utc = "commit-timezone-neg-utc" in rev.properties
333
309
        commit.commit_timezone = rev.timezone
334
 
        commit._author_timezone_neg_utc = (
335
 
            u"author-timezone-neg-utc" in rev.properties)
336
 
        if u'author-timezone' in rev.properties:
337
 
            commit.author_timezone = int(rev.properties[u'author-timezone'])
 
310
        commit._author_timezone_neg_utc = "author-timezone-neg-utc" in rev.properties
 
311
        if 'author-timezone' in rev.properties:
 
312
            commit.author_timezone = int(rev.properties['author-timezone'])
338
313
        else:
339
314
            commit.author_timezone = commit.commit_timezone
340
 
        if u'git-gpg-signature' in rev.properties:
341
 
            commit.gpgsig = rev.properties[u'git-gpg-signature'].encode(
342
 
                'utf-8', 'surrogateescape')
 
315
        if 'git-gpg-signature' in rev.properties:
 
316
            commit.gpgsig = rev.properties['git-gpg-signature'].encode('ascii')
343
317
        commit.message = self._encode_commit_message(rev, rev.message,
344
 
                                                     encoding)
345
 
        if not isinstance(commit.message, bytes):
 
318
            encoding)
 
319
        if type(commit.message) is not str:
346
320
            raise TypeError(commit.message)
347
321
        if metadata is not None:
348
322
            try:
350
324
            except errors.InvalidRevisionId:
351
325
                metadata.revision_id = rev.revision_id
352
326
            mapping_properties = set(
353
 
                [u'author', u'author-timezone', u'author-timezone-neg-utc',
354
 
                 u'commit-timezone-neg-utc', u'git-implicit-encoding',
355
 
                 u'git-gpg-signature', u'git-explicit-encoding',
356
 
                 u'author-timestamp', u'file-modes'])
357
 
            for k, v in rev.properties.items():
358
 
                if k not in mapping_properties:
 
327
                ['author', 'author-timezone', 'author-timezone-neg-utc',
 
328
                 'commit-timezone-neg-utc', 'git-implicit-encoding',
 
329
                 'git-gpg-signature', 'git-explicit-encoding',
 
330
                 'author-timestamp', 'file-modes'])
 
331
            for k, v in rev.properties.iteritems():
 
332
                if not k in mapping_properties:
359
333
                    metadata.properties[k] = v
360
334
        if not lossy and metadata:
361
335
            if self.roundtripping:
362
336
                commit.message = inject_bzr_metadata(commit.message, metadata,
363
337
                                                     encoding)
364
338
            else:
365
 
                raise NoPushSupport(
366
 
                    None, None, self, revision_id=rev.revision_id)
367
 
        if not isinstance(commit.message, bytes):
 
339
                raise NoPushSupport()
 
340
        if type(commit.message) is not str:
368
341
            raise TypeError(commit.message)
369
342
        i = 0
370
 
        propname = u'git-mergetag-0'
 
343
        propname = 'git-mergetag-0'
371
344
        while propname in rev.properties:
372
 
            commit.mergetag.append(Tag.from_string(rev.properties[propname]))
 
345
            commit.mergetag.append(Tag.from_string(rev.properties[propname].encode(encoding)))
373
346
            i += 1
374
 
            propname = u'git-mergetag-%d' % i
375
 
        if u'git-extra' in rev.properties:
376
 
            commit.extra.extend(
377
 
                [l.split(b' ', 1)
378
 
                 for l in rev.properties[u'git-extra'].splitlines()])
 
347
            propname = 'git-mergetag-%d' % i
 
348
        if 'git-extra' in rev.properties:
 
349
            commit.extra.extend([l.split(' ', 1) for l in rev.properties['git-extra'].splitlines()])
379
350
        return commit
380
351
 
381
 
    def get_revision_id(self, commit):
382
 
        if commit.encoding:
383
 
            encoding = commit.encoding.decode('ascii')
384
 
        else:
385
 
            encoding = 'utf-8'
386
 
        try:
387
 
            message, metadata = self._decode_commit_message(
388
 
                None, commit.message, encoding)
389
 
        except UnicodeDecodeError:
390
 
            pass
391
 
        else:
392
 
            if metadata.revision_id:
393
 
                return metadata.revision_id
394
 
        return self.revision_id_foreign_to_bzr(commit.id)
395
 
 
396
 
    def import_commit(self, commit, lookup_parent_revid, strict=True):
 
352
    def import_fileid_map(self, blob):
 
353
        """Convert a git file id map blob.
 
354
 
 
355
        :param blob: Git blob object with fileid map
 
356
        :return: Dictionary mapping paths to file ids
 
357
        """
 
358
        return deserialize_fileid_map(blob.data)
 
359
 
 
360
    def import_commit(self, commit, lookup_parent_revid):
397
361
        """Convert a git commit to a bzr revision.
398
362
 
399
363
        :return: a `breezy.revision.Revision` object, foreign revid and a
402
366
        if commit is None:
403
367
            raise AssertionError("Commit object can't be None")
404
368
        rev = ForeignRevision(commit.id, self,
405
 
                              self.revision_id_foreign_to_bzr(commit.id))
 
369
                self.revision_id_foreign_to_bzr(commit.id))
406
370
        rev.git_metadata = None
407
 
 
408
371
        def decode_using_encoding(rev, commit, encoding):
409
 
            rev.committer = commit.committer.decode(encoding)
 
372
            rev.committer = str(commit.committer).decode(encoding)
410
373
            if commit.committer != commit.author:
411
 
                rev.properties[u'author'] = commit.author.decode(encoding)
 
374
                rev.properties['author'] = str(commit.author).decode(encoding)
412
375
            rev.message, rev.git_metadata = self._decode_commit_message(
413
376
                rev, commit.message, encoding)
414
377
        if commit.encoding is not None:
415
 
            rev.properties[u'git-explicit-encoding'] = commit.encoding.decode(
416
 
                'ascii')
417
 
            decode_using_encoding(rev, commit, commit.encoding.decode('ascii'))
 
378
            rev.properties['git-explicit-encoding'] = commit.encoding
 
379
            decode_using_encoding(rev, commit, commit.encoding)
418
380
        else:
419
381
            for encoding in ('utf-8', 'latin1'):
420
382
                try:
423
385
                    pass
424
386
                else:
425
387
                    if encoding != 'utf-8':
426
 
                        rev.properties[u'git-implicit-encoding'] = encoding
 
388
                        rev.properties['git-implicit-encoding'] = encoding
427
389
                    break
428
390
        if commit.commit_time != commit.author_time:
429
 
            rev.properties[u'author-timestamp'] = str(commit.author_time)
 
391
            rev.properties['author-timestamp'] = str(commit.author_time)
430
392
        if commit.commit_timezone != commit.author_timezone:
431
 
            rev.properties[u'author-timezone'] = "%d" % commit.author_timezone
 
393
            rev.properties['author-timezone'] = "%d" % commit.author_timezone
432
394
        if commit._author_timezone_neg_utc:
433
 
            rev.properties[u'author-timezone-neg-utc'] = ""
 
395
            rev.properties['author-timezone-neg-utc'] = ""
434
396
        if commit._commit_timezone_neg_utc:
435
 
            rev.properties[u'commit-timezone-neg-utc'] = ""
 
397
            rev.properties['commit-timezone-neg-utc'] = ""
436
398
        if commit.gpgsig:
437
 
            rev.properties[u'git-gpg-signature'] = commit.gpgsig.decode(
438
 
                'utf-8', 'surrogateescape')
 
399
            rev.properties['git-gpg-signature'] = commit.gpgsig.decode('ascii')
439
400
        if commit.mergetag:
440
401
            for i, tag in enumerate(commit.mergetag):
441
 
                rev.properties[u'git-mergetag-%d' % i] = tag.as_raw_string()
 
402
                rev.properties['git-mergetag-%d' % i] = tag.as_raw_string()
442
403
        rev.timestamp = commit.commit_time
443
404
        rev.timezone = commit.commit_timezone
444
405
        rev.parent_ids = None
459
420
                    parents.append(lookup_parent_revid(p))
460
421
                except KeyError:
461
422
                    parents.append(self.revision_id_foreign_to_bzr(p))
462
 
            rev.parent_ids = list(parents)
 
423
            rev.parent_ids = tuple(parents)
463
424
        unknown_extra_fields = []
464
425
        extra_lines = []
465
426
        for k, v in commit.extra:
466
427
            if k == HG_RENAME_SOURCE:
467
 
                extra_lines.append(k + b' ' + v + b'\n')
 
428
                extra_lines.append(k + ' ' + v + '\n')
468
429
            elif k == HG_EXTRA:
469
 
                hgk, hgv = v.split(b':', 1)
470
 
                if hgk not in (HG_EXTRA_AMEND_SOURCE, ) and strict:
471
 
                    raise UnknownMercurialCommitExtra(commit, [hgk])
472
 
                extra_lines.append(k + b' ' + v + b'\n')
 
430
                hgk, hgv = v.split(':', 1)
 
431
                if hgk not in (HG_EXTRA_AMEND_SOURCE, ):
 
432
                    raise UnknownMercurialCommitExtra(commit, hgk)
 
433
                extra_lines.append(k + ' ' + v + '\n')
473
434
            else:
474
435
                unknown_extra_fields.append(k)
475
 
        if unknown_extra_fields and strict:
476
 
            raise UnknownCommitExtra(
477
 
                commit,
478
 
                [f.decode('ascii', 'replace') for f in unknown_extra_fields])
 
436
        if unknown_extra_fields:
 
437
            raise UnknownCommitExtra(commit, unknown_extra_fields)
479
438
        if extra_lines:
480
 
            rev.properties[u'git-extra'] = b''.join(extra_lines)
 
439
            rev.properties['git-extra'] = ''.join(extra_lines)
481
440
        return rev, roundtrip_revid, verifiers
482
441
 
 
442
    def get_fileid_map(self, lookup_object, tree_sha):
 
443
        """Obtain a fileid map for a particular tree.
 
444
 
 
445
        :param lookup_object: Function for looking up an object
 
446
        :param tree_sha: SHA of the root tree
 
447
        :return: GitFileIdMap instance
 
448
        """
 
449
        try:
 
450
            file_id_map_sha = lookup_object(tree_sha)[self.BZR_FILE_IDS_FILE][1]
 
451
        except KeyError:
 
452
            file_ids = {}
 
453
        else:
 
454
            file_ids = self.import_fileid_map(lookup_object(file_id_map_sha))
 
455
        return GitFileIdMap(file_ids, self)
 
456
 
483
457
 
484
458
class BzrGitMappingv1(BzrGitMapping):
485
 
    revid_prefix = b'git-v1'
 
459
    revid_prefix = 'git-v1'
486
460
    experimental = False
487
461
 
488
462
    def __str__(self):
490
464
 
491
465
 
492
466
class BzrGitMappingExperimental(BzrGitMappingv1):
493
 
    revid_prefix = b'git-experimental'
 
467
    revid_prefix = 'git-experimental'
494
468
    experimental = True
495
 
    roundtripping = False
 
469
    roundtripping = True
 
470
 
 
471
    BZR_FILE_IDS_FILE = '.bzrfileids'
496
472
 
497
473
    BZR_DUMMY_FILE = '.bzrdummy'
498
474
 
499
475
    def _decode_commit_message(self, rev, message, encoding):
500
 
        if rev is None:
501
 
            rev = Revision()
502
476
        message = self._extract_hg_metadata(rev, message)
503
477
        message = self._extract_git_svn_metadata(rev, message)
504
478
        message, metadata = self._extract_bzr_metadata(rev, message)
510
484
        ret += self._generate_git_svn_metadata(rev, encoding)
511
485
        return ret
512
486
 
513
 
    def import_commit(self, commit, lookup_parent_revid, strict=True):
514
 
        rev, roundtrip_revid, verifiers = super(
515
 
            BzrGitMappingExperimental, self).import_commit(
516
 
                commit, lookup_parent_revid, strict)
517
 
        rev.properties[u'converted_revision'] = "git %s\n" % commit.id
 
487
    def import_commit(self, commit, lookup_parent_revid):
 
488
        rev, roundtrip_revid, verifiers = super(BzrGitMappingExperimental, self).import_commit(commit, lookup_parent_revid)
 
489
        rev.properties['converted_revision'] = "git %s\n" % commit.id
518
490
        return rev, roundtrip_revid, verifiers
519
491
 
520
492
 
525
497
        if bzr_revid == NULL_REVISION:
526
498
            from dulwich.protocol import ZERO_SHA
527
499
            return ZERO_SHA, None
528
 
        if not bzr_revid.startswith(b"git-"):
 
500
        if not bzr_revid.startswith("git-"):
529
501
            raise errors.InvalidRevisionId(bzr_revid, None)
530
 
        (mapping_version, git_sha) = bzr_revid.split(b":", 1)
 
502
        (mapping_version, git_sha) = bzr_revid.split(":", 1)
531
503
        mapping = self.get(mapping_version)
532
504
        return mapping.revision_id_bzr_to_foreign(bzr_revid)
533
505
 
535
507
 
536
508
 
537
509
mapping_registry = GitMappingRegistry()
538
 
mapping_registry.register_lazy(b'git-v1', __name__,
539
 
                               "BzrGitMappingv1")
540
 
mapping_registry.register_lazy(b'git-experimental',
541
 
                               __name__, "BzrGitMappingExperimental")
 
510
mapping_registry.register_lazy('git-v1', __name__,
 
511
    "BzrGitMappingv1")
 
512
mapping_registry.register_lazy('git-experimental',
 
513
    __name__, "BzrGitMappingExperimental")
542
514
# Uncomment the next line to enable the experimental bzr-git mappings.
543
515
# This will make sure all bzr metadata is pushed into git, allowing for
544
516
# full roundtripping later.
545
517
# NOTE: THIS IS EXPERIMENTAL. IT MAY EAT YOUR DATA OR CORRUPT
546
518
# YOUR BZR OR GIT REPOSITORIES. USE WITH CARE.
547
 
# mapping_registry.set_default('git-experimental')
548
 
mapping_registry.set_default(b'git-v1')
 
519
#mapping_registry.set_default('git-experimental')
 
520
mapping_registry.set_default('git-v1')
549
521
 
550
522
 
551
523
class ForeignGit(ForeignVcs):
571
543
 
572
544
    @classmethod
573
545
    def show_foreign_revid(cls, foreign_revid):
574
 
        return {"git commit": foreign_revid.decode('utf-8')}
 
546
        return { "git commit": foreign_revid }
575
547
 
576
548
 
577
549
foreign_vcs_git = ForeignGit()
581
553
def symlink_to_blob(symlink_target):
582
554
    from dulwich.objects import Blob
583
555
    blob = Blob()
584
 
    if isinstance(symlink_target, str):
585
 
        symlink_target = encode_git_path(symlink_target)
 
556
    if type(symlink_target) == unicode:
 
557
        symlink_target = symlink_target.encode('utf-8')
586
558
    blob.data = symlink_target
587
559
    return blob
588
560
 
656
628
    return (full_url, int(rev), uuid)
657
629
 
658
630
 
 
631
class GitFileIdMap(object):
 
632
 
 
633
    def __init__(self, file_ids, mapping):
 
634
        self.file_ids = file_ids
 
635
        self.paths = None
 
636
        self.mapping = mapping
 
637
 
 
638
    def all_file_ids(self):
 
639
        return self.file_ids.values()
 
640
 
 
641
    def set_file_id(self, path, file_id):
 
642
        if type(path) is not str:
 
643
            raise TypeError(path)
 
644
        if type(file_id) is not str:
 
645
            raise TypeError(file_id)
 
646
        self.file_ids[path] = file_id
 
647
 
 
648
    def lookup_file_id(self, path):
 
649
        if type(path) is not str:
 
650
            raise TypeError(path)
 
651
        try:
 
652
            file_id = self.file_ids[path]
 
653
        except KeyError:
 
654
            file_id = self.mapping.generate_file_id(path)
 
655
        if type(file_id) is not str:
 
656
            raise TypeError(file_id)
 
657
        return file_id
 
658
 
 
659
    def lookup_path(self, file_id):
 
660
        if self.paths is None:
 
661
            self.paths = {}
 
662
            for k, v in self.file_ids.iteritems():
 
663
                self.paths[v] = k
 
664
        try:
 
665
            path = self.paths[file_id]
 
666
        except KeyError:
 
667
            return self.mapping.parse_file_id(file_id)
 
668
        else:
 
669
            if type(path) is not str:
 
670
                raise TypeError(path)
 
671
            return path
 
672
 
 
673
    def copy(self):
 
674
        return self.__class__(dict(self.file_ids), self.mapping)
 
675
 
 
676
 
659
677
def needs_roundtripping(repo, revid):
660
678
    try:
661
679
        mapping_registry.parse_revision_id(revid)