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(' ', 1)) for l in text.splitlines(True)]
35
parent_1 = annotation("""\
44
parent_2 = annotation("""\
54
expected_2_1 = annotation("""\
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("""\
90
expected_1 = annotation("""\
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("""\
130
duplicate_A = annotation("""\
137
duplicate_B = annotation("""\
144
duplicate_C = annotation("""\
151
duplicate_D = annotation("""\
158
duplicate_E = annotation("""\
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('rev-1', None, [
183
('add', ('', 'root-id', 'directory', None)),
184
('add', ('a', 'a-id', 'file', 'first\n')),
185
], timestamp=1166046000.00, timezone=0, committer="joe@foo.com")
186
builder.build_snapshot('rev-2', ['rev-1'], [
187
('modify', ('a-id', 'first\nsecond\n')),
188
], timestamp=1166046001.00, timezone=0, committer="joe@foo.com")
189
builder.build_snapshot('rev-1_1_1', ['rev-1'], [
190
('modify', ('a-id', 'first\nthird\n')),
191
], timestamp=1166046002.00, timezone=0, committer="barry@foo.com")
192
builder.build_snapshot('rev-3', ['rev-2', 'rev-1_1_1'], [
193
('modify', ('a-id', 'first\nsecond\nthird\n')),
194
], timestamp=1166046003.00, timezone=0, committer="sal@foo.com")
197
def create_deeply_merged_trees(self):
198
"""Create some trees with a more complex merge history.
206
rev-3 rev-1_1_2 rev-1_2_1 ------+
210
rev-4 rev-1_2_2 rev-1_3_1
212
+-----------------+ |
216
+--------------------------------+
220
builder = self.create_merged_trees()
221
builder.build_snapshot('rev-1_1_2', ['rev-1_1_1'], [])
222
builder.build_snapshot('rev-4', ['rev-3', 'rev-1_1_2'], [])
223
builder.build_snapshot('rev-1_2_1', ['rev-1_1_1'], [
224
('modify', ('a-id', 'first\nthird\nfourth\n')),
225
], timestamp=1166046003.00, timezone=0, committer="jerry@foo.com")
226
builder.build_snapshot('rev-1_2_2', ['rev-1_2_1'], [],
227
timestamp=1166046004.00, timezone=0, committer="jerry@foo.com")
228
builder.build_snapshot('rev-5', ['rev-4', 'rev-1_2_2'], [
229
('modify', ('a-id', 'first\nsecond\nthird\nfourth\n')),
230
], timestamp=1166046004.00, timezone=0, committer="jerry@foo.com")
231
builder.build_snapshot('rev-1_3_1', ['rev-1_2_1'], [
232
('modify', ('a-id', 'first\nthird\nfourth\nfifth\nsixth\n')),
233
], timestamp=1166046005.00, timezone=0, committer="george@foo.com")
234
builder.build_snapshot('rev-6', ['rev-5', 'rev-1_3_1'], [
236
'first\nsecond\nthird\nfourth\nfifth\nsixth\n')),
240
def create_duplicate_lines_tree(self):
241
builder = self.make_branch_builder('branch')
242
builder.start_series()
243
self.addCleanup(builder.finish_series)
244
base_text = ''.join(l for r, l in duplicate_base)
245
a_text = ''.join(l for r, l in duplicate_A)
246
b_text = ''.join(l for r, l in duplicate_B)
247
c_text = ''.join(l for r, l in duplicate_C)
248
d_text = ''.join(l for r, l in duplicate_D)
249
e_text = ''.join(l for r, l in duplicate_E)
250
builder.build_snapshot('rev-base', None, [
251
('add', ('', 'root-id', 'directory', None)),
252
('add', ('file', 'file-id', 'file', base_text)),
254
builder.build_snapshot('rev-A', ['rev-base'], [
255
('modify', ('file-id', a_text))])
256
builder.build_snapshot('rev-B', ['rev-base'], [
257
('modify', ('file-id', b_text))])
258
builder.build_snapshot('rev-C', ['rev-A'], [
259
('modify', ('file-id', c_text))])
260
builder.build_snapshot('rev-D', ['rev-B', 'rev-A'], [
261
('modify', ('file-id', d_text))])
262
builder.build_snapshot('rev-E', ['rev-C', 'rev-D'], [
263
('modify', ('file-id', e_text))])
266
def assertAnnotateEqualDiff(self, actual, expected):
267
if actual != expected:
268
# Create an easier to understand diff when the lines don't actually
270
self.assertEqualDiff(''.join('\t'.join(l) for l in expected),
271
''.join('\t'.join(l) for l in actual))
273
def assertBranchAnnotate(self, expected, branch, file_id, revision_id,
274
verbose=False, full=False, show_ids=False):
275
tree = branch.repository.revision_tree(revision_id)
277
annotate.annotate_file_tree(tree, file_id, to_file,
278
verbose=verbose, full=full, show_ids=show_ids, branch=branch)
279
self.assertAnnotateEqualDiff(to_file.getvalue(), expected)
281
def assertRepoAnnotate(self, expected, repo, file_id, revision_id):
282
"""Assert that the revision is properly annotated."""
283
actual = list(repo.revision_tree(revision_id).annotate_iter(file_id))
284
self.assertAnnotateEqualDiff(actual, expected)
286
def test_annotate_duplicate_lines(self):
287
# XXX: Should this be a per_repository test?
288
builder = self.create_duplicate_lines_tree()
289
repo = builder.get_branch().repository
291
self.addCleanup(repo.unlock)
292
self.assertRepoAnnotate(duplicate_base, repo, 'file-id', 'rev-base')
293
self.assertRepoAnnotate(duplicate_A, repo, 'file-id', 'rev-A')
294
self.assertRepoAnnotate(duplicate_B, repo, 'file-id', 'rev-B')
295
self.assertRepoAnnotate(duplicate_C, repo, 'file-id', 'rev-C')
296
self.assertRepoAnnotate(duplicate_D, repo, 'file-id', 'rev-D')
297
self.assertRepoAnnotate(duplicate_E, repo, 'file-id', 'rev-E')
299
def test_annotate_shows_dotted_revnos(self):
300
builder = self.create_merged_trees()
302
self.assertBranchAnnotate('1 joe@foo | first\n'
303
'2 joe@foo | second\n'
304
'1.1.1 barry@f | third\n',
305
builder.get_branch(), 'a-id', 'rev-3')
307
def test_annotate_limits_dotted_revnos(self):
308
"""Annotate should limit dotted revnos to a depth of 12"""
309
builder = self.create_deeply_merged_trees()
311
self.assertBranchAnnotate('1 joe@foo | first\n'
312
'2 joe@foo | second\n'
313
'1.1.1 barry@f | third\n'
314
'1.2.1 jerry@f | fourth\n'
315
'1.3.1 george@ | fifth\n'
317
builder.get_branch(), 'a-id', 'rev-6',
318
verbose=False, full=False)
320
self.assertBranchAnnotate('1 joe@foo | first\n'
321
'2 joe@foo | second\n'
322
'1.1.1 barry@f | third\n'
323
'1.2.1 jerry@f | fourth\n'
324
'1.3.1 george@ | fifth\n'
325
'1.3.1 george@ | sixth\n',
326
builder.get_branch(), 'a-id', 'rev-6',
327
verbose=False, full=True)
329
# verbose=True shows everything, the full revno, user id, and date
330
self.assertBranchAnnotate('1 joe@foo.com 20061213 | first\n'
331
'2 joe@foo.com 20061213 | second\n'
332
'1.1.1 barry@foo.com 20061213 | third\n'
333
'1.2.1 jerry@foo.com 20061213 | fourth\n'
334
'1.3.1 george@foo.com 20061213 | fifth\n'
336
builder.get_branch(), 'a-id', 'rev-6',
337
verbose=True, full=False)
339
self.assertBranchAnnotate('1 joe@foo.com 20061213 | first\n'
340
'2 joe@foo.com 20061213 | second\n'
341
'1.1.1 barry@foo.com 20061213 | third\n'
342
'1.2.1 jerry@foo.com 20061213 | fourth\n'
343
'1.3.1 george@foo.com 20061213 | fifth\n'
344
'1.3.1 george@foo.com 20061213 | sixth\n',
345
builder.get_branch(), 'a-id', 'rev-6',
346
verbose=True, full=True)
348
def test_annotate_uses_branch_context(self):
349
"""Dotted revnos should use the Branch context.
351
When annotating a non-mainline revision, the annotation should still
352
use dotted revnos from the mainline.
354
builder = self.create_deeply_merged_trees()
356
self.assertBranchAnnotate('1 joe@foo | first\n'
357
'1.1.1 barry@f | third\n'
358
'1.2.1 jerry@f | fourth\n'
359
'1.3.1 george@ | fifth\n'
361
builder.get_branch(), 'a-id', 'rev-1_3_1',
362
verbose=False, full=False)
364
def test_annotate_show_ids(self):
365
builder = self.create_deeply_merged_trees()
367
# It looks better with real revision ids :)
368
self.assertBranchAnnotate(' rev-1 | first\n'
370
'rev-1_1_1 | third\n'
371
'rev-1_2_1 | fourth\n'
372
'rev-1_3_1 | fifth\n'
374
builder.get_branch(), 'a-id', 'rev-6',
375
show_ids=True, full=False)
377
self.assertBranchAnnotate(' rev-1 | first\n'
379
'rev-1_1_1 | third\n'
380
'rev-1_2_1 | fourth\n'
381
'rev-1_3_1 | fifth\n'
382
'rev-1_3_1 | sixth\n',
383
builder.get_branch(), 'a-id', 'rev-6',
384
show_ids=True, full=True)
386
def test_annotate_unicode_author(self):
387
tree1 = self.make_branch_and_tree('tree1')
389
self.build_tree_contents([('tree1/a', 'adi\xc3\xb3s')])
390
tree1.add(['a'], ['a-id'])
391
tree1.commit('a', rev_id='rev-1',
392
committer=u'Pepe P\xe9rez <pperez@ejemplo.com>',
393
timestamp=1166046000.00, timezone=0)
395
self.build_tree_contents([('tree1/b', 'bye')])
396
tree1.add(['b'], ['b-id'])
397
tree1.commit('b', rev_id='rev-2',
398
committer=u'p\xe9rez',
399
timestamp=1166046000.00, timezone=0)
402
self.addCleanup(tree1.unlock)
404
revtree_1 = tree1.branch.repository.revision_tree('rev-1')
405
revtree_2 = tree1.branch.repository.revision_tree('rev-2')
407
# this passes if no exception is raised
409
annotate.annotate_file_tree(revtree_1, 'a-id',
410
to_file=to_file, branch=tree1.branch)
413
to_file = codecs.getwriter('ascii')(sio, 'replace')
414
annotate.annotate_file_tree(revtree_2, 'b-id',
415
to_file=to_file, branch=tree1.branch)
416
self.assertEqualDiff('2 p?rez | bye\n', sio.getvalue())
418
# test now with unicode file-like
419
to_file = StringIOWithEncoding()
420
annotate.annotate_file_tree(revtree_2, 'b-id',
421
to_file=to_file, branch=tree1.branch)
422
self.assertContainsRe(u'2 p\xe9rez | bye\n', to_file.getvalue())
424
def test_annotate_author_or_committer(self):
425
tree1 = self.make_branch_and_tree('tree1')
427
self.build_tree_contents([('tree1/a', 'hello')])
428
tree1.add(['a'], ['a-id'])
429
tree1.commit('a', rev_id='rev-1',
430
committer='Committer <committer@example.com>',
431
timestamp=1166046000.00, timezone=0)
433
self.build_tree_contents([('tree1/b', 'bye')])
434
tree1.add(['b'], ['b-id'])
435
tree1.commit('b', rev_id='rev-2',
436
committer='Committer <committer@example.com>',
437
authors=['Author <author@example.com>'],
438
timestamp=1166046000.00, timezone=0)
441
self.addCleanup(tree1.unlock)
443
self.assertBranchAnnotate('1 committ | hello\n', tree1.branch,
447
self.assertBranchAnnotate('2 author@ | bye\n', tree1.branch,
451
class TestReannotate(tests.TestCase):
453
def annotateEqual(self, expected, parents, newlines, revision_id,
455
annotate_list = list(annotate.reannotate(parents, newlines,
456
revision_id, blocks))
457
self.assertEqual(len(expected), len(annotate_list))
458
for e, a in zip(expected, annotate_list):
459
self.assertEqual(e, a)
461
def test_reannotate(self):
462
self.annotateEqual(parent_1, [parent_1], new_1, 'blahblah')
463
self.annotateEqual(expected_2_1, [parent_2], new_1, 'blahblah')
464
self.annotateEqual(expected_1_2_2, [parent_1, parent_2], new_2,
467
def test_reannotate_no_parents(self):
468
self.annotateEqual(expected_1, [], new_1, 'blahblah')
470
def test_reannotate_left_matching_blocks(self):
471
"""Ensure that left_matching_blocks has an impact.
473
In this case, the annotation is ambiguous, so the hint isn't actually
476
parent = [('rev1', 'a\n')]
477
new_text = ['a\n', 'a\n']
478
blocks = [(0, 0, 1), (1, 2, 0)]
479
self.annotateEqual([('rev1', 'a\n'), ('rev2', 'a\n')], [parent],
480
new_text, 'rev2', blocks)
481
blocks = [(0, 1, 1), (1, 2, 0)]
482
self.annotateEqual([('rev2', 'a\n'), ('rev1', 'a\n')], [parent],
483
new_text, 'rev2', blocks)