1
# Copyright (C) 2010 Jelmer Vernooij <jelmer@samba.org>
1
# Copyright (C) 2010-2018 Jelmer Vernooij <jelmer@jelmer.uk>
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
13
13
# You should have received a copy of the GNU General Public License
14
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.
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Roundtripping support.
19
Bazaar stores more data than Git, which means that in order to preserve
20
a commit when it is pushed from Bazaar into Git we have to stash
21
that extra metadata somewhere.
23
There are two kinds of metadata relevant here:
24
* per-file metadata (stored by revision+path)
25
- usually stored per tree
26
* per-revision metadata (stored by git commit id)
28
Bazaar revisions have the following information that is not
29
present in Git commits:
37
* path last changed revisions [1]
39
[1] path last changed revision information can usually
40
be induced from the existing history, unless
41
ghost revisions are involved.
43
This extra metadata is stored in so-called "supplements":
48
from .. import osutils
50
from io import BytesIO
53
class CommitSupplement(object):
54
"""Supplement for a Bazaar revision roundtripped into Git.
26
56
:ivar revision_id: Revision id, as string
27
57
:ivar properties: Revision properties, as dictionary
34
64
explicit_parent_ids = None
38
66
def __init__(self):
39
67
self.properties = {}
41
70
def __nonzero__(self):
42
return bool(self.revision_id or self.properties)
71
return bool(self.revision_id or self.properties or
72
self.explicit_parent_ids)
75
class TreeSupplement(object):
76
"""Supplement for a Bazaar tree roundtripped into Git.
78
This provides file ids (if they are different from the mapping default)
79
and can provide text revisions.
45
83
def parse_roundtripping_metadata(text):
46
84
"""Parse Bazaar roundtripping metadata."""
47
ret = BzrGitRevisionMetadata()
85
ret = CommitSupplement()
49
87
for l in f.readlines():
50
(key, value) = l.split(":", 1)
51
if key == "revision-id":
88
(key, value) = l.split(b":", 1)
89
if key == b"revision-id":
52
90
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.verifiers["testament3-sha1"] = value.strip()
57
elif key.startswith("property-"):
58
ret.properties[key[len("property-"):]] = value[1:].rstrip("\n")
91
elif key == b"parent-ids":
92
ret.explicit_parent_ids = tuple(value.strip().split(b" "))
93
elif key == b"testament3-sha1":
94
ret.verifiers[b"testament3-sha1"] = value.strip()
95
elif key.startswith(b"property-"):
96
name = key[len(b"property-"):]
97
if name not in ret.properties:
98
ret.properties[name] = value[1:].rstrip(b"\n")
100
ret.properties[name] += b"\n" + value[1:].rstrip(b"\n")
64
106
def generate_roundtripping_metadata(metadata, encoding):
65
107
"""Serialize the roundtripping metadata.
67
:param metadata: A `BzrGitRevisionMetadata` instance
109
:param metadata: A `CommitSupplement` instance
68
110
:return: String with revision metadata
71
113
if metadata.revision_id:
72
lines.append("revision-id: %s\n" % metadata.revision_id)
114
lines.append(b"revision-id: %s\n" % metadata.revision_id)
73
115
if metadata.explicit_parent_ids:
74
lines.append("parent-ids: %s\n" % " ".join(metadata.explicit_parent_ids))
116
lines.append(b"parent-ids: %s\n" %
117
b" ".join(metadata.explicit_parent_ids))
75
118
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"])
119
for l in metadata.properties[key].split(b"\n"):
120
lines.append(b"property-%s: %s\n" % (key, osutils.safe_utf8(l)))
121
if b"testament3-sha1" in metadata.verifiers:
122
lines.append(b"testament3-sha1: %s\n" %
123
metadata.verifiers[b"testament3-sha1"])
124
return b"".join(lines)
83
127
def extract_bzr_metadata(message):
86
130
:param message: Commit message to extract from
87
131
:return: Tuple with original commit message and metadata object
89
split = message.split("\n--BZR--\n", 1)
133
split = message.split(b"\n--BZR--\n", 1)
90
134
if len(split) != 2:
91
135
return message, None
92
136
return split[0], parse_roundtripping_metadata(split[1])
95
def inject_bzr_metadata(message, metadata, encoding):
139
def inject_bzr_metadata(message, commit_supplement, encoding):
140
if not commit_supplement:
98
rt_data = generate_roundtripping_metadata(metadata, encoding)
142
rt_data = generate_roundtripping_metadata(commit_supplement, encoding)
101
assert type(rt_data) == str
102
return message + "\n--BZR--\n" + rt_data
105
def serialize_fileid_map(file_ids):
106
"""Serialize a file id map."""
108
for path in sorted(file_ids.keys()):
109
lines.append("%s\0%s\n" % (path, file_ids[path]))
113
def deserialize_fileid_map(filetext):
114
"""Deserialize a file id map."""
116
f = StringIO(filetext)
117
lines = f.readlines()
119
(path, file_id) = l.rstrip("\n").split("\0")
145
if not isinstance(rt_data, bytes):
146
raise TypeError(rt_data)
147
return message + b"\n--BZR--\n" + rt_data