107
98
""".splitlines(True)
110
# For the 'duplicate' series, both sides introduce the same change, which then
111
# gets merged around. The last-modified should properly reflect this.
112
# We always change the fourth line so that the file is properly tracked as
113
# being modified in each revision. In reality, this probably would happen over
114
# many revisions, and it would be a different line that changes.
117
# A B # line should be annotated as new for A and B
119
# C D # line should 'converge' and say D
121
# E # D should supersede A and stay as D (not become E because C references
123
duplicate_base = annotation("""\
130
duplicate_A = annotation("""\
137
duplicate_B = annotation("""\
144
duplicate_C = annotation("""\
151
duplicate_D = annotation("""\
158
duplicate_E = annotation("""\
166
101
class TestAnnotate(tests.TestCaseWithTransport):
168
103
def create_merged_trees(self):
169
"""create 2 trees with merges between them.
180
104
tree1 = self.make_branch_and_tree('tree1')
181
105
self.build_tree_contents([('tree1/a', 'first\n')])
182
106
tree1.add(['a'], ['a-id'])
205
129
tree1.commit('merge 2', rev_id='rev-3',
206
130
committer='sal@foo.com',
207
131
timestamp=1166046003.00, timezone=0)
209
self.addCleanup(tree1.unlock)
210
132
return tree1, tree2
212
134
def create_deeply_merged_trees(self):
213
"""Create some trees with a more complex merge history.
221
rev-3 rev-1_1_2 rev-1_2_1 ------+
225
rev-4 rev-1_2_2 rev-1_3_1
227
+-----------------+ |
231
+--------------------------------+
235
135
tree1, tree2 = self.create_merged_trees()
238
137
tree3 = tree2.bzrdir.clone('tree3').open_workingtree()
240
tree2.commit('noop', rev_id='rev-1_1_2')
139
tree2.commit('noop', rev_id='rev-1.1.2')
241
140
self.assertEqual(0, tree1.merge_from_branch(tree2.branch))
242
141
tree1.commit('noop merge', rev_id='rev-4')
244
143
self.build_tree_contents([('tree3/a', 'first\nthird\nfourth\n')])
245
tree3.commit('four', rev_id='rev-1_2_1',
144
tree3.commit('four', rev_id='rev-1_1_1_1_1',
246
145
committer='jerry@foo.com',
247
146
timestamp=1166046003.00, timezone=0)
249
148
tree4 = tree3.bzrdir.clone('tree4').open_workingtree()
251
tree3.commit('noop', rev_id='rev-1_2_2',
150
tree3.commit('noop', rev_id='rev-1.1.1.1.2',
252
151
committer='jerry@foo.com',
253
152
timestamp=1166046004.00, timezone=0)
254
153
self.assertEqual(0, tree1.merge_from_branch(tree3.branch))
257
156
self.build_tree_contents([('tree4/a',
258
157
'first\nthird\nfourth\nfifth\nsixth\n')])
259
tree4.commit('five and six', rev_id='rev-1_3_1',
158
tree4.commit('five and six', rev_id='rev-1_1_1_1_1_1_1',
260
159
committer='george@foo.com',
261
160
timestamp=1166046005.00, timezone=0)
262
161
self.assertEqual(0, tree1.merge_from_branch(tree4.branch))
263
162
tree1.commit('merge five and six', rev_id='rev-6')
267
def create_duplicate_lines_tree(self):
268
tree1 = self.make_branch_and_tree('tree1')
269
base_text = ''.join(l for r, l in duplicate_base)
270
a_text = ''.join(l for r, l in duplicate_A)
271
b_text = ''.join(l for r, l in duplicate_B)
272
c_text = ''.join(l for r, l in duplicate_C)
273
d_text = ''.join(l for r, l in duplicate_D)
274
e_text = ''.join(l for r, l in duplicate_E)
275
self.build_tree_contents([('tree1/file', base_text)])
276
tree1.add(['file'], ['file-id'])
277
tree1.commit('base', rev_id='rev-base')
278
tree2 = tree1.bzrdir.clone('tree2').open_workingtree()
280
self.build_tree_contents([('tree1/file', a_text),
281
('tree2/file', b_text)])
282
tree1.commit('A', rev_id='rev-A')
283
tree2.commit('B', rev_id='rev-B')
285
tree2.merge_from_branch(tree1.branch)
286
conflicts.resolve(tree2, None) # Resolve the conflicts
287
self.build_tree_contents([('tree2/file', d_text)])
288
tree2.commit('D', rev_id='rev-D')
290
self.build_tree_contents([('tree1/file', c_text)])
291
tree1.commit('C', rev_id='rev-C')
293
tree1.merge_from_branch(tree2.branch)
294
conflicts.resolve(tree1, None) # Resolve the conflicts
295
self.build_tree_contents([('tree1/file', e_text)])
296
tree1.commit('E', rev_id='rev-E')
299
def assertRepoAnnotate(self, expected, repo, file_id, revision_id):
300
"""Assert that the revision is properly annotated."""
301
actual = list(repo.revision_tree(revision_id).annotate_iter(file_id))
302
if actual != expected:
303
# Create an easier to understand diff when the lines don't actually
305
self.assertEqualDiff(''.join('\t'.join(l) for l in expected),
306
''.join('\t'.join(l) for l in actual))
308
def test_annotate_duplicate_lines(self):
309
# XXX: Should this be a repository_implementations test?
310
tree1 = self.create_duplicate_lines_tree()
311
repo = tree1.branch.repository
313
self.addCleanup(repo.unlock)
314
self.assertRepoAnnotate(duplicate_base, repo, 'file-id', 'rev-base')
315
self.assertRepoAnnotate(duplicate_A, repo, 'file-id', 'rev-A')
316
self.assertRepoAnnotate(duplicate_B, repo, 'file-id', 'rev-B')
317
self.assertRepoAnnotate(duplicate_C, repo, 'file-id', 'rev-C')
318
self.assertRepoAnnotate(duplicate_D, repo, 'file-id', 'rev-D')
319
self.assertRepoAnnotate(duplicate_E, repo, 'file-id', 'rev-E')
321
165
def test_annotate_shows_dotted_revnos(self):
322
166
tree1, tree2 = self.create_merged_trees()
337
181
annotate.annotate_file(tree1.branch, 'rev-6', 'a-id',
338
182
to_file=sio, verbose=False, full=False)
339
self.assertEqualDiff('1 joe@foo | first\n'
340
'2 joe@foo | second\n'
341
'1.1.1 barry@f | third\n'
342
'1.2.1 jerry@f | fourth\n'
343
'1.3.1 george@ | fifth\n'
183
self.assertEqualDiff('1 joe@foo | first\n'
184
'2 joe@foo | second\n'
185
'1.1.1 barry@f | third\n'
186
'1.1.1.1.1 jerry@f | fourth\n'
187
'1.1.1.1.1.1> george@ | fifth\n'
348
192
annotate.annotate_file(tree1.branch, 'rev-6', 'a-id',
349
193
to_file=sio, verbose=False, full=True)
350
self.assertEqualDiff('1 joe@foo | first\n'
351
'2 joe@foo | second\n'
352
'1.1.1 barry@f | third\n'
353
'1.2.1 jerry@f | fourth\n'
354
'1.3.1 george@ | fifth\n'
355
'1.3.1 george@ | sixth\n',
194
self.assertEqualDiff('1 joe@foo | first\n'
195
'2 joe@foo | second\n'
196
'1.1.1 barry@f | third\n'
197
'1.1.1.1.1 jerry@f | fourth\n'
198
'1.1.1.1.1.1> george@ | fifth\n'
199
'1.1.1.1.1.1> george@ | sixth\n',
358
202
# verbose=True shows everything, the full revno, user id, and date
360
204
annotate.annotate_file(tree1.branch, 'rev-6', 'a-id',
361
205
to_file=sio, verbose=True, full=False)
362
self.assertEqualDiff('1 joe@foo.com 20061213 | first\n'
363
'2 joe@foo.com 20061213 | second\n'
364
'1.1.1 barry@foo.com 20061213 | third\n'
365
'1.2.1 jerry@foo.com 20061213 | fourth\n'
366
'1.3.1 george@foo.com 20061213 | fifth\n'
206
self.assertEqualDiff('1 joe@foo.com 20061213 | first\n'
207
'2 joe@foo.com 20061213 | second\n'
208
'1.1.1 barry@foo.com 20061213 | third\n'
209
'1.1.1.1.1 jerry@foo.com 20061213 | fourth\n'
210
'1.1.1.1.1.1.1 george@foo.com 20061213 | fifth\n'
371
215
annotate.annotate_file(tree1.branch, 'rev-6', 'a-id',
372
216
to_file=sio, verbose=True, full=True)
373
self.assertEqualDiff('1 joe@foo.com 20061213 | first\n'
374
'2 joe@foo.com 20061213 | second\n'
375
'1.1.1 barry@foo.com 20061213 | third\n'
376
'1.2.1 jerry@foo.com 20061213 | fourth\n'
377
'1.3.1 george@foo.com 20061213 | fifth\n'
378
'1.3.1 george@foo.com 20061213 | sixth\n',
381
def test_annotate_uses_branch_context(self):
382
"""Dotted revnos should use the Branch context.
384
When annotating a non-mainline revision, the annotation should still
385
use dotted revnos from the mainline.
387
tree1 = self.create_deeply_merged_trees()
390
annotate.annotate_file(tree1.branch, 'rev-1_3_1', 'a-id',
391
to_file=sio, verbose=False, full=False)
392
self.assertEqualDiff('1 joe@foo | first\n'
393
'1.1.1 barry@f | third\n'
394
'1.2.1 jerry@f | fourth\n'
395
'1.3.1 george@ | fifth\n'
217
self.assertEqualDiff('1 joe@foo.com 20061213 | first\n'
218
'2 joe@foo.com 20061213 | second\n'
219
'1.1.1 barry@foo.com 20061213 | third\n'
220
'1.1.1.1.1 jerry@foo.com 20061213 | fourth\n'
221
'1.1.1.1.1.1.1 george@foo.com 20061213 | fifth\n'
222
'1.1.1.1.1.1.1 george@foo.com 20061213 | sixth\n',
399
225
def test_annotate_show_ids(self):
400
226
tree1 = self.create_deeply_merged_trees()
404
230
to_file=sio, show_ids=True, full=False)
406
232
# It looks better with real revision ids :)
407
self.assertEqualDiff(' rev-1 | first\n'
409
'rev-1_1_1 | third\n'
410
'rev-1_2_1 | fourth\n'
411
'rev-1_3_1 | fifth\n'
233
self.assertEqualDiff(' rev-1 | first\n'
235
' rev-1_1_1 | third\n'
236
' rev-1_1_1_1_1 | fourth\n'
237
'rev-1_1_1_1_1_1_1 | fifth\n'
416
242
annotate.annotate_file(tree1.branch, 'rev-6', 'a-id',
417
243
to_file=sio, show_ids=True, full=True)
419
self.assertEqualDiff(' rev-1 | first\n'
421
'rev-1_1_1 | third\n'
422
'rev-1_2_1 | fourth\n'
423
'rev-1_3_1 | fifth\n'
424
'rev-1_3_1 | sixth\n',
245
self.assertEqualDiff(' rev-1 | first\n'
247
' rev-1_1_1 | third\n'
248
' rev-1_1_1_1_1 | fourth\n'
249
'rev-1_1_1_1_1_1_1 | fifth\n'
250
'rev-1_1_1_1_1_1_1 | sixth\n',
427
def test_annotate_unicode_author(self):
428
tree1 = self.make_branch_and_tree('tree1')
430
self.build_tree_contents([('tree1/a', 'adi\xc3\xb3s')])
431
tree1.add(['a'], ['a-id'])
432
tree1.commit('a', rev_id='rev-1',
433
committer=u'Pepe P\xe9rez <pperez@ejemplo.com>',
434
timestamp=1166046000.00, timezone=0)
436
self.build_tree_contents([('tree1/b', 'bye')])
437
tree1.add(['b'], ['b-id'])
438
tree1.commit('b', rev_id='rev-2',
439
committer=u'p\xe9rez',
440
timestamp=1166046000.00, timezone=0)
443
self.addCleanup(tree1.unlock)
444
# this passes if no exception is raised
446
annotate.annotate_file(tree1.branch, 'rev-1', 'a-id', to_file=to_file)
449
to_file = codecs.getwriter('ascii')(sio)
450
to_file.encoding = 'ascii' # codecs does not set it
451
annotate.annotate_file(tree1.branch, 'rev-2', 'b-id', to_file=to_file)
452
self.assertEqualDiff('2 p?rez | bye\n', sio.getvalue())
454
# test now with to_file.encoding = None
455
to_file = tests.StringIOWrapper()
456
to_file.encoding = None
457
annotate.annotate_file(tree1.branch, 'rev-2', 'b-id', to_file=to_file)
458
self.assertContainsRe('2 p.rez | bye\n', to_file.getvalue())
460
# and when it does not exist
462
annotate.annotate_file(tree1.branch, 'rev-2', 'b-id', to_file=to_file)
463
self.assertContainsRe('2 p.rez | bye\n', to_file.getvalue())
465
def test_annotate_author_or_committer(self):
466
tree1 = self.make_branch_and_tree('tree1')
468
self.build_tree_contents([('tree1/a', 'hello')])
469
tree1.add(['a'], ['a-id'])
470
tree1.commit('a', rev_id='rev-1',
471
committer='Committer <committer@example.com>',
472
timestamp=1166046000.00, timezone=0)
474
self.build_tree_contents([('tree1/b', 'bye')])
475
tree1.add(['b'], ['b-id'])
476
tree1.commit('b', rev_id='rev-2',
477
committer='Committer <committer@example.com>',
478
author='Author <author@example.com>',
479
timestamp=1166046000.00, timezone=0)
482
self.addCleanup(tree1.unlock)
484
annotate.annotate_file(tree1.branch, 'rev-1', 'a-id', to_file=to_file)
485
self.assertEqual('1 committ | hello\n', to_file.getvalue())
488
annotate.annotate_file(tree1.branch, 'rev-2', 'b-id', to_file=to_file)
489
self.assertEqual('2 author@ | bye\n', to_file.getvalue())
492
254
class TestReannotate(tests.TestCase):
494
def annotateEqual(self, expected, parents, newlines, revision_id,
256
def annotateEqual(self, expected, parents, newlines, revision_id):
496
257
annotate_list = list(annotate.reannotate(parents, newlines,
497
revision_id, blocks))
498
259
self.assertEqual(len(expected), len(annotate_list))
499
260
for e, a in zip(expected, annotate_list):
500
261
self.assertEqual(e, a)
504
265
self.annotateEqual(expected_2_1, [parent_2], new_1, 'blahblah')
505
266
self.annotateEqual(expected_1_2_2, [parent_1, parent_2], new_2,
508
def test_reannotate_no_parents(self):
509
self.annotateEqual(expected_1, [], new_1, 'blahblah')
511
def test_reannotate_left_matching_blocks(self):
512
"""Ensure that left_matching_blocks has an impact.
514
In this case, the annotation is ambiguous, so the hint isn't actually
517
parent = [('rev1', 'a\n')]
518
new_text = ['a\n', 'a\n']
519
blocks = [(0, 0, 1), (1, 2, 0)]
520
self.annotateEqual([('rev1', 'a\n'), ('rev2', 'a\n')], [parent],
521
new_text, 'rev2', blocks)
522
blocks = [(0, 1, 1), (1, 2, 0)]
523
self.annotateEqual([('rev2', 'a\n'), ('rev1', 'a\n')], [parent],
524
new_text, 'rev2', blocks)