1 # Copyright (C) 2009 Canonical Ltd
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.
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.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 """Routines for reading/writing a marks file."""
19 from bzrlib
.trace
import warning
22 def import_marks(filename
):
23 """Read the mapping of marks to revision-ids from a file.
25 :param filename: the file to read from
26 :return: None if an error is encountered or a dictionary with marks
27 as keys and revision-ids as values
29 # Check that the file is readable and in the right format
33 warning("Could not import marks file %s - not importing marks",
37 # Read the revision info
41 if line
== 'format=1\n':
42 # Cope with old-style marks files
43 # Read the branch info
45 for string
in f
.readline().rstrip('\n').split('\0'):
48 name
, integer
= string
.rsplit('.', 1)
49 branch_names
[name
] = int(integer
)
53 line
= line
.rstrip('\n')
54 mark
, revid
= line
.split(' ', 1)
55 mark
= mark
.lstrip(':')
56 revision_ids
[mark
] = revid
62 def export_marks(filename
, revision_ids
):
63 """Save marks to a file.
65 :param filename: filename to save data to
66 :param revision_ids: dictionary mapping marks -> bzr revision-ids
69 f
= file(filename
, 'w')
71 warning("Could not open export-marks file %s - not exporting marks",
75 # Write the revision info
76 for mark
, revid
in revision_ids
.iteritems():
77 f
.write(':%s %s\n' % (str(mark
).lstrip(':'), revid
))