/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: 2010-05-05 09:58:55 UTC
  • mto: (0.200.912 trunk)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@samba.org-20100505095855-i0165hooflvk9chy
Ignore control files in inventories.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
import stat
23
23
 
24
24
from bzrlib import (
25
 
    bencode,
26
25
    errors,
27
26
    foreign,
28
27
    trace,
29
28
    )
 
29
try:
 
30
    from bzrlib import bencode
 
31
except ImportError:
 
32
    from bzrlib.util import bencode
30
33
from bzrlib.inventory import (
31
34
    ROOT_ID,
32
35
    )
92
95
                 "property. ", mode, path, commit)
93
96
 
94
97
 
 
98
def squash_revision(target_repo, rev):
 
99
    """Remove characters that can't be stored from a revision, if necessary.
 
100
 
 
101
    :param target_repo: Repository in which the revision will be stored
 
102
    :param rev: Revision object, will be modified in-place
 
103
    """
 
104
    if not getattr(target_repo._serializer, "squashes_xml_invalid_characters", True):
 
105
        return
 
106
    from bzrlib.xml_serializer import escape_invalid_chars
 
107
    rev.message, num_escaped = escape_invalid_chars(rev.message)
 
108
    if num_escaped:
 
109
        warn_escaped(rev.foreign_revid, num_escaped)
 
110
    if 'author' in rev.properties:
 
111
        rev.properties['author'], num_escaped = escape_invalid_chars(
 
112
            rev.properties['author'])
 
113
        if num_escaped:
 
114
            warn_escaped(rev.foreign_revid, num_escaped)
 
115
    rev.committer, num_escaped = escape_invalid_chars(rev.committer)
 
116
    if num_escaped:
 
117
        warn_escaped(rev.foreign_revid, num_escaped)
 
118
 
 
119
 
95
120
class BzrGitMapping(foreign.VcsMapping):
96
121
    """Class that maps between Git and Bazaar semantics."""
97
122
    experimental = False
98
123
 
99
 
    BZR_FILE_IDS_FILE = None
 
124
    BZR_FILE_IDS_FILE = '.bzrfileids'
100
125
 
101
 
    BZR_DUMMY_FILE = None
 
126
    BZR_DUMMY_FILE = '.bzrdummy'
102
127
 
103
128
    def __init__(self):
104
129
        super(BzrGitMapping, self).__init__(foreign_git)
127
152
        # We must just hope they are valid UTF-8..
128
153
        if path == "":
129
154
            return ROOT_ID
130
 
        if type(path) is unicode:
131
 
            path = path.encode("utf-8")
132
155
        return escape_file_id(path)
133
156
 
134
157
    def is_control_file(self, path):
211
234
        return message, metadata
212
235
 
213
236
    def _decode_commit_message(self, rev, message, encoding):
214
 
        return message.decode(encoding), BzrGitRevisionMetadata()
 
237
        message, metadata = self._extract_bzr_metadata(rev, message)
 
238
        return message.decode(encoding), metadata
215
239
 
216
240
    def _encode_commit_message(self, rev, message, encoding):
217
241
        return message.encode(encoding)
242
266
            metadata = BzrGitRevisionMetadata()
243
267
        else:
244
268
            metadata = None
245
 
        parents = []
246
269
        for p in rev.parent_ids:
247
270
            try:
248
271
                git_p = parent_lookup(p)
252
275
                    metadata.explicit_parent_ids = rev.parent_ids
253
276
            if git_p is not None:
254
277
                assert len(git_p) == 40, "unexpected length for %r" % git_p
255
 
                parents.append(git_p)
256
 
        commit.parents = parents
 
278
                commit.parents.append(git_p)
257
279
        try:
258
280
            encoding = rev.properties['git-explicit-encoding']
259
281
        except KeyError:
277
299
            commit.author_timezone = commit.commit_timezone
278
300
        commit.message = self._encode_commit_message(rev, rev.message, 
279
301
            encoding)
280
 
        assert type(commit.message) == str
281
302
        if metadata is not None:
282
303
            try:
283
304
                mapping_registry.parse_revision_id(rev.revision_id)
290
311
            for k, v in rev.properties.iteritems():
291
312
                if not k in mapping_properties:
292
313
                    metadata.properties[k] = v
293
 
        if self.roundtripping:
294
 
            commit.message = inject_bzr_metadata(commit.message, metadata, 
295
 
                                                 encoding)
296
 
        assert type(commit.message) == str
 
314
        commit.message = inject_bzr_metadata(commit.message, metadata)
297
315
        return commit
298
316
 
299
317
    def import_fileid_map(self, blob):
304
322
        """
305
323
        return deserialize_fileid_map(blob.data)
306
324
 
307
 
    def import_commit(self, commit, lookup_parent_revid):
 
325
    def import_commit(self, commit):
308
326
        """Convert a git commit to a bzr revision.
309
327
 
310
328
        :return: a `bzrlib.revision.Revision` object and a 
314
332
            raise AssertionError("Commit object can't be None")
315
333
        rev = ForeignRevision(commit.id, self,
316
334
                self.revision_id_foreign_to_bzr(commit.id))
317
 
        rev.parent_ids = tuple([lookup_parent_revid(p) for p in commit.parents])
 
335
        rev.parent_ids = tuple([self.revision_id_foreign_to_bzr(p) for p in commit.parents])
318
336
        rev.git_metadata = None
319
337
        def decode_using_encoding(rev, commit, encoding):
320
338
            rev.committer = str(commit.committer).decode(encoding)
354
372
            rev.properties.update(md.properties)
355
373
        return rev
356
374
 
357
 
    def get_fileid_map(self, lookup_object, tree_sha):
358
 
        """Obtain a fileid map for a particular tree.
359
 
 
360
 
        :param lookup_object: Function for looking up an object
361
 
        :param tree_sha: SHA of the root tree
362
 
        :return: GitFileIdMap instance
363
 
        """
364
 
        try:
365
 
            file_id_map_sha = lookup_object(tree_sha)[self.BZR_FILE_IDS_FILE][1]
366
 
        except KeyError:
367
 
            file_ids = {}
368
 
        else:
369
 
            file_ids = self.import_fileid_map(lookup_object(file_id_map_sha))
370
 
        return GitFileIdMap(file_ids, self)
371
 
 
372
375
 
373
376
class BzrGitMappingv1(BzrGitMapping):
374
377
    revid_prefix = 'git-v1'
381
384
class BzrGitMappingExperimental(BzrGitMappingv1):
382
385
    revid_prefix = 'git-experimental'
383
386
    experimental = True
384
 
    roundtripping = True
385
 
 
386
 
    BZR_FILE_IDS_FILE = '.bzrfileids'
387
 
 
388
 
    BZR_DUMMY_FILE = '.bzrdummy'
389
387
 
390
388
    def _decode_commit_message(self, rev, message, encoding):
391
389
        message = self._extract_hg_metadata(rev, message)
399
397
        ret += self._generate_git_svn_metadata(rev, encoding)
400
398
        return ret
401
399
 
402
 
    def import_commit(self, commit, lookup_parent_revid):
403
 
        rev, file_ids = super(BzrGitMappingExperimental, self).import_commit(commit, lookup_parent_revid)
 
400
    def import_commit(self, commit):
 
401
        rev, file_ids = super(BzrGitMappingExperimental, self).import_commit(commit)
404
402
        rev.properties['converted_revision'] = "git %s\n" % commit.id
405
403
        return rev, file_ids
406
404
 
574
572
        self.mapping = mapping
575
573
 
576
574
    def lookup_file_id(self, path):
577
 
        assert type(path) is str
578
575
        try:
579
 
            file_id = self.file_ids[path]
 
576
            return self.file_ids[path]
580
577
        except KeyError:
581
 
            file_id = self.mapping.generate_file_id(path)
582
 
        assert type(file_id) is str
583
 
        return file_id
 
578
            return self.mapping.generate_file_id(path)
584
579
 
585
580
    def lookup_path(self, file_id):
586
581
        if self.paths is None:
588
583
            for k, v in self.file_ids.iteritems():
589
584
                self.paths[v] = k
590
585
        try:
591
 
            path = self.paths[file_id]
 
586
            return self.paths[file_id]
592
587
        except KeyError:
593
588
            return self.mapping.parse_file_id(file_id)
594
 
        else:
595
 
            assert type(path) is str
596
 
            return path