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.
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 bzrlib import osutils
50
from cStringIO import StringIO
53
class CommitSupplement(object):
54
"""Supplement for a Bazaar revision roundtripped into Git.
56
:ivar revision_id: Revision id, as string
57
:ivar properties: Revision properties, as dictionary
58
:ivar explicit_parent_ids: Parent ids (needed if there are ghosts)
59
:ivar verifiers: Verifier information
64
explicit_parent_ids = None
71
def __nonzero__(self):
72
return bool(self.revision_id or self.properties or 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.
84
def parse_roundtripping_metadata(text):
85
"""Parse Bazaar roundtripping metadata."""
86
ret = CommitSupplement()
88
for l in f.readlines():
89
(key, value) = l.split(":", 1)
90
if key == "revision-id":
91
ret.revision_id = value.strip()
92
elif key == "parent-ids":
93
ret.explicit_parent_ids = tuple(value.strip().split(" "))
94
elif key == "testament3-sha1":
95
ret.verifiers["testament3-sha1"] = value.strip()
96
elif key.startswith("property-"):
97
name = key[len("property-"):]
98
if not name in ret.properties:
99
ret.properties[name] = value[1:].rstrip("\n")
101
ret.properties[name] += "\n" + value[1:].rstrip("\n")
107
def generate_roundtripping_metadata(metadata, encoding):
108
"""Serialize the roundtripping metadata.
110
:param metadata: A `BzrGitRevisionMetadata` instance
111
:return: String with revision metadata
114
if metadata.revision_id:
115
lines.append("revision-id: %s\n" % metadata.revision_id)
116
if metadata.explicit_parent_ids:
117
lines.append("parent-ids: %s\n" % " ".join(metadata.explicit_parent_ids))
118
for key in sorted(metadata.properties.keys()):
119
for l in metadata.properties[key].split("\n"):
120
lines.append("property-%s: %s\n" % (key.encode(encoding), osutils.safe_utf8(l)))
121
if "testament3-sha1" in metadata.verifiers:
122
lines.append("testament3-sha1: %s\n" %
123
metadata.verifiers["testament3-sha1"])
124
return "".join(lines)
127
def extract_bzr_metadata(message):
128
"""Extract Bazaar metadata from a commit message.
130
:param message: Commit message to extract from
131
:return: Tuple with original commit message and metadata object
133
split = message.split("\n--BZR--\n", 1)
136
return split[0], parse_roundtripping_metadata(split[1])
139
def inject_bzr_metadata(message, commit_supplement, encoding):
140
if not commit_supplement:
142
rt_data = generate_roundtripping_metadata(commit_supplement, encoding)
145
assert type(rt_data) == str
146
return message + "\n--BZR--\n" + rt_data
149
def serialize_fileid_map(file_ids):
150
"""Serialize a file id map."""
152
for path in sorted(file_ids.keys()):
153
lines.append("%s\0%s\n" % (path, file_ids[path]))
157
def deserialize_fileid_map(filetext):
158
"""Deserialize a file id map."""
160
f = StringIO(filetext)
161
lines = f.readlines()
163
(path, file_id) = l.rstrip("\n").split("\0")