/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

Partially fix pull.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2007 Canonical Ltd
2
 
# Copyright (C) 2008-2009 Jelmer Vernooij <jelmer@samba.org>
3
 
# Copyright (C) 2008 John Carr
 
1
# Copyright (C) 2007-2008 Canonical Ltd
4
2
#
5
3
# This program is free software; you can redistribute it and/or modify
6
4
# it under the terms of the GNU General Public License as published by
18
16
 
19
17
"""Converters, etc for going between Bazaar and Git ids."""
20
18
 
21
 
from bzrlib import (
22
 
    errors,
23
 
    foreign,
24
 
    urlutils,
25
 
    )
26
 
from bzrlib.inventory import (
27
 
    ROOT_ID,
28
 
    )
 
19
from bzrlib import errors, foreign, urlutils
 
20
from bzrlib.inventory import ROOT_ID
29
21
from bzrlib.foreign import (
30
 
    ForeignVcs, 
31
 
    VcsMappingRegistry, 
32
 
    ForeignRevision,
33
 
    )
34
 
from bzrlib.plugins.git.foreign import (
35
 
    escape_commit_message,
36
 
    )
37
 
 
 
22
        ForeignVcs, 
 
23
        VcsMappingRegistry, 
 
24
        ForeignRevision,
 
25
        )
38
26
 
39
27
def escape_file_id(file_id):
40
28
    return file_id.replace('_', '__').replace(' ', '_s')
67
55
        return bzr_rev_id[len(cls.revid_prefix)+1:], cls()
68
56
 
69
57
    def generate_file_id(self, path):
70
 
        # Git paths are just bytestrings
71
 
        # We must just hope they are valid UTF-8..
72
 
        assert isinstance(path, str)
73
58
        if path == "":
74
59
            return ROOT_ID
75
 
        return escape_file_id(path)
76
 
 
77
 
    def parse_file_id(self, file_id):
78
 
        if file_id == ROOT_ID:
79
 
            return ""
80
 
        return unescape_file_id(file_id)
 
60
        return escape_file_id(path.encode('utf-8'))
81
61
 
82
62
    def import_commit(self, commit):
83
63
        """Convert a git commit to a bzr revision.
88
68
            raise AssertionError("Commit object can't be None")
89
69
        rev = ForeignRevision(commit.id, self, self.revision_id_foreign_to_bzr(commit.id))
90
70
        rev.parent_ids = tuple([self.revision_id_foreign_to_bzr(p) for p in commit.parents])
91
 
        rev.message = escape_commit_message(commit.message.decode("utf-8", "replace"))
92
 
        rev.committer = escape_commit_message(str(commit.committer).decode("utf-8", "replace"))
 
71
        rev.message = commit.message.decode("utf-8", "replace")
 
72
        rev.committer = str(commit.committer).decode("utf-8", "replace")
93
73
        if commit.committer != commit.author:
94
 
            rev.properties['author'] = escape_commit_message(str(commit.author).decode("utf-8", "replace"))
 
74
            rev.properties['author'] = str(commit.author).decode("utf-8", "replace")
95
75
        rev.timestamp = commit.commit_time
96
76
        rev.timezone = 0
97
77
        return rev
141
121
default_mapping = BzrGitMappingv1()
142
122
 
143
123
 
144
 
def text_to_blob(text):
145
 
    from dulwich.objects import Blob
146
 
    blob = Blob()
147
 
    blob._text = text
148
 
    return blob
149
 
 
150
 
 
151
124
def inventory_to_tree_and_blobs(repo, mapping, revision_id):
152
 
    from dulwich.objects import Tree
 
125
    from dulwich.objects import Tree, Blob
153
126
    from bzrlib.inventory import InventoryDirectory, InventoryFile
154
127
    import stat
155
128
    stack = []
160
133
 
161
134
    # stack contains the set of trees that we haven't 
162
135
    # finished constructing
 
136
 
163
137
    for path, entry in inv.iter_entries():
164
138
        while stack and not path.startswith(cur):
165
139
            tree.serialize()
166
 
            sha = tree.id
 
140
            sha = tree.sha().hexdigest()
167
141
            yield sha, tree, cur
168
142
            t = (stat.S_IFDIR, urlutils.basename(cur).encode('UTF-8'), sha)
169
143
            cur, tree = stack.pop()
177
151
        if type(entry) == InventoryFile:
178
152
            #FIXME: We can make potentially make this Lazy to avoid shaing lots of stuff
179
153
            # and having all these objects in memory at once
180
 
            text = repo.texts.get_record_stream([(entry.file_id, entry.revision)], 'unordered', True).next().get_bytes_as('fulltext')
181
 
            blob = text_to_blob(text)
182
 
            sha = blob.id
 
154
            blob = Blob()
 
155
            _, blob._text = repo.iter_files_bytes([(entry.file_id, entry.revision, path)]).next()
 
156
            sha = blob.sha().hexdigest()
183
157
            yield sha, blob, path
184
158
 
185
159
            name = urlutils.basename(path).encode("utf-8")
190
164
 
191
165
    while len(stack) > 1:
192
166
        tree.serialize()
193
 
        sha = tree.id
 
167
        sha = tree.sha().hexdigest()
194
168
        yield sha, tree, cur
195
169
        t = (stat.S_IFDIR, urlutils.basename(cur).encode('UTF-8'), sha)
196
170
        cur, tree = stack.pop()
197
171
        tree.add(*t)
198
172
 
199
173
    tree.serialize()
200
 
    yield tree.id, tree, cur
 
174
    yield tree.sha().hexdigest(), tree, cur
201
175
 
202
176
 
203
177
def revision_to_commit(rev, tree_sha, parent_lookup):
213
187
    for p in rev.parent_ids:
214
188
        git_p = parent_lookup(p)
215
189
        if git_p is not None:
216
 
            assert len(git_p) == 40, "unexpected length for %r" % git_p
217
190
            commit._parents.append(git_p)
218
191
    commit._message = rev.message.encode("utf-8")
219
192
    commit._committer = rev.committer.encode("utf-8")
220
 
    commit._author = rev.get_apparent_authors()[0].encode("utf-8")
 
193
    commit._author = rev.get_apparent_author().encode("utf-8")
221
194
    commit._commit_time = long(rev.timestamp)
222
195
    commit.serialize()
223
196
    return commit