/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

More work on roundtrip push support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
 
"""Roundtripping support.
18
 
 
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.
22
 
 
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)
27
 
 
28
 
Bazaar revisions have the following information that is not
29
 
present in Git commits:
30
 
 * revision ids
31
 
 * revision properties
32
 
 * ghost parents
33
 
 
34
 
Tree content:
35
 
 * empty directories
36
 
 * path file ids
37
 
 * path last changed revisions [1]
38
 
 
39
 
 [1] path last changed revision information can usually
40
 
     be induced from the existing history, unless
41
 
     ghost revisions are involved.
42
 
 
43
 
This extra metadata is stored in so-called "supplements":
44
 
  * CommitSupplement
45
 
  * TreeSupplement
46
 
"""
47
 
 
48
 
from bzrlib import osutils
 
17
"""Roundtripping support."""
 
18
 
49
19
 
50
20
from cStringIO import StringIO
51
21
 
52
22
 
53
 
class CommitSupplement(object):
54
 
    """Supplement for a Bazaar revision roundtripped into Git.
 
23
class BzrGitRevisionMetadata(object):
 
24
    """Metadata for a Bazaar revision roundtripped into Git.
55
25
 
56
26
    :ivar revision_id: Revision id, as string
57
27
    :ivar properties: Revision properties, as dictionary
63
33
 
64
34
    explicit_parent_ids = None
65
35
 
 
36
    verifiers = {}
 
37
 
66
38
    def __init__(self):
67
39
        self.properties = {}
68
 
        self.verifiers = {}
69
40
 
70
41
    def __nonzero__(self):
71
 
        return bool(self.revision_id or self.properties or self.explicit_parent_ids)
72
 
 
73
 
 
74
 
class TreeSupplement(object):
75
 
    """Supplement for a Bazaar tree roundtripped into Git.
76
 
 
77
 
    This provides file ids (if they are different from the mapping default)
78
 
    and can provide text revisions.
79
 
    """
80
 
 
 
42
        return bool(self.revision_id or self.properties)
81
43
 
82
44
 
83
45
def parse_roundtripping_metadata(text):
84
46
    """Parse Bazaar roundtripping metadata."""
85
 
    ret = CommitSupplement()
 
47
    ret = BzrGitRevisionMetadata()
86
48
    f = StringIO(text)
87
49
    for l in f.readlines():
88
50
        (key, value) = l.split(":", 1)
93
55
        elif key == "testament3-sha1":
94
56
            ret.verifiers["testament3-sha1"] = value.strip()
95
57
        elif key.startswith("property-"):
96
 
            name = key[len("property-"):]
97
 
            if not name in ret.properties:
98
 
                ret.properties[name] = value[1:].rstrip("\n")
99
 
            else:
100
 
                ret.properties[name] += "\n" + value[1:].rstrip("\n")
 
58
            ret.properties[key[len("property-"):]] = value[1:].rstrip("\n")
101
59
        else:
102
60
            raise ValueError
103
61
    return ret
106
64
def generate_roundtripping_metadata(metadata, encoding):
107
65
    """Serialize the roundtripping metadata.
108
66
 
109
 
    :param metadata: A `CommitSupplement` instance
 
67
    :param metadata: A `BzrGitRevisionMetadata` instance
110
68
    :return: String with revision metadata
111
69
    """
112
70
    lines = []
115
73
    if metadata.explicit_parent_ids:
116
74
        lines.append("parent-ids: %s\n" % " ".join(metadata.explicit_parent_ids))
117
75
    for key in sorted(metadata.properties.keys()):
118
 
        for l in metadata.properties[key].split("\n"):
119
 
            lines.append("property-%s: %s\n" % (key.encode(encoding), osutils.safe_utf8(l)))
 
76
        lines.append("property-%s: %s\n" % (key.encode(encoding), metadata.properties[key].encode(encoding)))
120
77
    if "testament3-sha1" in metadata.verifiers:
121
78
        lines.append("testament3-sha1: %s\n" %
122
79
                     metadata.verifiers["testament3-sha1"])
135
92
    return split[0], parse_roundtripping_metadata(split[1])
136
93
 
137
94
 
138
 
def inject_bzr_metadata(message, commit_supplement, encoding):
139
 
    if not commit_supplement:
 
95
def inject_bzr_metadata(message, metadata, encoding):
 
96
    if not metadata:
140
97
        return message
141
 
    rt_data = generate_roundtripping_metadata(commit_supplement, encoding)
 
98
    rt_data = generate_roundtripping_metadata(metadata, encoding)
142
99
    if not rt_data:
143
100
        return message
144
101
    assert type(rt_data) == str