/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2005-2012, 2016 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1246 by Martin Pool
- add very simple commit tests
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1246 by Martin Pool
- add very simple commit tests
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1246 by Martin Pool
- add very simple commit tests
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1246 by Martin Pool
- add very simple commit tests
16
17
18
import os
19
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
20
import breezy
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
21
from .. import (
6351.3.12 by Vincent Ladeuil
Use simpler config stacks and use strings as inputs to better respect the API.
22
    config,
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
23
    controldir,
1957.1.17 by John Arbash Meinel
Change tests that expect locking to fail to timeout sooner.
24
    errors,
25
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
26
from ..branch import Branch
6670.4.1 by Jelmer Vernooij
Update imports.
27
from ..bzr.bzrdir import BzrDirMetaFormat1
6699.1.1 by Jelmer Vernooij
Support excludes with "bzr commit -x".
28
from ..commit import (
29
    Commit,
30
    NullCommitReporter,
31
    filter_excluded,
32
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
33
from ..errors import (
5972.3.15 by Jelmer Vernooij
Use matchers.
34
    PointlessCommit,
35
    BzrError,
36
    SigningFailed,
37
    LockContention,
38
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
39
from . import (
6699.1.1 by Jelmer Vernooij
Support excludes with "bzr commit -x".
40
    TestCase,
5777.6.5 by Jelmer Vernooij
Add tests for lossy commit.
41
    TestCaseWithTransport,
42
    test_foreign,
43
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
44
from .features import (
5967.12.1 by Martin Pool
Move all test features into bzrlib.tests.features
45
    SymlinkFeature,
46
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
47
from .matchers import MatchesAncestry
1246 by Martin Pool
- add very simple commit tests
48
49
1257 by Martin Pool
doc
50
# TODO: Test commit with some added, and added-but-missing files
51
6393.1.1 by Vincent Ladeuil
Provides MemoryStack to simplify configuration setup in tests
52
class MustSignConfig(config.MemoryStack):
6351.3.12 by Vincent Ladeuil
Use simpler config stacks and use strings as inputs to better respect the API.
53
6393.1.1 by Vincent Ladeuil
Provides MemoryStack to simplify configuration setup in tests
54
    def __init__(self):
55
        super(MustSignConfig, self).__init__('''
6351.3.12 by Vincent Ladeuil
Use simpler config stacks and use strings as inputs to better respect the API.
56
gpg_signing_command=cat -
57
create_signatures=always
58
''')
1472 by Robert Collins
post commit hook, first pass implementation
59
60
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
61
class CapturingReporter(NullCommitReporter):
62
    """This reporter captures the calls made to it for evaluation later."""
63
64
    def __init__(self):
65
        # a list of the calls this received
66
        self.calls = []
67
68
    def snapshot_change(self, change, path):
69
        self.calls.append(('change', change, path))
70
71
    def deleted(self, file_id):
72
        self.calls.append(('deleted', file_id))
73
74
    def missing(self, path):
75
        self.calls.append(('missing', path))
76
77
    def renamed(self, change, old_path, new_path):
78
        self.calls.append(('renamed', change, old_path, new_path))
79
2789.2.1 by Ian Clatworthy
Make commit less verbose by default
80
    def is_verbose(self):
81
        return True
82
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
83
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
84
class TestCommit(TestCaseWithTransport):
1390 by Robert Collins
pair programming worx... merge integration and weave
85
1246 by Martin Pool
- add very simple commit tests
86
    def test_simple_commit(self):
87
        """Commit and check two versions of a single file."""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
88
        wt = self.make_branch_and_tree('.')
89
        b = wt.branch
6437.20.3 by Wouter van Heyst
mechanically replace file().write() pattern with a with-keyword version
90
        with file('hello', 'w') as f: f.write('hello world')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
91
        wt.add('hello')
6165.4.7 by Jelmer Vernooij
More fixes.
92
        rev1 = wt.commit(message='add hello')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
93
        file_id = wt.path2id('hello')
1246 by Martin Pool
- add very simple commit tests
94
6437.20.3 by Wouter van Heyst
mechanically replace file().write() pattern with a with-keyword version
95
        with file('hello', 'w') as f: f.write('version 2')
6165.4.7 by Jelmer Vernooij
More fixes.
96
        rev2 = wt.commit(message='commit 2')
1246 by Martin Pool
- add very simple commit tests
97
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
98
        eq = self.assertEqual
1246 by Martin Pool
- add very simple commit tests
99
        eq(b.revno(), 2)
6165.4.7 by Jelmer Vernooij
More fixes.
100
        rev = b.repository.get_revision(rev1)
1246 by Martin Pool
- add very simple commit tests
101
        eq(rev.message, 'add hello')
102
6165.4.7 by Jelmer Vernooij
More fixes.
103
        tree1 = b.repository.revision_tree(rev1)
3010.1.3 by Robert Collins
Lock RevisionTrees correctly in commit tests.
104
        tree1.lock_read()
1246 by Martin Pool
- add very simple commit tests
105
        text = tree1.get_file_text(file_id)
3010.1.3 by Robert Collins
Lock RevisionTrees correctly in commit tests.
106
        tree1.unlock()
107
        self.assertEqual('hello world', text)
1246 by Martin Pool
- add very simple commit tests
108
6165.4.7 by Jelmer Vernooij
More fixes.
109
        tree2 = b.repository.revision_tree(rev2)
3010.1.3 by Robert Collins
Lock RevisionTrees correctly in commit tests.
110
        tree2.lock_read()
111
        text = tree2.get_file_text(file_id)
112
        tree2.unlock()
113
        self.assertEqual('version 2', text)
1246 by Martin Pool
- add very simple commit tests
114
5777.6.5 by Jelmer Vernooij
Add tests for lossy commit.
115
    def test_commit_lossy_native(self):
116
        """Attempt a lossy commit to a native branch."""
117
        wt = self.make_branch_and_tree('.')
118
        b = wt.branch
6437.20.3 by Wouter van Heyst
mechanically replace file().write() pattern with a with-keyword version
119
        with file('hello', 'w') as f: f.write('hello world')
5777.6.5 by Jelmer Vernooij
Add tests for lossy commit.
120
        wt.add('hello')
121
        revid = wt.commit(message='add hello', rev_id='revid', lossy=True)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
122
        self.assertEqual('revid', revid)
5777.6.5 by Jelmer Vernooij
Add tests for lossy commit.
123
124
    def test_commit_lossy_foreign(self):
125
        """Attempt a lossy commit to a foreign branch."""
5777.6.6 by Jelmer Vernooij
Add lossy tests.
126
        test_foreign.register_dummy_foreign_for_test(self)
5777.6.5 by Jelmer Vernooij
Add tests for lossy commit.
127
        wt = self.make_branch_and_tree('.',
128
            format=test_foreign.DummyForeignVcsDirFormat())
129
        b = wt.branch
6437.20.3 by Wouter van Heyst
mechanically replace file().write() pattern with a with-keyword version
130
        with file('hello', 'w') as f: f.write('hello world')
5777.6.5 by Jelmer Vernooij
Add tests for lossy commit.
131
        wt.add('hello')
5777.6.6 by Jelmer Vernooij
Add lossy tests.
132
        revid = wt.commit(message='add hello', lossy=True,
133
            timestamp=1302659388, timezone=0)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
134
        self.assertEqual('dummy-v1:1302659388.0-0-UNKNOWN', revid)
5777.6.5 by Jelmer Vernooij
Add tests for lossy commit.
135
5777.7.5 by Jelmer Vernooij
Add tests for committing to a branch bound to a foreign branch.
136
    def test_commit_bound_lossy_foreign(self):
137
        """Attempt a lossy commit to a bzr branch bound to a foreign branch."""
138
        test_foreign.register_dummy_foreign_for_test(self)
139
        foreign_branch = self.make_branch('foreign',
140
            format=test_foreign.DummyForeignVcsDirFormat())
141
        wt = foreign_branch.create_checkout("local")
142
        b = wt.branch
6437.20.3 by Wouter van Heyst
mechanically replace file().write() pattern with a with-keyword version
143
        with file('local/hello', 'w') as f: f.write('hello world')
5777.7.5 by Jelmer Vernooij
Add tests for committing to a branch bound to a foreign branch.
144
        wt.add('hello')
145
        revid = wt.commit(message='add hello', lossy=True,
146
            timestamp=1302659388, timezone=0)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
147
        self.assertEqual('dummy-v1:1302659388.0-0-0', revid)
148
        self.assertEqual('dummy-v1:1302659388.0-0-0',
5777.7.5 by Jelmer Vernooij
Add tests for committing to a branch bound to a foreign branch.
149
            foreign_branch.last_revision())
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
150
        self.assertEqual('dummy-v1:1302659388.0-0-0',
5777.7.5 by Jelmer Vernooij
Add tests for committing to a branch bound to a foreign branch.
151
            wt.branch.last_revision())
152
4183.5.5 by Robert Collins
Enable record_iter_changes for cases where it can work.
153
    def test_missing_commit(self):
154
        """Test a commit with a missing file"""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
155
        wt = self.make_branch_and_tree('.')
156
        b = wt.branch
6437.20.3 by Wouter van Heyst
mechanically replace file().write() pattern with a with-keyword version
157
        with file('hello', 'w') as f: f.write('hello world')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
158
        wt.add(['hello'], ['hello-id'])
159
        wt.commit(message='add hello')
1247 by Martin Pool
- tests for deletion and removal of files in commits
160
161
        os.remove('hello')
6125.1.1 by Jelmer Vernooij
Report missing files as removed in 'bzr commit', rather than modified.
162
        reporter = CapturingReporter()
163
        wt.commit('removed hello', rev_id='rev2', reporter=reporter)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
164
        self.assertEqual(
6125.1.1 by Jelmer Vernooij
Report missing files as removed in 'bzr commit', rather than modified.
165
            [('missing', u'hello'), ('deleted', u'hello')],
166
            reporter.calls)
1247 by Martin Pool
- tests for deletion and removal of files in commits
167
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
168
        tree = b.repository.revision_tree('rev2')
1247 by Martin Pool
- tests for deletion and removal of files in commits
169
        self.assertFalse(tree.has_id('hello-id'))
170
2258.4.1 by Jelmer Vernooij
Add test that demonstrates a corner case bug in commit.
171
    def test_partial_commit_move(self):
3373.4.1 by John Arbash Meinel
Merge in and clean up the test for bug #83039.
172
        """Test a partial commit where a file was renamed but not committed.
173
174
        https://bugs.launchpad.net/bzr/+bug/83039
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
175
2258.4.1 by Jelmer Vernooij
Add test that demonstrates a corner case bug in commit.
176
        If not handled properly, commit will try to snapshot
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
177
        dialog.py with olive/ as a parent, while
2258.4.1 by Jelmer Vernooij
Add test that demonstrates a corner case bug in commit.
178
        olive/ has not been snapshotted yet.
179
        """
180
        wt = self.make_branch_and_tree('.')
181
        b = wt.branch
3373.4.1 by John Arbash Meinel
Merge in and clean up the test for bug #83039.
182
        self.build_tree(['annotate/', 'annotate/foo.py',
183
                         'olive/', 'olive/dialog.py'
184
                        ])
2258.4.1 by Jelmer Vernooij
Add test that demonstrates a corner case bug in commit.
185
        wt.add(['annotate', 'olive', 'annotate/foo.py', 'olive/dialog.py'])
186
        wt.commit(message='add files')
3373.4.1 by John Arbash Meinel
Merge in and clean up the test for bug #83039.
187
        wt.rename_one("olive/dialog.py", "aaa")
188
        self.build_tree_contents([('annotate/foo.py', 'modified\n')])
2258.4.1 by Jelmer Vernooij
Add test that demonstrates a corner case bug in commit.
189
        wt.commit('renamed hello', specific_files=["annotate"])
190
1253 by Martin Pool
- test that pointless commits are trapped
191
    def test_pointless_commit(self):
192
        """Commit refuses unless there are changes or it's forced."""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
193
        wt = self.make_branch_and_tree('.')
194
        b = wt.branch
6437.20.3 by Wouter van Heyst
mechanically replace file().write() pattern with a with-keyword version
195
        with file('hello', 'w') as f: f.write('hello')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
196
        wt.add(['hello'])
197
        wt.commit(message='add hello')
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
198
        self.assertEqual(b.revno(), 1)
1253 by Martin Pool
- test that pointless commits are trapped
199
        self.assertRaises(PointlessCommit,
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
200
                          wt.commit,
1253 by Martin Pool
- test that pointless commits are trapped
201
                          message='fails',
202
                          allow_pointless=False)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
203
        self.assertEqual(b.revno(), 1)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
204
1252 by Martin Pool
- add test for commit of an empty tree
205
    def test_commit_empty(self):
1253 by Martin Pool
- test that pointless commits are trapped
206
        """Commiting an empty tree works."""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
207
        wt = self.make_branch_and_tree('.')
208
        b = wt.branch
209
        wt.commit(message='empty tree', allow_pointless=True)
1253 by Martin Pool
- test that pointless commits are trapped
210
        self.assertRaises(PointlessCommit,
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
211
                          wt.commit,
1253 by Martin Pool
- test that pointless commits are trapped
212
                          message='empty tree',
213
                          allow_pointless=False)
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
214
        wt.commit(message='empty tree', allow_pointless=True)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
215
        self.assertEqual(b.revno(), 2)
1252 by Martin Pool
- add test for commit of an empty tree
216
217
    def test_selective_delete(self):
218
        """Selective commit in tree with deletions"""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
219
        wt = self.make_branch_and_tree('.')
220
        b = wt.branch
6437.20.3 by Wouter van Heyst
mechanically replace file().write() pattern with a with-keyword version
221
        with file('hello', 'w') as f: f.write('hello')
222
        with file('buongia', 'w') as f: f.write('buongia')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
223
        wt.add(['hello', 'buongia'],
1255 by Martin Pool
- more tests for selective commit of deletion
224
              ['hello-id', 'buongia-id'])
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
225
        wt.commit(message='add files',
1255 by Martin Pool
- more tests for selective commit of deletion
226
                 rev_id='test@rev-1')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
227
1254 by Martin Pool
- fix handling of selective commit with deleted files
228
        os.remove('hello')
6437.20.3 by Wouter van Heyst
mechanically replace file().write() pattern with a with-keyword version
229
        with file('buongia', 'w') as f: f.write('new text')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
230
        wt.commit(message='update text',
1254 by Martin Pool
- fix handling of selective commit with deleted files
231
                 specific_files=['buongia'],
1255 by Martin Pool
- more tests for selective commit of deletion
232
                 allow_pointless=False,
233
                 rev_id='test@rev-2')
1254 by Martin Pool
- fix handling of selective commit with deleted files
234
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
235
        wt.commit(message='remove hello',
1254 by Martin Pool
- fix handling of selective commit with deleted files
236
                 specific_files=['hello'],
1255 by Martin Pool
- more tests for selective commit of deletion
237
                 allow_pointless=False,
238
                 rev_id='test@rev-3')
1254 by Martin Pool
- fix handling of selective commit with deleted files
239
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
240
        eq = self.assertEqual
1254 by Martin Pool
- fix handling of selective commit with deleted files
241
        eq(b.revno(), 3)
1255 by Martin Pool
- more tests for selective commit of deletion
242
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
243
        tree2 = b.repository.revision_tree('test@rev-2')
3010.1.3 by Robert Collins
Lock RevisionTrees correctly in commit tests.
244
        tree2.lock_read()
245
        self.addCleanup(tree2.unlock)
1255 by Martin Pool
- more tests for selective commit of deletion
246
        self.assertTrue(tree2.has_filename('hello'))
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
247
        self.assertEqual(tree2.get_file_text('hello-id'), 'hello')
248
        self.assertEqual(tree2.get_file_text('buongia-id'), 'new text')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
249
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
250
        tree3 = b.repository.revision_tree('test@rev-3')
3010.1.3 by Robert Collins
Lock RevisionTrees correctly in commit tests.
251
        tree3.lock_read()
252
        self.addCleanup(tree3.unlock)
1255 by Martin Pool
- more tests for selective commit of deletion
253
        self.assertFalse(tree3.has_filename('hello'))
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
254
        self.assertEqual(tree3.get_file_text('buongia-id'), 'new text')
1285 by Martin Pool
- fix bug in committing files that are renamed but not modified
255
256
    def test_commit_rename(self):
257
        """Test commit of a revision where a file is renamed."""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
258
        tree = self.make_branch_and_tree('.')
259
        b = tree.branch
1185.38.7 by John Arbash Meinel
Updated build_tree to use fixed line-endings for tests which read the file contents and compare
260
        self.build_tree(['hello'], line_endings='binary')
1508.1.7 by Robert Collins
Move rename_one from Branch to WorkingTree. (Robert Collins).
261
        tree.add(['hello'], ['hello-id'])
262
        tree.commit(message='one', rev_id='test@rev-1', allow_pointless=False)
1285 by Martin Pool
- fix bug in committing files that are renamed but not modified
263
1508.1.7 by Robert Collins
Move rename_one from Branch to WorkingTree. (Robert Collins).
264
        tree.rename_one('hello', 'fruity')
265
        tree.commit(message='renamed', rev_id='test@rev-2', allow_pointless=False)
1285 by Martin Pool
- fix bug in committing files that are renamed but not modified
266
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
267
        eq = self.assertEqual
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
268
        tree1 = b.repository.revision_tree('test@rev-1')
3010.1.3 by Robert Collins
Lock RevisionTrees correctly in commit tests.
269
        tree1.lock_read()
270
        self.addCleanup(tree1.unlock)
1303 by Martin Pool
- commit updates entry_version
271
        eq(tree1.id2path('hello-id'), 'hello')
272
        eq(tree1.get_file_text('hello-id'), 'contents of hello\n')
1285 by Martin Pool
- fix bug in committing files that are renamed but not modified
273
        self.assertFalse(tree1.has_filename('fruity'))
5807.1.5 by Jelmer Vernooij
Fix more things to use tree objects.
274
        self.check_tree_shape(tree1, ['hello'])
275
        eq(tree1.get_file_revision('hello-id'), 'test@rev-1')
1285 by Martin Pool
- fix bug in committing files that are renamed but not modified
276
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
277
        tree2 = b.repository.revision_tree('test@rev-2')
3010.1.3 by Robert Collins
Lock RevisionTrees correctly in commit tests.
278
        tree2.lock_read()
279
        self.addCleanup(tree2.unlock)
1303 by Martin Pool
- commit updates entry_version
280
        eq(tree2.id2path('hello-id'), 'fruity')
281
        eq(tree2.get_file_text('hello-id'), 'contents of hello\n')
5807.1.5 by Jelmer Vernooij
Fix more things to use tree objects.
282
        self.check_tree_shape(tree2, ['fruity'])
283
        eq(tree2.get_file_revision('hello-id'), 'test@rev-2')
1291 by Martin Pool
- add test for moving files between directories
284
285
    def test_reused_rev_id(self):
1292 by Martin Pool
- add check that revision ids cannot be committed twice
286
        """Test that a revision id cannot be reused in a branch"""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
287
        wt = self.make_branch_and_tree('.')
288
        b = wt.branch
289
        wt.commit('initial', rev_id='test@rev-1', allow_pointless=True)
1292 by Martin Pool
- add check that revision ids cannot be committed twice
290
        self.assertRaises(Exception,
1534.4.35 by Robert Collins
Give branch its own basis tree and last_revision methods; deprecated branch.working_tree()
291
                          wt.commit,
1292 by Martin Pool
- add check that revision ids cannot be committed twice
292
                          message='reused id',
293
                          rev_id='test@rev-1',
294
                          allow_pointless=True)
1291 by Martin Pool
- add test for moving files between directories
295
296
    def test_commit_move(self):
297
        """Test commit of revisions with moved files and directories"""
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
298
        eq = self.assertEqual
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
299
        wt = self.make_branch_and_tree('.')
300
        b = wt.branch
1306 by Martin Pool
- tests that name_version is updated properly in renames/moves
301
        r1 = 'test@rev-1'
1291 by Martin Pool
- add test for moving files between directories
302
        self.build_tree(['hello', 'a/', 'b/'])
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
303
        wt.add(['hello', 'a', 'b'], ['hello-id', 'a-id', 'b-id'])
304
        wt.commit('initial', rev_id=r1, allow_pointless=False)
305
        wt.move(['hello'], 'a')
1306 by Martin Pool
- tests that name_version is updated properly in renames/moves
306
        r2 = 'test@rev-2'
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
307
        wt.commit('two', rev_id=r2, allow_pointless=False)
2255.2.136 by John Arbash Meinel
(James Westby) add read locks around read_working_inventory() in test_commit_move
308
        wt.lock_read()
309
        try:
5807.1.5 by Jelmer Vernooij
Fix more things to use tree objects.
310
            self.check_tree_shape(wt, ['a/', 'a/hello', 'b/'])
2255.2.136 by John Arbash Meinel
(James Westby) add read locks around read_working_inventory() in test_commit_move
311
        finally:
312
            wt.unlock()
1291 by Martin Pool
- add test for moving files between directories
313
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
314
        wt.move(['b'], 'a')
1306 by Martin Pool
- tests that name_version is updated properly in renames/moves
315
        r3 = 'test@rev-3'
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
316
        wt.commit('three', rev_id=r3, allow_pointless=False)
2255.2.136 by John Arbash Meinel
(James Westby) add read locks around read_working_inventory() in test_commit_move
317
        wt.lock_read()
318
        try:
5807.1.5 by Jelmer Vernooij
Fix more things to use tree objects.
319
            self.check_tree_shape(wt,
2545.3.2 by James Westby
Add a test for check_inventory_shape.
320
                                       ['a/', 'a/hello', 'a/b/'])
5807.1.5 by Jelmer Vernooij
Fix more things to use tree objects.
321
            self.check_tree_shape(b.repository.revision_tree(r3),
2545.3.2 by James Westby
Add a test for check_inventory_shape.
322
                                       ['a/', 'a/hello', 'a/b/'])
2255.2.136 by John Arbash Meinel
(James Westby) add read locks around read_working_inventory() in test_commit_move
323
        finally:
324
            wt.unlock()
1291 by Martin Pool
- add test for moving files between directories
325
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
326
        wt.move(['a/hello'], 'a/b')
1306 by Martin Pool
- tests that name_version is updated properly in renames/moves
327
        r4 = 'test@rev-4'
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
328
        wt.commit('four', rev_id=r4, allow_pointless=False)
2255.2.136 by John Arbash Meinel
(James Westby) add read locks around read_working_inventory() in test_commit_move
329
        wt.lock_read()
330
        try:
5807.1.5 by Jelmer Vernooij
Fix more things to use tree objects.
331
            self.check_tree_shape(wt, ['a/', 'a/b/hello', 'a/b/'])
2255.2.136 by John Arbash Meinel
(James Westby) add read locks around read_working_inventory() in test_commit_move
332
        finally:
333
            wt.unlock()
1306 by Martin Pool
- tests that name_version is updated properly in renames/moves
334
5035.3.1 by Jelmer Vernooij
Remove Repository.get_revision_inventory.
335
        inv = b.repository.get_inventory(r4)
1092.2.21 by Robert Collins
convert name_version to revision in inventory entries
336
        eq(inv['hello-id'].revision, r4)
337
        eq(inv['a-id'].revision, r1)
338
        eq(inv['b-id'].revision, r3)
2255.2.136 by John Arbash Meinel
(James Westby) add read locks around read_working_inventory() in test_commit_move
339
1246 by Martin Pool
- add very simple commit tests
340
    def test_removed_commit(self):
1185.16.72 by Martin Pool
[merge] from robert and fix up tests
341
        """Commit with a removed file"""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
342
        wt = self.make_branch_and_tree('.')
343
        b = wt.branch
6437.20.3 by Wouter van Heyst
mechanically replace file().write() pattern with a with-keyword version
344
        with file('hello', 'w') as f: f.write('hello world')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
345
        wt.add(['hello'], ['hello-id'])
346
        wt.commit(message='add hello')
1185.16.72 by Martin Pool
[merge] from robert and fix up tests
347
        wt.remove('hello')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
348
        wt.commit('removed hello', rev_id='rev2')
1247 by Martin Pool
- tests for deletion and removal of files in commits
349
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
350
        tree = b.repository.revision_tree('rev2')
1247 by Martin Pool
- tests for deletion and removal of files in commits
351
        self.assertFalse(tree.has_id('hello-id'))
1246 by Martin Pool
- add very simple commit tests
352
1256 by Martin Pool
- test that commits append to the tree's ancestry
353
    def test_committed_ancestry(self):
354
        """Test commit appends revisions to ancestry."""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
355
        wt = self.make_branch_and_tree('.')
356
        b = wt.branch
1256 by Martin Pool
- test that commits append to the tree's ancestry
357
        rev_ids = []
358
        for i in range(4):
6437.20.3 by Wouter van Heyst
mechanically replace file().write() pattern with a with-keyword version
359
            with file('hello', 'w') as f: f.write((str(i) * 4) + '\n')
1256 by Martin Pool
- test that commits append to the tree's ancestry
360
            if i == 0:
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
361
                wt.add(['hello'], ['hello-id'])
1256 by Martin Pool
- test that commits append to the tree's ancestry
362
            rev_id = 'test@rev-%d' % (i+1)
363
            rev_ids.append(rev_id)
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
364
            wt.commit(message='rev %d' % (i+1),
1256 by Martin Pool
- test that commits append to the tree's ancestry
365
                     rev_id=rev_id)
366
        for i in range(4):
5972.3.15 by Jelmer Vernooij
Use matchers.
367
            self.assertThat(rev_ids[:i+1],
368
                MatchesAncestry(b.repository, rev_ids[i]))
1416 by Robert Collins
when committing a specific file, include all its parents
369
370
    def test_commit_new_subdir_child_selective(self):
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
371
        wt = self.make_branch_and_tree('.')
372
        b = wt.branch
1416 by Robert Collins
when committing a specific file, include all its parents
373
        self.build_tree(['dir/', 'dir/file1', 'dir/file2'])
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
374
        wt.add(['dir', 'dir/file1', 'dir/file2'],
1416 by Robert Collins
when committing a specific file, include all its parents
375
              ['dirid', 'file1id', 'file2id'])
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
376
        wt.commit('dir/file1', specific_files=['dir/file1'], rev_id='1')
1185.67.2 by Aaron Bentley
Renamed Branch.storage to Branch.repository
377
        inv = b.repository.get_inventory('1')
1416 by Robert Collins
when committing a specific file, include all its parents
378
        self.assertEqual('1', inv['dirid'].revision)
379
        self.assertEqual('1', inv['file1id'].revision)
380
        # FIXME: This should raise a KeyError I think, rbc20051006
381
        self.assertRaises(BzrError, inv.__getitem__, 'file2id')
1185.16.65 by mbp at sourcefrog
- new commit --strict option
382
383
    def test_strict_commit(self):
384
        """Try and commit with unknown files and strict = True, should fail."""
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
385
        from ..errors import StrictCommitFailed
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
386
        wt = self.make_branch_and_tree('.')
387
        b = wt.branch
6437.20.3 by Wouter van Heyst
mechanically replace file().write() pattern with a with-keyword version
388
        with file('hello', 'w') as f: f.write('hello world')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
389
        wt.add('hello')
6437.20.3 by Wouter van Heyst
mechanically replace file().write() pattern with a with-keyword version
390
        with file('goodbye', 'w') as f: f.write('goodbye cruel world!')
1534.4.35 by Robert Collins
Give branch its own basis tree and last_revision methods; deprecated branch.working_tree()
391
        self.assertRaises(StrictCommitFailed, wt.commit,
1185.16.65 by mbp at sourcefrog
- new commit --strict option
392
            message='add hello but not goodbye', strict=True)
393
1185.22.4 by Michael Ellerman
Strict commit was a little .. ah .. too strict, oops :}
394
    def test_strict_commit_without_unknowns(self):
395
        """Try and commit with no unknown files and strict = True,
396
        should work."""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
397
        wt = self.make_branch_and_tree('.')
398
        b = wt.branch
6437.20.3 by Wouter van Heyst
mechanically replace file().write() pattern with a with-keyword version
399
        with file('hello', 'w') as f: f.write('hello world')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
400
        wt.add('hello')
401
        wt.commit(message='add hello', strict=True)
1185.22.4 by Michael Ellerman
Strict commit was a little .. ah .. too strict, oops :}
402
1185.16.65 by mbp at sourcefrog
- new commit --strict option
403
    def test_nonstrict_commit(self):
404
        """Try and commit with unknown files and strict = False, should work."""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
405
        wt = self.make_branch_and_tree('.')
406
        b = wt.branch
6437.20.3 by Wouter van Heyst
mechanically replace file().write() pattern with a with-keyword version
407
        with file('hello', 'w') as f: f.write('hello world')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
408
        wt.add('hello')
6437.20.3 by Wouter van Heyst
mechanically replace file().write() pattern with a with-keyword version
409
        with file('goodbye', 'w') as f: f.write('goodbye cruel world!')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
410
        wt.commit(message='add hello but not goodbye', strict=False)
1185.16.72 by Martin Pool
[merge] from robert and fix up tests
411
1185.22.4 by Michael Ellerman
Strict commit was a little .. ah .. too strict, oops :}
412
    def test_nonstrict_commit_without_unknowns(self):
413
        """Try and commit with no unknown files and strict = False,
414
        should work."""
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
415
        wt = self.make_branch_and_tree('.')
416
        b = wt.branch
6437.20.3 by Wouter van Heyst
mechanically replace file().write() pattern with a with-keyword version
417
        with file('hello', 'w') as f: f.write('hello world')
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
418
        wt.add('hello')
419
        wt.commit(message='add hello', strict=False)
1185.22.4 by Michael Ellerman
Strict commit was a little .. ah .. too strict, oops :}
420
1442.1.60 by Robert Collins
gpg sign commits if the policy says we need to
421
    def test_signed_commit(self):
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
422
        import breezy.gpg
423
        import breezy.commit as commit
424
        oldstrategy = breezy.gpg.GPGStrategy
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
425
        wt = self.make_branch_and_tree('.')
426
        branch = wt.branch
427
        wt.commit("base", allow_pointless=True, rev_id='A')
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
428
        self.assertFalse(branch.repository.has_signature_for_revision_id('A'))
1442.1.60 by Robert Collins
gpg sign commits if the policy says we need to
429
        try:
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
430
            from ..testament import Testament
1442.1.60 by Robert Collins
gpg sign commits if the policy says we need to
431
            # monkey patch gpg signing mechanism
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
432
            breezy.gpg.GPGStrategy = breezy.gpg.LoopbackGPGStrategy
6393.1.1 by Vincent Ladeuil
Provides MemoryStack to simplify configuration setup in tests
433
            conf = config.MemoryStack('''
434
gpg_signing_command=cat -
435
create_signatures=always
436
''')
437
            commit.Commit(config_stack=conf).commit(
438
                message="base", allow_pointless=True, rev_id='B',
439
                working_tree=wt)
1551.12.15 by Aaron Bentley
add header/trailer to fake clearsigned texts
440
            def sign(text):
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
441
                return breezy.gpg.LoopbackGPGStrategy(None).sign(text)
1551.12.15 by Aaron Bentley
add header/trailer to fake clearsigned texts
442
            self.assertEqual(sign(Testament.from_revision(branch.repository,
6351.3.12 by Vincent Ladeuil
Use simpler config stacks and use strings as inputs to better respect the API.
443
                                                          'B').as_short_text()),
1563.2.31 by Robert Collins
Convert Knit repositories to use knits.
444
                             branch.repository.get_signature_text('B'))
1442.1.60 by Robert Collins
gpg sign commits if the policy says we need to
445
        finally:
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
446
            breezy.gpg.GPGStrategy = oldstrategy
1442.1.62 by Robert Collins
Allow creation of testaments from uncommitted data, and use that to get signatures before committing revisions.
447
448
    def test_commit_failed_signature(self):
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
449
        import breezy.gpg
450
        import breezy.commit as commit
451
        oldstrategy = breezy.gpg.GPGStrategy
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
452
        wt = self.make_branch_and_tree('.')
453
        branch = wt.branch
454
        wt.commit("base", allow_pointless=True, rev_id='A')
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
455
        self.assertFalse(branch.repository.has_signature_for_revision_id('A'))
1442.1.62 by Robert Collins
Allow creation of testaments from uncommitted data, and use that to get signatures before committing revisions.
456
        try:
457
            # monkey patch gpg signing mechanism
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
458
            breezy.gpg.GPGStrategy = breezy.gpg.DisabledGPGStrategy
6393.1.1 by Vincent Ladeuil
Provides MemoryStack to simplify configuration setup in tests
459
            conf = config.MemoryStack('''
460
gpg_signing_command=cat -
461
create_signatures=always
462
''')
1442.1.62 by Robert Collins
Allow creation of testaments from uncommitted data, and use that to get signatures before committing revisions.
463
            self.assertRaises(SigningFailed,
6393.1.1 by Vincent Ladeuil
Provides MemoryStack to simplify configuration setup in tests
464
                              commit.Commit(config_stack=conf).commit,
1534.4.34 by Robert Collins
Fix remaining uses of deprecated apis within bzrlib.
465
                              message="base",
1442.1.62 by Robert Collins
Allow creation of testaments from uncommitted data, and use that to get signatures before committing revisions.
466
                              allow_pointless=True,
1534.4.34 by Robert Collins
Fix remaining uses of deprecated apis within bzrlib.
467
                              rev_id='B',
468
                              working_tree=wt)
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
469
            branch = Branch.open(self.get_url('.'))
6165.4.7 by Jelmer Vernooij
More fixes.
470
            self.assertEqual(branch.last_revision(), 'A')
5784.1.1 by Martin Pool
Stop using failIf, failUnless, etc
471
            self.assertFalse(branch.repository.has_revision('B'))
1442.1.62 by Robert Collins
Allow creation of testaments from uncommitted data, and use that to get signatures before committing revisions.
472
        finally:
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
473
            breezy.gpg.GPGStrategy = oldstrategy
1472 by Robert Collins
post commit hook, first pass implementation
474
475
    def test_commit_invokes_hooks(self):
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
476
        import breezy.commit as commit
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
477
        wt = self.make_branch_and_tree('.')
478
        branch = wt.branch
1472 by Robert Collins
post commit hook, first pass implementation
479
        calls = []
480
        def called(branch, rev_id):
481
            calls.append('called')
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
482
        breezy.ahook = called
1472 by Robert Collins
post commit hook, first pass implementation
483
        try:
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
484
            conf = config.MemoryStack('post_commit=breezy.ahook breezy.ahook')
6393.1.1 by Vincent Ladeuil
Provides MemoryStack to simplify configuration setup in tests
485
            commit.Commit(config_stack=conf).commit(
486
                message = "base", allow_pointless=True, rev_id='A',
487
                working_tree = wt)
1472 by Robert Collins
post commit hook, first pass implementation
488
            self.assertEqual(['called', 'called'], calls)
489
        finally:
6622.1.34 by Jelmer Vernooij
Rename brzlib => breezy.
490
            del breezy.ahook
1593.1.1 by Robert Collins
Move responsibility for setting branch nickname in commits to the WorkingTree convenience function.
491
492
    def test_commit_object_doesnt_set_nick(self):
493
        # using the Commit object directly does not set the branch nick.
494
        wt = self.make_branch_and_tree('.')
495
        c = Commit()
2149.1.3 by Aaron Bentley
Updates from review comments
496
        c.commit(working_tree=wt, message='empty tree', allow_pointless=True)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
497
        self.assertEqual(wt.branch.revno(), 1)
1593.1.1 by Robert Collins
Move responsibility for setting branch nickname in commits to the WorkingTree convenience function.
498
        self.assertEqual({},
499
                         wt.branch.repository.get_revision(
500
                            wt.branch.last_revision()).properties)
501
1614.1.1 by Aaron Bentley
Fixed master locking in commit
502
    def test_safe_master_lock(self):
503
        os.mkdir('master')
504
        master = BzrDirMetaFormat1().initialize('master')
505
        master.create_repository()
506
        master_branch = master.create_branch()
507
        master.create_workingtree()
508
        bound = master.sprout('bound')
509
        wt = bound.open_workingtree()
6404.6.7 by Vincent Ladeuil
Change set/remove to require a lock for the branch config files.
510
        wt.branch.set_bound_location(os.path.realpath('master'))
1614.1.1 by Aaron Bentley
Fixed master locking in commit
511
        master_branch.lock_write()
1658.1.5 by Martin Pool
Release more locks taken during test suite
512
        try:
513
            self.assertRaises(LockContention, wt.commit, 'silly')
514
        finally:
515
            master_branch.unlock()
1668.1.3 by Martin Pool
[patch] use the correct transaction when committing snapshot (Malone: #43959)
516
517
    def test_commit_bound_merge(self):
518
        # see bug #43959; commit of a merge in a bound branch fails to push
519
        # the new commit into the master
520
        master_branch = self.make_branch('master')
521
        bound_tree = self.make_branch_and_tree('bound')
522
        bound_tree.branch.bind(master_branch)
523
524
        self.build_tree_contents([('bound/content_file', 'initial contents\n')])
525
        bound_tree.add(['content_file'])
526
        bound_tree.commit(message='woo!')
527
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
528
        other_bzrdir = master_branch.controldir.sprout('other')
1668.1.3 by Martin Pool
[patch] use the correct transaction when committing snapshot (Malone: #43959)
529
        other_tree = other_bzrdir.open_workingtree()
530
4775.1.1 by Martin Pool
Remove several 'the the' typos
531
        # do a commit to the other branch changing the content file so
1668.1.3 by Martin Pool
[patch] use the correct transaction when committing snapshot (Malone: #43959)
532
        # that our commit after merging will have a merged revision in the
533
        # content file history.
534
        self.build_tree_contents([('other/content_file', 'change in other\n')])
535
        other_tree.commit('change in other')
536
537
        # do a merge into the bound branch from other, and then change the
538
        # content file locally to force a new revision (rather than using the
539
        # revision from other). This forces extra processing in commit.
1979.2.1 by Robert Collins
(robertc) adds a convenience method "merge_from_branch" to WorkingTree.
540
        bound_tree.merge_from_branch(other_tree.branch)
1668.1.3 by Martin Pool
[patch] use the correct transaction when committing snapshot (Malone: #43959)
541
        self.build_tree_contents([('bound/content_file', 'change in bound\n')])
542
543
        # before #34959 was fixed, this failed with 'revision not present in
544
        # weave' when trying to implicitly push from the bound branch to the master
545
        bound_tree.commit(message='commit of merge in bound tree')
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
546
547
    def test_commit_reporting_after_merge(self):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
548
        # when doing a commit of a merge, the reporter needs to still
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
549
        # be called for each item that is added/removed/deleted.
550
        this_tree = self.make_branch_and_tree('this')
551
        # we need a bunch of files and dirs, to perform one action on each.
552
        self.build_tree([
553
            'this/dirtorename/',
554
            'this/dirtoreparent/',
555
            'this/dirtoleave/',
556
            'this/dirtoremove/',
557
            'this/filetoreparent',
558
            'this/filetorename',
559
            'this/filetomodify',
560
            'this/filetoremove',
561
            'this/filetoleave']
562
            )
563
        this_tree.add([
564
            'dirtorename',
565
            'dirtoreparent',
566
            'dirtoleave',
567
            'dirtoremove',
568
            'filetoreparent',
569
            'filetorename',
570
            'filetomodify',
571
            'filetoremove',
572
            'filetoleave']
573
            )
574
        this_tree.commit('create_files')
6653.6.1 by Jelmer Vernooij
Rename a number of attributes from bzrdir to controldir.
575
        other_dir = this_tree.controldir.sprout('other')
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
576
        other_tree = other_dir.open_workingtree()
577
        other_tree.lock_write()
578
        # perform the needed actions on the files and dirs.
579
        try:
580
            other_tree.rename_one('dirtorename', 'renameddir')
581
            other_tree.rename_one('dirtoreparent', 'renameddir/reparenteddir')
582
            other_tree.rename_one('filetorename', 'renamedfile')
583
            other_tree.rename_one('filetoreparent', 'renameddir/reparentedfile')
584
            other_tree.remove(['dirtoremove', 'filetoremove'])
585
            self.build_tree_contents([
586
                ('other/newdir/', ),
587
                ('other/filetomodify', 'new content'),
588
                ('other/newfile', 'new file content')])
589
            other_tree.add('newfile')
590
            other_tree.add('newdir/')
591
            other_tree.commit('modify all sample files and dirs.')
592
        finally:
593
            other_tree.unlock()
1979.2.1 by Robert Collins
(robertc) adds a convenience method "merge_from_branch" to WorkingTree.
594
        this_tree.merge_from_branch(other_tree.branch)
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
595
        reporter = CapturingReporter()
596
        this_tree.commit('do the commit', reporter=reporter)
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
597
        expected = {
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
598
            ('change', 'modified', 'filetomodify'),
599
            ('change', 'added', 'newdir'),
600
            ('change', 'added', 'newfile'),
601
            ('renamed', 'renamed', 'dirtorename', 'renameddir'),
2829.1.1 by Ian Clatworthy
re-apply Aaron's fix for #94975 (Ian Clatworthy)
602
            ('renamed', 'renamed', 'filetorename', 'renamedfile'),
1668.1.5 by Martin Pool
[broken] fix up display of files changed by a commit
603
            ('renamed', 'renamed', 'dirtoreparent', 'renameddir/reparenteddir'),
604
            ('renamed', 'renamed', 'filetoreparent', 'renameddir/reparentedfile'),
605
            ('deleted', 'dirtoremove'),
606
            ('deleted', 'filetoremove'),
6619.3.12 by Jelmer Vernooij
Use 2to3 set_literal fixer.
607
            }
4183.5.5 by Robert Collins
Enable record_iter_changes for cases where it can work.
608
        result = set(reporter.calls)
609
        missing = expected - result
610
        new = result - expected
611
        self.assertEqual((set(), set()), (missing, new))
1551.7.24 by Aaron Bentley
Ensure commit respects file spec when committing removals
612
613
    def test_commit_removals_respects_filespec(self):
614
        """Commit respects the specified_files for removals."""
615
        tree = self.make_branch_and_tree('.')
616
        self.build_tree(['a', 'b'])
617
        tree.add(['a', 'b'])
618
        tree.commit('added a, b')
619
        tree.remove(['a', 'b'])
1906.1.1 by Robert Collins
(robertc) Trivial change to test_commit_removals_respects_filespec to be easir to read.
620
        tree.commit('removed a', specific_files='a')
2255.2.148 by John Arbash Meinel
lock a basis tree during a commit test.
621
        basis = tree.basis_tree()
622
        tree.lock_read()
623
        try:
624
            self.assertIs(None, basis.path2id('a'))
625
            self.assertFalse(basis.path2id('b') is None)
626
        finally:
627
            tree.unlock()
1864.2.1 by John Arbash Meinel
Commit timestamp restricted to 1ms precision.
628
629
    def test_commit_saves_1ms_timestamp(self):
630
        """Passing in a timestamp is saved with 1ms resolution"""
631
        tree = self.make_branch_and_tree('.')
632
        self.build_tree(['a'])
633
        tree.add('a')
634
        tree.commit('added a', timestamp=1153248633.4186721, timezone=0,
635
                    rev_id='a1')
636
637
        rev = tree.branch.repository.get_revision('a1')
638
        self.assertEqual(1153248633.419, rev.timestamp)
639
640
    def test_commit_has_1ms_resolution(self):
641
        """Allowing commit to generate the timestamp also has 1ms resolution"""
642
        tree = self.make_branch_and_tree('.')
643
        self.build_tree(['a'])
644
        tree.add('a')
645
        tree.commit('added a', rev_id='a1')
646
647
        rev = tree.branch.repository.get_revision('a1')
648
        timestamp = rev.timestamp
649
        timestamp_1ms = round(timestamp, 3)
650
        self.assertEqual(timestamp_1ms, timestamp)
1959.4.1 by Aaron Bentley
Correctly handle all file kind changes
651
2255.2.135 by John Arbash Meinel
Add locking in the test_commit_kind_changes test.
652
    def assertBasisTreeKind(self, kind, tree, file_id):
653
        basis = tree.basis_tree()
654
        basis.lock_read()
655
        try:
656
            self.assertEqual(kind, basis.kind(file_id))
657
        finally:
658
            basis.unlock()
659
1959.4.1 by Aaron Bentley
Correctly handle all file kind changes
660
    def test_commit_kind_changes(self):
2949.5.1 by Alexander Belchenko
selftest: use SymlinkFeature instead of TestSkipped where appropriate
661
        self.requireFeature(SymlinkFeature)
1959.4.1 by Aaron Bentley
Correctly handle all file kind changes
662
        tree = self.make_branch_and_tree('.')
663
        os.symlink('target', 'name')
664
        tree.add('name', 'a-file-id')
665
        tree.commit('Added a symlink')
2255.2.135 by John Arbash Meinel
Add locking in the test_commit_kind_changes test.
666
        self.assertBasisTreeKind('symlink', tree, 'a-file-id')
1959.4.1 by Aaron Bentley
Correctly handle all file kind changes
667
668
        os.unlink('name')
669
        self.build_tree(['name'])
670
        tree.commit('Changed symlink to file')
2255.2.135 by John Arbash Meinel
Add locking in the test_commit_kind_changes test.
671
        self.assertBasisTreeKind('file', tree, 'a-file-id')
1959.4.1 by Aaron Bentley
Correctly handle all file kind changes
672
673
        os.unlink('name')
674
        os.symlink('target', 'name')
675
        tree.commit('file to symlink')
2255.2.135 by John Arbash Meinel
Add locking in the test_commit_kind_changes test.
676
        self.assertBasisTreeKind('symlink', tree, 'a-file-id')
1959.4.1 by Aaron Bentley
Correctly handle all file kind changes
677
678
        os.unlink('name')
679
        os.mkdir('name')
680
        tree.commit('symlink to directory')
2255.2.135 by John Arbash Meinel
Add locking in the test_commit_kind_changes test.
681
        self.assertBasisTreeKind('directory', tree, 'a-file-id')
1959.4.1 by Aaron Bentley
Correctly handle all file kind changes
682
683
        os.rmdir('name')
684
        os.symlink('target', 'name')
685
        tree.commit('directory to symlink')
2255.2.135 by John Arbash Meinel
Add locking in the test_commit_kind_changes test.
686
        self.assertBasisTreeKind('symlink', tree, 'a-file-id')
1959.4.1 by Aaron Bentley
Correctly handle all file kind changes
687
688
        # prepare for directory <-> file tests
689
        os.unlink('name')
690
        os.mkdir('name')
691
        tree.commit('symlink to directory')
2255.2.135 by John Arbash Meinel
Add locking in the test_commit_kind_changes test.
692
        self.assertBasisTreeKind('directory', tree, 'a-file-id')
1959.4.1 by Aaron Bentley
Correctly handle all file kind changes
693
694
        os.rmdir('name')
695
        self.build_tree(['name'])
696
        tree.commit('Changed directory to file')
2255.2.135 by John Arbash Meinel
Add locking in the test_commit_kind_changes test.
697
        self.assertBasisTreeKind('file', tree, 'a-file-id')
1959.4.1 by Aaron Bentley
Correctly handle all file kind changes
698
699
        os.unlink('name')
700
        os.mkdir('name')
701
        tree.commit('file to directory')
2255.2.135 by John Arbash Meinel
Add locking in the test_commit_kind_changes test.
702
        self.assertBasisTreeKind('directory', tree, 'a-file-id')
1959.4.2 by Aaron Bentley
Merge bzr.dev
703
1551.8.29 by Aaron Bentley
Stop accepting non-existant files in commit (#50793)
704
    def test_commit_unversioned_specified(self):
705
        """Commit should raise if specified files isn't in basis or worktree"""
706
        tree = self.make_branch_and_tree('.')
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
707
        self.assertRaises(errors.PathsNotVersionedError, tree.commit,
1551.8.29 by Aaron Bentley
Stop accepting non-existant files in commit (#50793)
708
                          'message', specific_files=['bogus'])
2149.1.1 by Aaron Bentley
Provide a message_callback parameter to tree.commit
709
710
    class Callback(object):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
711
2149.1.4 by Aaron Bentley
Add additional test that callback is called with a Commit instance
712
        def __init__(self, message, testcase):
2149.1.1 by Aaron Bentley
Provide a message_callback parameter to tree.commit
713
            self.called = False
714
            self.message = message
2149.1.4 by Aaron Bentley
Add additional test that callback is called with a Commit instance
715
            self.testcase = testcase
2149.1.1 by Aaron Bentley
Provide a message_callback parameter to tree.commit
716
2149.1.4 by Aaron Bentley
Add additional test that callback is called with a Commit instance
717
        def __call__(self, commit_obj):
2149.1.1 by Aaron Bentley
Provide a message_callback parameter to tree.commit
718
            self.called = True
2149.1.4 by Aaron Bentley
Add additional test that callback is called with a Commit instance
719
            self.testcase.assertTrue(isinstance(commit_obj, Commit))
2149.1.1 by Aaron Bentley
Provide a message_callback parameter to tree.commit
720
            return self.message
721
722
    def test_commit_callback(self):
723
        """Commit should invoke a callback to get the message"""
724
725
        tree = self.make_branch_and_tree('.')
726
        try:
727
            tree.commit()
6619.3.2 by Jelmer Vernooij
Apply 2to3 except fix.
728
        except Exception as e:
2149.1.1 by Aaron Bentley
Provide a message_callback parameter to tree.commit
729
            self.assertTrue(isinstance(e, BzrError))
2149.1.3 by Aaron Bentley
Updates from review comments
730
            self.assertEqual('The message or message_callback keyword'
731
                             ' parameter is required for commit().', str(e))
2149.1.1 by Aaron Bentley
Provide a message_callback parameter to tree.commit
732
        else:
733
            self.fail('exception not raised')
2149.1.4 by Aaron Bentley
Add additional test that callback is called with a Commit instance
734
        cb = self.Callback(u'commit 1', self)
2149.1.1 by Aaron Bentley
Provide a message_callback parameter to tree.commit
735
        tree.commit(message_callback=cb)
736
        self.assertTrue(cb.called)
737
        repository = tree.branch.repository
738
        message = repository.get_revision(tree.last_revision()).message
739
        self.assertEqual('commit 1', message)
740
741
    def test_no_callback_pointless(self):
742
        """Callback should not be invoked for pointless commit"""
743
        tree = self.make_branch_and_tree('.')
2149.1.4 by Aaron Bentley
Add additional test that callback is called with a Commit instance
744
        cb = self.Callback(u'commit 2', self)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
745
        self.assertRaises(PointlessCommit, tree.commit, message_callback=cb,
2149.1.1 by Aaron Bentley
Provide a message_callback parameter to tree.commit
746
                          allow_pointless=False)
747
        self.assertFalse(cb.called)
748
749
    def test_no_callback_netfailure(self):
750
        """Callback should not be invoked if connectivity fails"""
2149.1.3 by Aaron Bentley
Updates from review comments
751
        tree = self.make_branch_and_tree('.')
2149.1.4 by Aaron Bentley
Add additional test that callback is called with a Commit instance
752
        cb = self.Callback(u'commit 2', self)
2149.1.3 by Aaron Bentley
Updates from review comments
753
        repository = tree.branch.repository
2149.1.1 by Aaron Bentley
Provide a message_callback parameter to tree.commit
754
        # simulate network failure
4617.2.1 by Robert Collins
Fix test_commit for use with 2a as the default repository.
755
        def raise_(self, arg, arg2, arg3=None, arg4=None):
2149.1.1 by Aaron Bentley
Provide a message_callback parameter to tree.commit
756
            raise errors.NoSuchFile('foo')
757
        repository.add_inventory = raise_
4617.2.1 by Robert Collins
Fix test_commit for use with 2a as the default repository.
758
        repository.add_inventory_by_delta = raise_
2149.1.1 by Aaron Bentley
Provide a message_callback parameter to tree.commit
759
        self.assertRaises(errors.NoSuchFile, tree.commit, message_callback=cb)
760
        self.assertFalse(cb.called)
1551.15.9 by Aaron Bentley
Better error for selected-file commit of merges
761
762
    def test_selected_file_merge_commit(self):
763
        """Ensure the correct error is raised"""
764
        tree = self.make_branch_and_tree('foo')
765
        # pending merge would turn into a left parent
766
        tree.commit('commit 1')
767
        tree.add_parent_tree_id('example')
768
        self.build_tree(['foo/bar', 'foo/baz'])
769
        tree.add(['bar', 'baz'])
770
        err = self.assertRaises(errors.CannotCommitSelectedFileMerge,
771
            tree.commit, 'commit 2', specific_files=['bar', 'baz'])
772
        self.assertEqual(['bar', 'baz'], err.files)
773
        self.assertEqual('Selected-file commit of merges is not supported'
774
                         ' yet: files bar, baz', str(err))
2671.2.2 by Lukáš Lalinský
Move setting of the author revision property to MutableTree.commit. Don't use try/except KeyError in LongLogFormatter to display authors and branch-nicks. Removed warning about missing e-mail in the authors name.
775
2829.1.1 by Ian Clatworthy
re-apply Aaron's fix for #94975 (Ian Clatworthy)
776
    def test_commit_ordering(self):
777
        """Test of corner-case commit ordering error"""
778
        tree = self.make_branch_and_tree('.')
779
        self.build_tree(['a/', 'a/z/', 'a/c/', 'a/z/x', 'a/z/y'])
780
        tree.add(['a/', 'a/z/', 'a/c/', 'a/z/x', 'a/z/y'])
781
        tree.commit('setup')
782
        self.build_tree(['a/c/d/'])
783
        tree.add('a/c/d')
784
        tree.rename_one('a/z/x', 'a/c/d/x')
785
        tree.commit('test', specific_files=['a/z/y'])
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
786
2671.2.2 by Lukáš Lalinský
Move setting of the author revision property to MutableTree.commit. Don't use try/except KeyError in LongLogFormatter to display authors and branch-nicks. Removed warning about missing e-mail in the authors name.
787
    def test_commit_no_author(self):
788
        """The default kwarg author in MutableTree.commit should not add
789
        the 'author' revision property.
790
        """
791
        tree = self.make_branch_and_tree('foo')
792
        rev_id = tree.commit('commit 1')
793
        rev = tree.branch.repository.get_revision(rev_id)
794
        self.assertFalse('author' in rev.properties)
4056.2.3 by James Westby
Use a new "authors" revision property to allow multiple authors
795
        self.assertFalse('authors' in rev.properties)
2671.2.2 by Lukáš Lalinský
Move setting of the author revision property to MutableTree.commit. Don't use try/except KeyError in LongLogFormatter to display authors and branch-nicks. Removed warning about missing e-mail in the authors name.
796
797
    def test_commit_author(self):
6630.1.1 by Jelmer Vernooij
Remove deprecated functionality.
798
        """Passing a non-empty authors kwarg to MutableTree.commit should add
2671.2.2 by Lukáš Lalinský
Move setting of the author revision property to MutableTree.commit. Don't use try/except KeyError in LongLogFormatter to display authors and branch-nicks. Removed warning about missing e-mail in the authors name.
799
        the 'author' revision property.
800
        """
801
        tree = self.make_branch_and_tree('foo')
6630.1.1 by Jelmer Vernooij
Remove deprecated functionality.
802
        rev_id = tree.commit(
803
            'commit 1',
804
            authors=['John Doe <jdoe@example.com>'])
2671.2.2 by Lukáš Lalinský
Move setting of the author revision property to MutableTree.commit. Don't use try/except KeyError in LongLogFormatter to display authors and branch-nicks. Removed warning about missing e-mail in the authors name.
805
        rev = tree.branch.repository.get_revision(rev_id)
806
        self.assertEqual('John Doe <jdoe@example.com>',
4056.2.3 by James Westby
Use a new "authors" revision property to allow multiple authors
807
                         rev.properties['authors'])
808
        self.assertFalse('author' in rev.properties)
3113.6.7 by Aaron Bentley
Fix commit for a checkout sharing a repo with its branch (abentley, #177592)
809
4056.2.1 by James Westby
Allow specifying multiple authors for a revision.
810
    def test_commit_empty_authors_list(self):
811
        """Passing an empty list to authors shouldn't add the property."""
812
        tree = self.make_branch_and_tree('foo')
813
        rev_id = tree.commit('commit 1', authors=[])
814
        rev = tree.branch.repository.get_revision(rev_id)
815
        self.assertFalse('author' in rev.properties)
4056.2.3 by James Westby
Use a new "authors" revision property to allow multiple authors
816
        self.assertFalse('authors' in rev.properties)
4056.2.1 by James Westby
Allow specifying multiple authors for a revision.
817
818
    def test_multiple_authors(self):
819
        tree = self.make_branch_and_tree('foo')
820
        rev_id = tree.commit('commit 1',
821
                authors=['John Doe <jdoe@example.com>',
822
                         'Jane Rey <jrey@example.com>'])
823
        rev = tree.branch.repository.get_revision(rev_id)
824
        self.assertEqual('John Doe <jdoe@example.com>\n'
4056.2.3 by James Westby
Use a new "authors" revision property to allow multiple authors
825
                'Jane Rey <jrey@example.com>', rev.properties['authors'])
826
        self.assertFalse('author' in rev.properties)
4056.2.1 by James Westby
Allow specifying multiple authors for a revision.
827
828
    def test_author_with_newline_rejected(self):
829
        tree = self.make_branch_and_tree('foo')
830
        self.assertRaises(AssertionError, tree.commit, 'commit 1',
831
                authors=['John\nDoe <jdoe@example.com>'])
832
3113.6.7 by Aaron Bentley
Fix commit for a checkout sharing a repo with its branch (abentley, #177592)
833
    def test_commit_with_checkout_and_branch_sharing_repo(self):
834
        repo = self.make_repository('repo', shared=True)
835
        # make_branch_and_tree ignores shared repos
6472.2.2 by Jelmer Vernooij
Use controldir rather than bzrdir in a couple more places.
836
        branch = controldir.ControlDir.create_branch_convenience('repo/branch')
3113.6.7 by Aaron Bentley
Fix commit for a checkout sharing a repo with its branch (abentley, #177592)
837
        tree2 = branch.create_checkout('repo/tree2')
838
        tree2.commit('message', rev_id='rev1')
839
        self.assertTrue(tree2.branch.repository.has_revision('rev1'))
6699.1.1 by Jelmer Vernooij
Support excludes with "bzr commit -x".
840
841
842
class FilterExcludedTests(TestCase):
843
844
    def test_add_file_not_excluded(self):
845
        changes = [
846
            ('fid', (None, 'newpath'),
847
             0, (False, False), ('pid', 'pid'), ('newpath', 'newpath'),
848
             ('file', 'file'), (True, True))]
849
        self.assertEqual(changes, list(filter_excluded(changes, ['otherpath'])))
850
851
    def test_add_file_excluded(self):
852
        changes = [
853
            ('fid', (None, 'newpath'),
854
             0, (False, False), ('pid', 'pid'), ('newpath', 'newpath'),
855
             ('file', 'file'), (True, True))]
856
        self.assertEqual([], list(filter_excluded(changes, ['newpath'])))
857
858
    def test_delete_file_excluded(self):
859
        changes = [
860
            ('fid', ('somepath', None),
861
             0, (False, None), ('pid', None), ('newpath', None),
862
             ('file', None), (True, None))]
863
        self.assertEqual([], list(filter_excluded(changes, ['somepath'])))
864
865
    def test_move_from_or_to_excluded(self):
866
        changes = [
867
            ('fid', ('oldpath', 'newpath'),
868
             0, (False, False), ('pid', 'pid'), ('oldpath', 'newpath'),
869
             ('file', 'file'), (True, True))]
870
        self.assertEqual([], list(filter_excluded(changes, ['oldpath'])))
871
        self.assertEqual([], list(filter_excluded(changes, ['newpath'])))