~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

import os
from cStringIO import StringIO

from bzrlib import (
    errors,
    log,
    registry,
    revision,
    revisionspec,
    symbol_versioning,
    tests,
    )


class TestLogMixin(object):

    def wt_commit(self, wt, message, **kwargs):
        """Use some mostly fixed values for commits to simplify tests.

        Tests can use this function to get some commit attributes. The time
        stamp is incremented at each commit.
        """
        if getattr(self, 'timestamp', None) is None:
            self.timestamp = 1132617600 # Mon 2005-11-22 00:00:00 +0000
        else:
            self.timestamp += 1 # 1 second between each commit
        kwargs.setdefault('timestamp', self.timestamp)
        kwargs.setdefault('timezone', 0) # UTC
        kwargs.setdefault('committer', 'Joe Foo <joe@foo.com>')

        return wt.commit(message, **kwargs)


class TestCaseForLogFormatter(tests.TestCaseWithTransport, TestLogMixin):

    def setUp(self):
        super(TestCaseForLogFormatter, self).setUp()
        # keep a reference to the "current" custom prop. handler registry
        self.properties_handler_registry = log.properties_handler_registry
        # Use a clean registry for log
        log.properties_handler_registry = registry.Registry()

        def restore():
            log.properties_handler_registry = self.properties_handler_registry
        self.addCleanup(restore)

    def assertFormatterResult(self, result, branch, formatter_class,
                              formatter_kwargs=None, show_log_kwargs=None):
        logfile = self.make_utf8_encoded_stringio()
        if formatter_kwargs is None:
            formatter_kwargs = {}
        formatter = formatter_class(to_file=logfile, **formatter_kwargs)
        if show_log_kwargs is None:
            show_log_kwargs = {}
        log.show_log(branch, formatter, **show_log_kwargs)
        self.assertEqualDiff(result, logfile.getvalue())

    def make_standard_commit(self, branch_nick, **kwargs):
        wt = self.make_branch_and_tree('.')
        wt.lock_write()
        self.addCleanup(wt.unlock)
        self.build_tree(['a'])
        wt.add(['a'])
        wt.branch.nick = branch_nick
        kwargs.setdefault('committer', 'Lorem Ipsum <test@example.com>')
        kwargs.setdefault('authors', ['John Doe <jdoe@example.com>'])
        self.wt_commit(wt, 'add a', **kwargs)
        return wt

    def make_commits_with_trailing_newlines(self, wt):
        """Helper method for LogFormatter tests"""
        b = wt.branch
        b.nick = 'test'
        self.build_tree_contents([('a', 'hello moto\n')])
        self.wt_commit(wt, 'simple log message', rev_id='a1')
        self.build_tree_contents([('b', 'goodbye\n')])
        wt.add('b')
        self.wt_commit(wt, 'multiline\nlog\nmessage\n', rev_id='a2')

        self.build_tree_contents([('c', 'just another manic monday\n')])
        wt.add('c')
        self.wt_commit(wt, 'single line with trailing newline\n', rev_id='a3')
        return b

    def _prepare_tree_with_merges(self, with_tags=False):
        wt = self.make_branch_and_memory_tree('.')
        wt.lock_write()
        self.addCleanup(wt.unlock)
        wt.add('')
        self.wt_commit(wt, 'rev-1', rev_id='rev-1')
        self.wt_commit(wt, 'rev-merged', rev_id='rev-2a')
        wt.set_parent_ids(['rev-1', 'rev-2a'])
        wt.branch.set_last_revision_info(1, 'rev-1')
        self.wt_commit(wt, 'rev-2', rev_id='rev-2b')
        if with_tags:
            branch = wt.branch
            branch.tags.set_tag('v0.2', 'rev-2b')
            self.wt_commit(wt, 'rev-3', rev_id='rev-3')
            branch.tags.set_tag('v1.0rc1', 'rev-3')
            branch.tags.set_tag('v1.0', 'rev-3')
        return wt

class LogCatcher(log.LogFormatter):
    """Pull log messages into a list rather than displaying them.

    To simplify testing we save logged revisions here rather than actually
    formatting anything, so that we can precisely check the result without
    being dependent on the formatting.
    """

    supports_merge_revisions = True
    supports_delta = True
    supports_diff = True
    preferred_levels = 0

    def __init__(self, *args, **kwargs):
        kwargs.update(dict(to_file=None))
        super(LogCatcher, self).__init__(*args, **kwargs)
        self.revisions = []

    def log_revision(self, revision):
        self.revisions.append(revision)


class TestShowLog(tests.TestCaseWithTransport):

    def checkDelta(self, delta, **kw):
        """Check the filenames touched by a delta are as expected.

        Caller only have to pass in the list of files for each part, all
        unspecified parts are considered empty (and checked as such).
        """
        for n in 'added', 'removed', 'renamed', 'modified', 'unchanged':
            # By default we expect an empty list
            expected = kw.get(n, [])
            # strip out only the path components
            got = [x[0] for x in getattr(delta, n)]
            self.assertEqual(expected, got)

    def assertInvalidRevisonNumber(self, br, start, end):
        lf = LogCatcher()
        self.assertRaises(errors.InvalidRevisionNumber,
                          log.show_log, br, lf,
                          start_revision=start, end_revision=end)

    def test_cur_revno(self):
        wt = self.make_branch_and_tree('.')
        b = wt.branch

        lf = LogCatcher()
        wt.commit('empty commit')
        log.show_log(b, lf, verbose=True, start_revision=1, end_revision=1)

        # Since there is a single revision in the branch all the combinations
        # below should fail.
        self.assertInvalidRevisonNumber(b, 2, 1)
        self.assertInvalidRevisonNumber(b, 1, 2)
        self.assertInvalidRevisonNumber(b, 0, 2)
        self.assertInvalidRevisonNumber(b, 1, 0)
        self.assertInvalidRevisonNumber(b, -1, 1)
        self.assertInvalidRevisonNumber(b, 1, -1)

    def test_empty_branch(self):
        wt = self.make_branch_and_tree('.')

        lf = LogCatcher()
        log.show_log(wt.branch, lf)
        # no entries yet
        self.assertEqual([], lf.revisions)

    def test_empty_commit(self):
        wt = self.make_branch_and_tree('.')

        wt.commit('empty commit')
        lf = LogCatcher()
        log.show_log(wt.branch, lf, verbose=True)
        revs = lf.revisions
        self.assertEqual(1, len(revs))
        self.assertEqual('1', revs[0].revno)
        self.assertEqual('empty commit', revs[0].rev.message)
        self.checkDelta(revs[0].delta)

    def test_simple_commit(self):
        wt = self.make_branch_and_tree('.')
        wt.commit('empty commit')
        self.build_tree(['hello'])
        wt.add('hello')
        wt.commit('add one file',
                  committer=u'\u013d\xf3r\xe9m \xcdp\u0161\xfam '
                            u'<test@example.com>')
        lf = LogCatcher()
        log.show_log(wt.branch, lf, verbose=True)
        self.assertEqual(2, len(lf.revisions))
        # first one is most recent
        log_entry = lf.revisions[0]
        self.assertEqual('2', log_entry.revno)
        self.assertEqual('add one file', log_entry.rev.message)
        self.checkDelta(log_entry.delta, added=['hello'])

    def test_commit_message_with_control_chars(self):
        wt = self.make_branch_and_tree('.')
        msg = u"All 8-bit chars: " +  ''.join([unichr(x) for x in range(256)])
        msg = msg.replace(u'\r', u'\n')
        wt.commit(msg)
        lf = LogCatcher()
        log.show_log(wt.branch, lf, verbose=True)
        committed_msg = lf.revisions[0].rev.message
        if wt.branch.repository._serializer.squashes_xml_invalid_characters:
            self.assertNotEqual(msg, committed_msg)
            self.assertTrue(len(committed_msg) > len(msg))
        else:
            self.assertEqual(msg, committed_msg)

    def test_commit_message_without_control_chars(self):
        wt = self.make_branch_and_tree('.')
        # escaped.  As ElementTree apparently does some kind of
        # newline conversion, neither LF (\x0A) nor CR (\x0D) are
        # included in the test commit message, even though they are
        # valid XML 1.0 characters.
        msg = "\x09" + ''.join([unichr(x) for x in range(0x20, 256)])
        wt.commit(msg)
        lf = LogCatcher()
        log.show_log(wt.branch, lf, verbose=True)
        committed_msg = lf.revisions[0].rev.message
        self.assertEqual(msg, committed_msg)

    def test_deltas_in_merge_revisions(self):
        """Check deltas created for both mainline and merge revisions"""
        wt = self.make_branch_and_tree('parent')
        self.build_tree(['parent/file1', 'parent/file2', 'parent/file3'])
        wt.add('file1')
        wt.add('file2')
        wt.commit(message='add file1 and file2')
        self.run_bzr('branch parent child')
        os.unlink('child/file1')
        file('child/file2', 'wb').write('hello\n')
        self.run_bzr(['commit', '-m', 'remove file1 and modify file2',
            'child'])
        os.chdir('parent')
        self.run_bzr('merge ../child')
        wt.commit('merge child branch')
        os.chdir('..')
        b = wt.branch
        lf = LogCatcher()
        lf.supports_merge_revisions = True
        log.show_log(b, lf, verbose=True)

        revs = lf.revisions
        self.assertEqual(3, len(revs))

        logentry = revs[0]
        self.assertEqual('2', logentry.revno)
        self.assertEqual('merge child branch', logentry.rev.message)
        self.checkDelta(logentry.delta, removed=['file1'], modified=['file2'])

        logentry = revs[1]
        self.assertEqual('1.1.1', logentry.revno)
        self.assertEqual('remove file1 and modify file2', logentry.rev.message)
        self.checkDelta(logentry.delta, removed=['file1'], modified=['file2'])

        logentry = revs[2]
        self.assertEqual('1', logentry.revno)
        self.assertEqual('add file1 and file2', logentry.rev.message)
        self.checkDelta(logentry.delta, added=['file1', 'file2'])


class TestShortLogFormatter(TestCaseForLogFormatter):

    def test_trailing_newlines(self):
        wt = self.make_branch_and_tree('.')
        b = self.make_commits_with_trailing_newlines(wt)
        self.assertFormatterResult("""\
    3 Joe Foo\t2005-11-22
      single line with trailing newline

    2 Joe Foo\t2005-11-22
      multiline
      log
      message

    1 Joe Foo\t2005-11-22
      simple log message

""",
            b, log.ShortLogFormatter)

    def test_short_log_with_merges(self):
        wt = self._prepare_tree_with_merges()
        self.assertFormatterResult("""\
    2 Joe Foo\t2005-11-22 [merge]
      rev-2

    1 Joe Foo\t2005-11-22
      rev-1

""",
            wt.branch, log.ShortLogFormatter)

    def test_short_log_with_merges_and_advice(self):
        wt = self._prepare_tree_with_merges()
        self.assertFormatterResult("""\
    2 Joe Foo\t2005-11-22 [merge]
      rev-2

    1 Joe Foo\t2005-11-22
      rev-1

Use --include-merges or -n0 to see merged revisions.
""",
            wt.branch, log.ShortLogFormatter,
            formatter_kwargs=dict(show_advice=True))

    def test_short_log_with_merges_and_range(self):
        wt = self._prepare_tree_with_merges()
        self.wt_commit(wt, 'rev-3a', rev_id='rev-3a')
        wt.branch.set_last_revision_info(2, 'rev-2b')
        wt.set_parent_ids(['rev-2b', 'rev-3a'])
        self.wt_commit(wt, 'rev-3b', rev_id='rev-3b')
        self.assertFormatterResult("""\
    3 Joe Foo\t2005-11-22 [merge]
      rev-3b

    2 Joe Foo\t2005-11-22 [merge]
      rev-2

""",
            wt.branch, log.ShortLogFormatter,
            show_log_kwargs=dict(start_revision=2, end_revision=3))

    def test_short_log_with_tags(self):
        wt = self._prepare_tree_with_merges(with_tags=True)
        self.assertFormatterResult("""\
    3 Joe Foo\t2005-11-22 {v1.0, v1.0rc1}
      rev-3

    2 Joe Foo\t2005-11-22 {v0.2} [merge]
      rev-2

    1 Joe Foo\t2005-11-22
      rev-1

""",
            wt.branch, log.ShortLogFormatter)

    def test_short_log_single_merge_revision(self):
        wt = self._prepare_tree_with_merges()
        revspec = revisionspec.RevisionSpec.from_string('1.1.1')
        rev = revspec.in_history(wt.branch)
        self.assertFormatterResult("""\
      1.1.1 Joe Foo\t2005-11-22
            rev-merged

""",
            wt.branch, log.ShortLogFormatter,
            show_log_kwargs=dict(start_revision=rev, end_revision=rev))


class TestShortLogFormatterWithMergeRevisions(TestCaseForLogFormatter):

    def test_short_merge_revs_log_with_merges(self):
        wt = self._prepare_tree_with_merges()
        # Note that the 1.1.1 indenting is in fact correct given that
        # the revision numbers are right justified within 5 characters
        # for mainline revnos and 9 characters for dotted revnos.
        self.assertFormatterResult("""\
    2 Joe Foo\t2005-11-22 [merge]
      rev-2

          1.1.1 Joe Foo\t2005-11-22
                rev-merged

    1 Joe Foo\t2005-11-22
      rev-1

""",
            wt.branch, log.ShortLogFormatter,
            formatter_kwargs=dict(levels=0))

    def test_short_merge_revs_log_single_merge_revision(self):
        wt = self._prepare_tree_with_merges()
        revspec = revisionspec.RevisionSpec.from_string('1.1.1')
        rev = revspec.in_history(wt.branch)
        self.assertFormatterResult("""\
      1.1.1 Joe Foo\t2005-11-22
            rev-merged

""",
            wt.branch, log.ShortLogFormatter,
            formatter_kwargs=dict(levels=0),
            show_log_kwargs=dict(start_revision=rev, end_revision=rev))


class TestLongLogFormatter(TestCaseForLogFormatter):

    def test_verbose_log(self):
        """Verbose log includes changed files

        bug #4676
        """
        wt = self.make_standard_commit('test_verbose_log', authors=[])
        self.assertFormatterResult('''\
------------------------------------------------------------
revno: 1
committer: Lorem Ipsum <test@example.com>
branch nick: test_verbose_log
timestamp: Tue 2005-11-22 00:00:00 +0000
message:
  add a
added:
  a
''',
            wt.branch, log.LongLogFormatter,
            show_log_kwargs=dict(verbose=True))

    def test_merges_are_indented_by_level(self):
        wt = self.make_branch_and_tree('parent')
        self.wt_commit(wt, 'first post')
        child_wt = wt.bzrdir.sprout('child').open_workingtree()
        self.wt_commit(child_wt, 'branch 1')
        smallerchild_wt = wt.bzrdir.sprout('smallerchild').open_workingtree()
        self.wt_commit(smallerchild_wt, 'branch 2')
        child_wt.merge_from_branch(smallerchild_wt.branch)
        self.wt_commit(child_wt, 'merge branch 2')
        wt.merge_from_branch(child_wt.branch)
        self.wt_commit(wt, 'merge branch 1')
        self.assertFormatterResult("""\
------------------------------------------------------------
revno: 2 [merge]
committer: Joe Foo <joe@foo.com>
branch nick: parent
timestamp: Tue 2005-11-22 00:00:04 +0000
message:
  merge branch 1
    ------------------------------------------------------------
    revno: 1.1.2 [merge]
    committer: Joe Foo <joe@foo.com>
    branch nick: child
    timestamp: Tue 2005-11-22 00:00:03 +0000
    message:
      merge branch 2
        ------------------------------------------------------------
        revno: 1.2.1
        committer: Joe Foo <joe@foo.com>
        branch nick: smallerchild
        timestamp: Tue 2005-11-22 00:00:02 +0000
        message:
          branch 2
    ------------------------------------------------------------
    revno: 1.1.1
    committer: Joe Foo <joe@foo.com>
    branch nick: child
    timestamp: Tue 2005-11-22 00:00:01 +0000
    message:
      branch 1
------------------------------------------------------------
revno: 1
committer: Joe Foo <joe@foo.com>
branch nick: parent
timestamp: Tue 2005-11-22 00:00:00 +0000
message:
  first post
""",
            wt.branch, log.LongLogFormatter,
            formatter_kwargs=dict(levels=0),
            show_log_kwargs=dict(verbose=True))

    def test_verbose_merge_revisions_contain_deltas(self):
        wt = self.make_branch_and_tree('parent')
        self.build_tree(['parent/f1', 'parent/f2'])
        wt.add(['f1','f2'])
        self.wt_commit(wt, 'first post')
        child_wt = wt.bzrdir.sprout('child').open_workingtree()
        os.unlink('child/f1')
        self.build_tree_contents([('child/f2', 'hello\n')])
        self.wt_commit(child_wt, 'removed f1 and modified f2')
        wt.merge_from_branch(child_wt.branch)
        self.wt_commit(wt, 'merge branch 1')
        self.assertFormatterResult("""\
------------------------------------------------------------
revno: 2 [merge]
committer: Joe Foo <joe@foo.com>
branch nick: parent
timestamp: Tue 2005-11-22 00:00:02 +0000
message:
  merge branch 1
removed:
  f1
modified:
  f2
    ------------------------------------------------------------
    revno: 1.1.1
    committer: Joe Foo <joe@foo.com>
    branch nick: child
    timestamp: Tue 2005-11-22 00:00:01 +0000
    message:
      removed f1 and modified f2
    removed:
      f1
    modified:
      f2
------------------------------------------------------------
revno: 1
committer: Joe Foo <joe@foo.com>
branch nick: parent
timestamp: Tue 2005-11-22 00:00:00 +0000
message:
  first post
added:
  f1
  f2
""",
            wt.branch, log.LongLogFormatter,
            formatter_kwargs=dict(levels=0),
            show_log_kwargs=dict(verbose=True))

    def test_trailing_newlines(self):
        wt = self.make_branch_and_tree('.')
        b = self.make_commits_with_trailing_newlines(wt)
        self.assertFormatterResult("""\
------------------------------------------------------------
revno: 3
committer: Joe Foo <joe@foo.com>
branch nick: test
timestamp: Tue 2005-11-22 00:00:02 +0000
message:
  single line with trailing newline
------------------------------------------------------------
revno: 2
committer: Joe Foo <joe@foo.com>
branch nick: test
timestamp: Tue 2005-11-22 00:00:01 +0000
message:
  multiline
  log
  message
------------------------------------------------------------
revno: 1
committer: Joe Foo <joe@foo.com>
branch nick: test
timestamp: Tue 2005-11-22 00:00:00 +0000
message:
  simple log message
""",
        b, log.LongLogFormatter)

    def test_author_in_log(self):
        """Log includes the author name if it's set in
        the revision properties
        """
        wt = self.make_standard_commit('test_author_log',
            authors=['John Doe <jdoe@example.com>',
                     'Jane Rey <jrey@example.com>'])
        self.assertFormatterResult("""\
------------------------------------------------------------
revno: 1
author: John Doe <jdoe@example.com>, Jane Rey <jrey@example.com>
committer: Lorem Ipsum <test@example.com>
branch nick: test_author_log
timestamp: Tue 2005-11-22 00:00:00 +0000
message:
  add a
""",
        wt.branch, log.LongLogFormatter)

    def test_properties_in_log(self):
        """Log includes the custom properties returned by the registered
        handlers.
        """
        wt = self.make_standard_commit('test_properties_in_log')
        def trivial_custom_prop_handler(revision):
            return {'test_prop':'test_value'}

        # Cleaned up in setUp()
        log.properties_handler_registry.register(
            'trivial_custom_prop_handler',
            trivial_custom_prop_handler)
        self.assertFormatterResult("""\
------------------------------------------------------------
revno: 1
test_prop: test_value
author: John Doe <jdoe@example.com>
committer: Lorem Ipsum <test@example.com>
branch nick: test_properties_in_log
timestamp: Tue 2005-11-22 00:00:00 +0000
message:
  add a
""",
            wt.branch, log.LongLogFormatter)

    def test_properties_in_short_log(self):
        """Log includes the custom properties returned by the registered
        handlers.
        """
        wt = self.make_standard_commit('test_properties_in_short_log')
        def trivial_custom_prop_handler(revision):
            return {'test_prop':'test_value'}

        log.properties_handler_registry.register(
            'trivial_custom_prop_handler',
            trivial_custom_prop_handler)
        self.assertFormatterResult("""\
    1 John Doe\t2005-11-22
      test_prop: test_value
      add a

""",
            wt.branch, log.ShortLogFormatter)

    def test_error_in_properties_handler(self):
        """Log includes the custom properties returned by the registered
        handlers.
        """
        wt = self.make_standard_commit('error_in_properties_handler',
            revprops={'first_prop':'first_value'})
        sio = self.make_utf8_encoded_stringio()
        formatter = log.LongLogFormatter(to_file=sio)
        def trivial_custom_prop_handler(revision):
            raise StandardError("a test error")

        log.properties_handler_registry.register(
            'trivial_custom_prop_handler',
            trivial_custom_prop_handler)
        self.assertRaises(StandardError, log.show_log, wt.branch, formatter,)

    def test_properties_handler_bad_argument(self):
        wt = self.make_standard_commit('bad_argument',
              revprops={'a_prop':'test_value'})
        sio = self.make_utf8_encoded_stringio()
        formatter = log.LongLogFormatter(to_file=sio)
        def bad_argument_prop_handler(revision):
            return {'custom_prop_name':revision.properties['a_prop']}

        log.properties_handler_registry.register(
            'bad_argument_prop_handler',
            bad_argument_prop_handler)

        self.assertRaises(AttributeError, formatter.show_properties,
                          'a revision', '')

        revision = wt.branch.repository.get_revision(wt.branch.last_revision())
        formatter.show_properties(revision, '')
        self.assertEqualDiff('''custom_prop_name: test_value\n''',
                             sio.getvalue())


class TestLongLogFormatterWithoutMergeRevisions(TestCaseForLogFormatter):

    def test_long_verbose_log(self):
        """Verbose log includes changed files

        bug #4676
        """
        wt = self.make_standard_commit('test_long_verbose_log', authors=[])
        self.assertFormatterResult("""\
------------------------------------------------------------
revno: 1
committer: Lorem Ipsum <test@example.com>
branch nick: test_long_verbose_log
timestamp: Tue 2005-11-22 00:00:00 +0000
message:
  add a
added:
  a
""",
            wt.branch, log.LongLogFormatter,
            formatter_kwargs=dict(levels=1),
            show_log_kwargs=dict(verbose=True))

    def test_long_verbose_contain_deltas(self):
        wt = self.make_branch_and_tree('parent')
        self.build_tree(['parent/f1', 'parent/f2'])
        wt.add(['f1','f2'])
        self.wt_commit(wt, 'first post')
        child_wt = wt.bzrdir.sprout('child').open_workingtree()
        os.unlink('child/f1')
        self.build_tree_contents([('child/f2', 'hello\n')])
        self.wt_commit(child_wt, 'removed f1 and modified f2')
        wt.merge_from_branch(child_wt.branch)
        self.wt_commit(wt, 'merge branch 1')
        self.assertFormatterResult("""\
------------------------------------------------------------
revno: 2 [merge]
committer: Joe Foo <joe@foo.com>
branch nick: parent
timestamp: Tue 2005-11-22 00:00:02 +0000
message:
  merge branch 1
removed:
  f1
modified:
  f2
------------------------------------------------------------
revno: 1
committer: Joe Foo <joe@foo.com>
branch nick: parent
timestamp: Tue 2005-11-22 00:00:00 +0000
message:
  first post
added:
  f1
  f2
""",
            wt.branch, log.LongLogFormatter,
            formatter_kwargs=dict(levels=1),
            show_log_kwargs=dict(verbose=True))

    def test_long_trailing_newlines(self):
        wt = self.make_branch_and_tree('.')
        b = self.make_commits_with_trailing_newlines(wt)
        self.assertFormatterResult("""\
------------------------------------------------------------
revno: 3
committer: Joe Foo <joe@foo.com>
branch nick: test
timestamp: Tue 2005-11-22 00:00:02 +0000
message:
  single line with trailing newline
------------------------------------------------------------
revno: 2
committer: Joe Foo <joe@foo.com>
branch nick: test
timestamp: Tue 2005-11-22 00:00:01 +0000
message:
  multiline
  log
  message
------------------------------------------------------------
revno: 1
committer: Joe Foo <joe@foo.com>
branch nick: test
timestamp: Tue 2005-11-22 00:00:00 +0000
message:
  simple log message
""",
        b, log.LongLogFormatter,
        formatter_kwargs=dict(levels=1))

    def test_long_author_in_log(self):
        """Log includes the author name if it's set in
        the revision properties
        """
        wt = self.make_standard_commit('test_author_log')
        self.assertFormatterResult("""\
------------------------------------------------------------
revno: 1
author: John Doe <jdoe@example.com>
committer: Lorem Ipsum <test@example.com>
branch nick: test_author_log
timestamp: Tue 2005-11-22 00:00:00 +0000
message:
  add a
""",
            wt.branch, log.LongLogFormatter,
            formatter_kwargs=dict(levels=1))

    def test_long_properties_in_log(self):
        """Log includes the custom properties returned by the registered
        handlers.
        """
        wt = self.make_standard_commit('test_properties_in_log')
        def trivial_custom_prop_handler(revision):
            return {'test_prop':'test_value'}

        log.properties_handler_registry.register(
            'trivial_custom_prop_handler',
            trivial_custom_prop_handler)
        self.assertFormatterResult("""\
------------------------------------------------------------
revno: 1
test_prop: test_value
author: John Doe <jdoe@example.com>
committer: Lorem Ipsum <test@example.com>
branch nick: test_properties_in_log
timestamp: Tue 2005-11-22 00:00:00 +0000
message:
  add a
""",
            wt.branch, log.LongLogFormatter,
            formatter_kwargs=dict(levels=1))


class TestLineLogFormatter(TestCaseForLogFormatter):

    def test_line_log(self):
        """Line log should show revno

        bug #5162
        """
        wt = self.make_standard_commit('test-line-log',
                committer='Line-Log-Formatter Tester <test@line.log>',
                authors=[])
        self.assertFormatterResult("""\
1: Line-Log-Formatte... 2005-11-22 add a
""",
            wt.branch, log.LineLogFormatter)

    def test_trailing_newlines(self):
        wt = self.make_branch_and_tree('.')
        b = self.make_commits_with_trailing_newlines(wt)
        self.assertFormatterResult("""\
3: Joe Foo 2005-11-22 single line with trailing newline
2: Joe Foo 2005-11-22 multiline
1: Joe Foo 2005-11-22 simple log message
""",
            b, log.LineLogFormatter)

    def test_line_log_single_merge_revision(self):
        wt = self._prepare_tree_with_merges()
        revspec = revisionspec.RevisionSpec.from_string('1.1.1')
        rev = revspec.in_history(wt.branch)
        self.assertFormatterResult("""\
1.1.1: Joe Foo 2005-11-22 rev-merged
""",
            wt.branch, log.LineLogFormatter,
            show_log_kwargs=dict(start_revision=rev, end_revision=rev))

    def test_line_log_with_tags(self):
        wt = self._prepare_tree_with_merges(with_tags=True)
        self.assertFormatterResult("""\
3: Joe Foo 2005-11-22 {v1.0, v1.0rc1} rev-3
2: Joe Foo 2005-11-22 [merge] {v0.2} rev-2
1: Joe Foo 2005-11-22 rev-1
""",
            wt.branch, log.LineLogFormatter)


class TestLineLogFormatterWithMergeRevisions(TestCaseForLogFormatter):

    def test_line_merge_revs_log(self):
        """Line log should show revno

        bug #5162
        """
        wt = self.make_standard_commit('test-line-log',
                committer='Line-Log-Formatter Tester <test@line.log>',
                authors=[])
        self.assertFormatterResult("""\
1: Line-Log-Formatte... 2005-11-22 add a
""",
            wt.branch, log.LineLogFormatter)

    def test_line_merge_revs_log_single_merge_revision(self):
        wt = self._prepare_tree_with_merges()
        revspec = revisionspec.RevisionSpec.from_string('1.1.1')
        rev = revspec.in_history(wt.branch)
        self.assertFormatterResult("""\
1.1.1: Joe Foo 2005-11-22 rev-merged
""",
            wt.branch, log.LineLogFormatter,
            formatter_kwargs=dict(levels=0),
            show_log_kwargs=dict(start_revision=rev, end_revision=rev))

    def test_line_merge_revs_log_with_merges(self):
        wt = self._prepare_tree_with_merges()
        self.assertFormatterResult("""\
2: Joe Foo 2005-11-22 [merge] rev-2
  1.1.1: Joe Foo 2005-11-22 rev-merged
1: Joe Foo 2005-11-22 rev-1
""",
            wt.branch, log.LineLogFormatter,
            formatter_kwargs=dict(levels=0))


class TestGetViewRevisions(tests.TestCaseWithTransport, TestLogMixin):

    def _get_view_revisions(self, *args, **kwargs):
        return self.applyDeprecated(symbol_versioning.deprecated_in((2, 2, 0)),
                                    log.get_view_revisions, *args, **kwargs)

    def make_tree_with_commits(self):
        """Create a tree with well-known revision ids"""
        wt = self.make_branch_and_tree('tree1')
        self.wt_commit(wt, 'commit one', rev_id='1')
        self.wt_commit(wt, 'commit two', rev_id='2')
        self.wt_commit(wt, 'commit three', rev_id='3')
        mainline_revs = [None, '1', '2', '3']
        rev_nos = {'1': 1, '2': 2, '3': 3}
        return mainline_revs, rev_nos, wt

    def make_tree_with_merges(self):
        """Create a tree with well-known revision ids and a merge"""
        mainline_revs, rev_nos, wt = self.make_tree_with_commits()
        tree2 = wt.bzrdir.sprout('tree2').open_workingtree()
        self.wt_commit(tree2, 'four-a', rev_id='4a')
        wt.merge_from_branch(tree2.branch)
        self.wt_commit(wt, 'four-b', rev_id='4b')
        mainline_revs.append('4b')
        rev_nos['4b'] = 4
        # 4a: 3.1.1
        return mainline_revs, rev_nos, wt

    def make_branch_with_many_merges(self):
        """Create a tree with well-known revision ids"""
        builder = self.make_branch_builder('tree1')
        builder.start_series()
        builder.build_snapshot('1', None, [
            ('add', ('', 'TREE_ROOT', 'directory', '')),
            ('add', ('f', 'f-id', 'file', '1\n'))])
        builder.build_snapshot('2', ['1'], [])
        builder.build_snapshot('3a', ['2'], [
            ('modify', ('f-id', '1\n2\n3a\n'))])
        builder.build_snapshot('3b', ['2', '3a'], [
            ('modify', ('f-id', '1\n2\n3a\n'))])
        builder.build_snapshot('3c', ['2', '3b'], [
            ('modify', ('f-id', '1\n2\n3a\n'))])
        builder.build_snapshot('4a', ['3b'], [])
        builder.build_snapshot('4b', ['3c', '4a'], [])
        builder.finish_series()

        # 1
        # |
        # 2-.
        # |\ \
        # | | 3a
        # | |/
        # | 3b
        # |/|
        # 3c4a
        # |/
        # 4b

        mainline_revs = [None, '1', '2', '3c', '4b']
        rev_nos = {'1':1, '2':2, '3c': 3, '4b':4}
        full_rev_nos_for_reference = {
            '1': '1',
            '2': '2',
            '3a': '2.1.1', #first commit tree 3
            '3b': '2.2.1', # first commit tree 2
            '3c': '3', #merges 3b to main
            '4a': '2.2.2', # second commit tree 2
            '4b': '4', # merges 4a to main
            }
        return mainline_revs, rev_nos, builder.get_branch()

    def test_get_view_revisions_forward(self):
        """Test the get_view_revisions method"""
        mainline_revs, rev_nos, wt = self.make_tree_with_commits()
        wt.lock_read()
        self.addCleanup(wt.unlock)
        revisions = list(self._get_view_revisions(
                mainline_revs, rev_nos, wt.branch, 'forward'))
        self.assertEqual([('1', '1', 0), ('2', '2', 0), ('3', '3', 0)],
                         revisions)
        revisions2 = list(self._get_view_revisions(
                mainline_revs, rev_nos, wt.branch, 'forward',
                include_merges=False))
        self.assertEqual(revisions, revisions2)

    def test_get_view_revisions_reverse(self):
        """Test the get_view_revisions with reverse"""
        mainline_revs, rev_nos, wt = self.make_tree_with_commits()
        wt.lock_read()
        self.addCleanup(wt.unlock)
        revisions = list(self._get_view_revisions(
                mainline_revs, rev_nos, wt.branch, 'reverse'))
        self.assertEqual([('3', '3', 0), ('2', '2', 0), ('1', '1', 0), ],
                         revisions)
        revisions2 = list(self._get_view_revisions(
                mainline_revs, rev_nos, wt.branch, 'reverse',
                include_merges=False))
        self.assertEqual(revisions, revisions2)

    def test_get_view_revisions_merge(self):
        """Test get_view_revisions when there are merges"""
        mainline_revs, rev_nos, wt = self.make_tree_with_merges()
        wt.lock_read()
        self.addCleanup(wt.unlock)
        revisions = list(self._get_view_revisions(
                mainline_revs, rev_nos, wt.branch, 'forward'))
        self.assertEqual([('1', '1', 0), ('2', '2', 0), ('3', '3', 0),
                          ('4b', '4', 0), ('4a', '3.1.1', 1)],
                         revisions)
        revisions = list(self._get_view_revisions(
                mainline_revs, rev_nos, wt.branch, 'forward',
                include_merges=False))
        self.assertEqual([('1', '1', 0), ('2', '2', 0), ('3', '3', 0),
                          ('4b', '4', 0)],
                         revisions)

    def test_get_view_revisions_merge_reverse(self):
        """Test get_view_revisions in reverse when there are merges"""
        mainline_revs, rev_nos, wt = self.make_tree_with_merges()
        wt.lock_read()
        self.addCleanup(wt.unlock)
        revisions = list(self._get_view_revisions(
                mainline_revs, rev_nos, wt.branch, 'reverse'))
        self.assertEqual([('4b', '4', 0), ('4a', '3.1.1', 1),
                          ('3', '3', 0), ('2', '2', 0), ('1', '1', 0)],
                         revisions)
        revisions = list(self._get_view_revisions(
                mainline_revs, rev_nos, wt.branch, 'reverse',
                include_merges=False))
        self.assertEqual([('4b', '4', 0), ('3', '3', 0), ('2', '2', 0),
                          ('1', '1', 0)],
                         revisions)

    def test_get_view_revisions_merge2(self):
        """Test get_view_revisions when there are merges"""
        mainline_revs, rev_nos, b = self.make_branch_with_many_merges()
        b.lock_read()
        self.addCleanup(b.unlock)
        revisions = list(self._get_view_revisions(
                mainline_revs, rev_nos, b, 'forward'))
        expected = [('1', '1', 0), ('2', '2', 0), ('3c', '3', 0),
                    ('3b', '2.2.1', 1), ('3a', '2.1.1', 2), ('4b', '4', 0),
                    ('4a', '2.2.2', 1)]
        self.assertEqual(expected, revisions)
        revisions = list(self._get_view_revisions(
                mainline_revs, rev_nos, b, 'forward',
                include_merges=False))
        self.assertEqual([('1', '1', 0), ('2', '2', 0), ('3c', '3', 0),
                          ('4b', '4', 0)],
                         revisions)

    def test_file_id_for_range(self):
        mainline_revs, rev_nos, b = self.make_branch_with_many_merges()
        b.lock_read()
        self.addCleanup(b.unlock)

        def rev_from_rev_id(revid, branch):
            revspec = revisionspec.RevisionSpec.from_string('revid:%s' % revid)
            return revspec.in_history(branch)

        def view_revs(start_rev, end_rev, file_id, direction):
            revs = self.applyDeprecated(
                symbol_versioning.deprecated_in((2, 2, 0)),
                log.calculate_view_revisions,
                b,
                start_rev, # start_revision
                end_rev, # end_revision
                direction, # direction
                file_id, # specific_fileid
                True, # generate_merge_revisions
                )
            return revs

        rev_3a = rev_from_rev_id('3a', b)
        rev_4b = rev_from_rev_id('4b', b)
        self.assertEqual([('3c', '3', 0), ('3b', '2.2.1', 1),
                          ('3a', '2.1.1', 2)],
                          view_revs(rev_3a, rev_4b, 'f-id', 'reverse'))
        # Note: 3c still appears before 3a here because of depth-based sorting
        self.assertEqual([('3c', '3', 0), ('3b', '2.2.1', 1),
                          ('3a', '2.1.1', 2)],
                          view_revs(rev_3a, rev_4b, 'f-id', 'forward'))


class TestGetRevisionsTouchingFileID(tests.TestCaseWithTransport):

    def get_view_revisions(self, *args):
        return self.applyDeprecated(symbol_versioning.deprecated_in((2, 2, 0)),
                                    log.get_view_revisions, *args)

    def create_tree_with_single_merge(self):
        """Create a branch with a moderate layout.

        The revision graph looks like:

           A
           |\
           B C
           |/
           D

        In this graph, A introduced files f1 and f2 and f3.
        B modifies f1 and f3, and C modifies f2 and f3.
        D merges the changes from B and C and resolves the conflict for f3.
        """
        # TODO: jam 20070218 This seems like it could really be done
        #       with make_branch_and_memory_tree() if we could just
        #       create the content of those files.
        # TODO: jam 20070218 Another alternative is that we would really
        #       like to only create this tree 1 time for all tests that
        #       use it. Since 'log' only uses the tree in a readonly
        #       fashion, it seems a shame to regenerate an identical
        #       tree for each test.
        # TODO: vila 20100122 One way to address the shame above will be to
        #       create a memory tree during test parametrization and give a
        #       *copy* of this tree to each test. Copying a memory tree ought
        #       to be cheap, at least cheaper than creating them with such
        #       complex setups.
        tree = self.make_branch_and_tree('tree')
        tree.lock_write()
        self.addCleanup(tree.unlock)

        self.build_tree_contents([('tree/f1', 'A\n'),
                                  ('tree/f2', 'A\n'),
                                  ('tree/f3', 'A\n'),
                                 ])
        tree.add(['f1', 'f2', 'f3'], ['f1-id', 'f2-id', 'f3-id'])
        tree.commit('A', rev_id='A')

        self.build_tree_contents([('tree/f2', 'A\nC\n'),
                                  ('tree/f3', 'A\nC\n'),
                                 ])
        tree.commit('C', rev_id='C')
        # Revert back to A to build the other history.
        tree.set_last_revision('A')
        tree.branch.set_last_revision_info(1, 'A')
        self.build_tree_contents([('tree/f1', 'A\nB\n'),
                                  ('tree/f2', 'A\n'),
                                  ('tree/f3', 'A\nB\n'),
                                 ])
        tree.commit('B', rev_id='B')
        tree.set_parent_ids(['B', 'C'])
        self.build_tree_contents([('tree/f1', 'A\nB\n'),
                                  ('tree/f2', 'A\nC\n'),
                                  ('tree/f3', 'A\nB\nC\n'),
                                 ])
        tree.commit('D', rev_id='D')

        # Switch to a read lock for this tree.
        # We still have an addCleanup(tree.unlock) pending
        tree.unlock()
        tree.lock_read()
        return tree

    def check_delta(self, delta, **kw):
        """Check the filenames touched by a delta are as expected.

        Caller only have to pass in the list of files for each part, all
        unspecified parts are considered empty (and checked as such).
        """
        for n in 'added', 'removed', 'renamed', 'modified', 'unchanged':
            # By default we expect an empty list
            expected = kw.get(n, [])
            # strip out only the path components
            got = [x[0] for x in getattr(delta, n)]
            self.assertEqual(expected, got)

    def test_tree_with_single_merge(self):
        """Make sure the tree layout is correct."""
        tree = self.create_tree_with_single_merge()
        rev_A_tree = tree.branch.repository.revision_tree('A')
        rev_B_tree = tree.branch.repository.revision_tree('B')
        rev_C_tree = tree.branch.repository.revision_tree('C')
        rev_D_tree = tree.branch.repository.revision_tree('D')

        self.check_delta(rev_B_tree.changes_from(rev_A_tree),
                         modified=['f1', 'f3'])

        self.check_delta(rev_C_tree.changes_from(rev_A_tree),
                         modified=['f2', 'f3'])

        self.check_delta(rev_D_tree.changes_from(rev_B_tree),
                         modified=['f2', 'f3'])

        self.check_delta(rev_D_tree.changes_from(rev_C_tree),
                         modified=['f1', 'f3'])

    def assertAllRevisionsForFileID(self, tree, file_id, revisions):
        """Ensure _filter_revisions_touching_file_id returns the right values.

        Get the return value from _filter_revisions_touching_file_id and make
        sure they are correct.
        """
        # The api for _filter_revisions_touching_file_id is a little crazy.
        # So we do the setup here.
        mainline = tree.branch.revision_history()
        mainline.insert(0, None)
        revnos = dict((rev, idx+1) for idx, rev in enumerate(mainline))
        view_revs_iter = self.get_view_revisions(
            mainline, revnos, tree.branch, 'reverse', True)
        actual_revs = log._filter_revisions_touching_file_id(
            tree.branch, file_id, list(view_revs_iter))
        self.assertEqual(revisions, [r for r, revno, depth in actual_revs])

    def test_file_id_f1(self):
        tree = self.create_tree_with_single_merge()
        # f1 should be marked as modified by revisions A and B
        self.assertAllRevisionsForFileID(tree, 'f1-id', ['B', 'A'])

    def test_file_id_f2(self):
        tree = self.create_tree_with_single_merge()
        # f2 should be marked as modified by revisions A, C, and D
        # because D merged the changes from C.
        self.assertAllRevisionsForFileID(tree, 'f2-id', ['D', 'C', 'A'])

    def test_file_id_f3(self):
        tree = self.create_tree_with_single_merge()
        # f3 should be marked as modified by revisions A, B, C, and D
        self.assertAllRevisionsForFileID(tree, 'f3-id', ['D', 'C', 'B', 'A'])

    def test_file_id_with_ghosts(self):
        # This is testing bug #209948, where having a ghost would cause
        # _filter_revisions_touching_file_id() to fail.
        tree = self.create_tree_with_single_merge()
        # We need to add a revision, so switch back to a write-locked tree
        # (still a single addCleanup(tree.unlock) pending).
        tree.unlock()
        tree.lock_write()
        first_parent = tree.last_revision()
        tree.set_parent_ids([first_parent, 'ghost-revision-id'])
        self.build_tree_contents([('tree/f1', 'A\nB\nXX\n')])
        tree.commit('commit with a ghost', rev_id='XX')
        self.assertAllRevisionsForFileID(tree, 'f1-id', ['XX', 'B', 'A'])
        self.assertAllRevisionsForFileID(tree, 'f2-id', ['D', 'C', 'A'])

    def test_unknown_file_id(self):
        tree = self.create_tree_with_single_merge()
        self.assertAllRevisionsForFileID(tree, 'unknown', [])

    def test_empty_branch_unknown_file_id(self):
        tree = self.make_branch_and_tree('tree')
        self.assertAllRevisionsForFileID(tree, 'unknown', [])


class TestShowChangedRevisions(tests.TestCaseWithTransport):

    def test_show_changed_revisions_verbose(self):
        tree = self.make_branch_and_tree('tree_a')
        self.build_tree(['tree_a/foo'])
        tree.add('foo')
        tree.commit('bar', rev_id='bar-id')
        s = self.make_utf8_encoded_stringio()
        log.show_changed_revisions(tree.branch, [], ['bar-id'], s)
        self.assertContainsRe(s.getvalue(), 'bar')
        self.assertNotContainsRe(s.getvalue(), 'foo')


class TestLogFormatter(tests.TestCase):

    def setUp(self):
        super(TestLogFormatter, self).setUp()
        self.rev = revision.Revision('a-id')
        self.lf = log.LogFormatter(None)

    def test_short_committer(self):
        def assertCommitter(expected, committer):
            self.rev.committer = committer
            self.assertEqual(expected, self.lf.short_committer(self.rev))

        assertCommitter('John Doe', 'John Doe <jdoe@example.com>')
        assertCommitter('John Smith', 'John Smith <jsmith@example.com>')
        assertCommitter('John Smith', 'John Smith')
        assertCommitter('jsmith@example.com', 'jsmith@example.com')
        assertCommitter('jsmith@example.com', '<jsmith@example.com>')
        assertCommitter('John Smith', 'John Smith jsmith@example.com')

    def test_short_author(self):
        def assertAuthor(expected, author):
            self.rev.properties['author'] = author
            self.assertEqual(expected, self.lf.short_author(self.rev))

        assertAuthor('John Smith', 'John Smith <jsmith@example.com>')
        assertAuthor('John Smith', 'John Smith')
        assertAuthor('jsmith@example.com', 'jsmith@example.com')
        assertAuthor('jsmith@example.com', '<jsmith@example.com>')
        assertAuthor('John Smith', 'John Smith jsmith@example.com')

    def test_short_author_from_committer(self):
        self.rev.committer = 'John Doe <jdoe@example.com>'
        self.assertEqual('John Doe', self.lf.short_author(self.rev))

    def test_short_author_from_authors(self):
        self.rev.properties['authors'] = ('John Smith <jsmith@example.com>\n'
                                          'Jane Rey <jrey@example.com>')
        self.assertEqual('John Smith', self.lf.short_author(self.rev))


class TestReverseByDepth(tests.TestCase):
    """Test reverse_by_depth behavior.

    This is used to present revisions in forward (oldest first) order in a nice
    layout.

    The tests use lighter revision description to ease reading.
    """

    def assertReversed(self, forward, backward):
        # Transform the descriptions to suit the API: tests use (revno, depth),
        # while the API expects (revid, revno, depth)
        def complete_revisions(l):
            """Transform the description to suit the API.

            Tests use (revno, depth) whil the API expects (revid, revno, depth).
            Since the revid is arbitrary, we just duplicate revno
            """
            return [ (r, r, d) for r, d in l]
        forward = complete_revisions(forward)
        backward= complete_revisions(backward)
        self.assertEqual(forward, log.reverse_by_depth(backward))


    def test_mainline_revisions(self):
        self.assertReversed([( '1', 0), ('2', 0)],
                            [('2', 0), ('1', 0)])

    def test_merged_revisions(self):
        self.assertReversed([('1', 0), ('2', 0), ('2.2', 1), ('2.1', 1),],
                            [('2', 0), ('2.1', 1), ('2.2', 1), ('1', 0),])
    def test_shifted_merged_revisions(self):
        """Test irregular layout.

        Requesting revisions touching a file can produce "holes" in the depths.
        """
        self.assertReversed([('1', 0), ('2', 0), ('1.1', 2), ('1.2', 2),],
                            [('2', 0), ('1.2', 2), ('1.1', 2), ('1', 0),])

    def test_merged_without_child_revisions(self):
        """Test irregular layout.

        Revision ranges can produce "holes" in the depths.
        """
        # When a revision of higher depth doesn't follow one of lower depth, we
        # assume a lower depth one is virtually there
        self.assertReversed([('1', 2), ('2', 2), ('3', 3), ('4', 4)],
                            [('4', 4), ('3', 3), ('2', 2), ('1', 2),])
        # So we get the same order after reversing below even if the original
        # revisions are not in the same order.
        self.assertReversed([('1', 2), ('2', 2), ('3', 3), ('4', 4)],
                            [('3', 3), ('4', 4), ('2', 2), ('1', 2),])


class TestHistoryChange(tests.TestCaseWithTransport):

    def setup_a_tree(self):
        tree = self.make_branch_and_tree('tree')
        tree.lock_write()
        self.addCleanup(tree.unlock)
        tree.commit('1a', rev_id='1a')
        tree.commit('2a', rev_id='2a')
        tree.commit('3a', rev_id='3a')
        return tree

    def setup_ab_tree(self):
        tree = self.setup_a_tree()
        tree.set_last_revision('1a')
        tree.branch.set_last_revision_info(1, '1a')
        tree.commit('2b', rev_id='2b')
        tree.commit('3b', rev_id='3b')
        return tree

    def setup_ac_tree(self):
        tree = self.setup_a_tree()
        tree.set_last_revision(revision.NULL_REVISION)
        tree.branch.set_last_revision_info(0, revision.NULL_REVISION)
        tree.commit('1c', rev_id='1c')
        tree.commit('2c', rev_id='2c')
        tree.commit('3c', rev_id='3c')
        return tree

    def test_all_new(self):
        tree = self.setup_ab_tree()
        old, new = log.get_history_change('1a', '3a', tree.branch.repository)
        self.assertEqual([], old)
        self.assertEqual(['2a', '3a'], new)

    def test_all_old(self):
        tree = self.setup_ab_tree()
        old, new = log.get_history_change('3a', '1a', tree.branch.repository)
        self.assertEqual([], new)
        self.assertEqual(['2a', '3a'], old)

    def test_null_old(self):
        tree = self.setup_ab_tree()
        old, new = log.get_history_change(revision.NULL_REVISION,
                                          '3a', tree.branch.repository)
        self.assertEqual([], old)
        self.assertEqual(['1a', '2a', '3a'], new)

    def test_null_new(self):
        tree = self.setup_ab_tree()
        old, new = log.get_history_change('3a', revision.NULL_REVISION,
                                          tree.branch.repository)
        self.assertEqual([], new)
        self.assertEqual(['1a', '2a', '3a'], old)

    def test_diverged(self):
        tree = self.setup_ab_tree()
        old, new = log.get_history_change('3a', '3b', tree.branch.repository)
        self.assertEqual(old, ['2a', '3a'])
        self.assertEqual(new, ['2b', '3b'])

    def test_unrelated(self):
        tree = self.setup_ac_tree()
        old, new = log.get_history_change('3a', '3c', tree.branch.repository)
        self.assertEqual(old, ['1a', '2a', '3a'])
        self.assertEqual(new, ['1c', '2c', '3c'])

    def test_show_branch_change(self):
        tree = self.setup_ab_tree()
        s = StringIO()
        log.show_branch_change(tree.branch, s, 3, '3a')
        self.assertContainsRe(s.getvalue(),
            '[*]{60}\nRemoved Revisions:\n(.|\n)*2a(.|\n)*3a(.|\n)*'
            '[*]{60}\n\nAdded Revisions:\n(.|\n)*2b(.|\n)*3b')

    def test_show_branch_change_no_change(self):
        tree = self.setup_ab_tree()
        s = StringIO()
        log.show_branch_change(tree.branch, s, 3, '3b')
        self.assertEqual(s.getvalue(),
            'Nothing seems to have changed\n')

    def test_show_branch_change_no_old(self):
        tree = self.setup_ab_tree()
        s = StringIO()
        log.show_branch_change(tree.branch, s, 2, '2b')
        self.assertContainsRe(s.getvalue(), 'Added Revisions:')
        self.assertNotContainsRe(s.getvalue(), 'Removed Revisions:')

    def test_show_branch_change_no_new(self):
        tree = self.setup_ab_tree()
        tree.branch.set_last_revision_info(2, '2b')
        s = StringIO()
        log.show_branch_change(tree.branch, s, 3, '3b')
        self.assertContainsRe(s.getvalue(), 'Removed Revisions:')
        self.assertNotContainsRe(s.getvalue(), 'Added Revisions:')



class TestLogWithBugs(TestCaseForLogFormatter, TestLogMixin):

    def setUp(self):
        TestCaseForLogFormatter.setUp(self)
        log.properties_handler_registry.register(
            'bugs_properties_handler',
            log._bugs_properties_handler)

    def make_commits_with_bugs(self):
        """Helper method for LogFormatter tests"""
        tree = self.make_branch_and_tree(u'.')
        self.build_tree(['a', 'b'])
        tree.add('a')
        self.wt_commit(tree, 'simple log message', rev_id='a1',
                       revprops={'bugs': 'test://bug/id fixed'})
        tree.add('b')
        self.wt_commit(tree, 'multiline\nlog\nmessage\n', rev_id='a2',
                       authors=['Joe Bar <joe@bar.com>'],
                       revprops={'bugs': 'test://bug/id fixed\n'
                                 'test://bug/2 fixed'})
        return tree


    def test_long_bugs(self):
        tree = self.make_commits_with_bugs()
        self.assertFormatterResult("""\
------------------------------------------------------------
revno: 2
fixes bug(s): test://bug/id test://bug/2
author: Joe Bar <joe@bar.com>
committer: Joe Foo <joe@foo.com>
branch nick: work
timestamp: Tue 2005-11-22 00:00:01 +0000
message:
  multiline
  log
  message
------------------------------------------------------------
revno: 1
fixes bug(s): test://bug/id
committer: Joe Foo <joe@foo.com>
branch nick: work
timestamp: Tue 2005-11-22 00:00:00 +0000
message:
  simple log message
""",
            tree.branch, log.LongLogFormatter)

    def test_short_bugs(self):
        tree = self.make_commits_with_bugs()
        self.assertFormatterResult("""\
    2 Joe Bar\t2005-11-22
      fixes bug(s): test://bug/id test://bug/2
      multiline
      log
      message

    1 Joe Foo\t2005-11-22
      fixes bug(s): test://bug/id
      simple log message

""",
            tree.branch, log.ShortLogFormatter)

    def test_wrong_bugs_property(self):
        tree = self.make_branch_and_tree(u'.')
        self.build_tree(['foo'])
        self.wt_commit(tree, 'simple log message', rev_id='a1',
                       revprops={'bugs': 'test://bug/id invalid_value'})
        self.assertFormatterResult("""\
    1 Joe Foo\t2005-11-22
      simple log message

""",
            tree.branch, log.ShortLogFormatter)

    def test_bugs_handler_present(self):
        self.properties_handler_registry.get('bugs_properties_handler')