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, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 """Routines for reading/writing a marks file."""
21 from bzrlib
.trace
import warning
24 def import_marks(filename
):
25 """Read the mapping of marks to revision-ids from a file.
27 :param filename: the file to read from
28 :return: None if an error is encountered or (revision_ids, branch_names)
30 * revision_ids is a dictionary with marks as keys and revision-ids
32 * branch_names is a dictionary mapping branch names to some magic #
34 # Check that the file is readable and in the right format
38 warning("Could not import marks file %s - not importing marks",
41 firstline
= f
.readline()
42 match
= re
.match(r
'^format=(\d+)$', firstline
)
44 warning("%r doesn't look like a marks file - not importing marks",
47 elif match
.group(1) != '1':
48 warning('format version in marks file %s not supported - not importing'
52 # Read the branch info
54 for string
in f
.readline().rstrip('\n').split('\0'):
57 name
, integer
= string
.rsplit('.', 1)
58 branch_names
[name
] = int(integer
)
60 # Read the revision info
63 line
= line
.rstrip('\n')
64 mark
, revid
= line
.split(' ', 1)
65 revision_ids
[mark
] = revid
67 return (revision_ids
, branch_names
)
70 def export_marks(filename
, revision_ids
, branch_names
=None):
71 """Save marks to a file.
73 :param filename: filename to save data to
74 :param revision_ids: dictionary mapping marks -> bzr revision-ids
75 :param branch_names: dictionary mapping branch names to some magic #
78 f
= file(filename
, 'w')
80 warning("Could not open export-marks file %s - not exporting marks",
85 # Write the branch names line
87 branch_names
= [ '%s.%d' % x
for x
in branch_names
.iteritems() ]
88 f
.write('\0'.join(branch_names
) + '\n')
92 # Write the revision info
93 for mark
, revid
in revision_ids
.iteritems():
94 f
.write('%s %s\n' % (mark
, revid
))