~zyga/bzr-fastimport/fixes

116.1.5 by Ian Clatworthy
move import/export of marks into a module
1
# Copyright (C) 2009 Canonical Ltd
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
334 by Jelmer Vernooij
Remove old FSF address. Thanks Dan Callaghan.
14
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
116.1.5 by Ian Clatworthy
move import/export of marks into a module
15
16
"""Routines for reading/writing a marks file."""
17
18
19
from bzrlib.trace import warning
20
21
22
def import_marks(filename):
23
    """Read the mapping of marks to revision-ids from a file.
24
25
    :param filename: the file to read from
222.1.1 by Ian Clatworthy
Use the new marks file format (introduced in git 1.6 apparently)
26
    :return: None if an error is encountered or a dictionary with marks
27
        as keys and revision-ids as values
116.1.5 by Ian Clatworthy
move import/export of marks into a module
28
    """
29
    # Check that the file is readable and in the right format
30
    try:
31
        f = file(filename)
32
    except IOError:
33
        warning("Could not import marks file %s - not importing marks",
34
            filename)
35
        return None
36
117.1.2 by Ian Clatworthy
extend & use marks_file API
37
    # Read the revision info
116.1.5 by Ian Clatworthy
move import/export of marks into a module
38
    revision_ids = {}
293.1.1 by Jelmer Vernooij
Merge in Ian's new mark file format branch, add support for still reading old style marks files.
39
40
    line = f.readline()
41
    if line == 'format=1\n':
42
        # Cope with old-style marks files
43
        # Read the branch info
44
        branch_names = {}
45
        for string in f.readline().rstrip('\n').split('\0'):
46
            if not string:
47
                continue
48
            name, integer = string.rsplit('.', 1)
49
            branch_names[name] = int(integer)
50
        line = f.readline()
51
52
    while line:
116.1.5 by Ian Clatworthy
move import/export of marks into a module
53
        line = line.rstrip('\n')
54
        mark, revid = line.split(' ', 1)
302.1.1 by termie
Add a bunch of mark id normalization.
55
        mark = mark.lstrip(':')
116.1.5 by Ian Clatworthy
move import/export of marks into a module
56
        revision_ids[mark] = revid
293.1.1 by Jelmer Vernooij
Merge in Ian's new mark file format branch, add support for still reading old style marks files.
57
        line = f.readline()
116.1.5 by Ian Clatworthy
move import/export of marks into a module
58
    f.close()
222.1.1 by Ian Clatworthy
Use the new marks file format (introduced in git 1.6 apparently)
59
    return revision_ids
60
61
62
def export_marks(filename, revision_ids):
116.1.5 by Ian Clatworthy
move import/export of marks into a module
63
    """Save marks to a file.
64
65
    :param filename: filename to save data to
66
    :param revision_ids: dictionary mapping marks -> bzr revision-ids
67
    """
68
    try:
69
        f = file(filename, 'w')
70
    except IOError:
71
        warning("Could not open export-marks file %s - not exporting marks",
72
            filename)
73
        return
117.1.2 by Ian Clatworthy
extend & use marks_file API
74
75
    # Write the revision info
116.1.5 by Ian Clatworthy
move import/export of marks into a module
76
    for mark, revid in revision_ids.iteritems():
302.1.1 by termie
Add a bunch of mark id normalization.
77
        f.write(':%s %s\n' % (str(mark).lstrip(':'), revid))
116.1.5 by Ian Clatworthy
move import/export of marks into a module
78
    f.close()