/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.358.2 by Jelmer Vernooij
Refresh copyright headers, add my email.
1
# Copyright (C) 2010-2018 Jelmer Vernooij <jelmer@jelmer.uk>
0.252.1 by Jelmer Vernooij
Support storing revision id data.
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
0.358.1 by Jelmer Vernooij
Fix FSF address.
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
0.252.1 by Jelmer Vernooij
Support storing revision id data.
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
0.200.1594 by Jelmer Vernooij
Use absolute_import everywhere.
48
from __future__ import absolute_import
49
6986.2.1 by Jelmer Vernooij
Move breezy.plugins.git to breezy.git.
50
from .. import osutils
0.252.1 by Jelmer Vernooij
Support storing revision id data.
51
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
52
from io import BytesIO
0.252.1 by Jelmer Vernooij
Support storing revision id data.
53
54
0.200.1324 by Jelmer Vernooij
More work on roundtripping support.
55
class CommitSupplement(object):
56
    """Supplement for a Bazaar revision roundtripped into Git.
0.200.1020 by Jelmer Vernooij
Store testament-sha1 in metadata.
57
0.252.1 by Jelmer Vernooij
Support storing revision id data.
58
    :ivar revision_id: Revision id, as string
59
    :ivar properties: Revision properties, as dictionary
0.252.8 by Jelmer Vernooij
Support ghost revisions while roundtripping.
60
    :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.
61
    :ivar verifiers: Verifier information
0.252.1 by Jelmer Vernooij
Support storing revision id data.
62
    """
63
64
    revision_id = None
65
0.252.8 by Jelmer Vernooij
Support ghost revisions while roundtripping.
66
    explicit_parent_ids = None
67
0.252.10 by Jelmer Vernooij
Support roundtripping custom revision properties.
68
    def __init__(self):
69
        self.properties = {}
0.200.1328 by Jelmer Vernooij
More test fixes.
70
        self.verifiers = {}
0.252.1 by Jelmer Vernooij
Support storing revision id data.
71
0.252.2 by Jelmer Vernooij
Add functions for adding metadata to revision messages.
72
    def __nonzero__(self):
0.200.1324 by Jelmer Vernooij
More work on roundtripping support.
73
        return bool(self.revision_id or self.properties or self.explicit_parent_ids)
74
75
76
class TreeSupplement(object):
77
    """Supplement for a Bazaar tree roundtripped into Git.
78
79
    This provides file ids (if they are different from the mapping default)
80
    and can provide text revisions.
81
    """
82
0.252.2 by Jelmer Vernooij
Add functions for adding metadata to revision messages.
83
0.252.1 by Jelmer Vernooij
Support storing revision id data.
84
85
def parse_roundtripping_metadata(text):
86
    """Parse Bazaar roundtripping metadata."""
0.200.1324 by Jelmer Vernooij
More work on roundtripping support.
87
    ret = CommitSupplement()
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
88
    f = BytesIO(text)
0.252.1 by Jelmer Vernooij
Support storing revision id data.
89
    for l in f.readlines():
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
90
        (key, value) = l.split(b":", 1)
91
        if key == b"revision-id":
0.252.1 by Jelmer Vernooij
Support storing revision id data.
92
            ret.revision_id = value.strip()
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
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-"):]
0.200.1324 by Jelmer Vernooij
More work on roundtripping support.
99
            if not name in ret.properties:
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
100
                ret.properties[name] = value[1:].rstrip(b"\n")
0.200.1324 by Jelmer Vernooij
More work on roundtripping support.
101
            else:
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
102
                ret.properties[name] += b"\n" + value[1:].rstrip(b"\n")
0.252.1 by Jelmer Vernooij
Support storing revision id data.
103
        else:
104
            raise ValueError
105
    return ret
0.252.2 by Jelmer Vernooij
Add functions for adding metadata to revision messages.
106
107
0.252.40 by Jelmer Vernooij
Checks for roundtripping.
108
def generate_roundtripping_metadata(metadata, encoding):
0.252.2 by Jelmer Vernooij
Add functions for adding metadata to revision messages.
109
    """Serialize the roundtripping metadata.
110
0.200.1328 by Jelmer Vernooij
More test fixes.
111
    :param metadata: A `CommitSupplement` instance
0.252.2 by Jelmer Vernooij
Add functions for adding metadata to revision messages.
112
    :return: String with revision metadata
113
    """
0.252.8 by Jelmer Vernooij
Support ghost revisions while roundtripping.
114
    lines = []
115
    if metadata.revision_id:
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
116
        lines.append(b"revision-id: %s\n" % metadata.revision_id)
0.252.8 by Jelmer Vernooij
Support ghost revisions while roundtripping.
117
    if metadata.explicit_parent_ids:
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
118
        lines.append(b"parent-ids: %s\n" % b" ".join(metadata.explicit_parent_ids))
0.252.10 by Jelmer Vernooij
Support roundtripping custom revision properties.
119
    for key in sorted(metadata.properties.keys()):
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
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)
0.252.2 by Jelmer Vernooij
Add functions for adding metadata to revision messages.
126
127
128
def extract_bzr_metadata(message):
129
    """Extract Bazaar metadata from a commit message.
130
131
    :param message: Commit message to extract from
132
    :return: Tuple with original commit message and metadata object
133
    """
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
134
    split = message.split(b"\n--BZR--\n", 1)
0.252.2 by Jelmer Vernooij
Add functions for adding metadata to revision messages.
135
    if len(split) != 2:
136
        return message, None
137
    return split[0], parse_roundtripping_metadata(split[1])
138
139
0.200.1324 by Jelmer Vernooij
More work on roundtripping support.
140
def inject_bzr_metadata(message, commit_supplement, encoding):
141
    if not commit_supplement:
0.252.2 by Jelmer Vernooij
Add functions for adding metadata to revision messages.
142
        return message
0.200.1324 by Jelmer Vernooij
More work on roundtripping support.
143
    rt_data = generate_roundtripping_metadata(commit_supplement, encoding)
0.200.1019 by Jelmer Vernooij
Handle empty metadata.
144
    if not rt_data:
145
        return message
6964.2.3 by Jelmer Vernooij
Review comments.
146
    if not isinstance(rt_data, bytes):
0.361.1 by Jelmer Vernooij
Don't use assert.
147
        raise TypeError(rt_data)
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
148
    return message + b"\n--BZR--\n" + rt_data
0.252.22 by Jelmer Vernooij
Fix file id map (de)serialization.
149
150
151
def serialize_fileid_map(file_ids):
0.274.1 by Jelmer Vernooij
Docstrings
152
    """Serialize a fileid map.
153
154
    :param file_ids: Path -> fileid map
155
    :return: Serialized fileid map, as sequence of chunks
156
    """
0.252.22 by Jelmer Vernooij
Fix file id map (de)serialization.
157
    lines = []
158
    for path in sorted(file_ids.keys()):
6973.13.2 by Jelmer Vernooij
Fix some more tests.
159
        lines.append(b"%s\0%s\n" % (path.encode('utf-8'), file_ids[path]))
0.252.23 by Jelmer Vernooij
More work on roundtripping support.
160
    return lines
0.252.22 by Jelmer Vernooij
Fix file id map (de)serialization.
161
162
0.200.1020 by Jelmer Vernooij
Store testament-sha1 in metadata.
163
def deserialize_fileid_map(filetext):
0.200.1519 by Jelmer Vernooij
Merge improved docstrings.
164
    """Deserialize a file id map.
0.274.1 by Jelmer Vernooij
Docstrings
165
166
    :param file: File
167
    :return: Fileid map (path -> fileid)
168
    """
0.252.22 by Jelmer Vernooij
Fix file id map (de)serialization.
169
    ret = {}
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
170
    f = BytesIO(filetext)
0.252.22 by Jelmer Vernooij
Fix file id map (de)serialization.
171
    lines = f.readlines()
172
    for l in lines:
6964.2.1 by Jelmer Vernooij
Initial work to support brz-git on python3.
173
        (path, file_id) = l.rstrip(b"\n").split(b"\0")
6973.13.2 by Jelmer Vernooij
Fix some more tests.
174
        ret[path.decode('utf-8')] = file_id
0.252.22 by Jelmer Vernooij
Fix file id map (de)serialization.
175
    return ret