143
142
b2 = self.make_branch('b2')
144
143
b1.tags.merge_to(b2.tags)
145
def test_read_lock_caches_tags(self):
146
"""Tags are read from a branch only once during a read-lock."""
147
# Open the same branch twice. Read-lock one, and then mutate the tags
148
# in the second. The read-locked branch never re-reads the tags, so it
149
# never observes the changed/new tags.
150
b1 = self.make_branch('b')
151
b1.tags.set_tag('one', 'rev-1')
152
b2 = b1.bzrdir.open_branch()
154
self.assertEqual({'one': 'rev-1'}, b1.tags.get_tag_dict())
155
# Add a tag and modify a tag in b2. b1 is read-locked and has already
156
# read the tags, so it is unaffected.
157
b2.tags.set_tag('one', 'rev-1-changed')
158
b2.tags.set_tag('two', 'rev-2')
159
self.assertEqual({'one': 'rev-1'}, b1.tags.get_tag_dict())
161
# Once unlocked the cached value is forgotten, so now the latest tags
164
{'one': 'rev-1-changed', 'two': 'rev-2'}, b1.tags.get_tag_dict())
166
def test_unlocked_does_not_cache_tags(self):
167
"""Unlocked branches do not cache tags."""
168
# Open the same branch twice.
169
b1 = self.make_branch('b')
170
b1.tags.set_tag('one', 'rev-1')
171
b2 = b1.bzrdir.open_branch()
172
self.assertEqual({'one': 'rev-1'}, b1.tags.get_tag_dict())
173
# Add a tag and modify a tag in b2. b1 isn't locked, so it will
174
# immediately return the new tags too.
175
b2.tags.set_tag('one', 'rev-1-changed')
176
b2.tags.set_tag('two', 'rev-2')
178
{'one': 'rev-1-changed', 'two': 'rev-2'}, b1.tags.get_tag_dict())
180
def test_cached_tag_dict_not_accidentally_mutable(self):
181
"""When there's a cached version of the tags, b.tags.get_tag_dict
182
returns a copy of the cached data so that callers cannot accidentally
185
b = self.make_branch('b')
186
b.tags.set_tag('one', 'rev-1')
187
self.addCleanup(b.lock_read().unlock)
188
# The first time the data returned will not be in the cache
189
tags_dict = b.tags.get_tag_dict()
190
tags_dict['two'] = 'rev-2'
191
# The second time the data comes from the cache
192
tags_dict = b.tags.get_tag_dict()
193
tags_dict['three'] = 'rev-3'
194
# The get_tag_dict() result should still be unchanged, even though we
195
# mutated its earlier return values.
196
self.assertEqual({'one': 'rev-1'}, b.tags.get_tag_dict())
198
def make_write_locked_branch_with_one_tag(self):
199
b = self.make_branch('b')
200
b.tags.set_tag('one', 'rev-1')
201
self.addCleanup(b.lock_write().unlock)
203
b.tags.get_tag_dict()
206
def test_set_tag_invalides_cache(self):
207
b = self.make_write_locked_branch_with_one_tag()
208
b.tags.set_tag('one', 'rev-1-changed')
209
self.assertEqual({'one': 'rev-1-changed'}, b.tags.get_tag_dict())
211
def test_delete_tag_invalides_cache(self):
212
b = self.make_write_locked_branch_with_one_tag()
213
b.tags.delete_tag('one')
214
self.assertEqual({}, b.tags.get_tag_dict())
216
def test_merge_to_invalides_cache(self):
217
b1 = self.make_write_locked_branch_with_one_tag()
218
b2 = self.make_branch('b2')
219
b2.tags.set_tag('two', 'rev-2')
220
b2.tags.merge_to(b1.tags)
222
{'one': 'rev-1', 'two': 'rev-2'}, b1.tags.get_tag_dict())
224
def test_rename_revisions_invalides_cache(self):
225
b = self.make_write_locked_branch_with_one_tag()
226
b.tags.rename_revisions({'rev-1': 'rev-1-changed'})
227
self.assertEqual({'one': 'rev-1-changed'}, b.tags.get_tag_dict())
230
class TestTagsMergeToInCheckouts(per_branch.TestCaseWithBranch):
231
"""Tests for checkout.branch.tags.merge_to.
233
In particular this exercises variations in tag conflicts in the master
234
branch and/or the checkout (child). It may seem strange to have different
235
tags in the child and master, but 'bzr merge' intentionally updates the
236
child and not the master (instead the next 'bzr commit', if the user
237
decides to commit, will update the master). Also, merge_to in bzr < 2.3
238
didn't propagate changes to the master, and current bzr versions may find
239
themselves operating on checkouts touched by older bzrs
241
So we need to make sure bzr copes gracefully with differing tags in the
242
master versus the child.
244
See also <https://bugs.launchpad.net/bzr/+bug/603395>.
248
super(TestTagsMergeToInCheckouts, self).setUp()
249
branch1 = self.make_branch('tags-probe')
250
if not branch1._format.supports_tags():
251
raise tests.TestSkipped(
252
"format %s doesn't support tags" % branch1._format)
253
branch2 = self.make_branch('bind-probe')
255
branch2.bind(branch1)
256
except errors.UpgradeRequired:
257
raise tests.TestNotApplicable(
258
"format %s doesn't support bound branches" % branch2._format)
260
def test_merge_to_propagates_tags(self):
261
"""merge_to(child) also merges tags to the master."""
262
master = self.make_branch('master')
263
other = self.make_branch('other')
264
other.tags.set_tag('foo', 'rev-1')
265
child = self.make_branch('child')
268
other.tags.merge_to(child.tags)
269
self.assertEquals('rev-1', child.tags.lookup_tag('foo'))
270
self.assertEquals('rev-1', master.tags.lookup_tag('foo'))
272
def test_ignore_master_disables_tag_propagation(self):
273
"""merge_to(child, ignore_master=True) does not merge tags to the
276
master = self.make_branch('master')
277
other = self.make_branch('other')
278
other.tags.set_tag('foo', 'rev-1')
279
child = self.make_branch('child')
282
other.tags.merge_to(child.tags, ignore_master=True)
283
self.assertEquals('rev-1', child.tags.lookup_tag('foo'))
284
self.assertRaises(errors.NoSuchTag, master.tags.lookup_tag, 'foo')
286
def test_merge_to_overwrite_conflict_in_master(self):
287
"""merge_to(child, overwrite=True) overwrites any conflicting tags in
290
master = self.make_branch('master')
291
other = self.make_branch('other')
292
other.tags.set_tag('foo', 'rev-1')
293
child = self.make_branch('child')
296
master.tags.set_tag('foo', 'rev-2')
297
tag_conflicts = other.tags.merge_to(child.tags, overwrite=True)
298
self.assertEquals('rev-1', child.tags.lookup_tag('foo'))
299
self.assertEquals('rev-1', master.tags.lookup_tag('foo'))
300
self.assertLength(0, tag_conflicts)
302
def test_merge_to_overwrite_conflict_in_child_and_master(self):
303
"""merge_to(child, overwrite=True) overwrites any conflicting tags in
304
both the child and the master.
306
master = self.make_branch('master')
307
master.tags.set_tag('foo', 'rev-2')
308
other = self.make_branch('other')
309
other.tags.set_tag('foo', 'rev-1')
310
child = self.make_branch('child')
313
tag_conflicts = other.tags.merge_to(child.tags, overwrite=True)
314
self.assertEquals('rev-1', child.tags.lookup_tag('foo'))
315
self.assertEquals('rev-1', master.tags.lookup_tag('foo'))
316
self.assertLength(0, tag_conflicts)
318
def test_merge_to_conflict_in_child_only(self):
319
"""When new_tags.merge_to(child.tags) conflicts with the child but not
320
the master, a conflict is reported and the child receives the new tag.
322
master = self.make_branch('master')
323
master.tags.set_tag('foo', 'rev-2')
324
other = self.make_branch('other')
325
other.tags.set_tag('foo', 'rev-1')
326
child = self.make_branch('child')
329
master.tags.delete_tag('foo')
330
tag_conflicts = other.tags.merge_to(child.tags)
331
# Conflict in child, so it is unchanged.
332
self.assertEquals('rev-2', child.tags.lookup_tag('foo'))
333
# No conflict in the master, so the 'foo' tag equals other's value here.
334
self.assertEquals('rev-1', master.tags.lookup_tag('foo'))
335
# The conflict is reported.
336
self.assertEquals([(u'foo', 'rev-1', 'rev-2')], list(tag_conflicts))
338
def test_merge_to_conflict_in_master_only(self):
339
"""When new_tags.merge_to(child.tags) conflicts with the master but not
340
the child, a conflict is reported and the child receives the new tag.
342
master = self.make_branch('master')
343
other = self.make_branch('other')
344
other.tags.set_tag('foo', 'rev-1')
345
child = self.make_branch('child')
348
master.tags.set_tag('foo', 'rev-2')
349
tag_conflicts = other.tags.merge_to(child.tags)
350
# No conflict in the child, so the 'foo' tag equals other's value here.
351
self.assertEquals('rev-1', child.tags.lookup_tag('foo'))
352
# Conflict in master, so it is unchanged.
353
self.assertEquals('rev-2', master.tags.lookup_tag('foo'))
354
# The conflict is reported.
355
self.assertEquals([(u'foo', 'rev-1', 'rev-2')], list(tag_conflicts))
357
def test_merge_to_same_conflict_in_master_and_child(self):
358
"""When new_tags.merge_to(child.tags) conflicts the same way with the
359
master and the child a single conflict is reported.
361
master = self.make_branch('master')
362
master.tags.set_tag('foo', 'rev-2')
363
other = self.make_branch('other')
364
other.tags.set_tag('foo', 'rev-1')
365
child = self.make_branch('child')
368
tag_conflicts = other.tags.merge_to(child.tags)
369
# Both master and child conflict, so both stay as rev-2
370
self.assertEquals('rev-2', child.tags.lookup_tag('foo'))
371
self.assertEquals('rev-2', master.tags.lookup_tag('foo'))
372
# The conflict is reported exactly once, even though it occurs in both
374
self.assertEquals([(u'foo', 'rev-1', 'rev-2')], list(tag_conflicts))
376
def test_merge_to_different_conflict_in_master_and_child(self):
377
"""When new_tags.merge_to(child.tags) conflicts differently in the
378
master and the child both conflicts are reported.
380
master = self.make_branch('master')
381
master.tags.set_tag('foo', 'rev-2')
382
other = self.make_branch('other')
383
other.tags.set_tag('foo', 'rev-1')
384
child = self.make_branch('child')
387
# We use the private method _set_tag_dict because normally bzr tries to
388
# avoid this scenario.
389
child.tags._set_tag_dict({'foo': 'rev-3'})
390
tag_conflicts = other.tags.merge_to(child.tags)
391
# Both master and child conflict, so both stay as they were.
392
self.assertEquals('rev-3', child.tags.lookup_tag('foo'))
393
self.assertEquals('rev-2', master.tags.lookup_tag('foo'))
394
# Both conflicts are reported.
396
[(u'foo', 'rev-1', 'rev-2'), (u'foo', 'rev-1', 'rev-3')],
397
sorted(tag_conflicts))
147
400
class TestUnsupportedTags(per_branch.TestCaseWithBranch):
148
401
"""Formats that don't support tags should give reasonable errors."""