1
# Copyright (C) 2007 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
These are contained within a branch and normally constructed
20
when the branch is opened. Clients should typically do
22
Branch.tags.add('name', 'value')
25
# NOTE: I was going to call this tags.py, but vim seems to think all files
26
# called tags* are ctags files... mbp 20070220.
29
from warnings import warn
40
def __init__(self, branch):
43
def has_tag(self, tag_name):
44
return self.get_tag_dict().has_key(tag_name)
47
class DisabledTags(_Tags):
48
"""Tag storage that refuses to store anything.
50
This is used by older formats that can't store tags.
53
def _not_supported(self, *a, **k):
54
raise errors.TagsNotSupported(self.branch)
56
set_tag = _not_supported
57
get_tag_dict = _not_supported
58
_set_tag_dict = _not_supported
59
lookup_tag = _not_supported
60
delete_tag = _not_supported
62
def merge_to(self, to_tags, overwrite=False):
63
# we never have anything to copy
66
def rename_revisions(self, rename_map):
67
# No tags, so nothing to rename
70
def get_reverse_tag_dict(self):
71
# There aren't any tags, so the reverse mapping is empty.
75
class BasicTags(_Tags):
76
"""Tag storage in an unversioned branch control file.
79
def set_tag(self, tag_name, tag_target):
80
"""Add a tag definition to the branch.
82
Behaviour if the tag is already present is not defined (yet).
84
# all done with a write lock held, so this looks atomic
85
self.branch.lock_write()
87
master = self.branch.get_master_branch()
88
if master is not None:
89
master.tags.set_tag(tag_name, tag_target)
90
td = self.get_tag_dict()
91
td[tag_name] = tag_target
92
self._set_tag_dict(td)
96
def lookup_tag(self, tag_name):
97
"""Return the referent string of a tag"""
98
td = self.get_tag_dict()
102
raise errors.NoSuchTag(tag_name)
104
def get_tag_dict(self):
105
self.branch.lock_read()
108
tag_content = self.branch._get_tags_bytes()
109
except errors.NoSuchFile, e:
110
# ugly, but only abentley should see this :)
111
trace.warning('No branch/tags file in %s. '
112
'This branch was probably created by bzr 0.15pre. '
113
'Create an empty file to silence this message.'
116
return self._deserialize_tag_dict(tag_content)
120
def get_reverse_tag_dict(self):
121
"""Returns a dict with revisions as keys
122
and a list of tags for that revision as value"""
123
d = self.get_tag_dict()
127
rev[d[key]].append(key)
132
def delete_tag(self, tag_name):
133
"""Delete a tag definition.
135
self.branch.lock_write()
137
d = self.get_tag_dict()
141
raise errors.NoSuchTag(tag_name)
142
master = self.branch.get_master_branch()
143
if master is not None:
145
master.tags.delete_tag(tag_name)
146
except errors.NoSuchTag:
148
self._set_tag_dict(d)
152
def _set_tag_dict(self, new_dict):
153
"""Replace all tag definitions
155
WARNING: Calling this on an unlocked branch will lock it, and will
156
replace the tags without warning on conflicts.
158
:param new_dict: Dictionary from tag name to target.
160
return self.branch._set_tags_bytes(self._serialize_tag_dict(new_dict))
162
def _serialize_tag_dict(self, tag_dict):
163
td = dict((k.encode('utf-8'), v)
164
for k,v in tag_dict.items())
165
return bencode.bencode(td)
167
def _deserialize_tag_dict(self, tag_content):
168
"""Convert the tag file into a dictionary of tags"""
169
# was a special case to make initialization easy, an empty definition
170
# is an empty dictionary
171
if tag_content == '':
175
for k, v in bencode.bdecode(tag_content).items():
176
r[k.decode('utf-8')] = v
178
except ValueError, e:
179
raise ValueError("failed to deserialize tag dictionary %r: %s"
182
def merge_to(self, to_tags, overwrite=False):
183
"""Copy tags between repositories if necessary and possible.
185
This method has common command-line behaviour about handling
188
All new definitions are copied across, except that tags that already
189
exist keep their existing definitions.
191
:param to_tags: Branch to receive these tags
192
:param overwrite: Overwrite conflicting tags in the target branch
194
:returns: A list of tags that conflicted, each of which is
195
(tagname, source_target, dest_target), or None if no copying was
198
if self.branch == to_tags.branch:
200
if not self.branch.supports_tags():
201
# obviously nothing to copy
203
source_dict = self.get_tag_dict()
205
# no tags in the source, and we don't want to clobber anything
206
# that's in the destination
208
to_tags.branch.lock_write()
210
dest_dict = to_tags.get_tag_dict()
211
result, conflicts = self._reconcile_tags(source_dict, dest_dict,
213
if result != dest_dict:
214
to_tags._set_tag_dict(result)
216
to_tags.branch.unlock()
219
def rename_revisions(self, rename_map):
220
"""Rename revisions in this tags dictionary.
222
:param rename_map: Dictionary mapping old revids to new revids
224
reverse_tags = self.get_reverse_tag_dict()
225
for revid, names in reverse_tags.iteritems():
226
if revid in rename_map:
228
self.set_tag(name, rename_map[revid])
230
def _reconcile_tags(self, source_dict, dest_dict, overwrite):
231
"""Do a two-way merge of two tag dictionaries.
233
only in source => source value
234
only in destination => destination value
235
same definitions => that
236
different definitions => if overwrite is False, keep destination
237
value and give a warning, otherwise use the source value
239
:returns: (result_dict,
240
[(conflicting_tag, source_target, dest_target)])
243
result = dict(dest_dict) # copy
244
for name, target in source_dict.items():
245
if name not in result or overwrite:
246
result[name] = target
247
elif result[name] == target:
250
conflicts.append((name, target, result[name]))
251
return result, conflicts
254
def _merge_tags_if_possible(from_branch, to_branch):
255
from_branch.tags.merge_to(to_branch.tags)