~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_log.py

  • Committer: John Arbash Meinel
  • Date: 2008-09-26 22:14:42 UTC
  • mto: This revision was merged to the branch mainline in revision 3747.
  • Revision ID: john@arbash-meinel.com-20080926221442-3r67j99sr9rwe9w0
Make message optional, don't check the memory flag directly.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
import os
18
18
from cStringIO import StringIO
19
19
 
20
 
from bzrlib import log
21
 
from bzrlib.tests import TestCaseWithTransport
 
20
from bzrlib import log, registry
 
21
from bzrlib.tests import TestCase, TestCaseWithTransport
22
22
from bzrlib.log import (show_log,
23
23
                        get_view_revisions,
24
24
                        LogRevision,
27
27
                        ShortLogFormatter,
28
28
                        LineLogFormatter)
29
29
from bzrlib.branch import Branch
30
 
from bzrlib.errors import InvalidRevisionNumber
 
30
from bzrlib.errors import (
 
31
    BzrCommandError,
 
32
    InvalidRevisionNumber,
 
33
    )
 
34
from bzrlib.revision import Revision
 
35
from bzrlib.revisionspec import (
 
36
    RevisionInfo,
 
37
    RevisionSpec,
 
38
    )
 
39
 
 
40
 
 
41
class TestCaseWithoutPropsHandler(TestCaseWithTransport):
 
42
 
 
43
    def setUp(self):
 
44
        super(TestCaseWithoutPropsHandler, self).setUp()
 
45
        # keep a reference to the "current" custom prop. handler registry
 
46
        self.properties_handler_registry = \
 
47
            log.properties_handler_registry
 
48
        # clean up the registry in log
 
49
        log.properties_handler_registry = registry.Registry()
 
50
        
 
51
    def _cleanup(self):
 
52
        super(TestCaseWithoutPropsHandler, self)._cleanup()
 
53
        # restore the custom properties handler registry
 
54
        log.properties_handler_registry = \
 
55
            self.properties_handler_registry
31
56
 
32
57
 
33
58
class LogCatcher(LogFormatter):
103
128
 
104
129
        self.build_tree(['hello'])
105
130
        wt.add('hello')
106
 
        wt.commit('add one file')
 
131
        wt.commit('add one file',
 
132
                  committer=u'\u013d\xf3r\xe9m \xcdp\u0161\xfam '
 
133
                            u'<test@example.com>')
107
134
 
108
 
        lf = StringIO()
 
135
        lf = self.make_utf8_encoded_stringio()
109
136
        # log using regular thing
110
137
        show_log(b, LongLogFormatter(lf))
111
138
        lf.seek(0)
163
190
        wt.commit(message='add file1 and file2')
164
191
        self.run_bzr('branch parent child')
165
192
        os.unlink('child/file1')
166
 
        print >> file('child/file2', 'wb'), 'hello'
 
193
        file('child/file2', 'wb').write('hello\n')
167
194
        self.run_bzr(['commit', '-m', 'remove file1 and modify file2',
168
195
            'child'])
169
196
        os.chdir('parent')
191
218
        d = logentry.delta
192
219
        self.checkDelta(d, added=['file1', 'file2'])
193
220
 
 
221
    def test_merges_nonsupporting_formatter(self):
 
222
        """Tests that show_log will raise if the formatter doesn't
 
223
        support merge revisions."""
 
224
        wt = self.make_branch_and_memory_tree('.')
 
225
        wt.lock_write()
 
226
        try:
 
227
            wt.add('')
 
228
            wt.commit('rev-1', rev_id='rev-1',
 
229
                      timestamp=1132586655, timezone=36000,
 
230
                      committer='Joe Foo <joe@foo.com>')
 
231
            wt.commit('rev-merged', rev_id='rev-2a',
 
232
                      timestamp=1132586700, timezone=36000,
 
233
                      committer='Joe Foo <joe@foo.com>')
 
234
            wt.set_parent_ids(['rev-1', 'rev-2a'])
 
235
            wt.branch.set_last_revision_info(1, 'rev-1')
 
236
            wt.commit('rev-2', rev_id='rev-2b',
 
237
                      timestamp=1132586800, timezone=36000,
 
238
                      committer='Joe Foo <joe@foo.com>')
 
239
            logfile = self.make_utf8_encoded_stringio()
 
240
            formatter = ShortLogFormatter(to_file=logfile)
 
241
            wtb = wt.branch
 
242
            lf = LogCatcher()
 
243
            revspec = RevisionSpec.from_string('1.1.1')
 
244
            rev = revspec.in_history(wtb)
 
245
            self.assertRaises(BzrCommandError, show_log, wtb, lf,
 
246
                              start_revision=rev, end_revision=rev)
 
247
        finally:
 
248
            wt.unlock()
 
249
 
194
250
 
195
251
def make_commits_with_trailing_newlines(wt):
196
252
    """Helper method for LogFormatter tests"""    
198
254
    b.nick='test'
199
255
    open('a', 'wb').write('hello moto\n')
200
256
    wt.add('a')
201
 
    wt.commit('simple log message', rev_id='a1'
202
 
            , timestamp=1132586655.459960938, timezone=-6*3600
203
 
            , committer='Joe Foo <joe@foo.com>')
 
257
    wt.commit('simple log message', rev_id='a1',
 
258
              timestamp=1132586655.459960938, timezone=-6*3600,
 
259
              committer='Joe Foo <joe@foo.com>')
204
260
    open('b', 'wb').write('goodbye\n')
205
261
    wt.add('b')
206
 
    wt.commit('multiline\nlog\nmessage\n', rev_id='a2'
207
 
            , timestamp=1132586842.411175966, timezone=-6*3600
208
 
            , committer='Joe Foo <joe@foo.com>')
 
262
    wt.commit('multiline\nlog\nmessage\n', rev_id='a2',
 
263
              timestamp=1132586842.411175966, timezone=-6*3600,
 
264
              committer='Joe Foo <joe@foo.com>',
 
265
              author='Joe Bar <joe@bar.com>')
209
266
 
210
267
    open('c', 'wb').write('just another manic monday\n')
211
268
    wt.add('c')
212
 
    wt.commit('single line with trailing newline\n', rev_id='a3'
213
 
            , timestamp=1132587176.835228920, timezone=-6*3600
214
 
            , committer = 'Joe Foo <joe@foo.com>')
 
269
    wt.commit('single line with trailing newline\n', rev_id='a3',
 
270
              timestamp=1132587176.835228920, timezone=-6*3600,
 
271
              committer = 'Joe Foo <joe@foo.com>')
215
272
    return b
216
273
 
217
274
 
 
275
def normalize_log(log):
 
276
    """Replaces the variable lines of logs with fixed lines"""
 
277
    author = 'author: Dolor Sit <test@example.com>'
 
278
    committer = 'committer: Lorem Ipsum <test@example.com>'
 
279
    lines = log.splitlines(True)
 
280
    for idx,line in enumerate(lines):
 
281
        stripped_line = line.lstrip()
 
282
        indent = ' ' * (len(line) - len(stripped_line))
 
283
        if stripped_line.startswith('author:'):
 
284
            lines[idx] = indent + author + '\n'
 
285
        elif stripped_line.startswith('committer:'):
 
286
            lines[idx] = indent + committer + '\n'
 
287
        elif stripped_line.startswith('timestamp:'):
 
288
            lines[idx] = indent + 'timestamp: Just now\n'
 
289
    return ''.join(lines)
 
290
 
 
291
 
218
292
class TestShortLogFormatter(TestCaseWithTransport):
219
293
 
220
294
    def test_trailing_newlines(self):
221
295
        wt = self.make_branch_and_tree('.')
222
296
        b = make_commits_with_trailing_newlines(wt)
223
 
        sio = StringIO()
 
297
        sio = self.make_utf8_encoded_stringio()
224
298
        lf = ShortLogFormatter(to_file=sio)
225
299
        show_log(b, lf)
226
 
        self.assertEquals(sio.getvalue(), """\
 
300
        self.assertEqualDiff(sio.getvalue(), """\
227
301
    3 Joe Foo\t2005-11-21
228
302
      single line with trailing newline
229
303
 
230
 
    2 Joe Foo\t2005-11-21
 
304
    2 Joe Bar\t2005-11-21
231
305
      multiline
232
306
      log
233
307
      message
237
311
 
238
312
""")
239
313
 
240
 
 
241
 
class TestLongLogFormatter(TestCaseWithTransport):
242
 
 
243
 
    def normalize_log(self,log):
244
 
        """Replaces the variable lines of logs with fixed lines"""
245
 
        committer = 'committer: Lorem Ipsum <test@example.com>'
246
 
        lines = log.splitlines(True)
247
 
        for idx,line in enumerate(lines):
248
 
            stripped_line = line.lstrip()
249
 
            indent = ' ' * (len(line) - len(stripped_line))
250
 
            if stripped_line.startswith('committer:'):
251
 
                lines[idx] = indent + committer + '\n'
252
 
            if stripped_line.startswith('timestamp:'):
253
 
                lines[idx] = indent + 'timestamp: Just now\n'
254
 
        return ''.join(lines)
 
314
    def test_short_log_with_merges(self):
 
315
        wt = self.make_branch_and_memory_tree('.')
 
316
        wt.lock_write()
 
317
        try:
 
318
            wt.add('')
 
319
            wt.commit('rev-1', rev_id='rev-1',
 
320
                      timestamp=1132586655, timezone=36000,
 
321
                      committer='Joe Foo <joe@foo.com>')
 
322
            wt.commit('rev-merged', rev_id='rev-2a',
 
323
                      timestamp=1132586700, timezone=36000,
 
324
                      committer='Joe Foo <joe@foo.com>')
 
325
            wt.set_parent_ids(['rev-1', 'rev-2a'])
 
326
            wt.branch.set_last_revision_info(1, 'rev-1')
 
327
            wt.commit('rev-2', rev_id='rev-2b',
 
328
                      timestamp=1132586800, timezone=36000,
 
329
                      committer='Joe Foo <joe@foo.com>')
 
330
            logfile = self.make_utf8_encoded_stringio()
 
331
            formatter = ShortLogFormatter(to_file=logfile)
 
332
            show_log(wt.branch, formatter)
 
333
            self.assertEqualDiff(logfile.getvalue(), """\
 
334
    2 Joe Foo\t2005-11-22 [merge]
 
335
      rev-2
 
336
 
 
337
    1 Joe Foo\t2005-11-22
 
338
      rev-1
 
339
 
 
340
""")
 
341
        finally:
 
342
            wt.unlock()
 
343
 
 
344
    def test_short_log_single_merge_revision(self):
 
345
        wt = self.make_branch_and_memory_tree('.')
 
346
        wt.lock_write()
 
347
        try:
 
348
            wt.add('')
 
349
            wt.commit('rev-1', rev_id='rev-1',
 
350
                      timestamp=1132586655, timezone=36000,
 
351
                      committer='Joe Foo <joe@foo.com>')
 
352
            wt.commit('rev-merged', rev_id='rev-2a',
 
353
                      timestamp=1132586700, timezone=36000,
 
354
                      committer='Joe Foo <joe@foo.com>')
 
355
            wt.set_parent_ids(['rev-1', 'rev-2a'])
 
356
            wt.branch.set_last_revision_info(1, 'rev-1')
 
357
            wt.commit('rev-2', rev_id='rev-2b',
 
358
                      timestamp=1132586800, timezone=36000,
 
359
                      committer='Joe Foo <joe@foo.com>')
 
360
            logfile = self.make_utf8_encoded_stringio()
 
361
            formatter = ShortLogFormatter(to_file=logfile)
 
362
            revspec = RevisionSpec.from_string('1.1.1')
 
363
            wtb = wt.branch
 
364
            rev = revspec.in_history(wtb)
 
365
            show_log(wtb, formatter, start_revision=rev, end_revision=rev)
 
366
            self.assertEqualDiff(logfile.getvalue(), """\
 
367
1.1.1 Joe Foo\t2005-11-22
 
368
      rev-merged
 
369
 
 
370
""")
 
371
        finally:
 
372
            wt.unlock()
 
373
 
 
374
 
 
375
class TestLongLogFormatter(TestCaseWithoutPropsHandler):
255
376
 
256
377
    def test_verbose_log(self):
257
378
        """Verbose log includes changed files
301
422
        self.run_bzr('merge ../child')
302
423
        wt.commit('merge branch 1')
303
424
        b = wt.branch
304
 
        sio = StringIO()
 
425
        sio = self.make_utf8_encoded_stringio()
305
426
        lf = LongLogFormatter(to_file=sio)
306
427
        show_log(b, lf, verbose=True)
307
 
        log = self.normalize_log(sio.getvalue())
308
 
        self.assertEqualDiff("""\
 
428
        log = normalize_log(sio.getvalue())
 
429
        self.assertEqualDiff(log, """\
309
430
------------------------------------------------------------
310
431
revno: 2
311
432
committer: Lorem Ipsum <test@example.com>
321
442
    message:
322
443
      merge branch 2
323
444
        ------------------------------------------------------------
324
 
        revno: 1.1.1.1.1
 
445
        revno: 1.2.1
325
446
        committer: Lorem Ipsum <test@example.com>
326
447
        branch nick: smallerchild
327
448
        timestamp: Just now
341
462
timestamp: Just now
342
463
message:
343
464
  first post
344
 
""", log)
 
465
""")
345
466
 
346
467
    def test_verbose_merge_revisions_contain_deltas(self):
347
468
        wt = self.make_branch_and_tree('parent')
350
471
        wt.commit('first post')
351
472
        self.run_bzr('branch parent child')
352
473
        os.unlink('child/f1')
353
 
        print >> file('child/f2', 'wb'), 'hello'
 
474
        file('child/f2', 'wb').write('hello\n')
354
475
        self.run_bzr(['commit', '-m', 'removed f1 and modified f2',
355
476
            'child'])
356
477
        os.chdir('parent')
357
478
        self.run_bzr('merge ../child')
358
479
        wt.commit('merge branch 1')
359
480
        b = wt.branch
360
 
        sio = StringIO()
 
481
        sio = self.make_utf8_encoded_stringio()
361
482
        lf = LongLogFormatter(to_file=sio)
362
483
        show_log(b, lf, verbose=True)
363
 
        log = self.normalize_log(sio.getvalue())
364
 
        self.assertEqualDiff("""\
 
484
        log = normalize_log(sio.getvalue())
 
485
        self.assertEqualDiff(log, """\
365
486
------------------------------------------------------------
366
487
revno: 2
367
488
committer: Lorem Ipsum <test@example.com>
394
515
added:
395
516
  f1
396
517
  f2
397
 
""", log)
 
518
""")
398
519
 
399
520
    def test_trailing_newlines(self):
400
521
        wt = self.make_branch_and_tree('.')
401
522
        b = make_commits_with_trailing_newlines(wt)
402
 
        sio = StringIO()
 
523
        sio = self.make_utf8_encoded_stringio()
403
524
        lf = LongLogFormatter(to_file=sio)
404
525
        show_log(b, lf)
405
526
        self.assertEqualDiff(sio.getvalue(), """\
412
533
  single line with trailing newline
413
534
------------------------------------------------------------
414
535
revno: 2
 
536
author: Joe Bar <joe@bar.com>
415
537
committer: Joe Foo <joe@foo.com>
416
538
branch nick: test
417
539
timestamp: Mon 2005-11-21 09:27:22 -0600
428
550
  simple log message
429
551
""")
430
552
 
 
553
    def test_author_in_log(self):
 
554
        """Log includes the author name if it's set in
 
555
        the revision properties
 
556
        """
 
557
        wt = self.make_branch_and_tree('.')
 
558
        b = wt.branch
 
559
        self.build_tree(['a'])
 
560
        wt.add('a')
 
561
        b.nick = 'test_author_log'
 
562
        wt.commit(message='add a',
 
563
                  timestamp=1132711707,
 
564
                  timezone=36000,
 
565
                  committer='Lorem Ipsum <test@example.com>',
 
566
                  author='John Doe <jdoe@example.com>')
 
567
        sio = StringIO()
 
568
        formatter = LongLogFormatter(to_file=sio)
 
569
        show_log(b, formatter)
 
570
        self.assertEqualDiff(sio.getvalue(), '''\
 
571
------------------------------------------------------------
 
572
revno: 1
 
573
author: John Doe <jdoe@example.com>
 
574
committer: Lorem Ipsum <test@example.com>
 
575
branch nick: test_author_log
 
576
timestamp: Wed 2005-11-23 12:08:27 +1000
 
577
message:
 
578
  add a
 
579
''')
 
580
 
 
581
    def test_properties_in_log(self):
 
582
        """Log includes the custom properties returned by the registered 
 
583
        handlers.
 
584
        """
 
585
        wt = self.make_branch_and_tree('.')
 
586
        b = wt.branch
 
587
        self.build_tree(['a'])
 
588
        wt.add('a')
 
589
        b.nick = 'test_properties_in_log'
 
590
        wt.commit(message='add a',
 
591
                  timestamp=1132711707,
 
592
                  timezone=36000,
 
593
                  committer='Lorem Ipsum <test@example.com>',
 
594
                  author='John Doe <jdoe@example.com>')
 
595
        sio = StringIO()
 
596
        formatter = LongLogFormatter(to_file=sio)
 
597
        try:
 
598
            def trivial_custom_prop_handler(revision):
 
599
                return {'test_prop':'test_value'}
 
600
            
 
601
            log.properties_handler_registry.register(
 
602
                'trivial_custom_prop_handler', 
 
603
                trivial_custom_prop_handler)
 
604
            show_log(b, formatter)
 
605
        finally:
 
606
            log.properties_handler_registry.remove(
 
607
                'trivial_custom_prop_handler')
 
608
            self.assertEqualDiff(sio.getvalue(), '''\
 
609
------------------------------------------------------------
 
610
revno: 1
 
611
test_prop: test_value
 
612
author: John Doe <jdoe@example.com>
 
613
committer: Lorem Ipsum <test@example.com>
 
614
branch nick: test_properties_in_log
 
615
timestamp: Wed 2005-11-23 12:08:27 +1000
 
616
message:
 
617
  add a
 
618
''')
 
619
 
 
620
    def test_error_in_properties_handler(self):
 
621
        """Log includes the custom properties returned by the registered 
 
622
        handlers.
 
623
        """
 
624
        wt = self.make_branch_and_tree('.')
 
625
        b = wt.branch
 
626
        self.build_tree(['a'])
 
627
        wt.add('a')
 
628
        b.nick = 'test_author_log'
 
629
        wt.commit(message='add a',
 
630
                  timestamp=1132711707,
 
631
                  timezone=36000,
 
632
                  committer='Lorem Ipsum <test@example.com>',
 
633
                  author='John Doe <jdoe@example.com>',
 
634
                  revprops={'first_prop':'first_value'})
 
635
        sio = StringIO()
 
636
        formatter = LongLogFormatter(to_file=sio)
 
637
        try:
 
638
            def trivial_custom_prop_handler(revision):
 
639
                raise StandardError("a test error")
 
640
            
 
641
            log.properties_handler_registry.register(
 
642
                'trivial_custom_prop_handler', 
 
643
                trivial_custom_prop_handler)
 
644
            self.assertRaises(StandardError, show_log, b, formatter,)
 
645
        finally:
 
646
            log.properties_handler_registry.remove(
 
647
                'trivial_custom_prop_handler')
 
648
                
 
649
    def test_properties_handler_bad_argument(self):
 
650
        wt = self.make_branch_and_tree('.')
 
651
        b = wt.branch
 
652
        self.build_tree(['a'])
 
653
        wt.add('a')
 
654
        b.nick = 'test_author_log'
 
655
        wt.commit(message='add a',
 
656
                  timestamp=1132711707,
 
657
                  timezone=36000,
 
658
                  committer='Lorem Ipsum <test@example.com>',
 
659
                  author='John Doe <jdoe@example.com>',
 
660
                  revprops={'a_prop':'test_value'})
 
661
        sio = StringIO()
 
662
        formatter = LongLogFormatter(to_file=sio)
 
663
        try:
 
664
            def bad_argument_prop_handler(revision):
 
665
                return {'custom_prop_name':revision.properties['a_prop']}
 
666
                
 
667
            log.properties_handler_registry.register(
 
668
                'bad_argument_prop_handler', 
 
669
                bad_argument_prop_handler)
 
670
            
 
671
            self.assertRaises(AttributeError, formatter.show_properties, 
 
672
                'a revision', '')
 
673
            
 
674
            revision = b.repository.get_revision(b.last_revision())
 
675
            formatter.show_properties(revision, '')
 
676
            self.assertEqualDiff(sio.getvalue(),
 
677
                '''custom_prop_name: test_value\n''')
 
678
        finally:
 
679
            log.properties_handler_registry.remove(
 
680
                'bad_argument_prop_handler')
 
681
 
431
682
 
432
683
class TestLineLogFormatter(TestCaseWithTransport):
433
684
 
441
692
        self.build_tree(['a'])
442
693
        wt.add('a')
443
694
        b.nick = 'test-line-log'
444
 
        wt.commit(message='add a', 
445
 
                  timestamp=1132711707, 
 
695
        wt.commit(message='add a',
 
696
                  timestamp=1132711707,
446
697
                  timezone=36000,
447
698
                  committer='Line-Log-Formatter Tester <test@line.log>')
448
699
        logfile = file('out.tmp', 'w+')
451
702
        logfile.flush()
452
703
        logfile.seek(0)
453
704
        log_contents = logfile.read()
454
 
        self.assertEqualDiff(log_contents, '1: Line-Log-Formatte... 2005-11-23 add a\n')
455
 
 
456
 
    def test_short_log_with_merges(self):
457
 
        wt = self.make_branch_and_memory_tree('.')
458
 
        wt.lock_write()
459
 
        try:
460
 
            wt.add('')
461
 
            wt.commit('rev-1', rev_id='rev-1',
462
 
                      timestamp=1132586655, timezone=36000,
463
 
                      committer='Joe Foo <joe@foo.com>')
464
 
            wt.commit('rev-merged', rev_id='rev-2a',
465
 
                      timestamp=1132586700, timezone=36000,
466
 
                      committer='Joe Foo <joe@foo.com>')
467
 
            wt.set_parent_ids(['rev-1', 'rev-2a'])
468
 
            wt.branch.set_last_revision_info(1, 'rev-1')
469
 
            wt.commit('rev-2', rev_id='rev-2b',
470
 
                      timestamp=1132586800, timezone=36000,
471
 
                      committer='Joe Foo <joe@foo.com>')
472
 
            logfile = StringIO()
473
 
            formatter = ShortLogFormatter(to_file=logfile)
474
 
            show_log(wt.branch, formatter)
475
 
            logfile.flush()
476
 
            self.assertEqualDiff("""\
477
 
    2 Joe Foo\t2005-11-22 [merge]
478
 
      rev-2
479
 
 
480
 
    1 Joe Foo\t2005-11-22
481
 
      rev-1
482
 
 
483
 
""", logfile.getvalue())
484
 
        finally:
485
 
            wt.unlock()
 
705
        self.assertEqualDiff(log_contents,
 
706
            '1: Line-Log-Formatte... 2005-11-23 add a\n')
486
707
 
487
708
    def test_trailing_newlines(self):
488
709
        wt = self.make_branch_and_tree('.')
489
710
        b = make_commits_with_trailing_newlines(wt)
490
 
        sio = StringIO()
 
711
        sio = self.make_utf8_encoded_stringio()
491
712
        lf = LineLogFormatter(to_file=sio)
492
713
        show_log(b, lf)
493
714
        self.assertEqualDiff(sio.getvalue(), """\
494
715
3: Joe Foo 2005-11-21 single line with trailing newline
495
 
2: Joe Foo 2005-11-21 multiline
 
716
2: Joe Bar 2005-11-21 multiline
496
717
1: Joe Foo 2005-11-21 simple log message
497
718
""")
498
719
 
 
720
    def test_line_log_single_merge_revision(self):
 
721
        wt = self.make_branch_and_memory_tree('.')
 
722
        wt.lock_write()
 
723
        try:
 
724
            wt.add('')
 
725
            wt.commit('rev-1', rev_id='rev-1',
 
726
                      timestamp=1132586655, timezone=36000,
 
727
                      committer='Joe Foo <joe@foo.com>')
 
728
            wt.commit('rev-merged', rev_id='rev-2a',
 
729
                      timestamp=1132586700, timezone=36000,
 
730
                      committer='Joe Foo <joe@foo.com>')
 
731
            wt.set_parent_ids(['rev-1', 'rev-2a'])
 
732
            wt.branch.set_last_revision_info(1, 'rev-1')
 
733
            wt.commit('rev-2', rev_id='rev-2b',
 
734
                      timestamp=1132586800, timezone=36000,
 
735
                      committer='Joe Foo <joe@foo.com>')
 
736
            logfile = self.make_utf8_encoded_stringio()
 
737
            formatter = LineLogFormatter(to_file=logfile)
 
738
            revspec = RevisionSpec.from_string('1.1.1')
 
739
            wtb = wt.branch
 
740
            rev = revspec.in_history(wtb)
 
741
            show_log(wtb, formatter, start_revision=rev, end_revision=rev)
 
742
            self.assertEqualDiff(logfile.getvalue(), """\
 
743
1.1.1: Joe Foo 2005-11-22 rev-merged
 
744
""")
 
745
        finally:
 
746
            wt.unlock()
 
747
 
 
748
 
499
749
 
500
750
class TestGetViewRevisions(TestCaseWithTransport):
501
751
 
541
791
        full_rev_nos_for_reference = {
542
792
            '1': '1',
543
793
            '2': '2',
544
 
            '3a': '2.2.1', #first commit tree 3
545
 
            '3b': '2.1.1', # first commit tree 2
 
794
            '3a': '2.1.1', #first commit tree 3
 
795
            '3b': '2.2.1', # first commit tree 2
546
796
            '3c': '3', #merges 3b to main
547
 
            '4a': '2.1.2', # second commit tree 2
 
797
            '4a': '2.2.2', # second commit tree 2
548
798
            '4b': '4', # merges 4a to main
549
799
            }
550
800
        return mainline_revs, rev_nos, wt
552
802
    def test_get_view_revisions_forward(self):
553
803
        """Test the get_view_revisions method"""
554
804
        mainline_revs, rev_nos, wt = self.make_tree_with_commits()
 
805
        wt.lock_read()
 
806
        self.addCleanup(wt.unlock)
555
807
        revisions = list(get_view_revisions(mainline_revs, rev_nos, wt.branch,
556
808
                                            'forward'))
557
809
        self.assertEqual([('1', '1', 0), ('2', '2', 0), ('3', '3', 0)],
563
815
    def test_get_view_revisions_reverse(self):
564
816
        """Test the get_view_revisions with reverse"""
565
817
        mainline_revs, rev_nos, wt = self.make_tree_with_commits()
 
818
        wt.lock_read()
 
819
        self.addCleanup(wt.unlock)
566
820
        revisions = list(get_view_revisions(mainline_revs, rev_nos, wt.branch,
567
821
                                            'reverse'))
568
822
        self.assertEqual([('3', '3', 0), ('2', '2', 0), ('1', '1', 0), ],
574
828
    def test_get_view_revisions_merge(self):
575
829
        """Test get_view_revisions when there are merges"""
576
830
        mainline_revs, rev_nos, wt = self.make_tree_with_merges()
 
831
        wt.lock_read()
 
832
        self.addCleanup(wt.unlock)
577
833
        revisions = list(get_view_revisions(mainline_revs, rev_nos, wt.branch,
578
834
                                            'forward'))
579
835
        self.assertEqual([('1', '1', 0), ('2', '2', 0), ('3', '3', 0),
588
844
    def test_get_view_revisions_merge_reverse(self):
589
845
        """Test get_view_revisions in reverse when there are merges"""
590
846
        mainline_revs, rev_nos, wt = self.make_tree_with_merges()
 
847
        wt.lock_read()
 
848
        self.addCleanup(wt.unlock)
591
849
        revisions = list(get_view_revisions(mainline_revs, rev_nos, wt.branch,
592
850
                                            'reverse'))
593
851
        self.assertEqual([('4b', '4', 0), ('4a', '3.1.1', 1),
602
860
    def test_get_view_revisions_merge2(self):
603
861
        """Test get_view_revisions when there are merges"""
604
862
        mainline_revs, rev_nos, wt = self.make_tree_with_many_merges()
 
863
        wt.lock_read()
 
864
        self.addCleanup(wt.unlock)
605
865
        revisions = list(get_view_revisions(mainline_revs, rev_nos, wt.branch,
606
866
                                            'forward'))
607
867
        expected = [('1', '1', 0), ('2', '2', 0), ('3c', '3', 0),
608
 
            ('3a', '2.2.1', 1), ('3b', '2.1.1', 1), ('4b', '4', 0),
609
 
            ('4a', '2.1.2', 1)]
 
868
            ('3a', '2.1.1', 1), ('3b', '2.2.1', 1), ('4b', '4', 0),
 
869
            ('4a', '2.2.2', 1)]
610
870
        self.assertEqual(expected, revisions)
611
871
        revisions = list(get_view_revisions(mainline_revs, rev_nos, wt.branch,
612
872
                                             'forward', include_merges=False))
747
1007
        tree = self.create_tree_with_single_merge()
748
1008
        # f3 should be marked as modified by revisions A, B, C, and D
749
1009
        self.assertAllRevisionsForFileID(tree, 'f2-id', ['D', 'C', 'A'])
 
1010
 
 
1011
    def test_file_id_with_ghosts(self):
 
1012
        # This is testing bug #209948, where having a ghost would cause
 
1013
        # _filter_revisions_touching_file_id() to fail.
 
1014
        tree = self.create_tree_with_single_merge()
 
1015
        # We need to add a revision, so switch back to a write-locked tree
 
1016
        tree.unlock()
 
1017
        tree.lock_write()
 
1018
        first_parent = tree.last_revision()
 
1019
        tree.set_parent_ids([first_parent, 'ghost-revision-id'])
 
1020
        self.build_tree_contents([('tree/f1', 'A\nB\nXX\n')])
 
1021
        tree.commit('commit with a ghost', rev_id='XX')
 
1022
        self.assertAllRevisionsForFileID(tree, 'f1-id', ['XX', 'B', 'A'])
 
1023
        self.assertAllRevisionsForFileID(tree, 'f2-id', ['D', 'C', 'A'])
 
1024
 
 
1025
 
 
1026
class TestShowChangedRevisions(TestCaseWithTransport):
 
1027
 
 
1028
    def test_show_changed_revisions_verbose(self):
 
1029
        tree = self.make_branch_and_tree('tree_a')
 
1030
        self.build_tree(['tree_a/foo'])
 
1031
        tree.add('foo')
 
1032
        tree.commit('bar', rev_id='bar-id')
 
1033
        s = self.make_utf8_encoded_stringio()
 
1034
        log.show_changed_revisions(tree.branch, [], ['bar-id'], s)
 
1035
        self.assertContainsRe(s.getvalue(), 'bar')
 
1036
        self.assertNotContainsRe(s.getvalue(), 'foo')
 
1037
 
 
1038
 
 
1039
class TestLogFormatter(TestCase):
 
1040
 
 
1041
    def test_short_committer(self):
 
1042
        rev = Revision('a-id')
 
1043
        rev.committer = 'John Doe <jdoe@example.com>'
 
1044
        lf = LogFormatter(None)
 
1045
        self.assertEqual('John Doe', lf.short_committer(rev))
 
1046
        rev.committer = 'John Smith <jsmith@example.com>'
 
1047
        self.assertEqual('John Smith', lf.short_committer(rev))
 
1048
        rev.committer = 'John Smith'
 
1049
        self.assertEqual('John Smith', lf.short_committer(rev))
 
1050
        rev.committer = 'jsmith@example.com'
 
1051
        self.assertEqual('jsmith@example.com', lf.short_committer(rev))
 
1052
        rev.committer = '<jsmith@example.com>'
 
1053
        self.assertEqual('jsmith@example.com', lf.short_committer(rev))
 
1054
        rev.committer = 'John Smith jsmith@example.com'
 
1055
        self.assertEqual('John Smith', lf.short_committer(rev))
 
1056
 
 
1057
    def test_short_author(self):
 
1058
        rev = Revision('a-id')
 
1059
        rev.committer = 'John Doe <jdoe@example.com>'
 
1060
        lf = LogFormatter(None)
 
1061
        self.assertEqual('John Doe', lf.short_author(rev))
 
1062
        rev.properties['author'] = 'John Smith <jsmith@example.com>'
 
1063
        self.assertEqual('John Smith', lf.short_author(rev))
 
1064
        rev.properties['author'] = 'John Smith'
 
1065
        self.assertEqual('John Smith', lf.short_author(rev))
 
1066
        rev.properties['author'] = 'jsmith@example.com'
 
1067
        self.assertEqual('jsmith@example.com', lf.short_author(rev))
 
1068
        rev.properties['author'] = '<jsmith@example.com>'
 
1069
        self.assertEqual('jsmith@example.com', lf.short_author(rev))
 
1070
        rev.properties['author'] = 'John Smith jsmith@example.com'
 
1071
        self.assertEqual('John Smith', lf.short_author(rev))