/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

Update docs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
    errors,
25
25
    foreign,
26
26
    osutils,
27
 
    trace,
28
27
    urlutils,
29
28
    )
30
 
try:
31
 
    from bzrlib import bencode
32
 
except ImportError:
33
 
    from bzrlib.util import bencode
34
29
from bzrlib.inventory import (
35
30
    ROOT_ID,
36
31
    )
39
34
    VcsMappingRegistry, 
40
35
    ForeignRevision,
41
36
    )
 
37
from bzrlib.xml_serializer import (
 
38
    escape_invalid_chars,
 
39
    )
42
40
 
43
41
DEFAULT_FILE_MODE = stat.S_IFREG | 0644
44
42
 
71
69
    return "%s <%s>" % (text, text)
72
70
 
73
71
 
74
 
def warn_escaped(commit, num_escaped):
75
 
    trace.warning("Escaped %d XML-invalid characters in %s. Will be unable "
76
 
                  "to regenerate the SHA map.", num_escaped, commit)
77
 
 
78
 
 
79
 
def warn_unusual_mode(commit, path, mode):
80
 
    trace.mutter("Unusual file mode %o for %s in %s. Storing as revision property. ",
81
 
                 mode, path, commit)
82
 
 
83
 
 
84
 
def squash_revision(target_repo, rev):
85
 
    """Remove characters that can't be stored from a revision, if necessary.
86
 
    
87
 
    :param target_repo: Repository in which the revision will be stored
88
 
    :param rev: Revision object, will be modified in-place
89
 
    """
90
 
    if not getattr(target_repo._serializer, "squashes_xml_invalid_characters", True):
91
 
        return
92
 
    from bzrlib.xml_serializer import escape_invalid_chars
93
 
    rev.message, num_escaped = escape_invalid_chars(rev.message)
94
 
    if num_escaped:
95
 
        warn_escaped(rev.foreign_revid, num_escaped)
96
 
    if 'author' in rev.properties:
97
 
        rev.properties['author'], num_escaped = escape_invalid_chars(
98
 
            rev.properties['author'])
99
 
        if num_escaped:
100
 
            warn_escaped(rev.foreign_revid, num_escaped)
101
 
    rev.committer, num_escaped = escape_invalid_chars(rev.committer)
102
 
    if num_escaped:
103
 
        warn_escaped(rev.foreign_revid, num_escaped)
104
 
 
105
 
 
106
72
class BzrGitMapping(foreign.VcsMapping):
107
73
    """Class that maps between Git and Bazaar semantics."""
108
74
    experimental = False
137
103
            return ""
138
104
        return unescape_file_id(file_id)
139
105
 
140
 
    def import_unusual_file_modes(self, rev, unusual_file_modes):
141
 
        if unusual_file_modes:
142
 
            ret = [(name, unusual_file_modes[name]) for name in sorted(unusual_file_modes.keys())]
143
 
            rev.properties['file-modes'] = bencode.bencode(ret)
144
 
 
145
 
    def export_unusual_file_modes(self, rev):
146
 
        try:
147
 
            return dict([(self.generate_file_id(path), mode) for (path, mode) in bencode.bdecode(rev.properties['file-modes'])])
148
 
        except KeyError:
149
 
            return {}
150
 
 
151
106
    def import_commit(self, commit):
152
107
        """Convert a git commit to a bzr revision.
153
108
 
157
112
            raise AssertionError("Commit object can't be None")
158
113
        rev = ForeignRevision(commit.id, self, self.revision_id_foreign_to_bzr(commit.id))
159
114
        rev.parent_ids = tuple([self.revision_id_foreign_to_bzr(p) for p in commit.parents])
160
 
        rev.message = commit.message.decode("utf-8", "replace")
161
 
        rev.committer = str(commit.committer).decode("utf-8", "replace")
 
115
        rev.message = escape_invalid_chars(commit.message.decode("utf-8", "replace"))[0]
 
116
        rev.committer = escape_invalid_chars(str(commit.committer).decode("utf-8", "replace"))[0]
162
117
        if commit.committer != commit.author:
163
 
            rev.properties['author'] = str(commit.author).decode("utf-8", "replace")
 
118
            rev.properties['author'] = escape_invalid_chars(str(commit.author).decode("utf-8", "replace"))[0]
164
119
 
165
120
        if commit.commit_time != commit.author_time:
166
121
            rev.properties['author-timestamp'] = str(commit.author_time)
185
140
 
186
141
 
187
142
class GitMappingRegistry(VcsMappingRegistry):
188
 
    """Registry with available git mappings."""
189
143
 
190
144
    def revision_id_bzr_to_foreign(self, bzr_revid):
191
145
        if not bzr_revid.startswith("git-"):
234
188
    return blob
235
189
 
236
190
 
237
 
def mode_is_executable(mode):
238
 
    """Check if mode should be considered executable."""
239
 
    return bool(mode & 0111)
240
 
 
241
 
 
242
 
def mode_kind(mode):
243
 
    """Determine the Bazaar inventory kind based on Unix file mode."""
244
 
    entry_kind = (mode & 0700000) / 0100000
245
 
    if entry_kind == 0:
246
 
        return 'directory'
247
 
    elif entry_kind == 1:
248
 
        file_kind = (mode & 070000) / 010000
249
 
        if file_kind == 0:
250
 
            return 'file'
251
 
        elif file_kind == 2:
252
 
            return 'symlink'
253
 
        elif file_kind == 6:
254
 
            return 'tree-reference'
255
 
        else:
256
 
            raise AssertionError(
257
 
                "Unknown file kind %d, perms=%o." % (file_kind, mode,))
258
 
    else:
259
 
        raise AssertionError(
260
 
            "Unknown kind, perms=%r." % (mode,))
261
 
 
262
 
 
263
191
def entry_mode(entry):
264
 
    """Determine the git file mode for an inventory entry."""
265
192
    if entry.kind == 'directory':
266
193
        return stat.S_IFDIR
267
194
    elif entry.kind == 'symlink':
275
202
        raise AssertionError
276
203
 
277
204
 
278
 
def directory_to_tree(entry, lookup_ie_sha1, unusual_modes):
 
205
def directory_to_tree(entry, lookup_ie_sha1):
279
206
    from dulwich.objects import Tree
280
207
    tree = Tree()
281
208
    for name in sorted(entry.children.keys()):
282
209
        ie = entry.children[name]
283
 
        try:
284
 
            mode = unusual_modes[ie.file_id]
285
 
        except KeyError:
286
 
            mode = entry_mode(ie)
287
 
        tree.add(mode, name.encode("utf-8"), lookup_ie_sha1(ie))
 
210
        tree.add(entry_mode(ie), name.encode("utf-8"), lookup_ie_sha1(ie))
288
211
    tree.serialize()
289
212
    return tree
290
213
 
291
214
 
292
 
def extract_unusual_modes(rev):
293
 
    try:
294
 
        foreign_revid, mapping = mapping_registry.parse_revision_id(rev.revision_id)
295
 
    except errors.InvalidRevisionId:
296
 
        return {}
297
 
    else:
298
 
        return mapping.export_unusual_file_modes(rev)
299
 
 
300
 
 
301
 
def inventory_to_tree_and_blobs(inventory, texts, mapping, unusual_modes, cur=None):
 
215
def inventory_to_tree_and_blobs(inventory, texts, mapping, cur=None):
302
216
    """Convert a Bazaar tree to a Git tree.
303
217
 
304
218
    :return: Yields tuples with object sha1, object and path
318
232
            tree.serialize()
319
233
            sha = tree.id
320
234
            yield sha, tree, cur.encode("utf-8")
321
 
            mode = unusual_modes.get(cur.encode("utf-8"), stat.S_IFDIR)
322
 
            t = (mode, urlutils.basename(cur).encode('UTF-8'), sha)
 
235
            t = (stat.S_IFDIR, urlutils.basename(cur).encode('UTF-8'), sha)
323
236
            cur, tree = stack.pop()
324
237
            tree.add(*t)
325
238
 
337
250
            sha = blob.id
338
251
            yield sha, blob, path.encode("utf-8")
339
252
            name = urlutils.basename(path).encode("utf-8")
340
 
            mode = unusual_modes.get(path.encode("utf-8"), entry_mode(entry))
341
 
            tree.add(mode, name, sha)
 
253
            tree.add(entry_mode(entry), name, sha)
342
254
 
343
255
    while len(stack) > 1:
344
256
        tree.serialize()
345
257
        sha = tree.id
346
258
        yield sha, tree, cur.encode("utf-8")
347
 
        mode = unusual_modes.get(cur.encode('utf-8'), stat.S_IFDIR)
348
 
        t = (mode, urlutils.basename(cur).encode('UTF-8'), sha)
 
259
        t = (stat.S_IFDIR, urlutils.basename(cur).encode('UTF-8'), sha)
349
260
        cur, tree = stack.pop()
350
261
        tree.add(*t)
351
262