/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

  • Committer: Jelmer Vernooij
  • Date: 2009-07-22 13:10:44 UTC
  • mfrom: (0.200.588 trunk)
  • mto: (0.200.597 trunk)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@samba.org-20090722131044-r30g8w06xd8f40fl
merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
    trace,
28
28
    urlutils,
29
29
    )
 
30
try:
 
31
    from bzrlib import bencode
 
32
except ImportError:
 
33
    from bzrlib.util import bencode
30
34
from bzrlib.inventory import (
31
35
    ROOT_ID,
32
36
    )
35
39
    VcsMappingRegistry, 
36
40
    ForeignRevision,
37
41
    )
38
 
from bzrlib.xml_serializer import (
39
 
    escape_invalid_chars,
40
 
    )
41
42
 
42
43
DEFAULT_FILE_MODE = stat.S_IFREG | 0644
43
44
 
76
77
 
77
78
 
78
79
def warn_unusual_mode(commit, path, mode):
79
 
    trace.warning("Unusual file mode %o for %s in %s. Will be unable to "
80
 
                  "regenerate the SHA map.", mode, path, commit)
 
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)
81
104
 
82
105
 
83
106
class BzrGitMapping(foreign.VcsMapping):
114
137
            return ""
115
138
        return unescape_file_id(file_id)
116
139
 
 
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
 
117
151
    def import_commit(self, commit):
118
152
        """Convert a git commit to a bzr revision.
119
153
 
123
157
            raise AssertionError("Commit object can't be None")
124
158
        rev = ForeignRevision(commit.id, self, self.revision_id_foreign_to_bzr(commit.id))
125
159
        rev.parent_ids = tuple([self.revision_id_foreign_to_bzr(p) for p in commit.parents])
126
 
        rev.message, num_escaped = escape_invalid_chars(commit.message.decode("utf-8", "replace"))
127
 
        if num_escaped:
128
 
            warn_escaped(commit.id, num_escaped)
129
 
        rev.committer, num_escaped = escape_invalid_chars(str(commit.committer).decode("utf-8", "replace"))
130
 
        if num_escaped:
131
 
            warn_escaped(commit.id, num_escaped)
 
160
        rev.message = commit.message.decode("utf-8", "replace")
 
161
        rev.committer = str(commit.committer).decode("utf-8", "replace")
132
162
        if commit.committer != commit.author:
133
 
            rev.properties['author'], num_escaped = escape_invalid_chars(str(commit.author).decode("utf-8", "replace"))
134
 
            if num_escaped:
135
 
                warn_escaped(commit.id, num_escaped)
 
163
            rev.properties['author'] = str(commit.author).decode("utf-8", "replace")
136
164
 
137
165
        if commit.commit_time != commit.author_time:
138
166
            rev.properties['author-timestamp'] = str(commit.author_time)
157
185
 
158
186
 
159
187
class GitMappingRegistry(VcsMappingRegistry):
 
188
    """Registry with available git mappings."""
160
189
 
161
190
    def revision_id_bzr_to_foreign(self, bzr_revid):
162
191
        if not bzr_revid.startswith("git-"):
204
233
    blob._text = entry.symlink_target
205
234
    return blob
206
235
 
 
236
 
207
237
def mode_is_executable(mode):
 
238
    """Check if mode should be considered executable."""
208
239
    return bool(mode & 0111)
209
240
 
 
241
 
210
242
def mode_kind(mode):
 
243
    """Determine the Bazaar inventory kind based on Unix file mode."""
211
244
    entry_kind = (mode & 0700000) / 0100000
212
245
    if entry_kind == 0:
213
246
        return 'directory'
228
261
 
229
262
 
230
263
def entry_mode(entry):
 
264
    """Determine the git file mode for an inventory entry."""
231
265
    if entry.kind == 'directory':
232
266
        return stat.S_IFDIR
233
267
    elif entry.kind == 'symlink':
241
275
        raise AssertionError
242
276
 
243
277
 
244
 
def directory_to_tree(entry, lookup_ie_sha1):
245
 
    from dulwich.objects import Tree
 
278
def directory_to_tree(entry, lookup_ie_sha1, unusual_modes):
 
279
    from dulwich.objects import Tree, EMPTY_TREE_HEXSHA
246
280
    tree = Tree()
247
281
    for name in sorted(entry.children.keys()):
248
282
        ie = entry.children[name]
249
 
        tree.add(entry_mode(ie), name.encode("utf-8"), lookup_ie_sha1(ie))
 
283
        try:
 
284
            mode = unusual_modes[ie.file_id]
 
285
        except KeyError:
 
286
            mode = entry_mode(ie)
 
287
        hexsha = lookup_ie_sha1(ie)
 
288
        if hexsha == EMPTY_TREE_HEXSHA:
 
289
            # Not allowed to add empty trees
 
290
            continue
 
291
        tree.add(mode, name.encode("utf-8"), hexsha)
250
292
    tree.serialize()
251
293
    return tree
252
294
 
253
295
 
254
 
def inventory_to_tree_and_blobs(inventory, texts, mapping, cur=None):
 
296
def extract_unusual_modes(rev):
 
297
    try:
 
298
        foreign_revid, mapping = mapping_registry.parse_revision_id(rev.revision_id)
 
299
    except errors.InvalidRevisionId:
 
300
        return {}
 
301
    else:
 
302
        return mapping.export_unusual_file_modes(rev)
 
303
 
 
304
 
 
305
def inventory_to_tree_and_blobs(inventory, texts, mapping, unusual_modes, cur=None):
255
306
    """Convert a Bazaar tree to a Git tree.
256
307
 
257
308
    :return: Yields tuples with object sha1, object and path
271
322
            tree.serialize()
272
323
            sha = tree.id
273
324
            yield sha, tree, cur.encode("utf-8")
274
 
            t = (stat.S_IFDIR, urlutils.basename(cur).encode('UTF-8'), sha)
 
325
            mode = unusual_modes.get(cur.encode("utf-8"), stat.S_IFDIR)
 
326
            t = (mode, urlutils.basename(cur).encode('UTF-8'), sha)
275
327
            cur, tree = stack.pop()
276
328
            tree.add(*t)
277
329
 
289
341
            sha = blob.id
290
342
            yield sha, blob, path.encode("utf-8")
291
343
            name = urlutils.basename(path).encode("utf-8")
292
 
            tree.add(entry_mode(entry), name, sha)
 
344
            mode = unusual_modes.get(path.encode("utf-8"), entry_mode(entry))
 
345
            tree.add(mode, name, sha)
293
346
 
294
347
    while len(stack) > 1:
295
348
        tree.serialize()
296
349
        sha = tree.id
297
350
        yield sha, tree, cur.encode("utf-8")
298
 
        t = (stat.S_IFDIR, urlutils.basename(cur).encode('UTF-8'), sha)
 
351
        mode = unusual_modes.get(cur.encode('utf-8'), stat.S_IFDIR)
 
352
        t = (mode, urlutils.basename(cur).encode('UTF-8'), sha)
299
353
        cur, tree = stack.pop()
300
354
        tree.add(*t)
301
355