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

Implement to_files() for git merge directives.

Show diffs side-by-side

added added

removed removed

Lines of Context:
67
67
            elif file_id[i+1] == 's':
68
68
                ret.append(" ")
69
69
            else:
70
 
                raise AssertionError("unknown escape character %s" %
71
 
                    file_id[i+1])
 
70
                raise AssertionError("unknown escape character %s" % file_id[i+1])
72
71
            i += 1
73
72
        i += 1
74
73
    return "".join(ret)
86
85
 
87
86
 
88
87
def warn_unusual_mode(commit, path, mode):
89
 
    trace.mutter("Unusual file mode %o for %s in %s. Storing as revision "
90
 
                 "property. ", mode, path, commit)
 
88
    trace.mutter("Unusual file mode %o for %s in %s. Storing as revision property. ",
 
89
                 mode, path, commit)
91
90
 
92
91
 
93
92
def squash_revision(target_repo, rev):
126
125
    @classmethod
127
126
    def revision_id_foreign_to_bzr(cls, git_rev_id):
128
127
        """Convert a git revision id handle to a Bazaar revision id."""
129
 
        if git_rev_id == "0" * 40:
130
 
            return NULL_REVISION
131
128
        return "%s:%s" % (cls.revid_prefix, git_rev_id)
132
129
 
133
130
    @classmethod
163
160
 
164
161
    def _generate_git_svn_metadata(self, rev, encoding):
165
162
        try:
166
 
            return "\ngit-svn-id: %s\n" % rev.properties["git-svn-id"].encode(
167
 
                encoding)
 
163
            return "\ngit-svn-id: %s\n" % rev.properties["git-svn-id"].encode(encoding)
168
164
        except KeyError:
169
165
            return ""
170
166
 
176
172
            if name == 'hg:extra:branch':
177
173
                branch = rev.properties['hg:extra:branch']
178
174
            elif name.startswith('hg:extra'):
179
 
                extra[name[len('hg:extra:'):]] = base64.b64decode(
180
 
                    rev.properties[name])
 
175
                extra[name[len('hg:extra:'):]] = base64.b64decode(rev.properties[name])
181
176
            elif name == 'hg:renames':
182
 
                renames = bencode.bdecode(base64.b64decode(
183
 
                    rev.properties['hg:renames']))
 
177
                renames = bencode.bdecode(base64.b64decode(rev.properties['hg:renames']))
184
178
            # TODO: Export other properties as 'bzr:' extras?
185
179
        ret = format_hg_metadata(renames, branch, extra)
186
180
        assert isinstance(ret, str)
205
199
        for name, value in extra.iteritems():
206
200
            rev.properties['hg:extra:' + name] = base64.b64encode(value)
207
201
        if renames:
208
 
            rev.properties['hg:renames'] = base64.b64encode(bencode.bencode(
209
 
                [(new, old) for (old, new) in renames.iteritems()]))
 
202
            rev.properties['hg:renames'] = base64.b64encode(bencode.bencode([(new, old) for (old, new) in renames.iteritems()]))
210
203
        return message
211
204
 
212
205
    def _decode_commit_message(self, rev, message, encoding):
219
212
        """Turn a Bazaar revision in to a Git commit
220
213
 
221
214
        :param tree_sha: Tree sha for the commit
222
 
        :param parent_lookup: Function for looking up the GIT sha equiv of a
223
 
            bzr revision
 
215
        :param parent_lookup: Function for looking up the GIT sha equiv of a bzr revision
224
216
        :return dulwich.objects.Commit represent the revision:
225
217
        """
226
218
        from dulwich.objects import Commit
264
256
        """
265
257
        if commit is None:
266
258
            raise AssertionError("Commit object can't be None")
267
 
        rev = ForeignRevision(commit.id, self,
268
 
                self.revision_id_foreign_to_bzr(commit.id))
 
259
        rev = ForeignRevision(commit.id, self, self.revision_id_foreign_to_bzr(commit.id))
269
260
        rev.parent_ids = tuple([self.revision_id_foreign_to_bzr(p) for p in commit.parents])
270
261
        def decode_using_encoding(rev, commit, encoding):
271
262
            rev.committer = str(commit.committer).decode(encoding)
289
280
        if commit.commit_time != commit.author_time:
290
281
            rev.properties['author-timestamp'] = str(commit.author_time)
291
282
        if commit.commit_timezone != commit.author_timezone:
292
 
            rev.properties['author-timezone'] = "%d" % commit.author_timezone
 
283
            rev.properties['author-timezone'] = "%d" % (commit.author_timezone, )
293
284
        rev.timestamp = commit.commit_time
294
285
        rev.timezone = commit.commit_timezone
295
286
        return rev
341
332
 
342
333
mapping_registry = GitMappingRegistry()
343
334
mapping_registry.register_lazy('git-v1', "bzrlib.plugins.git.mapping",
344
 
    "BzrGitMappingv1")
345
 
mapping_registry.register_lazy('git-experimental',
346
 
    "bzrlib.plugins.git.mapping", "BzrGitMappingExperimental")
 
335
                                   "BzrGitMappingv1")
 
336
mapping_registry.register_lazy('git-experimental', "bzrlib.plugins.git.mapping",
 
337
                                   "BzrGitMappingExperimental")
347
338
mapping_registry.set_default('git-v1')
348
339
 
349
340
 
377
368
default_mapping = mapping_registry.get_default()()
378
369
 
379
370
 
 
371
def text_to_blob(texts, entry):
 
372
    from dulwich.objects import Blob
 
373
    text = texts.get_record_stream([(entry.file_id, entry.revision)], 'unordered', True).next().get_bytes_as('fulltext')
 
374
    blob = Blob()
 
375
    blob._text = text
 
376
    return blob
 
377
 
 
378
 
380
379
def symlink_to_blob(entry):
381
380
    from dulwich.objects import Blob
382
381
    blob = Blob()
383
 
    symlink_target = entry.symlink_target
384
 
    if type(symlink_target) == unicode:
385
 
        symlink_target = symlink_target.encode('utf-8')
386
 
    blob.data = symlink_target
 
382
    blob._text = entry.symlink_target
387
383
    return blob
388
384
 
389
385
 
441
437
def directory_to_tree(entry, lookup_ie_sha1, unusual_modes):
442
438
    from dulwich.objects import Tree
443
439
    tree = Tree()
444
 
    for name, value in entry.children.iteritems():
 
440
    for name in sorted(entry.children.keys()):
445
441
        ie = entry.children[name]
446
442
        try:
447
443
            mode = unusual_modes[ie.file_id]
453
449
    if entry.parent_id is not None and len(tree) == 0:
454
450
        # Only the root can be an empty tree
455
451
        return None
 
452
    tree.serialize()
456
453
    return tree
457
454
 
458
455
 
459
456
def extract_unusual_modes(rev):
460
457
    try:
461
 
        foreign_revid, mapping = mapping_registry.parse_revision_id(
462
 
            rev.revision_id)
 
458
        foreign_revid, mapping = mapping_registry.parse_revision_id(rev.revision_id)
463
459
    except errors.InvalidRevisionId:
464
460
        return {}
465
461
    else:
466
462
        return mapping.export_unusual_file_modes(rev)
467
463
 
468
464
 
469
 
def inventory_to_tree_and_blobs(inventory, texts, mapping, unusual_modes,
470
 
                                cur=None):
 
465
def inventory_to_tree_and_blobs(inventory, texts, mapping, unusual_modes, cur=None):
471
466
    """Convert a Bazaar tree to a Git tree.
472
467
 
473
468
    :return: Yields tuples with object sha1, object and path
484
479
    for path, entry in inventory.iter_entries():
485
480
        while stack and not path.startswith(osutils.pathjoin(cur, "")):
486
481
            # We've hit a file that's not a child of the previous path
 
482
            tree.serialize()
487
483
            sha = tree.id
488
484
            yield sha, tree, cur.encode("utf-8")
489
485
            mode = unusual_modes.get(cur.encode("utf-8"), stat.S_IFDIR)
497
493
            tree = Tree()
498
494
        else:
499
495
            if entry.kind == "file":
500
 
                from dulwich.objects import Blob
501
 
                stream = texts.get_record_stream(
502
 
                    [(entry.file_id, entry.revision)], 'unordered', True)
503
 
                blob = Blob()
504
 
                blob.chunked = stream.next().get_bytes_as('chunks')
 
496
                blob = text_to_blob(texts, entry)
505
497
            elif entry.kind == "symlink":
506
498
                blob = symlink_to_blob(entry)
507
499
            else:
513
505
            tree.add(mode, name, sha)
514
506
 
515
507
    while len(stack) > 1:
 
508
        tree.serialize()
516
509
        sha = tree.id
517
510
        yield sha, tree, cur.encode("utf-8")
518
511
        mode = unusual_modes.get(cur.encode('utf-8'), stat.S_IFDIR)
520
513
        cur, tree = stack.pop()
521
514
        tree.add(*t)
522
515
 
 
516
    tree.serialize()
523
517
    yield tree.id, tree, cur.encode("utf-8")
524
518
 
525
519