1
# Copyright (C) 2005, 2006, 2007 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
from cStringIO import StringIO
20
from bzrlib import log
21
from bzrlib.tests import TestCaseWithTransport
22
from bzrlib.log import (show_log,
29
from bzrlib.branch import Branch
30
from bzrlib.errors import InvalidRevisionNumber
33
class LogCatcher(LogFormatter):
34
"""Pull log messages into list rather than displaying them.
36
For ease of testing we save log messages here rather than actually
37
formatting them, so that we can precisely check the result without
38
being too dependent on the exact formatting.
40
We should also test the LogFormatter.
46
super(LogCatcher, self).__init__(to_file=None)
49
def log_revision(self, revision):
50
self.logs.append(revision)
53
class TestShowLog(TestCaseWithTransport):
55
def checkDelta(self, delta, **kw):
56
"""Check the filenames touched by a delta are as expected."""
57
for n in 'added', 'removed', 'renamed', 'modified', 'unchanged':
58
expected = kw.get(n, [])
59
# strip out only the path components
60
got = [x[0] for x in getattr(delta, n)]
61
self.assertEquals(expected, got)
63
def test_cur_revno(self):
64
wt = self.make_branch_and_tree('.')
68
wt.commit('empty commit')
69
show_log(b, lf, verbose=True, start_revision=1, end_revision=1)
70
self.assertRaises(InvalidRevisionNumber, show_log, b, lf,
71
start_revision=2, end_revision=1)
72
self.assertRaises(InvalidRevisionNumber, show_log, b, lf,
73
start_revision=1, end_revision=2)
74
self.assertRaises(InvalidRevisionNumber, show_log, b, lf,
75
start_revision=0, end_revision=2)
76
self.assertRaises(InvalidRevisionNumber, show_log, b, lf,
77
start_revision=1, end_revision=0)
78
self.assertRaises(InvalidRevisionNumber, show_log, b, lf,
79
start_revision=-1, end_revision=1)
80
self.assertRaises(InvalidRevisionNumber, show_log, b, lf,
81
start_revision=1, end_revision=-1)
83
def test_simple_log(self):
84
eq = self.assertEquals
86
wt = self.make_branch_and_tree('.')
94
wt.commit('empty commit')
96
show_log(b, lf, verbose=True)
98
eq(lf.logs[0].revno, '1')
99
eq(lf.logs[0].rev.message, 'empty commit')
101
self.log('log delta: %r' % d)
104
self.build_tree(['hello'])
106
wt.commit('add one file')
109
# log using regular thing
110
show_log(b, LongLogFormatter(lf))
112
for l in lf.readlines():
115
# get log as data structure
117
show_log(b, lf, verbose=True)
119
self.log('log entries:')
120
for logentry in lf.logs:
121
self.log('%4s %s' % (logentry.revno, logentry.rev.message))
123
# first one is most recent
124
logentry = lf.logs[0]
125
eq(logentry.revno, '2')
126
eq(logentry.rev.message, 'add one file')
128
self.log('log 2 delta: %r' % d)
129
self.checkDelta(d, added=['hello'])
131
# commit a log message with control characters
132
msg = "All 8-bit chars: " + ''.join([unichr(x) for x in range(256)])
133
self.log("original commit message: %r", msg)
136
show_log(b, lf, verbose=True)
137
committed_msg = lf.logs[0].rev.message
138
self.log("escaped commit message: %r", committed_msg)
139
self.assert_(msg != committed_msg)
140
self.assert_(len(committed_msg) > len(msg))
142
# Check that log message with only XML-valid characters isn't
143
# escaped. As ElementTree apparently does some kind of
144
# newline conversion, neither LF (\x0A) nor CR (\x0D) are
145
# included in the test commit message, even though they are
146
# valid XML 1.0 characters.
147
msg = "\x09" + ''.join([unichr(x) for x in range(0x20, 256)])
148
self.log("original commit message: %r", msg)
151
show_log(b, lf, verbose=True)
152
committed_msg = lf.logs[0].rev.message
153
self.log("escaped commit message: %r", committed_msg)
154
self.assert_(msg == committed_msg)
156
def test_deltas_in_merge_revisions(self):
157
"""Check deltas created for both mainline and merge revisions"""
158
eq = self.assertEquals
159
wt = self.make_branch_and_tree('parent')
160
self.build_tree(['parent/file1', 'parent/file2', 'parent/file3'])
163
wt.commit(message='add file1 and file2')
164
self.run_bzr('branch parent child')
165
os.unlink('child/file1')
166
print >> file('child/file2', 'wb'), 'hello'
167
self.run_bzr(['commit', '-m', 'remove file1 and modify file2',
170
self.run_bzr('merge ../child')
171
wt.commit('merge child branch')
175
lf.supports_merge_revisions = True
176
show_log(b, lf, verbose=True)
178
logentry = lf.logs[0]
179
eq(logentry.revno, '2')
180
eq(logentry.rev.message, 'merge child branch')
182
self.checkDelta(d, removed=['file1'], modified=['file2'])
183
logentry = lf.logs[1]
184
eq(logentry.revno, '1.1.1')
185
eq(logentry.rev.message, 'remove file1 and modify file2')
187
self.checkDelta(d, removed=['file1'], modified=['file2'])
188
logentry = lf.logs[2]
189
eq(logentry.revno, '1')
190
eq(logentry.rev.message, 'add file1 and file2')
192
self.checkDelta(d, added=['file1', 'file2'])
195
def make_commits_with_trailing_newlines(wt):
196
"""Helper method for LogFormatter tests"""
199
open('a', 'wb').write('hello moto\n')
201
wt.commit('simple log message', rev_id='a1'
202
, timestamp=1132586655.459960938, timezone=-6*3600
203
, committer='Joe Foo <joe@foo.com>')
204
open('b', 'wb').write('goodbye\n')
206
wt.commit('multiline\nlog\nmessage\n', rev_id='a2'
207
, timestamp=1132586842.411175966, timezone=-6*3600
208
, committer='Joe Foo <joe@foo.com>')
210
open('c', 'wb').write('just another manic monday\n')
212
wt.commit('single line with trailing newline\n', rev_id='a3'
213
, timestamp=1132587176.835228920, timezone=-6*3600
214
, committer = 'Joe Foo <joe@foo.com>')
218
class TestShortLogFormatter(TestCaseWithTransport):
220
def test_trailing_newlines(self):
221
wt = self.make_branch_and_tree('.')
222
b = make_commits_with_trailing_newlines(wt)
224
lf = ShortLogFormatter(to_file=sio)
226
self.assertEquals(sio.getvalue(), """\
227
3 Joe Foo\t2005-11-21
228
single line with trailing newline
230
2 Joe Foo\t2005-11-21
235
1 Joe Foo\t2005-11-21
241
class TestLongLogFormatter(TestCaseWithTransport):
243
def normalize_log(self,log):
244
"""Replaces the variable lines of logs with fixed lines"""
245
committer = 'committer: Lorem Ipsum <test@example.com>'
246
lines = log.splitlines(True)
247
for idx,line in enumerate(lines):
248
stripped_line = line.lstrip()
249
indent = ' ' * (len(line) - len(stripped_line))
250
if stripped_line.startswith('committer:'):
251
lines[idx] = indent + committer + '\n'
252
if stripped_line.startswith('timestamp:'):
253
lines[idx] = indent + 'timestamp: Just now\n'
254
return ''.join(lines)
256
def test_verbose_log(self):
257
"""Verbose log includes changed files
261
wt = self.make_branch_and_tree('.')
263
self.build_tree(['a'])
265
# XXX: why does a longer nick show up?
266
b.nick = 'test_verbose_log'
267
wt.commit(message='add a',
268
timestamp=1132711707,
270
committer='Lorem Ipsum <test@example.com>')
271
logfile = file('out.tmp', 'w+')
272
formatter = LongLogFormatter(to_file=logfile)
273
show_log(b, formatter, verbose=True)
276
log_contents = logfile.read()
277
self.assertEqualDiff(log_contents, '''\
278
------------------------------------------------------------
280
committer: Lorem Ipsum <test@example.com>
281
branch nick: test_verbose_log
282
timestamp: Wed 2005-11-23 12:08:27 +1000
289
def test_merges_are_indented_by_level(self):
290
wt = self.make_branch_and_tree('parent')
291
wt.commit('first post')
292
self.run_bzr('branch parent child')
293
self.run_bzr(['commit', '-m', 'branch 1', '--unchanged', 'child'])
294
self.run_bzr('branch child smallerchild')
295
self.run_bzr(['commit', '-m', 'branch 2', '--unchanged',
298
self.run_bzr('merge ../smallerchild')
299
self.run_bzr(['commit', '-m', 'merge branch 2'])
300
os.chdir('../parent')
301
self.run_bzr('merge ../child')
302
wt.commit('merge branch 1')
305
lf = LongLogFormatter(to_file=sio)
306
show_log(b, lf, verbose=True)
307
log = self.normalize_log(sio.getvalue())
308
self.assertEqualDiff("""\
309
------------------------------------------------------------
311
committer: Lorem Ipsum <test@example.com>
316
------------------------------------------------------------
318
committer: Lorem Ipsum <test@example.com>
323
------------------------------------------------------------
325
committer: Lorem Ipsum <test@example.com>
326
branch nick: smallerchild
330
------------------------------------------------------------
332
committer: Lorem Ipsum <test@example.com>
337
------------------------------------------------------------
339
committer: Lorem Ipsum <test@example.com>
346
def test_verbose_merge_revisions_contain_deltas(self):
347
wt = self.make_branch_and_tree('parent')
348
self.build_tree(['parent/f1', 'parent/f2'])
350
wt.commit('first post')
351
self.run_bzr('branch parent child')
352
os.unlink('child/f1')
353
print >> file('child/f2', 'wb'), 'hello'
354
self.run_bzr(['commit', '-m', 'removed f1 and modified f2',
357
self.run_bzr('merge ../child')
358
wt.commit('merge branch 1')
361
lf = LongLogFormatter(to_file=sio)
362
show_log(b, lf, verbose=True)
363
log = self.normalize_log(sio.getvalue())
364
self.assertEqualDiff("""\
365
------------------------------------------------------------
367
committer: Lorem Ipsum <test@example.com>
376
------------------------------------------------------------
378
committer: Lorem Ipsum <test@example.com>
382
removed f1 and modified f2
387
------------------------------------------------------------
389
committer: Lorem Ipsum <test@example.com>
399
def test_trailing_newlines(self):
400
wt = self.make_branch_and_tree('.')
401
b = make_commits_with_trailing_newlines(wt)
403
lf = LongLogFormatter(to_file=sio)
405
self.assertEqualDiff(sio.getvalue(), """\
406
------------------------------------------------------------
408
committer: Joe Foo <joe@foo.com>
410
timestamp: Mon 2005-11-21 09:32:56 -0600
412
single line with trailing newline
413
------------------------------------------------------------
415
committer: Joe Foo <joe@foo.com>
417
timestamp: Mon 2005-11-21 09:27:22 -0600
422
------------------------------------------------------------
424
committer: Joe Foo <joe@foo.com>
426
timestamp: Mon 2005-11-21 09:24:15 -0600
432
class TestLineLogFormatter(TestCaseWithTransport):
434
def test_line_log(self):
435
"""Line log should show revno
439
wt = self.make_branch_and_tree('.')
441
self.build_tree(['a'])
443
b.nick = 'test-line-log'
444
wt.commit(message='add a',
445
timestamp=1132711707,
447
committer='Line-Log-Formatter Tester <test@line.log>')
448
logfile = file('out.tmp', 'w+')
449
formatter = LineLogFormatter(to_file=logfile)
450
show_log(b, formatter)
453
log_contents = logfile.read()
454
self.assertEqualDiff(log_contents, '1: Line-Log-Formatte... 2005-11-23 add a\n')
456
def test_short_log_with_merges(self):
457
wt = self.make_branch_and_memory_tree('.')
461
wt.commit('rev-1', rev_id='rev-1',
462
timestamp=1132586655, timezone=36000,
463
committer='Joe Foo <joe@foo.com>')
464
wt.commit('rev-merged', rev_id='rev-2a',
465
timestamp=1132586700, timezone=36000,
466
committer='Joe Foo <joe@foo.com>')
467
wt.set_parent_ids(['rev-1', 'rev-2a'])
468
wt.branch.set_last_revision_info(1, 'rev-1')
469
wt.commit('rev-2', rev_id='rev-2b',
470
timestamp=1132586800, timezone=36000,
471
committer='Joe Foo <joe@foo.com>')
473
formatter = ShortLogFormatter(to_file=logfile)
474
show_log(wt.branch, formatter)
476
self.assertEqualDiff("""\
477
2 Joe Foo\t2005-11-22 [merge]
480
1 Joe Foo\t2005-11-22
483
""", logfile.getvalue())
487
def test_trailing_newlines(self):
488
wt = self.make_branch_and_tree('.')
489
b = make_commits_with_trailing_newlines(wt)
491
lf = LineLogFormatter(to_file=sio)
493
self.assertEqualDiff(sio.getvalue(), """\
494
3: Joe Foo 2005-11-21 single line with trailing newline
495
2: Joe Foo 2005-11-21 multiline
496
1: Joe Foo 2005-11-21 simple log message
500
class TestGetViewRevisions(TestCaseWithTransport):
502
def make_tree_with_commits(self):
503
"""Create a tree with well-known revision ids"""
504
wt = self.make_branch_and_tree('tree1')
505
wt.commit('commit one', rev_id='1')
506
wt.commit('commit two', rev_id='2')
507
wt.commit('commit three', rev_id='3')
508
mainline_revs = [None, '1', '2', '3']
509
rev_nos = {'1': 1, '2': 2, '3': 3}
510
return mainline_revs, rev_nos, wt
512
def make_tree_with_merges(self):
513
"""Create a tree with well-known revision ids and a merge"""
514
mainline_revs, rev_nos, wt = self.make_tree_with_commits()
515
tree2 = wt.bzrdir.sprout('tree2').open_workingtree()
516
tree2.commit('four-a', rev_id='4a')
517
wt.merge_from_branch(tree2.branch)
518
wt.commit('four-b', rev_id='4b')
519
mainline_revs.append('4b')
522
return mainline_revs, rev_nos, wt
524
def make_tree_with_many_merges(self):
525
"""Create a tree with well-known revision ids"""
526
wt = self.make_branch_and_tree('tree1')
527
wt.commit('commit one', rev_id='1')
528
wt.commit('commit two', rev_id='2')
529
tree3 = wt.bzrdir.sprout('tree3').open_workingtree()
530
tree3.commit('commit three a', rev_id='3a')
531
tree2 = wt.bzrdir.sprout('tree2').open_workingtree()
532
tree2.merge_from_branch(tree3.branch)
533
tree2.commit('commit three b', rev_id='3b')
534
wt.merge_from_branch(tree2.branch)
535
wt.commit('commit three c', rev_id='3c')
536
tree2.commit('four-a', rev_id='4a')
537
wt.merge_from_branch(tree2.branch)
538
wt.commit('four-b', rev_id='4b')
539
mainline_revs = [None, '1', '2', '3c', '4b']
540
rev_nos = {'1':1, '2':2, '3c': 3, '4b':4}
541
full_rev_nos_for_reference = {
544
'3a': '2.2.1', #first commit tree 3
545
'3b': '2.1.1', # first commit tree 2
546
'3c': '3', #merges 3b to main
547
'4a': '2.1.2', # second commit tree 2
548
'4b': '4', # merges 4a to main
550
return mainline_revs, rev_nos, wt
552
def test_get_view_revisions_forward(self):
553
"""Test the get_view_revisions method"""
554
mainline_revs, rev_nos, wt = self.make_tree_with_commits()
555
revisions = list(get_view_revisions(mainline_revs, rev_nos, wt.branch,
557
self.assertEqual([('1', '1', 0), ('2', '2', 0), ('3', '3', 0)],
559
revisions2 = list(get_view_revisions(mainline_revs, rev_nos, wt.branch,
560
'forward', include_merges=False))
561
self.assertEqual(revisions, revisions2)
563
def test_get_view_revisions_reverse(self):
564
"""Test the get_view_revisions with reverse"""
565
mainline_revs, rev_nos, wt = self.make_tree_with_commits()
566
revisions = list(get_view_revisions(mainline_revs, rev_nos, wt.branch,
568
self.assertEqual([('3', '3', 0), ('2', '2', 0), ('1', '1', 0), ],
570
revisions2 = list(get_view_revisions(mainline_revs, rev_nos, wt.branch,
571
'reverse', include_merges=False))
572
self.assertEqual(revisions, revisions2)
574
def test_get_view_revisions_merge(self):
575
"""Test get_view_revisions when there are merges"""
576
mainline_revs, rev_nos, wt = self.make_tree_with_merges()
577
revisions = list(get_view_revisions(mainline_revs, rev_nos, wt.branch,
579
self.assertEqual([('1', '1', 0), ('2', '2', 0), ('3', '3', 0),
580
('4b', '4', 0), ('4a', '3.1.1', 1)],
582
revisions = list(get_view_revisions(mainline_revs, rev_nos, wt.branch,
583
'forward', include_merges=False))
584
self.assertEqual([('1', '1', 0), ('2', '2', 0), ('3', '3', 0),
588
def test_get_view_revisions_merge_reverse(self):
589
"""Test get_view_revisions in reverse when there are merges"""
590
mainline_revs, rev_nos, wt = self.make_tree_with_merges()
591
revisions = list(get_view_revisions(mainline_revs, rev_nos, wt.branch,
593
self.assertEqual([('4b', '4', 0), ('4a', '3.1.1', 1),
594
('3', '3', 0), ('2', '2', 0), ('1', '1', 0)],
596
revisions = list(get_view_revisions(mainline_revs, rev_nos, wt.branch,
597
'reverse', include_merges=False))
598
self.assertEqual([('4b', '4', 0), ('3', '3', 0), ('2', '2', 0),
602
def test_get_view_revisions_merge2(self):
603
"""Test get_view_revisions when there are merges"""
604
mainline_revs, rev_nos, wt = self.make_tree_with_many_merges()
605
revisions = list(get_view_revisions(mainline_revs, rev_nos, wt.branch,
607
expected = [('1', '1', 0), ('2', '2', 0), ('3c', '3', 0),
608
('3a', '2.2.1', 1), ('3b', '2.1.1', 1), ('4b', '4', 0),
610
self.assertEqual(expected, revisions)
611
revisions = list(get_view_revisions(mainline_revs, rev_nos, wt.branch,
612
'forward', include_merges=False))
613
self.assertEqual([('1', '1', 0), ('2', '2', 0), ('3c', '3', 0),
618
class TestGetRevisionsTouchingFileID(TestCaseWithTransport):
620
def create_tree_with_single_merge(self):
621
"""Create a branch with a moderate layout.
623
The revision graph looks like:
631
In this graph, A introduced files f1 and f2 and f3.
632
B modifies f1 and f3, and C modifies f2 and f3.
633
D merges the changes from B and C and resolves the conflict for f3.
635
# TODO: jam 20070218 This seems like it could really be done
636
# with make_branch_and_memory_tree() if we could just
637
# create the content of those files.
638
# TODO: jam 20070218 Another alternative is that we would really
639
# like to only create this tree 1 time for all tests that
640
# use it. Since 'log' only uses the tree in a readonly
641
# fashion, it seems a shame to regenerate an identical
642
# tree for each test.
643
tree = self.make_branch_and_tree('tree')
645
self.addCleanup(tree.unlock)
647
self.build_tree_contents([('tree/f1', 'A\n'),
651
tree.add(['f1', 'f2', 'f3'], ['f1-id', 'f2-id', 'f3-id'])
652
tree.commit('A', rev_id='A')
654
self.build_tree_contents([('tree/f2', 'A\nC\n'),
655
('tree/f3', 'A\nC\n'),
657
tree.commit('C', rev_id='C')
658
# Revert back to A to build the other history.
659
tree.set_last_revision('A')
660
tree.branch.set_last_revision_info(1, 'A')
661
self.build_tree_contents([('tree/f1', 'A\nB\n'),
663
('tree/f3', 'A\nB\n'),
665
tree.commit('B', rev_id='B')
666
tree.set_parent_ids(['B', 'C'])
667
self.build_tree_contents([('tree/f1', 'A\nB\n'),
668
('tree/f2', 'A\nC\n'),
669
('tree/f3', 'A\nB\nC\n'),
671
tree.commit('D', rev_id='D')
673
# Switch to a read lock for this tree.
674
# We still have addCleanup(unlock)
679
def test_tree_with_single_merge(self):
680
"""Make sure the tree layout is correct."""
681
tree = self.create_tree_with_single_merge()
682
rev_A_tree = tree.branch.repository.revision_tree('A')
683
rev_B_tree = tree.branch.repository.revision_tree('B')
685
f1_changed = (u'f1', 'f1-id', 'file', True, False)
686
f2_changed = (u'f2', 'f2-id', 'file', True, False)
687
f3_changed = (u'f3', 'f3-id', 'file', True, False)
689
delta = rev_B_tree.changes_from(rev_A_tree)
690
self.assertEqual([f1_changed, f3_changed], delta.modified)
691
self.assertEqual([], delta.renamed)
692
self.assertEqual([], delta.added)
693
self.assertEqual([], delta.removed)
695
rev_C_tree = tree.branch.repository.revision_tree('C')
696
delta = rev_C_tree.changes_from(rev_A_tree)
697
self.assertEqual([f2_changed, f3_changed], delta.modified)
698
self.assertEqual([], delta.renamed)
699
self.assertEqual([], delta.added)
700
self.assertEqual([], delta.removed)
702
rev_D_tree = tree.branch.repository.revision_tree('D')
703
delta = rev_D_tree.changes_from(rev_B_tree)
704
self.assertEqual([f2_changed, f3_changed], delta.modified)
705
self.assertEqual([], delta.renamed)
706
self.assertEqual([], delta.added)
707
self.assertEqual([], delta.removed)
709
delta = rev_D_tree.changes_from(rev_C_tree)
710
self.assertEqual([f1_changed, f3_changed], delta.modified)
711
self.assertEqual([], delta.renamed)
712
self.assertEqual([], delta.added)
713
self.assertEqual([], delta.removed)
715
def assertAllRevisionsForFileID(self, tree, file_id, revisions):
716
"""Make sure _filter_revisions_touching_file_id returns the right values.
718
Get the return value from _filter_revisions_touching_file_id and make
719
sure they are correct.
721
# The api for _get_revisions_touching_file_id is a little crazy,
722
# So we do the setup here.
723
mainline = tree.branch.revision_history()
724
mainline.insert(0, None)
725
revnos = dict((rev, idx+1) for idx, rev in enumerate(mainline))
726
view_revs_iter = log.get_view_revisions(mainline, revnos, tree.branch,
728
actual_revs = log._filter_revisions_touching_file_id(
732
list(view_revs_iter))
733
self.assertEqual(revisions, [r for r, revno, depth in actual_revs])
735
def test_file_id_f1(self):
736
tree = self.create_tree_with_single_merge()
737
# f1 should be marked as modified by revisions A and B
738
self.assertAllRevisionsForFileID(tree, 'f1-id', ['B', 'A'])
740
def test_file_id_f2(self):
741
tree = self.create_tree_with_single_merge()
742
# f2 should be marked as modified by revisions A, C, and D
743
# because D merged the changes from C.
744
self.assertAllRevisionsForFileID(tree, 'f2-id', ['D', 'C', 'A'])
746
def test_file_id_f3(self):
747
tree = self.create_tree_with_single_merge()
748
# f3 should be marked as modified by revisions A, B, C, and D
749
self.assertAllRevisionsForFileID(tree, 'f2-id', ['D', 'C', 'A'])