~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
# Copyright (C) 2005, 2006, 2007, 2009 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


"""Black-box tests for bzr log."""

import os, re

from bzrlib import osutils
from bzrlib.tests.blackbox import ExternalBase
from bzrlib.tests import KnownFailure, TestCaseInTempDir, TestCaseWithTransport
from bzrlib.tests.test_log import (
    normalize_log,
    )
from bzrlib.tests import test_log


class TestCaseWithoutPropsHandler(ExternalBase,
                                  test_log.TestCaseWithoutPropsHandler):
    pass


class TestLog(ExternalBase):

    def _prepare(self, path='.', format=None):
        tree = self.make_branch_and_tree(path, format=format)
        self.build_tree(
            [path + '/hello.txt', path + '/goodbye.txt', path + '/meep.txt'])
        tree.add('hello.txt')
        tree.commit(message='message1')
        tree.add('goodbye.txt')
        tree.commit(message='message2')
        tree.add('meep.txt')
        tree.commit(message='message3')
        self.full_log = self.run_bzr(["log", path])[0]
        return tree

    def test_log_null_end_revspec(self):
        self._prepare()
        self.assertTrue('revno: 1\n' in self.full_log)
        self.assertTrue('revno: 2\n' in self.full_log)
        self.assertTrue('revno: 3\n' in self.full_log)
        self.assertTrue('message:\n  message1\n' in self.full_log)
        self.assertTrue('message:\n  message2\n' in self.full_log)
        self.assertTrue('message:\n  message3\n' in self.full_log)

        log = self.run_bzr("log -r 1..")[0]
        self.assertEqualDiff(log, self.full_log)

    def test_log_null_begin_revspec(self):
        self._prepare()
        log = self.run_bzr("log -r ..3")[0]
        self.assertEqualDiff(self.full_log, log)

    def test_log_null_both_revspecs(self):
        self._prepare()
        log = self.run_bzr("log -r ..")[0]
        self.assertEqualDiff(self.full_log, log)

    def test_log_zero_revspec(self):
        self._prepare()
        self.run_bzr_error('bzr: ERROR: Logging revision 0 is invalid.',
                           ['log', '-r0'])

    def test_log_zero_begin_revspec(self):
        self._prepare()
        self.run_bzr_error('bzr: ERROR: Logging revision 0 is invalid.',
                           ['log', '-r0..2'])

    def test_log_zero_end_revspec(self):
        self._prepare()
        self.run_bzr_error('bzr: ERROR: Logging revision 0 is invalid.',
                           ['log', '-r-2..0'])

    def test_log_unsupported_timezone(self):
        self._prepare()
        self.run_bzr_error('bzr: ERROR: Unsupported timezone format "foo", '
                           'options are "utc", "original", "local".',
                           ['log', '--timezone', 'foo'])

    def test_log_negative_begin_revspec_full_log(self):
        self._prepare()
        log = self.run_bzr("log -r -3..")[0]
        self.assertEqualDiff(self.full_log, log)

    def test_log_negative_both_revspec_full_log(self):
        self._prepare()
        log = self.run_bzr("log -r -3..-1")[0]
        self.assertEqualDiff(self.full_log, log)

    def test_log_negative_both_revspec_partial(self):
        self._prepare()
        log = self.run_bzr("log -r -3..-2")[0]
        self.assertTrue('revno: 1\n' in log)
        self.assertTrue('revno: 2\n' in log)
        self.assertTrue('revno: 3\n' not in log)

    def test_log_negative_begin_revspec(self):
        self._prepare()
        log = self.run_bzr("log -r -2..")[0]
        self.assertTrue('revno: 1\n' not in log)
        self.assertTrue('revno: 2\n' in log)
        self.assertTrue('revno: 3\n' in log)

    def test_log_positive_revspecs(self):
        self._prepare()
        log = self.run_bzr("log -r 1..3")[0]
        self.assertEqualDiff(self.full_log, log)

    def test_log_reversed_revspecs(self):
        self._prepare()
        self.run_bzr_error(('bzr: ERROR: Start revision must be older than '
                            'the end revision.\n',),
                           ['log', '-r3..1'])

    def test_log_revno_n_path(self):
        self._prepare(path='branch1')
        self._prepare(path='branch2')
        log = self.run_bzr("log -r revno:2:branch1..revno:3:branch2",
                          retcode=3)[0]
        log = self.run_bzr("log -r revno:1:branch2..revno:3:branch2")[0]
        self.assertEqualDiff(self.full_log, log)
        log = self.run_bzr("log -r revno:1:branch2")[0]
        self.assertTrue('revno: 1\n' in log)
        self.assertTrue('revno: 2\n' not in log)
        self.assertTrue('branch nick: branch2\n' in log)
        self.assertTrue('branch nick: branch1\n' not in log)

    def test_log_nonexistent_revno(self):
        self._prepare()
        (out, err) = self.run_bzr_error(args="log -r 1234",
            error_regexes=["bzr: ERROR: Requested revision: '1234' "
                "does not exist in branch:"])

    def test_log_nonexistent_dotted_revno(self):
        self._prepare()
        (out, err) = self.run_bzr_error(args="log -r 123.123",
            error_regexes=["bzr: ERROR: Requested revision: '123.123' "
                "does not exist in branch:"])

    def test_log_change_revno(self):
        self._prepare()
        expected_log = self.run_bzr("log -r 1")[0]
        log = self.run_bzr("log -c 1")[0]
        self.assertEqualDiff(expected_log, log)

    def test_log_change_nonexistent_revno(self):
        self._prepare()
        (out, err) = self.run_bzr_error(args="log -c 1234",
            error_regexes=["bzr: ERROR: Requested revision: '1234' "
                "does not exist in branch:"])

    def test_log_change_nonexistent_dotted_revno(self):
        self._prepare()
        (out, err) = self.run_bzr_error(args="log -c 123.123",
            error_regexes=["bzr: ERROR: Requested revision: '123.123' "
                "does not exist in branch:"])

    def test_log_change_single_revno(self):
        self._prepare()
        self.run_bzr_error('bzr: ERROR: Option --change does not'
                           ' accept revision ranges',
                           ['log', '--change', '2..3'])

    def test_log_change_incompatible_with_revision(self):
        self._prepare()
        self.run_bzr_error('bzr: ERROR: --revision and --change'
                           ' are mutually exclusive',
                           ['log', '--change', '2', '--revision', '3'])

    def test_log_nonexistent_file(self):
        # files that don't exist in either the basis tree or working tree
        # should give an error
        wt = self.make_branch_and_tree('.')
        out, err = self.run_bzr('log does-not-exist', retcode=3)
        self.assertContainsRe(
            err, 'Path unknown at end or start of revision range: does-not-exist')

    def test_log_with_tags(self):
        tree = self._prepare(format='dirstate-tags')
        branch = tree.branch
        branch.tags.set_tag('tag1', branch.get_rev_id(1))
        branch.tags.set_tag('tag1.1', branch.get_rev_id(1))
        branch.tags.set_tag('tag3', branch.last_revision())

        log = self.run_bzr("log -r-1")[0]
        self.assertTrue('tags: tag3' in log)

        log = self.run_bzr("log -r1")[0]
        # I guess that we can't know the order of tags in the output
        # since dicts are unordered, need to check both possibilities
        self.assertContainsRe(log, r'tags: (tag1, tag1\.1|tag1\.1, tag1)')

    def test_merged_log_with_tags(self):
        branch1_tree = self._prepare(path='branch1', format='dirstate-tags')
        branch1 = branch1_tree.branch
        branch2_tree = branch1_tree.bzrdir.sprout('branch2').open_workingtree()
        branch1_tree.commit(message='foobar', allow_pointless=True)
        branch1.tags.set_tag('tag1', branch1.last_revision())
        os.chdir('branch2')
        self.run_bzr('merge ../branch1') # tags don't propagate otherwise
        branch2_tree.commit(message='merge branch 1')
        log = self.run_bzr("log -n0 -r-1")[0]
        self.assertContainsRe(log, r'    tags: tag1')
        log = self.run_bzr("log -n0 -r3.1.1")[0]
        self.assertContainsRe(log, r'tags: tag1')

    def test_log_limit(self):
        tree = self.make_branch_and_tree('.')
        # We want more commits than our batch size starts at
        for pos in range(10):
            tree.commit("%s" % pos)
        log = self.run_bzr("log --limit 2")[0]
        self.assertNotContainsRe(log, r'revno: 1\n')
        self.assertNotContainsRe(log, r'revno: 2\n')
        self.assertNotContainsRe(log, r'revno: 3\n')
        self.assertNotContainsRe(log, r'revno: 4\n')
        self.assertNotContainsRe(log, r'revno: 5\n')
        self.assertNotContainsRe(log, r'revno: 6\n')
        self.assertNotContainsRe(log, r'revno: 7\n')
        self.assertNotContainsRe(log, r'revno: 8\n')
        self.assertContainsRe(log, r'revno: 9\n')
        self.assertContainsRe(log, r'revno: 10\n')

    def test_log_limit_short(self):
        self._prepare()
        log = self.run_bzr("log -l 2")[0]
        self.assertNotContainsRe(log, r'revno: 1\n')
        self.assertContainsRe(log, r'revno: 2\n')
        self.assertContainsRe(log, r'revno: 3\n')

    def test_log_bad_message_re(self):
        """Bad --message argument gives a sensible message
        
        See https://bugs.launchpad.net/bzr/+bug/251352
        """
        self._prepare()
        out, err = self.run_bzr(['log', '-m', '*'], retcode=3)
        self.assertEqual("bzr: ERROR: Invalid regular expression"
            " in log message filter"
            ": '*'"
            ": nothing to repeat\n", err)
        self.assertEqual('', out)


class TestLogVerbose(TestCaseWithTransport):

    def setUp(self):
        super(TestLogVerbose, self).setUp()
        tree = self.make_branch_and_tree('.')
        self.build_tree(['hello.txt'])
        tree.add('hello.txt')
        tree.commit(message='message1')

    def assertUseShortDeltaFormat(self, cmd):
        log = self.run_bzr(cmd)[0]
        # Check that we use the short status format
        self.assertContainsRe(log, '(?m)^\s*A  hello.txt$')
        self.assertNotContainsRe(log, '(?m)^\s*added:$')

    def assertUseLongDeltaFormat(self, cmd):
        log = self.run_bzr(cmd)[0]
        # Check that we use the long status format
        self.assertNotContainsRe(log, '(?m)^\s*A  hello.txt$')
        self.assertContainsRe(log, '(?m)^\s*added:$')

    def test_log_short_verbose(self):
        self.assertUseShortDeltaFormat(['log', '--short', '-v'])

    def test_log_short_verbose_verbose(self):
        self.assertUseLongDeltaFormat(['log', '--short', '-vv'])

    def test_log_long_verbose(self):
        # Check that we use the long status format, ignoring the verbosity
        # level
        self.assertUseLongDeltaFormat(['log', '--long', '-v'])

    def test_log_long_verbose_verbose(self):
        # Check that we use the long status format, ignoring the verbosity
        # level
        self.assertUseLongDeltaFormat(['log', '--long', '-vv'])


class TestLogMerges(TestCaseWithoutPropsHandler):

    def _prepare(self):
        parent_tree = self.make_branch_and_tree('parent')
        parent_tree.commit(message='first post', allow_pointless=True)
        child_tree = parent_tree.bzrdir.sprout('child').open_workingtree()
        child_tree.commit(message='branch 1', allow_pointless=True)
        smaller_tree = \
                child_tree.bzrdir.sprout('smallerchild').open_workingtree()
        smaller_tree.commit(message='branch 2', allow_pointless=True)
        child_tree.merge_from_branch(smaller_tree.branch)
        child_tree.commit(message='merge branch 2')
        parent_tree.merge_from_branch(child_tree.branch)
        parent_tree.commit(message='merge branch 1')
        os.chdir('parent')

    def _prepare_short(self):
        parent_tree = self.make_branch_and_tree('parent')
        parent_tree.commit(message='first post',
            timestamp=1132586700, timezone=36000,
            committer='Joe Foo <joe@foo.com>')
        child_tree = parent_tree.bzrdir.sprout('child').open_workingtree()
        child_tree.commit(message='branch 1',
            timestamp=1132586800, timezone=36000,
            committer='Joe Foo <joe@foo.com>')
        smaller_tree = \
                child_tree.bzrdir.sprout('smallerchild').open_workingtree()
        smaller_tree.commit(message='branch 2',
            timestamp=1132586900, timezone=36000,
            committer='Joe Foo <joe@foo.com>')
        child_tree.merge_from_branch(smaller_tree.branch)
        child_tree.commit(message='merge branch 2',
            timestamp=1132587000, timezone=36000,
            committer='Joe Foo <joe@foo.com>')
        parent_tree.merge_from_branch(child_tree.branch)
        parent_tree.commit(message='merge branch 1',
            timestamp=1132587100, timezone=36000,
            committer='Joe Foo <joe@foo.com>')
        os.chdir('parent')

    def test_merges_are_indented_by_level(self):
        self._prepare()
        out,err = self.run_bzr('log -n0')
        self.assertEqual('', err)
        log = normalize_log(out)
        self.assertEqualDiff(log, """\
------------------------------------------------------------
revno: 2 [merge]
committer: Lorem Ipsum <test@example.com>
branch nick: parent
timestamp: Just now
message:
  merge branch 1
    ------------------------------------------------------------
    revno: 1.1.2 [merge]
    committer: Lorem Ipsum <test@example.com>
    branch nick: child
    timestamp: Just now
    message:
      merge branch 2
        ------------------------------------------------------------
        revno: 1.2.1
        committer: Lorem Ipsum <test@example.com>
        branch nick: smallerchild
        timestamp: Just now
        message:
          branch 2
    ------------------------------------------------------------
    revno: 1.1.1
    committer: Lorem Ipsum <test@example.com>
    branch nick: child
    timestamp: Just now
    message:
      branch 1
------------------------------------------------------------
revno: 1
committer: Lorem Ipsum <test@example.com>
branch nick: parent
timestamp: Just now
message:
  first post
""")

    def test_force_merge_revisions_off(self):
        self._prepare()
        out,err = self.run_bzr('log --long -n1')
        self.assertEqual('', err)
        log = normalize_log(out)
        self.assertEqualDiff(log, """\
------------------------------------------------------------
revno: 2 [merge]
committer: Lorem Ipsum <test@example.com>
branch nick: parent
timestamp: Just now
message:
  merge branch 1
------------------------------------------------------------
revno: 1
committer: Lorem Ipsum <test@example.com>
branch nick: parent
timestamp: Just now
message:
  first post
------------------------------------------------------------
Use --levels 0 (or -n0) to see merged revisions.
""")

    def test_force_merge_revisions_on(self):
        self._prepare_short()
        out,err = self.run_bzr('log --short -n0')
        self.assertEqual('', err)
        log = normalize_log(out)
        self.assertEqualDiff(log, """\
    2 Joe Foo\t2005-11-22 [merge]
      merge branch 1

          1.1.2 Joe Foo\t2005-11-22 [merge]
                merge branch 2

              1.2.1 Joe Foo\t2005-11-22
                    branch 2

          1.1.1 Joe Foo\t2005-11-22
                branch 1

    1 Joe Foo\t2005-11-22
      first post

""")

    def test_force_merge_revisions_N(self):
        self._prepare_short()
        out,err = self.run_bzr('log --short -n2')
        self.assertEqual('', err)
        log = normalize_log(out)
        self.assertEqualDiff(log, """\
    2 Joe Foo\t2005-11-22 [merge]
      merge branch 1

          1.1.2 Joe Foo\t2005-11-22 [merge]
                merge branch 2

          1.1.1 Joe Foo\t2005-11-22
                branch 1

    1 Joe Foo\t2005-11-22
      first post

""")

    def test_merges_single_merge_rev(self):
        self._prepare()
        out,err = self.run_bzr('log -n0 -r1.1.2')
        self.assertEqual('', err)
        log = normalize_log(out)
        self.assertEqualDiff(log, """\
------------------------------------------------------------
revno: 1.1.2 [merge]
committer: Lorem Ipsum <test@example.com>
branch nick: child
timestamp: Just now
message:
  merge branch 2
    ------------------------------------------------------------
    revno: 1.2.1
    committer: Lorem Ipsum <test@example.com>
    branch nick: smallerchild
    timestamp: Just now
    message:
      branch 2
""")

    def test_merges_partial_range(self):
        self._prepare()
        out, err = self.run_bzr('log -n0 -r1.1.1..1.1.2')
        self.assertEqual('', err)
        log = normalize_log(out)
        self.assertEqualDiff(log, """\
------------------------------------------------------------
revno: 1.1.2 [merge]
committer: Lorem Ipsum <test@example.com>
branch nick: child
timestamp: Just now
message:
  merge branch 2
    ------------------------------------------------------------
    revno: 1.2.1
    committer: Lorem Ipsum <test@example.com>
    branch nick: smallerchild
    timestamp: Just now
    message:
      branch 2
------------------------------------------------------------
revno: 1.1.1
committer: Lorem Ipsum <test@example.com>
branch nick: child
timestamp: Just now
message:
  branch 1
""")


def subst_dates(string):
    """Replace date strings with constant values."""
    return re.sub(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} [-\+]\d{4}',
                  'YYYY-MM-DD HH:MM:SS +ZZZZ', string)


class TestLogDiff(TestCaseWithoutPropsHandler):

    def _prepare(self):
        parent_tree = self.make_branch_and_tree('parent')
        self.build_tree(['parent/file1', 'parent/file2'])
        parent_tree.add('file1')
        parent_tree.add('file2')
        parent_tree.commit(message='first post',
            timestamp=1132586655, timezone=36000,
            committer='Lorem Ipsum <test@example.com>')
        child_tree = parent_tree.bzrdir.sprout('child').open_workingtree()
        self.build_tree_contents([('child/file2', 'hello\n')])
        child_tree.commit(message='branch 1',
            timestamp=1132586700, timezone=36000,
            committer='Lorem Ipsum <test@example.com>')
        parent_tree.merge_from_branch(child_tree.branch)
        parent_tree.commit(message='merge branch 1',
            timestamp=1132586800, timezone=36000,
            committer='Lorem Ipsum <test@example.com>')
        os.chdir('parent')

    def test_log_show_diff_long_with_merges(self):
        self._prepare()
        out,err = self.run_bzr('log -p -n0')
        self.assertEqual('', err)
        log = normalize_log(out)
        self.assertEqualDiff(subst_dates(log), """\
------------------------------------------------------------
revno: 2 [merge]
committer: Lorem Ipsum <test@example.com>
branch nick: parent
timestamp: Just now
message:
  merge branch 1
diff:
=== modified file 'file2'
--- file2\tYYYY-MM-DD HH:MM:SS +ZZZZ
+++ file2\tYYYY-MM-DD HH:MM:SS +ZZZZ
@@ -1,1 +1,1 @@
-contents of parent/file2
+hello
    ------------------------------------------------------------
    revno: 1.1.1
    committer: Lorem Ipsum <test@example.com>
    branch nick: child
    timestamp: Just now
    message:
      branch 1
    diff:
    === modified file 'file2'
    --- file2\tYYYY-MM-DD HH:MM:SS +ZZZZ
    +++ file2\tYYYY-MM-DD HH:MM:SS +ZZZZ
    @@ -1,1 +1,1 @@
    -contents of parent/file2
    +hello
------------------------------------------------------------
revno: 1
committer: Lorem Ipsum <test@example.com>
branch nick: parent
timestamp: Just now
message:
  first post
diff:
=== added file 'file1'
--- file1\tYYYY-MM-DD HH:MM:SS +ZZZZ
+++ file1\tYYYY-MM-DD HH:MM:SS +ZZZZ
@@ -0,0 +1,1 @@
+contents of parent/file1

=== added file 'file2'
--- file2\tYYYY-MM-DD HH:MM:SS +ZZZZ
+++ file2\tYYYY-MM-DD HH:MM:SS +ZZZZ
@@ -0,0 +1,1 @@
+contents of parent/file2
""")

    def test_log_show_diff_short(self):
        self._prepare()
        out,err = self.run_bzr('log -p --short')
        self.assertEqual('', err)
        log = normalize_log(out)
        self.assertEqualDiff(subst_dates(log), """\
    2 Lorem Ipsum\t2005-11-22 [merge]
      merge branch 1
      === modified file 'file2'
      --- file2\tYYYY-MM-DD HH:MM:SS +ZZZZ
      +++ file2\tYYYY-MM-DD HH:MM:SS +ZZZZ
      @@ -1,1 +1,1 @@
      -contents of parent/file2
      +hello

    1 Lorem Ipsum\t2005-11-22
      first post
      === added file 'file1'
      --- file1\tYYYY-MM-DD HH:MM:SS +ZZZZ
      +++ file1\tYYYY-MM-DD HH:MM:SS +ZZZZ
      @@ -0,0 +1,1 @@
      +contents of parent/file1
\x20\x20\x20\x20\x20\x20
      === added file 'file2'
      --- file2\tYYYY-MM-DD HH:MM:SS +ZZZZ
      +++ file2\tYYYY-MM-DD HH:MM:SS +ZZZZ
      @@ -0,0 +1,1 @@
      +contents of parent/file2

Use --levels 0 (or -n0) to see merged revisions.
""")

    def test_log_show_diff_line(self):
        self._prepare()
        out,err = self.run_bzr('log -p --line')
        self.assertEqual('', err)
        log = normalize_log(out)
        # Not supported by this formatter so expect plain output
        self.assertEqualDiff(subst_dates(log), """\
2: Lorem Ipsum 2005-11-22 [merge] merge branch 1
1: Lorem Ipsum 2005-11-22 first post
""")

    def test_log_show_diff_file(self):
        """Only the diffs for the given file are to be shown"""
        self._prepare()
        out,err = self.run_bzr('log -p --short file2')
        self.assertEqual('', err)
        log = normalize_log(out)
        self.assertEqualDiff(subst_dates(log), """\
    2 Lorem Ipsum\t2005-11-22 [merge]
      merge branch 1
      === modified file 'file2'
      --- file2\tYYYY-MM-DD HH:MM:SS +ZZZZ
      +++ file2\tYYYY-MM-DD HH:MM:SS +ZZZZ
      @@ -1,1 +1,1 @@
      -contents of parent/file2
      +hello

    1 Lorem Ipsum\t2005-11-22
      first post
      === added file 'file2'
      --- file2\tYYYY-MM-DD HH:MM:SS +ZZZZ
      +++ file2\tYYYY-MM-DD HH:MM:SS +ZZZZ
      @@ -0,0 +1,1 @@
      +contents of parent/file2

Use --levels 0 (or -n0) to see merged revisions.
""")
        out,err = self.run_bzr('log -p --short file1')
        self.assertEqual('', err)
        log = normalize_log(out)
        self.assertEqualDiff(subst_dates(log), """\
    1 Lorem Ipsum\t2005-11-22
      first post
      === added file 'file1'
      --- file1\tYYYY-MM-DD HH:MM:SS +ZZZZ
      +++ file1\tYYYY-MM-DD HH:MM:SS +ZZZZ
      @@ -0,0 +1,1 @@
      +contents of parent/file1

""")

    def test_log_show_diff_non_ascii(self):
        # Smoke test for bug #328007 UnicodeDecodeError on 'log -p'
        message = u'Message with \xb5'
        body = 'Body with \xb5\n'
        wt = self.make_branch_and_tree('.')
        self.build_tree_contents([('foo', body)])
        wt.add('foo')
        wt.commit(message=message)
        # check that command won't fail with unicode error
        # don't care about exact output because we have other tests for this
        out,err = self.run_bzr('log -p --long')
        self.assertNotEqual('', out)
        self.assertEqual('', err)
        out,err = self.run_bzr('log -p --short')
        self.assertNotEqual('', out)
        self.assertEqual('', err)
        out,err = self.run_bzr('log -p --line')
        self.assertNotEqual('', out)
        self.assertEqual('', err)


class TestLogEncodings(TestCaseInTempDir):

    _mu = u'\xb5'
    _message = u'Message with \xb5'

    # Encodings which can encode mu
    good_encodings = [
        'utf-8',
        'latin-1',
        'iso-8859-1',
        'cp437', # Common windows encoding
        'cp1251', # Russian windows encoding
        'cp1258', # Common windows encoding
    ]
    # Encodings which cannot encode mu
    bad_encodings = [
        'ascii',
        'iso-8859-2',
        'koi8_r',
    ]

    def setUp(self):
        TestCaseInTempDir.setUp(self)
        self.user_encoding = osutils._cached_user_encoding

    def tearDown(self):
        osutils._cached_user_encoding = self.user_encoding
        TestCaseInTempDir.tearDown(self)

    def create_branch(self):
        bzr = self.run_bzr
        bzr('init')
        open('a', 'wb').write('some stuff\n')
        bzr('add a')
        bzr(['commit', '-m', self._message])

    def try_encoding(self, encoding, fail=False):
        bzr = self.run_bzr
        if fail:
            self.assertRaises(UnicodeEncodeError,
                self._mu.encode, encoding)
            encoded_msg = self._message.encode(encoding, 'replace')
        else:
            encoded_msg = self._message.encode(encoding)

        old_encoding = osutils._cached_user_encoding
        # This test requires that 'run_bzr' uses the current
        # bzrlib, because we override user_encoding, and expect
        # it to be used
        try:
            osutils._cached_user_encoding = 'ascii'
            # We should be able to handle any encoding
            out, err = bzr('log', encoding=encoding)
            if not fail:
                # Make sure we wrote mu as we expected it to exist
                self.assertNotEqual(-1, out.find(encoded_msg))
                out_unicode = out.decode(encoding)
                self.assertNotEqual(-1, out_unicode.find(self._message))
            else:
                self.assertNotEqual(-1, out.find('Message with ?'))
        finally:
            osutils._cached_user_encoding = old_encoding

    def test_log_handles_encoding(self):
        self.create_branch()

        for encoding in self.good_encodings:
            self.try_encoding(encoding)

    def test_log_handles_bad_encoding(self):
        self.create_branch()

        for encoding in self.bad_encodings:
            self.try_encoding(encoding, fail=True)

    def test_stdout_encoding(self):
        bzr = self.run_bzr
        osutils._cached_user_encoding = "cp1251"

        bzr('init')
        self.build_tree(['a'])
        bzr('add a')
        bzr(['commit', '-m', u'\u0422\u0435\u0441\u0442'])
        stdout, stderr = self.run_bzr('log', encoding='cp866')

        message = stdout.splitlines()[-1]

        # explanation of the check:
        # u'\u0422\u0435\u0441\u0442' is word 'Test' in russian
        # in cp866  encoding this is string '\x92\xa5\xe1\xe2'
        # in cp1251 encoding this is string '\xd2\xe5\xf1\xf2'
        # This test should check that output of log command
        # encoded to sys.stdout.encoding
        test_in_cp866 = '\x92\xa5\xe1\xe2'
        test_in_cp1251 = '\xd2\xe5\xf1\xf2'
        # Make sure the log string is encoded in cp866
        self.assertEquals(test_in_cp866, message[2:])
        # Make sure the cp1251 string is not found anywhere
        self.assertEquals(-1, stdout.find(test_in_cp1251))


class TestLogFile(TestCaseWithTransport):

    def test_log_local_branch_file(self):
        """We should be able to log files in local treeless branches"""
        tree = self.make_branch_and_tree('tree')
        self.build_tree(['tree/file'])
        tree.add('file')
        tree.commit('revision 1')
        tree.bzrdir.destroy_workingtree()
        self.run_bzr('log tree/file')

    def prepare_tree(self, complex=False):
        # The complex configuration includes deletes and renames
        tree = self.make_branch_and_tree('parent')
        self.build_tree(['parent/file1', 'parent/file2', 'parent/file3'])
        tree.add('file1')
        tree.commit('add file1')
        tree.add('file2')
        tree.commit('add file2')
        tree.add('file3')
        tree.commit('add file3')
        child_tree = tree.bzrdir.sprout('child').open_workingtree()
        self.build_tree_contents([('child/file2', 'hello')])
        child_tree.commit(message='branch 1')
        tree.merge_from_branch(child_tree.branch)
        tree.commit(message='merge child branch')
        if complex:
            tree.remove('file2')
            tree.commit('remove file2')
            tree.rename_one('file3', 'file4')
            tree.commit('file3 is now called file4')
            tree.remove('file1')
            tree.commit('remove file1')
        os.chdir('parent')

    def test_log_file(self):
        """The log for a particular file should only list revs for that file"""
        self.prepare_tree()
        log = self.run_bzr('log -n0 file1')[0]
        self.assertContainsRe(log, 'revno: 1\n')
        self.assertNotContainsRe(log, 'revno: 2\n')
        self.assertNotContainsRe(log, 'revno: 3\n')
        self.assertNotContainsRe(log, 'revno: 3.1.1\n')
        self.assertNotContainsRe(log, 'revno: 4 ')
        log = self.run_bzr('log -n0 file2')[0]
        self.assertNotContainsRe(log, 'revno: 1\n')
        self.assertContainsRe(log, 'revno: 2\n')
        self.assertNotContainsRe(log, 'revno: 3\n')
        self.assertContainsRe(log, 'revno: 3.1.1\n')
        self.assertContainsRe(log, 'revno: 4 ')
        log = self.run_bzr('log -n0 file3')[0]
        self.assertNotContainsRe(log, 'revno: 1\n')
        self.assertNotContainsRe(log, 'revno: 2\n')
        self.assertContainsRe(log, 'revno: 3\n')
        self.assertNotContainsRe(log, 'revno: 3.1.1\n')
        self.assertNotContainsRe(log, 'revno: 4 ')
        log = self.run_bzr('log -n0 -r3.1.1 file2')[0]
        self.assertNotContainsRe(log, 'revno: 1\n')
        self.assertNotContainsRe(log, 'revno: 2\n')
        self.assertNotContainsRe(log, 'revno: 3\n')
        self.assertContainsRe(log, 'revno: 3.1.1\n')
        self.assertNotContainsRe(log, 'revno: 4 ')
        log = self.run_bzr('log -n0 -r4 file2')[0]
        self.assertNotContainsRe(log, 'revno: 1\n')
        self.assertNotContainsRe(log, 'revno: 2\n')
        self.assertNotContainsRe(log, 'revno: 3\n')
        self.assertContainsRe(log, 'revno: 3.1.1\n')
        self.assertContainsRe(log, 'revno: 4 ')
        log = self.run_bzr('log -n0 -r3.. file2')[0]
        self.assertNotContainsRe(log, 'revno: 1\n')
        self.assertNotContainsRe(log, 'revno: 2\n')
        self.assertNotContainsRe(log, 'revno: 3\n')
        self.assertContainsRe(log, 'revno: 3.1.1\n')
        self.assertContainsRe(log, 'revno: 4 ')
        log = self.run_bzr('log -n0 -r..3 file2')[0]
        self.assertNotContainsRe(log, 'revno: 1\n')
        self.assertContainsRe(log, 'revno: 2\n')
        self.assertNotContainsRe(log, 'revno: 3\n')
        self.assertNotContainsRe(log, 'revno: 3.1.1\n')
        self.assertNotContainsRe(log, 'revno: 4 ')

    def test_log_file_historical_missing(self):
        # Check logging a deleted file gives an error if the
        # file isn't found at the end or start of the revision range
        self.prepare_tree(complex=True)
        err_msg = "Path unknown at end or start of revision range: file2"
        err = self.run_bzr('log file2', retcode=3)[1]
        self.assertContainsRe(err, err_msg)

    def test_log_file_historical_end(self):
        # Check logging a deleted file is ok if the file existed
        # at the end the revision range
        self.prepare_tree(complex=True)
        log, err = self.run_bzr('log -n0 -r..4 file2')
        self.assertEquals('', err)
        self.assertNotContainsRe(log, 'revno: 1\n')
        self.assertContainsRe(log, 'revno: 2\n')
        self.assertNotContainsRe(log, 'revno: 3\n')
        self.assertContainsRe(log, 'revno: 3.1.1\n')
        self.assertContainsRe(log, 'revno: 4 ')

    def test_log_file_historical_start(self):
        # Check logging a deleted file is ok if the file existed
        # at the start of the revision range
        self.prepare_tree(complex=True)
        log, err = self.run_bzr('log file1')
        self.assertEquals('', err)
        self.assertContainsRe(log, 'revno: 1\n')
        self.assertNotContainsRe(log, 'revno: 2\n')
        self.assertNotContainsRe(log, 'revno: 3\n')
        self.assertNotContainsRe(log, 'revno: 3.1.1\n')
        self.assertNotContainsRe(log, 'revno: 4 ')

    def test_log_file_renamed(self):
        """File matched against revision range, not current tree."""
        self.prepare_tree(complex=True)

        # Check logging a renamed file gives an error by default
        err_msg = "Path unknown at end or start of revision range: file3"
        err = self.run_bzr('log file3', retcode=3)[1]
        self.assertContainsRe(err, err_msg)

        # Check we can see a renamed file if we give the right end revision
        log, err = self.run_bzr('log -r..4 file3')
        self.assertEquals('', err)
        self.assertNotContainsRe(log, 'revno: 1\n')
        self.assertNotContainsRe(log, 'revno: 2\n')
        self.assertContainsRe(log, 'revno: 3\n')
        self.assertNotContainsRe(log, 'revno: 3.1.1\n')
        self.assertNotContainsRe(log, 'revno: 4 ')

    def test_line_log_file(self):
        """The line log for a file should only list relevant mainline revs"""
        # Note: this also implicitly  covers the short logging case.
        # We test using --line in preference to --short because matching
        # revnos in the output of --line is more reliable.
        self.prepare_tree()

        # full history of file1
        log = self.run_bzr('log --line file1')[0]
        self.assertContainsRe(log, '^1:', re.MULTILINE)
        self.assertNotContainsRe(log, '^2:', re.MULTILINE)
        self.assertNotContainsRe(log, '^3:', re.MULTILINE)
        self.assertNotContainsRe(log, '^3.1.1:', re.MULTILINE)
        self.assertNotContainsRe(log, '^4:', re.MULTILINE)

        # full history of file2
        log = self.run_bzr('log --line file2')[0]
        self.assertNotContainsRe(log, '^1:', re.MULTILINE)
        self.assertContainsRe(log, '^2:', re.MULTILINE)
        self.assertNotContainsRe(log, '^3:', re.MULTILINE)
        self.assertNotContainsRe(log, '^3.1.1:', re.MULTILINE)
        self.assertContainsRe(log, '^4:', re.MULTILINE)

        # full history of file3
        log = self.run_bzr('log --line file3')[0]
        self.assertNotContainsRe(log, '^1:', re.MULTILINE)
        self.assertNotContainsRe(log, '^2:', re.MULTILINE)
        self.assertContainsRe(log, '^3:', re.MULTILINE)
        self.assertNotContainsRe(log, '^3.1.1:', re.MULTILINE)
        self.assertNotContainsRe(log, '^4:', re.MULTILINE)

        # file in a merge revision
        log = self.run_bzr('log --line -r3.1.1 file2')[0]
        self.assertNotContainsRe(log, '^1:', re.MULTILINE)
        self.assertNotContainsRe(log, '^2:', re.MULTILINE)
        self.assertNotContainsRe(log, '^3:', re.MULTILINE)
        self.assertContainsRe(log, '^3.1.1:', re.MULTILINE)
        self.assertNotContainsRe(log, '^4:', re.MULTILINE)

        # file in a mainline revision
        log = self.run_bzr('log --line -r4 file2')[0]
        self.assertNotContainsRe(log, '^1:', re.MULTILINE)
        self.assertNotContainsRe(log, '^2:', re.MULTILINE)
        self.assertNotContainsRe(log, '^3:', re.MULTILINE)
        self.assertNotContainsRe(log, '^3.1.1:', re.MULTILINE)
        self.assertContainsRe(log, '^4:', re.MULTILINE)

        # file since a revision
        log = self.run_bzr('log --line -r3.. file2')[0]
        self.assertNotContainsRe(log, '^1:', re.MULTILINE)
        self.assertNotContainsRe(log, '^2:', re.MULTILINE)
        self.assertNotContainsRe(log, '^3:', re.MULTILINE)
        self.assertNotContainsRe(log, '^3.1.1:', re.MULTILINE)
        self.assertContainsRe(log, '^4:', re.MULTILINE)

        # file up to a revision
        log = self.run_bzr('log --line -r..3 file2')[0]
        self.assertNotContainsRe(log, '^1:', re.MULTILINE)
        self.assertContainsRe(log, '^2:', re.MULTILINE)
        self.assertNotContainsRe(log, '^3:', re.MULTILINE)
        self.assertNotContainsRe(log, '^3.1.1:', re.MULTILINE)
        self.assertNotContainsRe(log, '^4:', re.MULTILINE)


class TestLogMultiple(TestCaseWithTransport):

    def prepare_tree(self):
        tree = self.make_branch_and_tree('parent')
        self.build_tree([
            'parent/file1',
            'parent/file2',
            'parent/dir1/',
            'parent/dir1/file5',
            'parent/dir1/dir2/',
            'parent/dir1/dir2/file3',
            'parent/file4'])
        tree.add('file1')
        tree.commit('add file1')
        tree.add('file2')
        tree.commit('add file2')
        tree.add(['dir1', 'dir1/dir2', 'dir1/dir2/file3'])
        tree.commit('add file3')
        tree.add('file4')
        tree.commit('add file4')
        tree.add('dir1/file5')
        tree.commit('add file5')
        child_tree = tree.bzrdir.sprout('child').open_workingtree()
        self.build_tree_contents([('child/file2', 'hello')])
        child_tree.commit(message='branch 1')
        tree.merge_from_branch(child_tree.branch)
        tree.commit(message='merge child branch')
        os.chdir('parent')

    def assertRevnos(self, paths_str, expected_revnos):
        # confirm the revision numbers in log --line output are those expected
        out, err = self.run_bzr('log --line -n0 %s' % (paths_str,))
        self.assertEqual('', err)
        revnos = [s.split(':', 1)[0].lstrip() for s in out.splitlines()]
        self.assertEqual(expected_revnos, revnos)

    def test_log_files(self):
        """The log for multiple file should only list revs for those files"""
        self.prepare_tree()
        self.assertRevnos('file1 file2 dir1/dir2/file3',
            ['6', '5.1.1', '3', '2', '1'])

    def test_log_directory(self):
        """The log for a directory should show all nested files."""
        self.prepare_tree()
        self.assertRevnos('dir1', ['5', '3'])

    def test_log_nested_directory(self):
        """The log for a directory should show all nested files."""
        self.prepare_tree()
        self.assertRevnos('dir1/dir2', ['3'])

    def test_log_in_nested_directory(self):
        """The log for a directory should show all nested files."""
        self.prepare_tree()
        os.chdir("dir1")
        self.assertRevnos('.', ['5', '3'])

    def test_log_files_and_directories(self):
        """Logging files and directories together should be fine."""
        self.prepare_tree()
        self.assertRevnos('file4 dir1/dir2', ['4', '3'])

    def test_log_files_and_dirs_in_nested_directory(self):
        """The log for a directory should show all nested files."""
        self.prepare_tree()
        os.chdir("dir1")
        self.assertRevnos('dir2 file5', ['5', '3'])