/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: Martin Pool
  • Date: 2005-06-28 03:02:31 UTC
  • Revision ID: mbp@sourcefrog.net-20050628030231-d311e4ebcd467ef4
Merge John's import-speedup branch:

                                                                                         
  777 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 22:20:32 -0500
      revision-id: john@arbash-meinel.com-20050627032031-e82a50db3863b18e
      bzr selftest was not using the correct bzr

  776 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 22:20:22 -0500
      revision-id: john@arbash-meinel.com-20050627032021-c9f21fde989ddaee
      Add was using an old mutter

  775 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 22:02:33 -0500
      revision-id: john@arbash-meinel.com-20050627030233-9165cfe98fc63298
      Cleaned up to be less different

  774 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 21:54:53 -0500
      revision-id: john@arbash-meinel.com-20050627025452-4260d0e744edef43
      Allow BZR_PLUGIN_PATH='' to negate plugin loading.

  773 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 21:49:34 -0500
      revision-id: john@arbash-meinel.com-20050627024933-b7158f67b7b9eae5
      Finished the previous cleanup (allowing load_plugins to be called twice)

  772 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 21:45:08 -0500
      revision-id: john@arbash-meinel.com-20050627024508-723b1df510d196fc
      Work on making the tests pass. versioning.py is calling run_cmd directly, but plugins have been loaded.

  771 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 21:32:29 -0500
      revision-id: john@arbash-meinel.com-20050627023228-79972744d7c53e15
      Got it down a little bit more by removing import of tree and inventory.

  770 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 21:26:05 -0500
      revision-id: john@arbash-meinel.com-20050627022604-350b9773ef622f95
      Reducing the number of import from bzrlib/__init__.py and bzrlib/branch.py

  769 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 20:32:25 -0500
      revision-id: john@arbash-meinel.com-20050627013225-32dd044f10d23948
      Updated revision.py and xml.py to include SubElement.

  768 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 20:03:56 -0500
      revision-id: john@arbash-meinel.com-20050627010356-ee66919e1c377faf
      Minor typo

  767 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 20:03:13 -0500
      revision-id: john@arbash-meinel.com-20050627010312-40d024007eb85051
      Caching the import

  766 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 19:51:47 -0500
      revision-id: john@arbash-meinel.com-20050627005147-5281c99e48ed1834
      Created wrapper functions for lazy import of ElementTree

  765 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 19:46:37 -0500
      revision-id: john@arbash-meinel.com-20050627004636-bf432902004a94c5
      Removed all of the test imports of cElementTree

  764 John Arbash Meinel <john@arbash-meinel.com>       Sun 2005-06-26 19:43:59 -0500
      revision-id: john@arbash-meinel.com-20050627004358-d137fbe9570dd71b
      Trying to make bzr startup faster.

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
 
    :ivar verifiers: Verifier information
30
 
    """
31
 
 
32
 
    revision_id = None
33
 
 
34
 
    explicit_parent_ids = None
35
 
 
36
 
    verifiers = {}
37
 
 
38
 
    def __init__(self):
39
 
        self.properties = {}
40
 
 
41
 
    def __nonzero__(self):
42
 
        return bool(self.revision_id or self.properties)
43
 
 
44
 
 
45
 
def parse_roundtripping_metadata(text):
46
 
    """Parse Bazaar roundtripping metadata."""
47
 
    ret = BzrGitRevisionMetadata()
48
 
    f = StringIO(text)
49
 
    for l in f.readlines():
50
 
        (key, value) = l.split(":", 1)
51
 
        if key == "revision-id":
52
 
            ret.revision_id = value.strip()
53
 
        elif key == "parent-ids":
54
 
            ret.explicit_parent_ids = tuple(value.strip().split(" "))
55
 
        elif key == "testament3-sha1":
56
 
            ret.verifiers["testament3-sha1"] = value.strip()
57
 
        elif key.startswith("property-"):
58
 
            ret.properties[key[len("property-"):]] = value[1:].rstrip("\n")
59
 
        else:
60
 
            raise ValueError
61
 
    return ret
62
 
 
63
 
 
64
 
def generate_roundtripping_metadata(metadata, encoding):
65
 
    """Serialize the roundtripping metadata.
66
 
 
67
 
    :param metadata: A `BzrGitRevisionMetadata` instance
68
 
    :return: String with revision metadata
69
 
    """
70
 
    lines = []
71
 
    if metadata.revision_id:
72
 
        lines.append("revision-id: %s\n" % metadata.revision_id)
73
 
    if metadata.explicit_parent_ids:
74
 
        lines.append("parent-ids: %s\n" % " ".join(metadata.explicit_parent_ids))
75
 
    for key in sorted(metadata.properties.keys()):
76
 
        lines.append("property-%s: %s\n" % (key.encode(encoding), metadata.properties[key].encode(encoding)))
77
 
    if "testament3-sha1" in metadata.verifiers:
78
 
        lines.append("testament3-sha1: %s\n" %
79
 
                     metadata.verifiers["testament3-sha1"])
80
 
    return "".join(lines)
81
 
 
82
 
 
83
 
def extract_bzr_metadata(message):
84
 
    """Extract Bazaar metadata from a commit message.
85
 
 
86
 
    :param message: Commit message to extract from
87
 
    :return: Tuple with original commit message and metadata object
88
 
    """
89
 
    split = message.split("\n--BZR--\n", 1)
90
 
    if len(split) != 2:
91
 
        return message, None
92
 
    return split[0], parse_roundtripping_metadata(split[1])
93
 
 
94
 
 
95
 
def inject_bzr_metadata(message, metadata, encoding):
96
 
    if not metadata:
97
 
        return message
98
 
    rt_data = generate_roundtripping_metadata(metadata, encoding)
99
 
    if not rt_data:
100
 
        return message
101
 
    assert type(rt_data) == str
102
 
    return message + "\n--BZR--\n" + rt_data
103
 
 
104
 
 
105
 
def serialize_fileid_map(file_ids):
106
 
    """Serialize a file id map."""
107
 
    lines = []
108
 
    for path in sorted(file_ids.keys()):
109
 
        lines.append("%s\0%s\n" % (path, file_ids[path]))
110
 
    return lines
111
 
 
112
 
 
113
 
def deserialize_fileid_map(filetext):
114
 
    """Deserialize a file id map."""
115
 
    ret = {}
116
 
    f = StringIO(filetext)
117
 
    lines = f.readlines()
118
 
    for l in lines:
119
 
        (path, file_id) = l.rstrip("\n").split("\0")
120
 
        ret[path] = file_id
121
 
    return ret