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

  • Committer: Robert Collins
  • Date: 2005-10-19 10:11:57 UTC
  • mfrom: (1185.16.78)
  • mto: This revision was merged to the branch mainline in revision 1470.
  • Revision ID: robertc@robertcollins.net-20051019101157-17438d311e746b4f
mergeĀ fromĀ upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2010-2018 Jelmer Vernooij <jelmer@jelmer.uk>
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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 __future__ import absolute_import
49
 
 
50
 
from .. import osutils
51
 
 
52
 
from io import BytesIO
53
 
 
54
 
 
55
 
class CommitSupplement(object):
56
 
    """Supplement for a Bazaar revision roundtripped into Git.
57
 
 
58
 
    :ivar revision_id: Revision id, as string
59
 
    :ivar properties: Revision properties, as dictionary
60
 
    :ivar explicit_parent_ids: Parent ids (needed if there are ghosts)
61
 
    :ivar verifiers: Verifier information
62
 
    """
63
 
 
64
 
    revision_id = None
65
 
 
66
 
    explicit_parent_ids = None
67
 
 
68
 
    def __init__(self):
69
 
        self.properties = {}
70
 
        self.verifiers = {}
71
 
 
72
 
    def __nonzero__(self):
73
 
        return bool(self.revision_id or self.properties or
74
 
                    self.explicit_parent_ids)
75
 
 
76
 
 
77
 
class TreeSupplement(object):
78
 
    """Supplement for a Bazaar tree roundtripped into Git.
79
 
 
80
 
    This provides file ids (if they are different from the mapping default)
81
 
    and can provide text revisions.
82
 
    """
83
 
 
84
 
 
85
 
def parse_roundtripping_metadata(text):
86
 
    """Parse Bazaar roundtripping metadata."""
87
 
    ret = CommitSupplement()
88
 
    f = BytesIO(text)
89
 
    for l in f.readlines():
90
 
        (key, value) = l.split(b":", 1)
91
 
        if key == b"revision-id":
92
 
            ret.revision_id = value.strip()
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
 
            if name not in ret.properties:
100
 
                ret.properties[name] = value[1:].rstrip(b"\n")
101
 
            else:
102
 
                ret.properties[name] += b"\n" + value[1:].rstrip(b"\n")
103
 
        else:
104
 
            raise ValueError
105
 
    return ret
106
 
 
107
 
 
108
 
def generate_roundtripping_metadata(metadata, encoding):
109
 
    """Serialize the roundtripping metadata.
110
 
 
111
 
    :param metadata: A `CommitSupplement` instance
112
 
    :return: String with revision metadata
113
 
    """
114
 
    lines = []
115
 
    if metadata.revision_id:
116
 
        lines.append(b"revision-id: %s\n" % metadata.revision_id)
117
 
    if metadata.explicit_parent_ids:
118
 
        lines.append(b"parent-ids: %s\n" %
119
 
                     b" ".join(metadata.explicit_parent_ids))
120
 
    for key in sorted(metadata.properties.keys()):
121
 
        for l in metadata.properties[key].split(b"\n"):
122
 
            lines.append(b"property-%s: %s\n" % (key, osutils.safe_utf8(l)))
123
 
    if b"testament3-sha1" in metadata.verifiers:
124
 
        lines.append(b"testament3-sha1: %s\n" %
125
 
                     metadata.verifiers[b"testament3-sha1"])
126
 
    return b"".join(lines)
127
 
 
128
 
 
129
 
def extract_bzr_metadata(message):
130
 
    """Extract Bazaar metadata from a commit message.
131
 
 
132
 
    :param message: Commit message to extract from
133
 
    :return: Tuple with original commit message and metadata object
134
 
    """
135
 
    split = message.split(b"\n--BZR--\n", 1)
136
 
    if len(split) != 2:
137
 
        return message, None
138
 
    return split[0], parse_roundtripping_metadata(split[1])
139
 
 
140
 
 
141
 
def inject_bzr_metadata(message, commit_supplement, encoding):
142
 
    if not commit_supplement:
143
 
        return message
144
 
    rt_data = generate_roundtripping_metadata(commit_supplement, encoding)
145
 
    if not rt_data:
146
 
        return message
147
 
    if not isinstance(rt_data, bytes):
148
 
        raise TypeError(rt_data)
149
 
    return message + b"\n--BZR--\n" + rt_data