/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

  • Committer: Robert Collins
  • Date: 2010-05-06 07:48:22 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100506074822-0bsgf2j4h8jx0xkk
Added ``bzrlib.tests.matchers`` as a place to put matchers, along with
our first in-tree matcher. See the module docstring for details.
(Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
17
 
"""Roundtripping support."""
18
 
 
19
 
 
20
 
from cStringIO import StringIO
21
 
 
22
 
 
23
 
class BzrGitRevisionMetadata(object):
24
 
    """Metadata for a Bazaar revision roundtripped into Git.
25
 
    
26
 
    :ivar revision_id: Revision id, as string
27
 
    :ivar properties: Revision properties, as dictionary
28
 
    :ivar explicit_parent_ids: Parent ids (needed if there are ghosts)
29
 
    """
30
 
 
31
 
    revision_id = None
32
 
 
33
 
    explicit_parent_ids = None
34
 
 
35
 
    def __init__(self):
36
 
        self.properties = {}
37
 
 
38
 
    def __nonzero__(self):
39
 
        return bool(self.revision_id or self.properties)
40
 
 
41
 
 
42
 
def parse_roundtripping_metadata(text):
43
 
    """Parse Bazaar roundtripping metadata."""
44
 
    ret = BzrGitRevisionMetadata()
45
 
    f = StringIO(text)
46
 
    for l in f.readlines():
47
 
        (key, value) = l.split(":", 1)
48
 
        if key == "revision-id":
49
 
            ret.revision_id = value.strip()
50
 
        elif key == "parent-ids":
51
 
            ret.explicit_parent_ids = tuple(value.strip().split(" "))
52
 
        elif key.startswith("property-"):
53
 
            ret.properties[key[len("property-"):]] = value[1:].rstrip("\n")
54
 
        else:
55
 
            raise ValueError
56
 
    return ret
57
 
 
58
 
 
59
 
def generate_roundtripping_metadata(metadata, encoding):
60
 
    """Serialize the roundtripping metadata.
61
 
 
62
 
    :param metadata: A `BzrGitRevisionMetadata` instance
63
 
    :return: String with revision metadata
64
 
    """
65
 
    lines = []
66
 
    if metadata.revision_id:
67
 
        lines.append("revision-id: %s\n" % metadata.revision_id)
68
 
    if metadata.explicit_parent_ids:
69
 
        lines.append("parent-ids: %s\n" % " ".join(metadata.explicit_parent_ids))
70
 
    for key in sorted(metadata.properties.keys()):
71
 
        lines.append("property-%s: %s\n" % (key.encode(encoding), metadata.properties[key].encode(encoding)))
72
 
    return "".join(lines)
73
 
 
74
 
 
75
 
def extract_bzr_metadata(message):
76
 
    """Extract Bazaar metadata from a commit message.
77
 
 
78
 
    :param message: Commit message to extract from
79
 
    :return: Tuple with original commit message and metadata object
80
 
    """
81
 
    split = message.split("\n--BZR--\n", 1)
82
 
    if len(split) != 2:
83
 
        return message, None
84
 
    return split[0], parse_roundtripping_metadata(split[1])
85
 
 
86
 
 
87
 
def inject_bzr_metadata(message, metadata, encoding):
88
 
    if not metadata:
89
 
        return message
90
 
    rt_data = generate_roundtripping_metadata(metadata, encoding)
91
 
    assert type(rt_data) == str
92
 
    return message + "\n--BZR--\n" + rt_data
93
 
 
94
 
 
95
 
def serialize_fileid_map(file_ids):
96
 
    lines = []
97
 
    for path in sorted(file_ids.keys()):
98
 
        lines.append("%s\0%s\n" % (path, file_ids[path]))
99
 
    return lines
100
 
 
101
 
 
102
 
def deserialize_fileid_map(file):
103
 
    ret = {}
104
 
    f = StringIO(file)
105
 
    lines = f.readlines()
106
 
    for l in lines:
107
 
        (path, file_id) = l.rstrip("\n").split("\0")
108
 
        ret[path] = file_id
109
 
    return ret