~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
# Copyright (C) 2006-2012 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


"""Tests for the commit CLI of bzr."""

import doctest
import os
import re
import sys

from testtools.matchers import DocTestMatches

from bzrlib import (
    config,
    osutils,
    ignores,
    msgeditor,
    )
from bzrlib.bzrdir import BzrDir
from bzrlib.tests import (
    test_foreign,
    features,
    )
from bzrlib.tests import TestCaseWithTransport
from bzrlib.tests.matchers import ContainsNoVfsCalls


class TestCommit(TestCaseWithTransport):

    def test_05_empty_commit(self):
        """Commit of tree with no versioned files should fail"""
        # If forced, it should succeed, but this is not tested here.
        self.make_branch_and_tree('.')
        self.build_tree(['hello.txt'])
        out,err = self.run_bzr('commit -m empty', retcode=3)
        self.assertEqual('', out)
        # Two ugly bits here.
        # 1) We really don't want 'aborting commit write group' anymore.
        # 2) bzr: ERROR: is a really long line, so we wrap it with '\'
        self.assertThat(
            err,
            DocTestMatches("""\
Committing to: ...
bzr: ERROR: No changes to commit.\
 Please 'bzr add' the files you want to commit,\
 or use --unchanged to force an empty commit.
""", flags=doctest.ELLIPSIS|doctest.REPORT_UDIFF))

    def test_commit_success(self):
        """Successful commit should not leave behind a bzr-commit-* file"""
        self.make_branch_and_tree('.')
        self.run_bzr('commit --unchanged -m message')
        self.assertEqual('', self.run_bzr('unknowns')[0])

        # same for unicode messages
        self.run_bzr(["commit", "--unchanged", "-m", u'foo\xb5'])
        self.assertEqual('', self.run_bzr('unknowns')[0])

    def test_commit_lossy_native(self):
        """A --lossy option to commit is supported."""
        self.make_branch_and_tree('.')
        self.run_bzr('commit --lossy --unchanged -m message')
        self.assertEqual('', self.run_bzr('unknowns')[0])

    def test_commit_lossy_foreign(self):
        test_foreign.register_dummy_foreign_for_test(self)
        self.make_branch_and_tree('.',
            format=test_foreign.DummyForeignVcsDirFormat())
        self.run_bzr('commit --lossy --unchanged -m message')
        output = self.run_bzr('revision-info')[0]
        self.assertTrue(output.startswith('1 dummy-'))

    def test_commit_with_path(self):
        """Commit tree with path of root specified"""
        a_tree = self.make_branch_and_tree('a')
        self.build_tree(['a/a_file'])
        a_tree.add('a_file')
        self.run_bzr(['commit', '-m', 'first commit', 'a'])

        b_tree = a_tree.bzrdir.sprout('b').open_workingtree()
        self.build_tree_contents([('b/a_file', 'changes in b')])
        self.run_bzr(['commit', '-m', 'first commit in b', 'b'])

        self.build_tree_contents([('a/a_file', 'new contents')])
        self.run_bzr(['commit', '-m', 'change in a', 'a'])

        b_tree.merge_from_branch(a_tree.branch)
        self.assertEqual(len(b_tree.conflicts()), 1)
        self.run_bzr('resolved b/a_file')
        self.run_bzr(['commit', '-m', 'merge into b', 'b'])

    def test_10_verbose_commit(self):
        """Add one file and examine verbose commit output"""
        tree = self.make_branch_and_tree('.')
        self.build_tree(['hello.txt'])
        tree.add("hello.txt")
        out,err = self.run_bzr('commit -m added')
        self.assertEqual('', out)
        self.assertContainsRe(err, '^Committing to: .*\n'
                              'added hello.txt\n'
                              'Committed revision 1.\n$',)

    def prepare_simple_history(self):
        """Prepare and return a working tree with one commit of one file"""
        # Commit with modified file should say so
        wt = BzrDir.create_standalone_workingtree('.')
        self.build_tree(['hello.txt', 'extra.txt'])
        wt.add(['hello.txt'])
        wt.commit(message='added')
        return wt

    def test_verbose_commit_modified(self):
        # Verbose commit of modified file should say so
        wt = self.prepare_simple_history()
        self.build_tree_contents([('hello.txt', 'new contents')])
        out, err = self.run_bzr('commit -m modified')
        self.assertEqual('', out)
        self.assertContainsRe(err, '^Committing to: .*\n'
                              'modified hello\.txt\n'
                              'Committed revision 2\.\n$')

    def test_unicode_commit_message_is_filename(self):
        """Unicode commit message same as a filename (Bug #563646).
        """
        self.requireFeature(features.UnicodeFilenameFeature)
        file_name = u'\N{euro sign}'
        self.run_bzr(['init'])
        with open(file_name, 'w') as f: f.write('hello world')
        self.run_bzr(['add'])
        out, err = self.run_bzr(['commit', '-m', file_name])
        reflags = re.MULTILINE|re.DOTALL|re.UNICODE
        te = osutils.get_terminal_encoding()
        self.assertContainsRe(err.decode(te),
            u'The commit message is a file name:',
            flags=reflags)

        # Run same test with a filename that causes encode
        # error for the terminal encoding. We do this
        # by forcing terminal encoding of ascii for
        # osutils.get_terminal_encoding which is used
        # by ui.text.show_warning
        default_get_terminal_enc = osutils.get_terminal_encoding
        try:
            osutils.get_terminal_encoding = lambda trace=None: 'ascii'
            file_name = u'foo\u1234'
            with open(file_name, 'w') as f: f.write('hello world')
            self.run_bzr(['add'])
            out, err = self.run_bzr(['commit', '-m', file_name])
            reflags = re.MULTILINE|re.DOTALL|re.UNICODE
            te = osutils.get_terminal_encoding()
            self.assertContainsRe(err.decode(te, 'replace'),
                u'The commit message is a file name:',
                flags=reflags)
        finally:
            osutils.get_terminal_encoding = default_get_terminal_enc

    def test_non_ascii_file_unversioned_utf8(self):
        self.requireFeature(features.UnicodeFilenameFeature)
        tree = self.make_branch_and_tree(".")
        self.build_tree(["f"])
        tree.add(["f"])
        out, err = self.run_bzr(["commit", "-m", "Wrong filename", u"\xa7"],
            encoding="utf-8", retcode=3)
        self.assertContainsRe(err, "(?m)not versioned: \"\xc2\xa7\"$")

    def test_non_ascii_file_unversioned_iso_8859_5(self):
        self.requireFeature(features.UnicodeFilenameFeature)
        tree = self.make_branch_and_tree(".")
        self.build_tree(["f"])
        tree.add(["f"])
        out, err = self.run_bzr(["commit", "-m", "Wrong filename", u"\xa7"],
            encoding="iso-8859-5", retcode=3)
        self.expectFailure("Error messages are always written as UTF-8",
            self.assertNotContainsString, err, "\xc2\xa7")
        self.assertContainsRe(err, "(?m)not versioned: \"\xfd\"$")

    def test_warn_about_forgotten_commit_message(self):
        """Test that the lack of -m parameter is caught"""
        wt = self.make_branch_and_tree('.')
        self.build_tree(['one', 'two'])
        wt.add(['two'])
        out, err = self.run_bzr('commit -m one two')
        self.assertContainsRe(err, "The commit message is a file name")

    def test_verbose_commit_renamed(self):
        # Verbose commit of renamed file should say so
        wt = self.prepare_simple_history()
        wt.rename_one('hello.txt', 'gutentag.txt')
        out, err = self.run_bzr('commit -m renamed')
        self.assertEqual('', out)
        self.assertContainsRe(err, '^Committing to: .*\n'
                              'renamed hello\.txt => gutentag\.txt\n'
                              'Committed revision 2\.$\n')

    def test_verbose_commit_moved(self):
        # Verbose commit of file moved to new directory should say so
        wt = self.prepare_simple_history()
        os.mkdir('subdir')
        wt.add(['subdir'])
        wt.rename_one('hello.txt', 'subdir/hello.txt')
        out, err = self.run_bzr('commit -m renamed')
        self.assertEqual('', out)
        self.assertEqual(set([
            'Committing to: %s/' % osutils.getcwd(),
            'added subdir',
            'renamed hello.txt => subdir/hello.txt',
            'Committed revision 2.',
            '',
            ]), set(err.split('\n')))

    def test_verbose_commit_with_unknown(self):
        """Unknown files should not be listed by default in verbose output"""
        # Is that really the best policy?
        wt = BzrDir.create_standalone_workingtree('.')
        self.build_tree(['hello.txt', 'extra.txt'])
        wt.add(['hello.txt'])
        out,err = self.run_bzr('commit -m added')
        self.assertEqual('', out)
        self.assertContainsRe(err, '^Committing to: .*\n'
                              'added hello\.txt\n'
                              'Committed revision 1\.\n$')

    def test_verbose_commit_with_unchanged(self):
        """Unchanged files should not be listed by default in verbose output"""
        tree = self.make_branch_and_tree('.')
        self.build_tree(['hello.txt', 'unchanged.txt'])
        tree.add('unchanged.txt')
        self.run_bzr('commit -m unchanged unchanged.txt')
        tree.add("hello.txt")
        out,err = self.run_bzr('commit -m added')
        self.assertEqual('', out)
        self.assertContainsRe(err, '^Committing to: .*\n'
                              'added hello\.txt\n'
                              'Committed revision 2\.$\n')

    def test_verbose_commit_includes_master_location(self):
        """Location of master is displayed when committing to bound branch"""
        a_tree = self.make_branch_and_tree('a')
        self.build_tree(['a/b'])
        a_tree.add('b')
        a_tree.commit(message='Initial message')

        b_tree = a_tree.branch.create_checkout('b')
        expected = "%s/" % (osutils.abspath('a'), )
        out, err = self.run_bzr('commit -m blah --unchanged', working_dir='b')
        self.assertEqual(err, 'Committing to: %s\n'
                         'Committed revision 2.\n' % expected)

    def test_commit_sanitizes_CR_in_message(self):
        # See bug #433779, basically Emacs likes to pass '\r\n' style line
        # endings to 'bzr commit -m ""' which breaks because we don't allow
        # '\r' in commit messages. (Mostly because of issues where XML style
        # formats arbitrarily strip it out of the data while parsing.)
        # To make life easier for users, we just always translate '\r\n' =>
        # '\n'. And '\r' => '\n'.
        a_tree = self.make_branch_and_tree('a')
        self.build_tree(['a/b'])
        a_tree.add('b')
        self.run_bzr(['commit',
                      '-m', 'a string\r\n\r\nwith mixed\r\rendings\n'],
                     working_dir='a')
        rev_id = a_tree.branch.last_revision()
        rev = a_tree.branch.repository.get_revision(rev_id)
        self.assertEqualDiff('a string\n\nwith mixed\n\nendings\n',
                             rev.message)

    def test_commit_merge_reports_all_modified_files(self):
        # the commit command should show all the files that are shown by
        # bzr diff or bzr status when committing, even when they were not
        # changed by the user but rather through doing a merge.
        this_tree = self.make_branch_and_tree('this')
        # we need a bunch of files and dirs, to perform one action on each.
        self.build_tree([
            'this/dirtorename/',
            'this/dirtoreparent/',
            'this/dirtoleave/',
            'this/dirtoremove/',
            'this/filetoreparent',
            'this/filetorename',
            'this/filetomodify',
            'this/filetoremove',
            'this/filetoleave']
            )
        this_tree.add([
            'dirtorename',
            'dirtoreparent',
            'dirtoleave',
            'dirtoremove',
            'filetoreparent',
            'filetorename',
            'filetomodify',
            'filetoremove',
            'filetoleave']
            )
        this_tree.commit('create_files')
        other_dir = this_tree.bzrdir.sprout('other')
        other_tree = other_dir.open_workingtree()
        other_tree.lock_write()
        # perform the needed actions on the files and dirs.
        try:
            other_tree.rename_one('dirtorename', 'renameddir')
            other_tree.rename_one('dirtoreparent', 'renameddir/reparenteddir')
            other_tree.rename_one('filetorename', 'renamedfile')
            other_tree.rename_one('filetoreparent',
                                  'renameddir/reparentedfile')
            other_tree.remove(['dirtoremove', 'filetoremove'])
            self.build_tree_contents([
                ('other/newdir/',),
                ('other/filetomodify', 'new content'),
                ('other/newfile', 'new file content')])
            other_tree.add('newfile')
            other_tree.add('newdir/')
            other_tree.commit('modify all sample files and dirs.')
        finally:
            other_tree.unlock()
        this_tree.merge_from_branch(other_tree.branch)
        out, err = self.run_bzr('commit -m added', working_dir='this')
        self.assertEqual('', out)
        self.assertEqual(set([
            'Committing to: %s/' % osutils.pathjoin(osutils.getcwd(), 'this'),
            'modified filetomodify',
            'added newdir',
            'added newfile',
            'renamed dirtorename => renameddir',
            'renamed filetorename => renamedfile',
            'renamed dirtoreparent => renameddir/reparenteddir',
            'renamed filetoreparent => renameddir/reparentedfile',
            'deleted dirtoremove',
            'deleted filetoremove',
            'Committed revision 2.',
            ''
            ]), set(err.split('\n')))

    def test_empty_commit_message(self):
        tree = self.make_branch_and_tree('.')
        self.build_tree_contents([('foo.c', 'int main() {}')])
        tree.add('foo.c')
        self.run_bzr('commit -m ""')

    def test_other_branch_commit(self):
        # this branch is to ensure consistent behaviour, whether we're run
        # inside a branch, or not.
        outer_tree = self.make_branch_and_tree('.')
        inner_tree = self.make_branch_and_tree('branch')
        self.build_tree_contents([
            ('branch/foo.c', 'int main() {}'),
            ('branch/bar.c', 'int main() {}')])
        inner_tree.add(['foo.c', 'bar.c'])
        # can't commit files in different trees; sane error
        self.run_bzr('commit -m newstuff branch/foo.c .', retcode=3)
        # can commit to branch - records foo.c only
        self.run_bzr('commit -m newstuff branch/foo.c')
        # can commit to branch - records bar.c
        self.run_bzr('commit -m newstuff branch')
        # No changes left
        self.run_bzr_error(["No changes to commit"], 'commit -m newstuff branch')

    def test_out_of_date_tree_commit(self):
        # check we get an error code and a clear message committing with an out
        # of date checkout
        tree = self.make_branch_and_tree('branch')
        # make a checkout
        checkout = tree.branch.create_checkout('checkout', lightweight=True)
        # commit to the original branch to make the checkout out of date
        tree.commit('message branch', allow_pointless=True)
        # now commit to the checkout should emit
        # ERROR: Out of date with the branch, 'bzr update' is suggested
        output = self.run_bzr('commit --unchanged -m checkout_message '
                             'checkout', retcode=3)
        self.assertEqual(output,
                         ('',
                          "bzr: ERROR: Working tree is out of date, please "
                          "run 'bzr update'.\n"))

    def test_local_commit_unbound(self):
        # a --local commit on an unbound branch is an error
        self.make_branch_and_tree('.')
        out, err = self.run_bzr('commit --local', retcode=3)
        self.assertEqualDiff('', out)
        self.assertEqualDiff('bzr: ERROR: Cannot perform local-only commits '
                             'on unbound branches.\n', err)

    def test_commit_a_text_merge_in_a_checkout(self):
        # checkouts perform multiple actions in a transaction across bond
        # branches and their master, and have been observed to fail in the
        # past. This is a user story reported to fail in bug #43959 where
        # a merge done in a checkout (using the update command) failed to
        # commit correctly.
        trunk = self.make_branch_and_tree('trunk')

        u1 = trunk.branch.create_checkout('u1')
        self.build_tree_contents([('u1/hosts', 'initial contents\n')])
        u1.add('hosts')
        self.run_bzr('commit -m add-hosts u1')

        u2 = trunk.branch.create_checkout('u2')
        self.build_tree_contents([('u2/hosts', 'altered in u2\n')])
        self.run_bzr('commit -m checkin-from-u2 u2')

        # make an offline commits
        self.build_tree_contents([('u1/hosts', 'first offline change in u1\n')])
        self.run_bzr('commit -m checkin-offline --local u1')

        # now try to pull in online work from u2, and then commit our offline
        # work as a merge
        # retcode 1 as we expect a text conflict
        self.run_bzr('update u1', retcode=1)
        self.assertFileEqual('''\
<<<<<<< TREE
first offline change in u1
=======
altered in u2
>>>>>>> MERGE-SOURCE
''',
                             'u1/hosts')

        self.run_bzr('resolved u1/hosts')
        # add a text change here to represent resolving the merge conflicts in
        # favour of a new version of the file not identical to either the u1
        # version or the u2 version.
        self.build_tree_contents([('u1/hosts', 'merge resolution\n')])
        self.run_bzr('commit -m checkin-merge-of-the-offline-work-from-u1 u1')

    def test_commit_exclude_excludes_modified_files(self):
        """Commit -x foo should ignore changes to foo."""
        tree = self.make_branch_and_tree('.')
        self.build_tree(['a', 'b', 'c'])
        tree.smart_add(['.'])
        out, err = self.run_bzr(['commit', '-m', 'test', '-x', 'b'])
        self.assertFalse('added b' in out)
        self.assertFalse('added b' in err)
        # If b was excluded it will still be 'added' in status.
        out, err = self.run_bzr(['added'])
        self.assertEqual('b\n', out)
        self.assertEqual('', err)

    def test_commit_exclude_twice_uses_both_rules(self):
        """Commit -x foo -x bar should ignore changes to foo and bar."""
        tree = self.make_branch_and_tree('.')
        self.build_tree(['a', 'b', 'c'])
        tree.smart_add(['.'])
        out, err = self.run_bzr(['commit', '-m', 'test', '-x', 'b', '-x', 'c'])
        self.assertFalse('added b' in out)
        self.assertFalse('added c' in out)
        self.assertFalse('added b' in err)
        self.assertFalse('added c' in err)
        # If b was excluded it will still be 'added' in status.
        out, err = self.run_bzr(['added'])
        self.assertTrue('b\n' in out)
        self.assertTrue('c\n' in out)
        self.assertEqual('', err)

    def test_commit_respects_spec_for_removals(self):
        """Commit with a file spec should only commit removals that match"""
        t = self.make_branch_and_tree('.')
        self.build_tree(['file-a', 'dir-a/', 'dir-a/file-b'])
        t.add(['file-a', 'dir-a', 'dir-a/file-b'])
        t.commit('Create')
        t.remove(['file-a', 'dir-a/file-b'])
        result = self.run_bzr('commit . -m removed-file-b',
                              working_dir='dir-a')[1]
        self.assertNotContainsRe(result, 'file-a')
        result = self.run_bzr('status', working_dir='dir-a')[0]
        self.assertContainsRe(result, 'removed:\n  file-a')

    def test_strict_commit(self):
        """Commit with --strict works if everything is known"""
        ignores._set_user_ignores([])
        tree = self.make_branch_and_tree('tree')
        self.build_tree(['tree/a'])
        tree.add('a')
        # A simple change should just work
        self.run_bzr('commit --strict -m adding-a', working_dir='tree')

    def test_strict_commit_no_changes(self):
        """commit --strict gives "no changes" if there is nothing to commit"""
        tree = self.make_branch_and_tree('tree')
        self.build_tree(['tree/a'])
        tree.add('a')
        tree.commit('adding a')

        # With no changes, it should just be 'no changes'
        # Make sure that commit is failing because there is nothing to do
        self.run_bzr_error(['No changes to commit'],
                           'commit --strict -m no-changes',
                           working_dir='tree')

        # But --strict doesn't care if you supply --unchanged
        self.run_bzr('commit --strict --unchanged -m no-changes',
                     working_dir='tree')

    def test_strict_commit_unknown(self):
        """commit --strict fails if a file is unknown"""
        tree = self.make_branch_and_tree('tree')
        self.build_tree(['tree/a'])
        tree.add('a')
        tree.commit('adding a')

        # Add one file so there is a change, but forget the other
        self.build_tree(['tree/b', 'tree/c'])
        tree.add('b')
        self.run_bzr_error(['Commit refused because there are unknown files'],
                           'commit --strict -m add-b',
                           working_dir='tree')

        # --no-strict overrides --strict
        self.run_bzr('commit --strict -m add-b --no-strict',
                     working_dir='tree')

    def test_fixes_bug_output(self):
        """commit --fixes=lp:23452 succeeds without output."""
        tree = self.make_branch_and_tree('tree')
        self.build_tree(['tree/hello.txt'])
        tree.add('hello.txt')
        output, err = self.run_bzr(
            'commit -m hello --fixes=lp:23452 tree/hello.txt')
        self.assertEqual('', output)
        self.assertContainsRe(err, 'Committing to: .*\n'
                              'added hello\.txt\n'
                              'Committed revision 1\.\n')

    def test_no_bugs_no_properties(self):
        """If no bugs are fixed, the bugs property is not set.

        see https://beta.launchpad.net/bzr/+bug/109613
        """
        tree = self.make_branch_and_tree('tree')
        self.build_tree(['tree/hello.txt'])
        tree.add('hello.txt')
        self.run_bzr( 'commit -m hello tree/hello.txt')
        # Get the revision properties, ignoring the branch-nick property, which
        # we don't care about for this test.
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
        properties = dict(last_rev.properties)
        del properties['branch-nick']
        self.assertFalse('bugs' in properties)

    def test_fixes_bug_sets_property(self):
        """commit --fixes=lp:234 sets the lp:234 revprop to 'fixed'."""
        tree = self.make_branch_and_tree('tree')
        self.build_tree(['tree/hello.txt'])
        tree.add('hello.txt')
        self.run_bzr('commit -m hello --fixes=lp:234 tree/hello.txt')

        # Get the revision properties, ignoring the branch-nick property, which
        # we don't care about for this test.
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
        properties = dict(last_rev.properties)
        del properties['branch-nick']

        self.assertEqual({'bugs': 'https://launchpad.net/bugs/234 fixed'},
                         properties)

    def test_fixes_multiple_bugs_sets_properties(self):
        """--fixes can be used more than once to show that bugs are fixed."""
        tree = self.make_branch_and_tree('tree')
        self.build_tree(['tree/hello.txt'])
        tree.add('hello.txt')
        self.run_bzr('commit -m hello --fixes=lp:123 --fixes=lp:235'
                     ' tree/hello.txt')

        # Get the revision properties, ignoring the branch-nick property, which
        # we don't care about for this test.
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
        properties = dict(last_rev.properties)
        del properties['branch-nick']

        self.assertEqual(
            {'bugs': 'https://launchpad.net/bugs/123 fixed\n'
                     'https://launchpad.net/bugs/235 fixed'},
            properties)

    def test_fixes_bug_with_alternate_trackers(self):
        """--fixes can be used on a properly configured branch to mark bug
        fixes on multiple trackers.
        """
        tree = self.make_branch_and_tree('tree')
        tree.branch.get_config().set_user_option(
            'trac_twisted_url', 'http://twistedmatrix.com/trac')
        self.build_tree(['tree/hello.txt'])
        tree.add('hello.txt')
        self.run_bzr('commit -m hello --fixes=lp:123 --fixes=twisted:235 tree/')

        # Get the revision properties, ignoring the branch-nick property, which
        # we don't care about for this test.
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
        properties = dict(last_rev.properties)
        del properties['branch-nick']

        self.assertEqual(
            {'bugs': 'https://launchpad.net/bugs/123 fixed\n'
                     'http://twistedmatrix.com/trac/ticket/235 fixed'},
            properties)

    def test_fixes_unknown_bug_prefix(self):
        tree = self.make_branch_and_tree('tree')
        self.build_tree(['tree/hello.txt'])
        tree.add('hello.txt')
        self.run_bzr_error(
            ["Unrecognized bug %s. Commit refused." % 'xxx:123'],
            'commit -m add-b --fixes=xxx:123',
            working_dir='tree')

    def test_fixes_bug_with_default_tracker(self):
        """commit --fixes=234 uses the default bug tracker."""
        tree = self.make_branch_and_tree('tree')
        self.build_tree(['tree/hello.txt'])
        tree.add('hello.txt')
        self.run_bzr_error(
            ["bzr: ERROR: No tracker specified for bug 123. Use the form "
            "'tracker:id' or specify a default bug tracker using the "
            "`bugtracker` option.\n"
            "See \"bzr help bugs\" for more information on this feature. "
            "Commit refused."],
            'commit -m add-b --fixes=123',
            working_dir='tree')
        tree.branch.get_config_stack().set("bugtracker", "lp")
        self.run_bzr('commit -m hello --fixes=234 tree/hello.txt')
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
        self.assertEqual('https://launchpad.net/bugs/234 fixed',
                         last_rev.properties['bugs'])

    def test_fixes_invalid_bug_number(self):
        tree = self.make_branch_and_tree('tree')
        self.build_tree(['tree/hello.txt'])
        tree.add('hello.txt')
        self.run_bzr_error(
            ["Did not understand bug identifier orange: Must be an integer. "
             "See \"bzr help bugs\" for more information on this feature.\n"
             "Commit refused."],
            'commit -m add-b --fixes=lp:orange',
            working_dir='tree')

    def test_fixes_invalid_argument(self):
        """Raise an appropriate error when the fixes argument isn't tag:id."""
        tree = self.make_branch_and_tree('tree')
        self.build_tree(['tree/hello.txt'])
        tree.add('hello.txt')
        self.run_bzr_error(
            [r"Invalid bug orange:apples:bananas. Must be in the form of "
             r"'tracker:id'\. See \"bzr help bugs\" for more information on "
             r"this feature.\nCommit refused\."],
            'commit -m add-b --fixes=orange:apples:bananas',
            working_dir='tree')

    def test_no_author(self):
        """If the author is not specified, the author property is not set."""
        tree = self.make_branch_and_tree('tree')
        self.build_tree(['tree/hello.txt'])
        tree.add('hello.txt')
        self.run_bzr( 'commit -m hello tree/hello.txt')
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
        properties = last_rev.properties
        self.assertFalse('author' in properties)

    def test_author_sets_property(self):
        """commit --author='John Doe <jdoe@example.com>' sets the author
           revprop.
        """
        tree = self.make_branch_and_tree('tree')
        self.build_tree(['tree/hello.txt'])
        tree.add('hello.txt')
        self.run_bzr(["commit", '-m', 'hello',
                      '--author', u'John D\xf6 <jdoe@example.com>',
                     "tree/hello.txt"])
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
        properties = last_rev.properties
        self.assertEqual(u'John D\xf6 <jdoe@example.com>', properties['authors'])

    def test_author_no_email(self):
        """Author's name without an email address is allowed, too."""
        tree = self.make_branch_and_tree('tree')
        self.build_tree(['tree/hello.txt'])
        tree.add('hello.txt')
        out, err = self.run_bzr("commit -m hello --author='John Doe' "
                                "tree/hello.txt")
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
        properties = last_rev.properties
        self.assertEqual('John Doe', properties['authors'])

    def test_multiple_authors(self):
        """Multiple authors can be specyfied, and all are stored."""
        tree = self.make_branch_and_tree('tree')
        self.build_tree(['tree/hello.txt'])
        tree.add('hello.txt')
        out, err = self.run_bzr("commit -m hello --author='John Doe' "
                                "--author='Jane Rey' tree/hello.txt")
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
        properties = last_rev.properties
        self.assertEqual('John Doe\nJane Rey', properties['authors'])

    def test_commit_time(self):
        tree = self.make_branch_and_tree('tree')
        self.build_tree(['tree/hello.txt'])
        tree.add('hello.txt')
        out, err = self.run_bzr("commit -m hello "
            "--commit-time='2009-10-10 08:00:00 +0100' tree/hello.txt")
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
        self.assertEqual(
            'Sat 2009-10-10 08:00:00 +0100',
            osutils.format_date(last_rev.timestamp, last_rev.timezone))
        
    def test_commit_time_bad_time(self):
        tree = self.make_branch_and_tree('tree')
        self.build_tree(['tree/hello.txt'])
        tree.add('hello.txt')
        out, err = self.run_bzr("commit -m hello "
            "--commit-time='NOT A TIME' tree/hello.txt", retcode=3)
        self.assertStartsWith(
            err, "bzr: ERROR: Could not parse --commit-time:")

    def test_commit_time_missing_tz(self):
        tree = self.make_branch_and_tree('tree')
        self.build_tree(['tree/hello.txt'])
        tree.add('hello.txt')
        out, err = self.run_bzr("commit -m hello "
            "--commit-time='2009-10-10 08:00:00' tree/hello.txt", retcode=3)
        self.assertStartsWith(
            err, "bzr: ERROR: Could not parse --commit-time:")
        # Test that it is actually checking and does not simply crash with
        # some other exception
        self.assertContainsString(err, "missing a timezone offset")

    def test_partial_commit_with_renames_in_tree(self):
        # this test illustrates bug #140419
        t = self.make_branch_and_tree('.')
        self.build_tree(['dir/', 'dir/a', 'test'])
        t.add(['dir/', 'dir/a', 'test'])
        t.commit('initial commit')
        # important part: file dir/a should change parent
        # and should appear before old parent
        # then during partial commit we have error
        # parent_id {dir-XXX} not in inventory
        t.rename_one('dir/a', 'a')
        self.build_tree_contents([('test', 'changes in test')])
        # partial commit
        out, err = self.run_bzr('commit test -m "partial commit"')
        self.assertEquals('', out)
        self.assertContainsRe(err, r'modified test\nCommitted revision 2.')

    def test_commit_readonly_checkout(self):
        # https://bugs.launchpad.net/bzr/+bug/129701
        # "UnlockableTransport error trying to commit in checkout of readonly
        # branch"
        self.make_branch('master')
        master = BzrDir.open_from_transport(
            self.get_readonly_transport('master')).open_branch()
        master.create_checkout('checkout')
        out, err = self.run_bzr(['commit', '--unchanged', '-mfoo', 'checkout'],
            retcode=3)
        self.assertContainsRe(err,
            r'^bzr: ERROR: Cannot lock.*readonly transport')

    def setup_editor(self):
        # Test that commit template hooks work
        if sys.platform == "win32":
            f = file('fed.bat', 'w')
            f.write('@rem dummy fed')
            f.close()
            self.overrideEnv('BZR_EDITOR', "fed.bat")
        else:
            f = file('fed.sh', 'wb')
            f.write('#!/bin/sh\n')
            f.close()
            os.chmod('fed.sh', 0755)
            self.overrideEnv('BZR_EDITOR', "./fed.sh")

    def setup_commit_with_template(self):
        self.setup_editor()
        msgeditor.hooks.install_named_hook("commit_message_template",
                lambda commit_obj, msg: "save me some typing\n", None)
        tree = self.make_branch_and_tree('tree')
        self.build_tree(['tree/hello.txt'])
        tree.add('hello.txt')
        return tree

    def test_edit_empty_message(self):
        tree = self.make_branch_and_tree('tree')
        self.setup_editor()
        self.build_tree(['tree/hello.txt'])
        tree.add('hello.txt')
        out, err = self.run_bzr("commit tree/hello.txt", retcode=3,
            stdin="y\n")
        self.assertContainsRe(err,
            "bzr: ERROR: Empty commit message specified")

    def test_commit_hook_template_accepted(self):
        tree = self.setup_commit_with_template()
        out, err = self.run_bzr("commit tree/hello.txt", stdin="y\n")
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
        self.assertEqual('save me some typing\n', last_rev.message)

    def test_commit_hook_template_rejected(self):
        tree = self.setup_commit_with_template()
        expected = tree.last_revision()
        out, err = self.run_bzr_error(["Empty commit message specified."
                  " Please specify a commit message with either"
                  " --message or --file or leave a blank message"
                  " with --message \"\"."],
            "commit tree/hello.txt", stdin="n\n")
        self.assertEqual(expected, tree.last_revision())

    def test_set_commit_message(self):
        msgeditor.hooks.install_named_hook("set_commit_message",
                lambda commit_obj, msg: "save me some typing\n", None)
        tree = self.make_branch_and_tree('tree')
        self.build_tree(['tree/hello.txt'])
        tree.add('hello.txt')
        out, err = self.run_bzr("commit tree/hello.txt")
        last_rev = tree.branch.repository.get_revision(tree.last_revision())
        self.assertEqual('save me some typing\n', last_rev.message)

    def test_commit_without_username(self):
        """Ensure commit error if username is not set.
        """
        self.run_bzr(['init', 'foo'])
        with open('foo/foo.txt', 'w') as f:
            f.write('hello')
        self.run_bzr(['add'], working_dir='foo')
        self.overrideEnv('EMAIL', None)
        self.overrideEnv('BZR_EMAIL', None)
        # Also, make sure that it's not inferred from mailname.
        self.overrideAttr(config, '_auto_user_id',
            lambda: (None, None))
        self.run_bzr_error(
            ['Unable to determine your name'],
            ['commit', '-m', 'initial'], working_dir='foo')

    def test_commit_recursive_checkout(self):
        """Ensure that a commit to a recursive checkout fails cleanly.
        """
        self.run_bzr(['init', 'test_branch'])
        self.run_bzr(['checkout', 'test_branch', 'test_checkout'])
        self.run_bzr(['bind', '.'], working_dir='test_checkout') # bind to self
        with open('test_checkout/foo.txt', 'w') as f:
            f.write('hello')
        self.run_bzr(['add'], working_dir='test_checkout')
        out, err = self.run_bzr_error(
            ['Branch.*test_checkout.*appears to be bound to itself'],
            ['commit', '-m', 'addedfoo'], working_dir='test_checkout')

    def test_mv_dirs_non_ascii(self):
        """Move directory with non-ascii name and containing files.

        Regression test for bug 185211.
        """
        tree = self.make_branch_and_tree('.')
        self.build_tree([u'abc\xa7/', u'abc\xa7/foo'])

        tree.add([u'abc\xa7/', u'abc\xa7/foo'])
        tree.commit('checkin')

        tree.rename_one(u'abc\xa7','abc')

        self.run_bzr('ci -m "non-ascii mv"')


class TestSmartServerCommit(TestCaseWithTransport):

    def test_commit_to_lightweight(self):
        self.setup_smart_server_with_call_log()
        t = self.make_branch_and_tree('from')
        for count in range(9):
            t.commit(message='commit %d' % count)
        out, err = self.run_bzr(['checkout', '--lightweight', self.get_url('from'),
            'target'])
        self.reset_smart_call_log()
        self.build_tree(['target/afile'])
        self.run_bzr(['add', 'target/afile'])
        out, err = self.run_bzr(['commit', '-m', 'do something', 'target'])
        # This figure represent the amount of work to perform this use case. It
        # is entirely ok to reduce this number if a test fails due to rpc_count
        # being too low. If rpc_count increases, more network roundtrips have
        # become necessary for this use case. Please do not adjust this number
        # upwards without agreement from bzr's network support maintainers.
        self.assertLength(211, self.hpss_calls)
        self.assertLength(2, self.hpss_connections)
        self.expectFailure("commit still uses VFS calls",
            self.assertThat, self.hpss_calls, ContainsNoVfsCalls)