1
# Copyright (C) 2006-2009, 2011 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
17
"""Whitebox tests for annotate functionality."""
20
from io import BytesIO
26
from ..sixish import (
29
from .ui_testing import StringIOWithEncoding
33
return [tuple(l.split(b' ', 1)) for l in text.splitlines(True)]
36
parent_1 = annotation(b"""\
45
parent_2 = annotation(b"""\
55
expected_2_1 = annotation(b"""\
64
# a: in both, same value, kept
66
# c: in both, same value, kept
67
# d: in both, same value, kept
68
# e: 1 and 2 disagree, so it goes to blahblah
69
# f: in 2, but not in new, so ignored
70
# g: not in 1 or 2, so it goes to blahblah
71
# h: only in parent 2, so 2 gets it
72
expected_1_2_2 = annotation(b"""\
91
expected_1 = annotation(b"""\
111
# For the 'duplicate' series, both sides introduce the same change, which then
112
# gets merged around. The last-modified should properly reflect this.
113
# We always change the fourth line so that the file is properly tracked as
114
# being modified in each revision. In reality, this probably would happen over
115
# many revisions, and it would be a different line that changes.
118
# A B # line should be annotated as new for A and B
120
# C D # line should 'converge' and say A
122
# E # D should supersede A and stay as D (not become E because C references
124
duplicate_base = annotation(b"""\
131
duplicate_A = annotation(b"""\
138
duplicate_B = annotation(b"""\
145
duplicate_C = annotation(b"""\
152
duplicate_D = annotation(b"""\
159
duplicate_E = annotation(b"""\
167
class TestAnnotate(tests.TestCaseWithTransport):
169
def create_merged_trees(self):
170
"""create 2 trees with merges between them.
180
builder = self.make_branch_builder('branch')
181
builder.start_series()
182
self.addCleanup(builder.finish_series)
183
builder.build_snapshot(None, [
184
('add', ('', b'root-id', 'directory', None)),
185
('add', ('a', b'a-id', 'file', b'first\n')),
186
], timestamp=1166046000.00, timezone=0, committer="joe@foo.com",
187
revision_id=b'rev-1')
188
builder.build_snapshot([b'rev-1'], [
189
('modify', ('a', b'first\nsecond\n')),
190
], timestamp=1166046001.00, timezone=0, committer="joe@foo.com",
191
revision_id=b'rev-2')
192
builder.build_snapshot([b'rev-1'], [
193
('modify', ('a', b'first\nthird\n')),
194
], timestamp=1166046002.00, timezone=0, committer="barry@foo.com",
195
revision_id=b'rev-1_1_1')
196
builder.build_snapshot([b'rev-2', b'rev-1_1_1'], [
197
('modify', ('a', b'first\nsecond\nthird\n')),
198
], timestamp=1166046003.00, timezone=0, committer="sal@foo.com",
199
revision_id=b'rev-3')
202
def create_deeply_merged_trees(self):
203
"""Create some trees with a more complex merge history.
211
rev-3 rev-1_1_2 rev-1_2_1 ------+
215
rev-4 rev-1_2_2 rev-1_3_1
217
+-----------------+ |
221
+--------------------------------+
225
builder = self.create_merged_trees()
226
builder.build_snapshot([b'rev-1_1_1'], [], revision_id=b'rev-1_1_2')
227
builder.build_snapshot([b'rev-3', b'rev-1_1_2'],
228
[], revision_id=b'rev-4')
229
builder.build_snapshot([b'rev-1_1_1'], [
230
('modify', ('a', b'first\nthird\nfourth\n')),
231
], timestamp=1166046003.00, timezone=0, committer="jerry@foo.com",
232
revision_id=b'rev-1_2_1')
233
builder.build_snapshot([b'rev-1_2_1'], [],
234
timestamp=1166046004.00, timezone=0, committer="jerry@foo.com",
235
revision_id=b'rev-1_2_2')
236
builder.build_snapshot([b'rev-4', b'rev-1_2_2'], [
237
('modify', ('a', b'first\nsecond\nthird\nfourth\n')),
238
], timestamp=1166046004.00, timezone=0, committer="jerry@foo.com",
239
revision_id=b'rev-5')
240
builder.build_snapshot([b'rev-1_2_1'], [
241
('modify', ('a', b'first\nthird\nfourth\nfifth\nsixth\n')),
242
], timestamp=1166046005.00, timezone=0, committer="george@foo.com",
243
revision_id=b'rev-1_3_1')
244
builder.build_snapshot([b'rev-5', b'rev-1_3_1'], [
246
b'first\nsecond\nthird\nfourth\nfifth\nsixth\n')),
247
], revision_id=b'rev-6')
250
def create_duplicate_lines_tree(self):
251
builder = self.make_branch_builder('branch')
252
builder.start_series()
253
self.addCleanup(builder.finish_series)
254
base_text = b''.join(l for r, l in duplicate_base)
255
a_text = b''.join(l for r, l in duplicate_A)
256
b_text = b''.join(l for r, l in duplicate_B)
257
c_text = b''.join(l for r, l in duplicate_C)
258
d_text = b''.join(l for r, l in duplicate_D)
259
e_text = b''.join(l for r, l in duplicate_E)
260
builder.build_snapshot(None, [
261
('add', ('', b'root-id', 'directory', None)),
262
('add', ('file', b'file-id', 'file', base_text)),
263
], revision_id=b'rev-base')
264
builder.build_snapshot([b'rev-base'], [
265
('modify', ('file', a_text))],
266
revision_id=b'rev-A')
267
builder.build_snapshot([b'rev-base'], [
268
('modify', ('file', b_text))],
269
revision_id=b'rev-B')
270
builder.build_snapshot([b'rev-A'], [
271
('modify', ('file', c_text))],
272
revision_id=b'rev-C')
273
builder.build_snapshot([b'rev-B', b'rev-A'], [
274
('modify', ('file', d_text))],
275
revision_id=b'rev-D')
276
builder.build_snapshot([b'rev-C', b'rev-D'], [
277
('modify', ('file', e_text))],
278
revision_id=b'rev-E')
281
def assertAnnotateEqualDiff(self, actual, expected):
282
if actual != expected:
283
# Create an easier to understand diff when the lines don't actually
285
self.assertEqualDiff(''.join('\t'.join(l) for l in expected),
286
''.join('\t'.join(l) for l in actual))
288
def assertBranchAnnotate(self, expected, branch, path, revision_id,
289
verbose=False, full=False, show_ids=False):
290
tree = branch.repository.revision_tree(revision_id)
292
annotate.annotate_file_tree(tree, path, to_file,
293
verbose=verbose, full=full, show_ids=show_ids, branch=branch)
294
self.assertAnnotateEqualDiff(to_file.getvalue(), expected)
296
def assertRepoAnnotate(self, expected, repo, path, revision_id):
297
"""Assert that the revision is properly annotated."""
298
actual = list(repo.revision_tree(revision_id).annotate_iter(path))
299
self.assertAnnotateEqualDiff(actual, expected)
301
def test_annotate_duplicate_lines(self):
302
# XXX: Should this be a per_repository test?
303
builder = self.create_duplicate_lines_tree()
304
repo = builder.get_branch().repository
306
self.addCleanup(repo.unlock)
307
self.assertRepoAnnotate(duplicate_base, repo, 'file', b'rev-base')
308
self.assertRepoAnnotate(duplicate_A, repo, 'file', b'rev-A')
309
self.assertRepoAnnotate(duplicate_B, repo, 'file', b'rev-B')
310
self.assertRepoAnnotate(duplicate_C, repo, 'file', b'rev-C')
311
self.assertRepoAnnotate(duplicate_D, repo, 'file', b'rev-D')
312
self.assertRepoAnnotate(duplicate_E, repo, 'file', b'rev-E')
314
def test_annotate_shows_dotted_revnos(self):
315
builder = self.create_merged_trees()
317
self.assertBranchAnnotate('1 joe@foo | first\n'
318
'2 joe@foo | second\n'
319
'1.1.1 barry@f | third\n',
320
builder.get_branch(), 'a', b'rev-3')
322
def test_annotate_limits_dotted_revnos(self):
323
"""Annotate should limit dotted revnos to a depth of 12"""
324
builder = self.create_deeply_merged_trees()
326
self.assertBranchAnnotate('1 joe@foo | first\n'
327
'2 joe@foo | second\n'
328
'1.1.1 barry@f | third\n'
329
'1.2.1 jerry@f | fourth\n'
330
'1.3.1 george@ | fifth\n'
332
builder.get_branch(), 'a', b'rev-6',
333
verbose=False, full=False)
335
self.assertBranchAnnotate('1 joe@foo | first\n'
336
'2 joe@foo | second\n'
337
'1.1.1 barry@f | third\n'
338
'1.2.1 jerry@f | fourth\n'
339
'1.3.1 george@ | fifth\n'
340
'1.3.1 george@ | sixth\n',
341
builder.get_branch(), 'a', b'rev-6',
342
verbose=False, full=True)
344
# verbose=True shows everything, the full revno, user id, and date
345
self.assertBranchAnnotate('1 joe@foo.com 20061213 | first\n'
346
'2 joe@foo.com 20061213 | second\n'
347
'1.1.1 barry@foo.com 20061213 | third\n'
348
'1.2.1 jerry@foo.com 20061213 | fourth\n'
349
'1.3.1 george@foo.com 20061213 | fifth\n'
351
builder.get_branch(), 'a', b'rev-6',
352
verbose=True, full=False)
354
self.assertBranchAnnotate('1 joe@foo.com 20061213 | first\n'
355
'2 joe@foo.com 20061213 | second\n'
356
'1.1.1 barry@foo.com 20061213 | third\n'
357
'1.2.1 jerry@foo.com 20061213 | fourth\n'
358
'1.3.1 george@foo.com 20061213 | fifth\n'
359
'1.3.1 george@foo.com 20061213 | sixth\n',
360
builder.get_branch(), 'a', b'rev-6',
361
verbose=True, full=True)
363
def test_annotate_uses_branch_context(self):
364
"""Dotted revnos should use the Branch context.
366
When annotating a non-mainline revision, the annotation should still
367
use dotted revnos from the mainline.
369
builder = self.create_deeply_merged_trees()
371
self.assertBranchAnnotate('1 joe@foo | first\n'
372
'1.1.1 barry@f | third\n'
373
'1.2.1 jerry@f | fourth\n'
374
'1.3.1 george@ | fifth\n'
376
builder.get_branch(), 'a', b'rev-1_3_1',
377
verbose=False, full=False)
379
def test_annotate_show_ids(self):
380
builder = self.create_deeply_merged_trees()
382
# It looks better with real revision ids :)
383
self.assertBranchAnnotate(' rev-1 | first\n'
385
'rev-1_1_1 | third\n'
386
'rev-1_2_1 | fourth\n'
387
'rev-1_3_1 | fifth\n'
389
builder.get_branch(), 'a', b'rev-6',
390
show_ids=True, full=False)
392
self.assertBranchAnnotate(' rev-1 | first\n'
394
'rev-1_1_1 | third\n'
395
'rev-1_2_1 | fourth\n'
396
'rev-1_3_1 | fifth\n'
397
'rev-1_3_1 | sixth\n',
398
builder.get_branch(), 'a', b'rev-6',
399
show_ids=True, full=True)
401
def test_annotate_unicode_author(self):
402
tree1 = self.make_branch_and_tree('tree1')
404
self.build_tree_contents([('tree1/a', b'adi\xc3\xb3s')])
405
tree1.add(['a'], [b'a-id'])
406
tree1.commit('a', rev_id=b'rev-1',
407
committer=u'Pepe P\xe9rez <pperez@ejemplo.com>',
408
timestamp=1166046000.00, timezone=0)
410
self.build_tree_contents([('tree1/b', b'bye')])
411
tree1.add(['b'], [b'b-id'])
412
tree1.commit('b', rev_id=b'rev-2',
413
committer=u'p\xe9rez',
414
timestamp=1166046000.00, timezone=0)
417
self.addCleanup(tree1.unlock)
419
revtree_1 = tree1.branch.repository.revision_tree(b'rev-1')
420
revtree_2 = tree1.branch.repository.revision_tree(b'rev-2')
422
# this passes if no exception is raised
424
annotate.annotate_file_tree(revtree_1, 'a',
425
to_file=to_file, branch=tree1.branch)
428
to_file = codecs.getwriter('ascii')(sio, 'replace')
429
annotate.annotate_file_tree(revtree_2, 'b',
430
to_file=to_file, branch=tree1.branch)
431
self.assertEqualDiff(b'2 p?rez | bye\n', sio.getvalue())
433
# test now with unicode file-like
434
to_file = StringIOWithEncoding()
435
annotate.annotate_file_tree(revtree_2, 'b',
436
to_file=to_file, branch=tree1.branch)
437
self.assertContainsRe(u'2 p\xe9rez | bye\n', to_file.getvalue())
439
def test_annotate_author_or_committer(self):
440
tree1 = self.make_branch_and_tree('tree1')
442
self.build_tree_contents([('tree1/a', b'hello')])
443
tree1.add(['a'], [b'a-id'])
444
tree1.commit('a', rev_id=b'rev-1',
445
committer='Committer <committer@example.com>',
446
timestamp=1166046000.00, timezone=0)
448
self.build_tree_contents([('tree1/b', b'bye')])
449
tree1.add(['b'], [b'b-id'])
450
tree1.commit('b', rev_id=b'rev-2',
451
committer='Committer <committer@example.com>',
452
authors=['Author <author@example.com>'],
453
timestamp=1166046000.00, timezone=0)
456
self.addCleanup(tree1.unlock)
458
self.assertBranchAnnotate('1 committ | hello\n', tree1.branch,
461
self.assertBranchAnnotate('2 author@ | bye\n', tree1.branch,
465
class TestReannotate(tests.TestCase):
467
def annotateEqual(self, expected, parents, newlines, revision_id,
469
annotate_list = list(annotate.reannotate(parents, newlines,
470
revision_id, blocks))
471
self.assertEqual(len(expected), len(annotate_list))
472
for e, a in zip(expected, annotate_list):
473
self.assertEqual(e, a)
475
def test_reannotate(self):
476
self.annotateEqual(parent_1, [parent_1], new_1, b'blahblah')
477
self.annotateEqual(expected_2_1, [parent_2], new_1, b'blahblah')
478
self.annotateEqual(expected_1_2_2, [parent_1, parent_2], new_2,
481
def test_reannotate_no_parents(self):
482
self.annotateEqual(expected_1, [], new_1, b'blahblah')
484
def test_reannotate_left_matching_blocks(self):
485
"""Ensure that left_matching_blocks has an impact.
487
In this case, the annotation is ambiguous, so the hint isn't actually
490
parent = [(b'rev1', b'a\n')]
491
new_text = [b'a\n', b'a\n']
492
blocks = [(0, 0, 1), (1, 2, 0)]
493
self.annotateEqual([(b'rev1', b'a\n'), (b'rev2', b'a\n')], [parent],
494
new_text, b'rev2', blocks)
495
blocks = [(0, 1, 1), (1, 2, 0)]
496
self.annotateEqual([(b'rev2', b'a\n'), (b'rev1', b'a\n')], [parent],
497
new_text, b'rev2', blocks)