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

  • Committer: Jelmer Vernooij
  • Date: 2010-05-01 22:29:16 UTC
  • mto: (0.200.912 trunk)
  • mto: This revision was merged to the branch mainline in revision 6960.
  • Revision ID: jelmer@samba.org-20100501222916-9cksxfudsnw9ypmn
make sure file ids are roundtripped properly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
class BzrGitRevisionMetadata(object):
24
24
    """Metadata for a Bazaar revision roundtripped into Git.
25
 
 
 
25
    
26
26
    :ivar revision_id: Revision id, as string
27
27
    :ivar properties: Revision properties, as dictionary
 
28
    :ivar file_ids: File ids, as map from path -> file id
28
29
    :ivar explicit_parent_ids: Parent ids (needed if there are ghosts)
29
 
    :ivar verifiers: Verifier information
30
30
    """
31
31
 
32
32
    revision_id = None
33
33
 
34
34
    explicit_parent_ids = None
35
35
 
36
 
    verifiers = {}
37
 
 
38
36
    def __init__(self):
 
37
        self.file_ids = {}
39
38
        self.properties = {}
40
39
 
41
40
    def __nonzero__(self):
42
 
        return bool(self.revision_id or self.properties)
 
41
        return bool(self.revision_id or self.file_ids or self.properties)
43
42
 
44
43
 
45
44
def parse_roundtripping_metadata(text):
52
51
            ret.revision_id = value.strip()
53
52
        elif key == "parent-ids":
54
53
            ret.explicit_parent_ids = tuple(value.strip().split(" "))
55
 
        elif key == "testament3-sha1":
56
 
            ret.verifiers["testament3-sha1"] = value.strip()
57
54
        elif key.startswith("property-"):
58
55
            ret.properties[key[len("property-"):]] = value[1:].rstrip("\n")
 
56
        elif key == "file-id":
 
57
            (file_id, path) = value[1:].rstrip("\n").split(" ", 1)
 
58
            ret.file_ids[path] = file_id
59
59
        else:
60
60
            raise ValueError
61
61
    return ret
62
62
 
63
63
 
64
 
def generate_roundtripping_metadata(metadata, encoding):
 
64
def generate_roundtripping_metadata(metadata):
65
65
    """Serialize the roundtripping metadata.
66
66
 
67
67
    :param metadata: A `BzrGitRevisionMetadata` instance
73
73
    if metadata.explicit_parent_ids:
74
74
        lines.append("parent-ids: %s\n" % " ".join(metadata.explicit_parent_ids))
75
75
    for key in sorted(metadata.properties.keys()):
76
 
        lines.append("property-%s: %s\n" % (key.encode(encoding), metadata.properties[key].encode(encoding)))
77
 
    if "testament3-sha1" in metadata.verifiers:
78
 
        lines.append("testament3-sha1: %s\n" %
79
 
                     metadata.verifiers["testament3-sha1"])
 
76
        lines.append("property-%s: %s\n" % (key, metadata.properties[key]))
 
77
    for key in sorted(metadata.file_ids.keys()):
 
78
        lines.append("file-id: %s %s\n" % (metadata.file_ids[key], key))
80
79
    return "".join(lines)
81
80
 
82
81
 
92
91
    return split[0], parse_roundtripping_metadata(split[1])
93
92
 
94
93
 
95
 
def inject_bzr_metadata(message, metadata, encoding):
 
94
def inject_bzr_metadata(message, metadata):
96
95
    if not metadata:
97
96
        return message
98
 
    rt_data = generate_roundtripping_metadata(metadata, encoding)
99
 
    if not rt_data:
100
 
        return message
101
 
    assert type(rt_data) == str
102
 
    return message + "\n--BZR--\n" + rt_data
103
 
 
104
 
 
105
 
def serialize_fileid_map(file_ids):
106
 
    """Serialize a file id map."""
107
 
    lines = []
108
 
    for path in sorted(file_ids.keys()):
109
 
        lines.append("%s\0%s\n" % (path, file_ids[path]))
110
 
    return lines
111
 
 
112
 
 
113
 
def deserialize_fileid_map(filetext):
114
 
    """Deserialize a file id map."""
115
 
    ret = {}
116
 
    f = StringIO(filetext)
117
 
    lines = f.readlines()
118
 
    for l in lines:
119
 
        (path, file_id) = l.rstrip("\n").split("\0")
120
 
        ret[path] = file_id
121
 
    return ret
 
97
    return message + "\n--BZR--\n" + generate_roundtripping_metadata(metadata)