/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.78.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
0.64.334 by Jelmer Vernooij
Remove old FSF address. Thanks Dan Callaghan.
14
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
0.78.5 by Ian Clatworthy
move import/export of marks into a module
15
16
"""Routines for reading/writing a marks file."""
17
6628.1.2 by Jelmer Vernooij
Fix imports, move exporter.py, drop explorer metadata.
18
from __future__ import absolute_import
0.78.5 by Ian Clatworthy
move import/export of marks into a module
19
6628.1.2 by Jelmer Vernooij
Fix imports, move exporter.py, drop explorer metadata.
20
from ...trace import warning
0.78.5 by Ian Clatworthy
move import/export of marks into a module
21
22
23
def import_marks(filename):
24
    """Read the mapping of marks to revision-ids from a file.
25
26
    :param filename: the file to read from
0.125.1 by Ian Clatworthy
Use the new marks file format (introduced in git 1.6 apparently)
27
    :return: None if an error is encountered or a dictionary with marks
28
        as keys and revision-ids as values
0.78.5 by Ian Clatworthy
move import/export of marks into a module
29
    """
30
    # Check that the file is readable and in the right format
31
    try:
7027.2.7 by Jelmer Vernooij
Review comments.
32
        f = open(filename, 'r')
0.78.5 by Ian Clatworthy
move import/export of marks into a module
33
    except IOError:
34
        warning("Could not import marks file %s - not importing marks",
35
            filename)
36
        return None
37
7027.2.7 by Jelmer Vernooij
Review comments.
38
    try:
39
        # Read the revision info
40
        revision_ids = {}
41
42
        line = f.readline()
43
        if line == 'format=1\n':
44
            # Cope with old-style marks files
45
            # Read the branch info
46
            branch_names = {}
47
            for string in f.readline().rstrip('\n').split('\0'):
48
                if not string:
49
                    continue
50
                name, integer = string.rsplit('.', 1)
51
                branch_names[name] = int(integer)
52
            line = f.readline()
53
54
        while line:
55
            line = line.rstrip('\n')
56
            mark, revid = line.split(' ', 1)
57
            mark = mark.lstrip(':')
58
            revision_ids[mark] = revid.encode('utf-8')
59
            line = f.readline()
60
    finally:
61
        f.close()
0.125.1 by Ian Clatworthy
Use the new marks file format (introduced in git 1.6 apparently)
62
    return revision_ids
63
64
65
def export_marks(filename, revision_ids):
0.78.5 by Ian Clatworthy
move import/export of marks into a module
66
    """Save marks to a file.
67
68
    :param filename: filename to save data to
69
    :param revision_ids: dictionary mapping marks -> bzr revision-ids
70
    """
71
    try:
7027.2.7 by Jelmer Vernooij
Review comments.
72
        f = open(filename, 'w')
0.78.5 by Ian Clatworthy
move import/export of marks into a module
73
    except IOError:
74
        warning("Could not open export-marks file %s - not exporting marks",
75
            filename)
76
        return
0.79.2 by Ian Clatworthy
extend & use marks_file API
77
7027.2.7 by Jelmer Vernooij
Review comments.
78
    try:
79
        # Write the revision info
80
        for mark in revision_ids:
81
            f.write(':%s %s\n' % (mark.lstrip(b':').decode('utf-8'),
82
                revision_ids[mark].decode('utf-8')))
83
    finally:
84
        f.close()