/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 breezy/plugins/git/roundtrip.py

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2018-05-15 18:47:57 UTC
  • mfrom: (6964.2.7 python3-git)
  • Revision ID: breezy.the.bot@gmail.com-20180515184757-xozniaj9gztgtom8
Port some of brz-git to python3.

Merged from https://code.launchpad.net/~jelmer/brz/python3-git/+merge/345479

Show diffs side-by-side

added added

removed removed

Lines of Context:
49
49
 
50
50
from ... import osutils
51
51
 
52
 
from cStringIO import StringIO
 
52
from io import BytesIO
53
53
 
54
54
 
55
55
class CommitSupplement(object):
85
85
def parse_roundtripping_metadata(text):
86
86
    """Parse Bazaar roundtripping metadata."""
87
87
    ret = CommitSupplement()
88
 
    f = StringIO(text)
 
88
    f = BytesIO(text)
89
89
    for l in f.readlines():
90
 
        (key, value) = l.split(":", 1)
91
 
        if key == "revision-id":
 
90
        (key, value) = l.split(b":", 1)
 
91
        if key == b"revision-id":
92
92
            ret.revision_id = value.strip()
93
 
        elif key == "parent-ids":
94
 
            ret.explicit_parent_ids = tuple(value.strip().split(" "))
95
 
        elif key == "testament3-sha1":
96
 
            ret.verifiers["testament3-sha1"] = value.strip()
97
 
        elif key.startswith("property-"):
98
 
            name = key[len("property-"):]
 
93
        elif key == b"parent-ids":
 
94
            ret.explicit_parent_ids = tuple(value.strip().split(b" "))
 
95
        elif key == b"testament3-sha1":
 
96
            ret.verifiers[b"testament3-sha1"] = value.strip()
 
97
        elif key.startswith(b"property-"):
 
98
            name = key[len(b"property-"):]
99
99
            if not name in ret.properties:
100
 
                ret.properties[name] = value[1:].rstrip("\n")
 
100
                ret.properties[name] = value[1:].rstrip(b"\n")
101
101
            else:
102
 
                ret.properties[name] += "\n" + value[1:].rstrip("\n")
 
102
                ret.properties[name] += b"\n" + value[1:].rstrip(b"\n")
103
103
        else:
104
104
            raise ValueError
105
105
    return ret
113
113
    """
114
114
    lines = []
115
115
    if metadata.revision_id:
116
 
        lines.append("revision-id: %s\n" % metadata.revision_id)
 
116
        lines.append(b"revision-id: %s\n" % metadata.revision_id)
117
117
    if metadata.explicit_parent_ids:
118
 
        lines.append("parent-ids: %s\n" % " ".join(metadata.explicit_parent_ids))
 
118
        lines.append(b"parent-ids: %s\n" % b" ".join(metadata.explicit_parent_ids))
119
119
    for key in sorted(metadata.properties.keys()):
120
 
        for l in metadata.properties[key].split("\n"):
121
 
            lines.append("property-%s: %s\n" % (key.encode(encoding), osutils.safe_utf8(l)))
122
 
    if "testament3-sha1" in metadata.verifiers:
123
 
        lines.append("testament3-sha1: %s\n" %
124
 
                     metadata.verifiers["testament3-sha1"])
125
 
    return "".join(lines)
 
120
        for l in metadata.properties[key].split(b"\n"):
 
121
            lines.append(b"property-%s: %s\n" % (key, osutils.safe_utf8(l)))
 
122
    if b"testament3-sha1" in metadata.verifiers:
 
123
        lines.append(b"testament3-sha1: %s\n" %
 
124
                     metadata.verifiers[b"testament3-sha1"])
 
125
    return b"".join(lines)
126
126
 
127
127
 
128
128
def extract_bzr_metadata(message):
131
131
    :param message: Commit message to extract from
132
132
    :return: Tuple with original commit message and metadata object
133
133
    """
134
 
    split = message.split("\n--BZR--\n", 1)
 
134
    split = message.split(b"\n--BZR--\n", 1)
135
135
    if len(split) != 2:
136
136
        return message, None
137
137
    return split[0], parse_roundtripping_metadata(split[1])
143
143
    rt_data = generate_roundtripping_metadata(commit_supplement, encoding)
144
144
    if not rt_data:
145
145
        return message
146
 
    if type(rt_data) is not str:
 
146
    if not isinstance(rt_data, bytes):
147
147
        raise TypeError(rt_data)
148
 
    return message + "\n--BZR--\n" + rt_data
 
148
    return message + b"\n--BZR--\n" + rt_data
149
149
 
150
150
 
151
151
def serialize_fileid_map(file_ids):
156
156
    """
157
157
    lines = []
158
158
    for path in sorted(file_ids.keys()):
159
 
        lines.append("%s\0%s\n" % (path, file_ids[path]))
 
159
        lines.append(b"%s\0%s\n" % (path, file_ids[path]))
160
160
    return lines
161
161
 
162
162
 
167
167
    :return: Fileid map (path -> fileid)
168
168
    """
169
169
    ret = {}
170
 
    f = StringIO(filetext)
 
170
    f = BytesIO(filetext)
171
171
    lines = f.readlines()
172
172
    for l in lines:
173
 
        (path, file_id) = l.rstrip("\n").split("\0")
 
173
        (path, file_id) = l.rstrip(b"\n").split(b"\0")
174
174
        ret[path] = file_id
175
175
    return ret