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."""
25
from ..sixish import (
28
from .ui_testing import StringIOWithEncoding
32
return [tuple(l.split(b' ', 1)) for l in text.splitlines(True)]
35
parent_1 = annotation(b"""\
44
parent_2 = annotation(b"""\
54
expected_2_1 = annotation(b"""\
63
# a: in both, same value, kept
65
# c: in both, same value, kept
66
# d: in both, same value, kept
67
# e: 1 and 2 disagree, so it goes to blahblah
68
# f: in 2, but not in new, so ignored
69
# g: not in 1 or 2, so it goes to blahblah
70
# h: only in parent 2, so 2 gets it
71
expected_1_2_2 = annotation(b"""\
90
expected_1 = annotation(b"""\
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 A
121
# E # D should supersede A and stay as D (not become E because C references
123
duplicate_base = annotation(b"""\
130
duplicate_A = annotation(b"""\
137
duplicate_B = annotation(b"""\
144
duplicate_C = annotation(b"""\
151
duplicate_D = annotation(b"""\
158
duplicate_E = annotation(b"""\
166
class TestAnnotate(tests.TestCaseWithTransport):
168
def create_merged_trees(self):
169
"""create 2 trees with merges between them.
179
builder = self.make_branch_builder('branch')
180
builder.start_series()
181
self.addCleanup(builder.finish_series)
182
builder.build_snapshot(None, [
183
('add', ('', b'root-id', 'directory', None)),
184
('add', ('a', b'a-id', 'file', b'first\n')),
185
], timestamp=1166046000.00, timezone=0, committer="joe@foo.com",
186
revision_id=b'rev-1')
187
builder.build_snapshot([b'rev-1'], [
188
('modify', ('a', b'first\nsecond\n')),
189
], timestamp=1166046001.00, timezone=0, committer="joe@foo.com",
190
revision_id=b'rev-2')
191
builder.build_snapshot([b'rev-1'], [
192
('modify', ('a', b'first\nthird\n')),
193
], timestamp=1166046002.00, timezone=0, committer="barry@foo.com",
194
revision_id=b'rev-1_1_1')
195
builder.build_snapshot([b'rev-2', b'rev-1_1_1'], [
196
('modify', ('a', b'first\nsecond\nthird\n')),
197
], timestamp=1166046003.00, timezone=0, committer="sal@foo.com",
198
revision_id=b'rev-3')
201
def create_deeply_merged_trees(self):
202
"""Create some trees with a more complex merge history.
210
rev-3 rev-1_1_2 rev-1_2_1 ------+
214
rev-4 rev-1_2_2 rev-1_3_1
216
+-----------------+ |
220
+--------------------------------+
224
builder = self.create_merged_trees()
225
builder.build_snapshot([b'rev-1_1_1'], [], revision_id=b'rev-1_1_2')
226
builder.build_snapshot([b'rev-3', b'rev-1_1_2'], [], revision_id=b'rev-4')
227
builder.build_snapshot([b'rev-1_1_1'], [
228
('modify', ('a', b'first\nthird\nfourth\n')),
229
], timestamp=1166046003.00, timezone=0, committer="jerry@foo.com",
230
revision_id=b'rev-1_2_1')
231
builder.build_snapshot([b'rev-1_2_1'], [],
232
timestamp=1166046004.00, timezone=0, committer="jerry@foo.com",
233
revision_id=b'rev-1_2_2')
234
builder.build_snapshot([b'rev-4', b'rev-1_2_2'], [
235
('modify', ('a', b'first\nsecond\nthird\nfourth\n')),
236
], timestamp=1166046004.00, timezone=0, committer="jerry@foo.com",
237
revision_id=b'rev-5')
238
builder.build_snapshot([b'rev-1_2_1'], [
239
('modify', ('a', b'first\nthird\nfourth\nfifth\nsixth\n')),
240
], timestamp=1166046005.00, timezone=0, committer="george@foo.com",
241
revision_id=b'rev-1_3_1')
242
builder.build_snapshot([b'rev-5', b'rev-1_3_1'], [
244
b'first\nsecond\nthird\nfourth\nfifth\nsixth\n')),
245
], revision_id=b'rev-6')
248
def create_duplicate_lines_tree(self):
249
builder = self.make_branch_builder('branch')
250
builder.start_series()
251
self.addCleanup(builder.finish_series)
252
base_text = b''.join(l for r, l in duplicate_base)
253
a_text = b''.join(l for r, l in duplicate_A)
254
b_text = b''.join(l for r, l in duplicate_B)
255
c_text = b''.join(l for r, l in duplicate_C)
256
d_text = b''.join(l for r, l in duplicate_D)
257
e_text = b''.join(l for r, l in duplicate_E)
258
builder.build_snapshot(None, [
259
('add', ('', b'root-id', 'directory', None)),
260
('add', ('file', b'file-id', 'file', base_text)),
261
], revision_id=b'rev-base')
262
builder.build_snapshot([b'rev-base'], [
263
('modify', ('file', a_text))],
264
revision_id=b'rev-A')
265
builder.build_snapshot([b'rev-base'], [
266
('modify', ('file', b_text))],
267
revision_id=b'rev-B')
268
builder.build_snapshot([b'rev-A'], [
269
('modify', ('file', c_text))],
270
revision_id=b'rev-C')
271
builder.build_snapshot([b'rev-B', b'rev-A'], [
272
('modify', ('file', d_text))],
273
revision_id=b'rev-D')
274
builder.build_snapshot([b'rev-C', b'rev-D'], [
275
('modify', ('file', e_text))],
276
revision_id=b'rev-E')
279
def assertAnnotateEqualDiff(self, actual, expected):
280
if actual != expected:
281
# Create an easier to understand diff when the lines don't actually
283
self.assertEqualDiff(''.join('\t'.join(l) for l in expected),
284
''.join('\t'.join(l) for l in actual))
286
def assertBranchAnnotate(self, expected, branch, path, revision_id,
287
verbose=False, full=False, show_ids=False):
288
tree = branch.repository.revision_tree(revision_id)
290
annotate.annotate_file_tree(tree, path, to_file,
291
verbose=verbose, full=full, show_ids=show_ids, branch=branch)
292
self.assertAnnotateEqualDiff(to_file.getvalue(), expected)
294
def assertRepoAnnotate(self, expected, repo, path, revision_id):
295
"""Assert that the revision is properly annotated."""
296
actual = list(repo.revision_tree(revision_id).annotate_iter(path))
297
self.assertAnnotateEqualDiff(actual, expected)
299
def test_annotate_duplicate_lines(self):
300
# XXX: Should this be a per_repository test?
301
builder = self.create_duplicate_lines_tree()
302
repo = builder.get_branch().repository
304
self.addCleanup(repo.unlock)
305
self.assertRepoAnnotate(duplicate_base, repo, 'file', b'rev-base')
306
self.assertRepoAnnotate(duplicate_A, repo, 'file', b'rev-A')
307
self.assertRepoAnnotate(duplicate_B, repo, 'file', b'rev-B')
308
self.assertRepoAnnotate(duplicate_C, repo, 'file', b'rev-C')
309
self.assertRepoAnnotate(duplicate_D, repo, 'file', b'rev-D')
310
self.assertRepoAnnotate(duplicate_E, repo, 'file', b'rev-E')
312
def test_annotate_shows_dotted_revnos(self):
313
builder = self.create_merged_trees()
315
self.assertBranchAnnotate('1 joe@foo | first\n'
316
'2 joe@foo | second\n'
317
'1.1.1 barry@f | third\n',
318
builder.get_branch(), 'a', b'rev-3')
320
def test_annotate_limits_dotted_revnos(self):
321
"""Annotate should limit dotted revnos to a depth of 12"""
322
builder = self.create_deeply_merged_trees()
324
self.assertBranchAnnotate('1 joe@foo | first\n'
325
'2 joe@foo | second\n'
326
'1.1.1 barry@f | third\n'
327
'1.2.1 jerry@f | fourth\n'
328
'1.3.1 george@ | fifth\n'
330
builder.get_branch(), 'a', b'rev-6',
331
verbose=False, full=False)
333
self.assertBranchAnnotate('1 joe@foo | first\n'
334
'2 joe@foo | second\n'
335
'1.1.1 barry@f | third\n'
336
'1.2.1 jerry@f | fourth\n'
337
'1.3.1 george@ | fifth\n'
338
'1.3.1 george@ | sixth\n',
339
builder.get_branch(), 'a', b'rev-6',
340
verbose=False, full=True)
342
# verbose=True shows everything, the full revno, user id, and date
343
self.assertBranchAnnotate('1 joe@foo.com 20061213 | first\n'
344
'2 joe@foo.com 20061213 | second\n'
345
'1.1.1 barry@foo.com 20061213 | third\n'
346
'1.2.1 jerry@foo.com 20061213 | fourth\n'
347
'1.3.1 george@foo.com 20061213 | fifth\n'
349
builder.get_branch(), 'a', b'rev-6',
350
verbose=True, full=False)
352
self.assertBranchAnnotate('1 joe@foo.com 20061213 | first\n'
353
'2 joe@foo.com 20061213 | second\n'
354
'1.1.1 barry@foo.com 20061213 | third\n'
355
'1.2.1 jerry@foo.com 20061213 | fourth\n'
356
'1.3.1 george@foo.com 20061213 | fifth\n'
357
'1.3.1 george@foo.com 20061213 | sixth\n',
358
builder.get_branch(), 'a', b'rev-6',
359
verbose=True, full=True)
361
def test_annotate_uses_branch_context(self):
362
"""Dotted revnos should use the Branch context.
364
When annotating a non-mainline revision, the annotation should still
365
use dotted revnos from the mainline.
367
builder = self.create_deeply_merged_trees()
369
self.assertBranchAnnotate('1 joe@foo | first\n'
370
'1.1.1 barry@f | third\n'
371
'1.2.1 jerry@f | fourth\n'
372
'1.3.1 george@ | fifth\n'
374
builder.get_branch(), 'a', b'rev-1_3_1',
375
verbose=False, full=False)
377
def test_annotate_show_ids(self):
378
builder = self.create_deeply_merged_trees()
380
# It looks better with real revision ids :)
381
self.assertBranchAnnotate(' rev-1 | first\n'
383
'rev-1_1_1 | third\n'
384
'rev-1_2_1 | fourth\n'
385
'rev-1_3_1 | fifth\n'
387
builder.get_branch(), 'a', b'rev-6',
388
show_ids=True, full=False)
390
self.assertBranchAnnotate(' rev-1 | first\n'
392
'rev-1_1_1 | third\n'
393
'rev-1_2_1 | fourth\n'
394
'rev-1_3_1 | fifth\n'
395
'rev-1_3_1 | sixth\n',
396
builder.get_branch(), 'a', b'rev-6',
397
show_ids=True, full=True)
399
def test_annotate_unicode_author(self):
400
tree1 = self.make_branch_and_tree('tree1')
402
self.build_tree_contents([('tree1/a', b'adi\xc3\xb3s')])
403
tree1.add(['a'], [b'a-id'])
404
tree1.commit('a', rev_id=b'rev-1',
405
committer=u'Pepe P\xe9rez <pperez@ejemplo.com>',
406
timestamp=1166046000.00, timezone=0)
408
self.build_tree_contents([('tree1/b', b'bye')])
409
tree1.add(['b'], [b'b-id'])
410
tree1.commit('b', rev_id=b'rev-2',
411
committer=u'p\xe9rez',
412
timestamp=1166046000.00, timezone=0)
415
self.addCleanup(tree1.unlock)
417
revtree_1 = tree1.branch.repository.revision_tree(b'rev-1')
418
revtree_2 = tree1.branch.repository.revision_tree(b'rev-2')
420
# this passes if no exception is raised
422
annotate.annotate_file_tree(revtree_1, 'a',
423
to_file=to_file, branch=tree1.branch)
426
to_file = codecs.getwriter('ascii')(sio, 'replace')
427
annotate.annotate_file_tree(revtree_2, 'b',
428
to_file=to_file, branch=tree1.branch)
429
self.assertEqualDiff('2 p?rez | bye\n', sio.getvalue())
431
# test now with unicode file-like
432
to_file = StringIOWithEncoding()
433
annotate.annotate_file_tree(revtree_2, 'b',
434
to_file=to_file, branch=tree1.branch)
435
self.assertContainsRe(u'2 p\xe9rez | bye\n', to_file.getvalue())
437
def test_annotate_author_or_committer(self):
438
tree1 = self.make_branch_and_tree('tree1')
440
self.build_tree_contents([('tree1/a', b'hello')])
441
tree1.add(['a'], [b'a-id'])
442
tree1.commit('a', rev_id=b'rev-1',
443
committer='Committer <committer@example.com>',
444
timestamp=1166046000.00, timezone=0)
446
self.build_tree_contents([('tree1/b', b'bye')])
447
tree1.add(['b'], [b'b-id'])
448
tree1.commit('b', rev_id=b'rev-2',
449
committer='Committer <committer@example.com>',
450
authors=['Author <author@example.com>'],
451
timestamp=1166046000.00, timezone=0)
454
self.addCleanup(tree1.unlock)
456
self.assertBranchAnnotate('1 committ | hello\n', tree1.branch,
459
self.assertBranchAnnotate('2 author@ | bye\n', tree1.branch,
463
class TestReannotate(tests.TestCase):
465
def annotateEqual(self, expected, parents, newlines, revision_id,
467
annotate_list = list(annotate.reannotate(parents, newlines,
468
revision_id, blocks))
469
self.assertEqual(len(expected), len(annotate_list))
470
for e, a in zip(expected, annotate_list):
471
self.assertEqual(e, a)
473
def test_reannotate(self):
474
self.annotateEqual(parent_1, [parent_1], new_1, b'blahblah')
475
self.annotateEqual(expected_2_1, [parent_2], new_1, b'blahblah')
476
self.annotateEqual(expected_1_2_2, [parent_1, parent_2], new_2,
479
def test_reannotate_no_parents(self):
480
self.annotateEqual(expected_1, [], new_1, b'blahblah')
482
def test_reannotate_left_matching_blocks(self):
483
"""Ensure that left_matching_blocks has an impact.
485
In this case, the annotation is ambiguous, so the hint isn't actually
488
parent = [(b'rev1', b'a\n')]
489
new_text = [b'a\n', b'a\n']
490
blocks = [(0, 0, 1), (1, 2, 0)]
491
self.annotateEqual([(b'rev1', b'a\n'), (b'rev2', b'a\n')], [parent],
492
new_text, b'rev2', blocks)
493
blocks = [(0, 1, 1), (1, 2, 0)]
494
self.annotateEqual([(b'rev2', b'a\n'), (b'rev1', b'a\n')], [parent],
495
new_text, b'rev2', blocks)