/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.1 by Jelmer Vernooij
Port fastimport to python3.
32
        f = open(filename, 'rb')
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
0.79.2 by Ian Clatworthy
extend & use marks_file API
38
    # Read the revision info
0.78.5 by Ian Clatworthy
move import/export of marks into a module
39
    revision_ids = {}
0.126.1 by Jelmer Vernooij
Merge in Ian's new mark file format branch, add support for still reading old style marks files.
40
41
    line = f.readline()
7027.2.1 by Jelmer Vernooij
Port fastimport to python3.
42
    if line == b'format=1\n':
0.126.1 by Jelmer Vernooij
Merge in Ian's new mark file format branch, add support for still reading old style marks files.
43
        # Cope with old-style marks files
44
        # Read the branch info
45
        branch_names = {}
7027.2.1 by Jelmer Vernooij
Port fastimport to python3.
46
        for string in f.readline().rstrip(b'\n').split(b'\0'):
0.126.1 by Jelmer Vernooij
Merge in Ian's new mark file format branch, add support for still reading old style marks files.
47
            if not string:
48
                continue
7027.2.1 by Jelmer Vernooij
Port fastimport to python3.
49
            name, integer = string.rsplit(b'.', 1)
0.126.1 by Jelmer Vernooij
Merge in Ian's new mark file format branch, add support for still reading old style marks files.
50
            branch_names[name] = int(integer)
51
        line = f.readline()
52
53
    while line:
7027.2.1 by Jelmer Vernooij
Port fastimport to python3.
54
        line = line.rstrip(b'\n')
55
        mark, revid = line.split(b' ', 1)
56
        mark = mark.lstrip(b':')
0.78.5 by Ian Clatworthy
move import/export of marks into a module
57
        revision_ids[mark] = revid
0.126.1 by Jelmer Vernooij
Merge in Ian's new mark file format branch, add support for still reading old style marks files.
58
        line = f.readline()
0.78.5 by Ian Clatworthy
move import/export of marks into a module
59
    f.close()
0.125.1 by Ian Clatworthy
Use the new marks file format (introduced in git 1.6 apparently)
60
    return revision_ids
61
62
63
def export_marks(filename, revision_ids):
0.78.5 by Ian Clatworthy
move import/export of marks into a module
64
    """Save marks to a file.
65
66
    :param filename: filename to save data to
67
    :param revision_ids: dictionary mapping marks -> bzr revision-ids
68
    """
69
    try:
7027.2.1 by Jelmer Vernooij
Port fastimport to python3.
70
        f = open(filename, 'wb')
0.78.5 by Ian Clatworthy
move import/export of marks into a module
71
    except IOError:
72
        warning("Could not open export-marks file %s - not exporting marks",
73
            filename)
74
        return
0.79.2 by Ian Clatworthy
extend & use marks_file API
75
76
    # Write the revision info
6656.1.1 by Martin
Apply 2to3 dict fixer and clean up resulting mess using view helpers
77
    for mark in revision_ids:
7027.2.1 by Jelmer Vernooij
Port fastimport to python3.
78
        f.write(b':%s %s\n' % (mark.lstrip(b':'), revision_ids[mark]))
0.78.5 by Ian Clatworthy
move import/export of marks into a module
79
    f.close()