/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.252.1 by Jelmer Vernooij
Support storing revision id data.
1
# Copyright (C) 2010 Jelmer Vernooij <jelmer@samba.org>
2
#
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.
7
#
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.
12
#
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
16
0.200.1324 by Jelmer Vernooij
More work on roundtripping support.
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
0.252.1 by Jelmer Vernooij
Support storing revision id data.
49
50
from cStringIO import StringIO
51
52
0.200.1324 by Jelmer Vernooij
More work on roundtripping support.
53
class CommitSupplement(object):
54
    """Supplement for a Bazaar revision roundtripped into Git.
0.200.1020 by Jelmer Vernooij
Store testament-sha1 in metadata.
55
0.252.1 by Jelmer Vernooij
Support storing revision id data.
56
    :ivar revision_id: Revision id, as string
57
    :ivar properties: Revision properties, as dictionary
0.252.8 by Jelmer Vernooij
Support ghost revisions while roundtripping.
58
    :ivar explicit_parent_ids: Parent ids (needed if there are ghosts)
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
59
    :ivar verifiers: Verifier information
0.252.1 by Jelmer Vernooij
Support storing revision id data.
60
    """
61
62
    revision_id = None
63
0.252.8 by Jelmer Vernooij
Support ghost revisions while roundtripping.
64
    explicit_parent_ids = None
65
0.252.10 by Jelmer Vernooij
Support roundtripping custom revision properties.
66
    def __init__(self):
67
        self.properties = {}
0.200.1328 by Jelmer Vernooij
More test fixes.
68
        self.verifiers = {}
0.252.1 by Jelmer Vernooij
Support storing revision id data.
69
0.252.2 by Jelmer Vernooij
Add functions for adding metadata to revision messages.
70
    def __nonzero__(self):
0.200.1324 by Jelmer Vernooij
More work on roundtripping support.
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
0.252.2 by Jelmer Vernooij
Add functions for adding metadata to revision messages.
81
0.252.1 by Jelmer Vernooij
Support storing revision id data.
82
83
def parse_roundtripping_metadata(text):
84
    """Parse Bazaar roundtripping metadata."""
0.200.1324 by Jelmer Vernooij
More work on roundtripping support.
85
    ret = CommitSupplement()
0.252.1 by Jelmer Vernooij
Support storing revision id data.
86
    f = StringIO(text)
87
    for l in f.readlines():
88
        (key, value) = l.split(":", 1)
89
        if key == "revision-id":
90
            ret.revision_id = value.strip()
0.252.8 by Jelmer Vernooij
Support ghost revisions while roundtripping.
91
        elif key == "parent-ids":
92
            ret.explicit_parent_ids = tuple(value.strip().split(" "))
0.200.1023 by Jelmer Vernooij
Set and verify testament.
93
        elif key == "testament3-sha1":
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
94
            ret.verifiers["testament3-sha1"] = value.strip()
0.252.10 by Jelmer Vernooij
Support roundtripping custom revision properties.
95
        elif key.startswith("property-"):
0.200.1324 by Jelmer Vernooij
More work on roundtripping support.
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")
0.252.1 by Jelmer Vernooij
Support storing revision id data.
101
        else:
102
            raise ValueError
103
    return ret
0.252.2 by Jelmer Vernooij
Add functions for adding metadata to revision messages.
104
105
0.252.40 by Jelmer Vernooij
Checks for roundtripping.
106
def generate_roundtripping_metadata(metadata, encoding):
0.252.2 by Jelmer Vernooij
Add functions for adding metadata to revision messages.
107
    """Serialize the roundtripping metadata.
108
0.200.1328 by Jelmer Vernooij
More test fixes.
109
    :param metadata: A `CommitSupplement` instance
0.252.2 by Jelmer Vernooij
Add functions for adding metadata to revision messages.
110
    :return: String with revision metadata
111
    """
0.252.8 by Jelmer Vernooij
Support ghost revisions while roundtripping.
112
    lines = []
113
    if metadata.revision_id:
114
        lines.append("revision-id: %s\n" % metadata.revision_id)
115
    if metadata.explicit_parent_ids:
116
        lines.append("parent-ids: %s\n" % " ".join(metadata.explicit_parent_ids))
0.252.10 by Jelmer Vernooij
Support roundtripping custom revision properties.
117
    for key in sorted(metadata.properties.keys()):
0.200.1324 by Jelmer Vernooij
More work on roundtripping support.
118
        for l in metadata.properties[key].split("\n"):
119
            lines.append("property-%s: %s\n" % (key.encode(encoding), osutils.safe_utf8(l)))
0.200.1029 by Jelmer Vernooij
Use dictionary with verifiers rather than requiring testament3-sha1 everywhere.
120
    if "testament3-sha1" in metadata.verifiers:
121
        lines.append("testament3-sha1: %s\n" %
122
                     metadata.verifiers["testament3-sha1"])
0.252.8 by Jelmer Vernooij
Support ghost revisions while roundtripping.
123
    return "".join(lines)
0.252.2 by Jelmer Vernooij
Add functions for adding metadata to revision messages.
124
125
126
def extract_bzr_metadata(message):
127
    """Extract Bazaar metadata from a commit message.
128
129
    :param message: Commit message to extract from
130
    :return: Tuple with original commit message and metadata object
131
    """
132
    split = message.split("\n--BZR--\n", 1)
133
    if len(split) != 2:
134
        return message, None
135
    return split[0], parse_roundtripping_metadata(split[1])
136
137
0.200.1324 by Jelmer Vernooij
More work on roundtripping support.
138
def inject_bzr_metadata(message, commit_supplement, encoding):
139
    if not commit_supplement:
0.252.2 by Jelmer Vernooij
Add functions for adding metadata to revision messages.
140
        return message
0.200.1324 by Jelmer Vernooij
More work on roundtripping support.
141
    rt_data = generate_roundtripping_metadata(commit_supplement, encoding)
0.200.1019 by Jelmer Vernooij
Handle empty metadata.
142
    if not rt_data:
143
        return message
0.252.40 by Jelmer Vernooij
Checks for roundtripping.
144
    assert type(rt_data) == str
145
    return message + "\n--BZR--\n" + rt_data
0.252.22 by Jelmer Vernooij
Fix file id map (de)serialization.
146
147
148
def serialize_fileid_map(file_ids):
0.200.1020 by Jelmer Vernooij
Store testament-sha1 in metadata.
149
    """Serialize a file id map."""
0.252.22 by Jelmer Vernooij
Fix file id map (de)serialization.
150
    lines = []
151
    for path in sorted(file_ids.keys()):
152
        lines.append("%s\0%s\n" % (path, file_ids[path]))
0.252.23 by Jelmer Vernooij
More work on roundtripping support.
153
    return lines
0.252.22 by Jelmer Vernooij
Fix file id map (de)serialization.
154
155
0.200.1020 by Jelmer Vernooij
Store testament-sha1 in metadata.
156
def deserialize_fileid_map(filetext):
157
    """Deserialize a file id map."""
0.252.22 by Jelmer Vernooij
Fix file id map (de)serialization.
158
    ret = {}
0.200.1020 by Jelmer Vernooij
Store testament-sha1 in metadata.
159
    f = StringIO(filetext)
0.252.22 by Jelmer Vernooij
Fix file id map (de)serialization.
160
    lines = f.readlines()
161
    for l in lines:
162
        (path, file_id) = l.rstrip("\n").split("\0")
163
        ret[path] = file_id
164
    return ret