1
# Copyright (C) 2010 Jelmer Vernooij <jelmer@samba.org>
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Roundtripping support."""
20
from cStringIO import StringIO
23
class BzrGitRevisionMetadata(object):
24
"""Metadata for a Bazaar revision roundtripped into Git.
26
:ivar revision_id: Revision id, as string
27
:ivar properties: Revision properties, as dictionary
28
:ivar explicit_parent_ids: Parent ids (needed if there are ghosts)
29
:ivar testament3_sha1: SHA1 of the testament.
34
explicit_parent_ids = None
36
testament3_sha1 = None
41
def __nonzero__(self):
42
return bool(self.revision_id or self.properties)
45
def parse_roundtripping_metadata(text):
46
"""Parse Bazaar roundtripping metadata."""
47
ret = BzrGitRevisionMetadata()
49
for l in f.readlines():
50
(key, value) = l.split(":", 1)
51
if key == "revision-id":
52
ret.revision_id = value.strip()
53
elif key == "parent-ids":
54
ret.explicit_parent_ids = tuple(value.strip().split(" "))
55
elif key == "testament3-sha1":
56
ret.testament3_sha1 = value.strip()
57
elif key.startswith("property-"):
58
ret.properties[key[len("property-"):]] = value[1:].rstrip("\n")
64
def generate_roundtripping_metadata(metadata, encoding):
65
"""Serialize the roundtripping metadata.
67
:param metadata: A `BzrGitRevisionMetadata` instance
68
:return: String with revision metadata
71
if metadata.revision_id:
72
lines.append("revision-id: %s\n" % metadata.revision_id)
73
if metadata.explicit_parent_ids:
74
lines.append("parent-ids: %s\n" % " ".join(metadata.explicit_parent_ids))
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 metadata.testament3_sha1:
78
lines.append("testament3-sha1: %s\n" % metadata.testament3_sha1)
82
def extract_bzr_metadata(message):
83
"""Extract Bazaar metadata from a commit message.
85
:param message: Commit message to extract from
86
:return: Tuple with original commit message and metadata object
88
split = message.split("\n--BZR--\n", 1)
91
return split[0], parse_roundtripping_metadata(split[1])
94
def inject_bzr_metadata(message, metadata, encoding):
97
rt_data = generate_roundtripping_metadata(metadata, encoding)
100
assert type(rt_data) == str
101
return message + "\n--BZR--\n" + rt_data
104
def serialize_fileid_map(file_ids):
105
"""Serialize a file id map."""
107
for path in sorted(file_ids.keys()):
108
lines.append("%s\0%s\n" % (path, file_ids[path]))
112
def deserialize_fileid_map(filetext):
113
"""Deserialize a file id map."""
115
f = StringIO(filetext)
116
lines = f.readlines()
118
(path, file_id) = l.rstrip("\n").split("\0")