/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to brzlib/tests/test_commit.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-21 12:41:27 UTC
  • mto: This revision was merged to the branch mainline in revision 6623.
  • Revision ID: jelmer@jelmer.uk-20170521124127-iv8etg0vwymyai6y
s/bzr/brz/ in apport config.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
 
18
18
import os
19
 
from io import BytesIO
20
19
 
21
 
import breezy
22
 
from .. import (
 
20
import brzlib
 
21
from brzlib import (
23
22
    config,
24
23
    controldir,
25
24
    errors,
26
 
    trace,
27
25
    )
28
 
from ..branch import Branch
29
 
from ..bzr.bzrdir import BzrDirMetaFormat1
30
 
from ..commit import (
31
 
    CannotCommitSelectedFileMerge,
32
 
    Commit,
33
 
    NullCommitReporter,
 
26
from brzlib.branch import Branch
 
27
from brzlib.bzrdir import BzrDirMetaFormat1
 
28
from brzlib.commit import Commit, NullCommitReporter
 
29
from brzlib.errors import (
34
30
    PointlessCommit,
35
 
    filter_excluded,
36
 
    )
37
 
from ..errors import (
38
31
    BzrError,
 
32
    SigningFailed,
39
33
    LockContention,
40
34
    )
41
 
from ..tree import TreeChange
42
 
from . import (
43
 
    TestCase,
 
35
from brzlib.tests import (
44
36
    TestCaseWithTransport,
45
37
    test_foreign,
46
38
    )
47
 
from .features import (
 
39
from brzlib.tests.features import (
48
40
    SymlinkFeature,
49
41
    )
50
 
from .matchers import MatchesAncestry
 
42
from brzlib.tests.matchers import MatchesAncestry
51
43
 
52
44
 
53
45
# TODO: Test commit with some added, and added-but-missing files
55
47
class MustSignConfig(config.MemoryStack):
56
48
 
57
49
    def __init__(self):
58
 
        super(MustSignConfig, self).__init__(b'''
 
50
        super(MustSignConfig, self).__init__('''
 
51
gpg_signing_command=cat -
59
52
create_signatures=always
60
53
''')
61
54
 
89
82
        """Commit and check two versions of a single file."""
90
83
        wt = self.make_branch_and_tree('.')
91
84
        b = wt.branch
92
 
        with open('hello', 'w') as f:
93
 
            f.write('hello world')
 
85
        with file('hello', 'w') as f: f.write('hello world')
94
86
        wt.add('hello')
95
87
        rev1 = wt.commit(message='add hello')
 
88
        file_id = wt.path2id('hello')
96
89
 
97
 
        with open('hello', 'w') as f:
98
 
            f.write('version 2')
 
90
        with file('hello', 'w') as f: f.write('version 2')
99
91
        rev2 = wt.commit(message='commit 2')
100
92
 
101
93
        eq = self.assertEqual
105
97
 
106
98
        tree1 = b.repository.revision_tree(rev1)
107
99
        tree1.lock_read()
108
 
        text = tree1.get_file_text('hello')
 
100
        text = tree1.get_file_text(file_id)
109
101
        tree1.unlock()
110
 
        self.assertEqual(b'hello world', text)
 
102
        self.assertEqual('hello world', text)
111
103
 
112
104
        tree2 = b.repository.revision_tree(rev2)
113
105
        tree2.lock_read()
114
 
        text = tree2.get_file_text('hello')
 
106
        text = tree2.get_file_text(file_id)
115
107
        tree2.unlock()
116
 
        self.assertEqual(b'version 2', text)
 
108
        self.assertEqual('version 2', text)
117
109
 
118
110
    def test_commit_lossy_native(self):
119
111
        """Attempt a lossy commit to a native branch."""
120
112
        wt = self.make_branch_and_tree('.')
121
113
        b = wt.branch
122
 
        with open('hello', 'w') as f:
123
 
            f.write('hello world')
 
114
        with file('hello', 'w') as f: f.write('hello world')
124
115
        wt.add('hello')
125
 
        revid = wt.commit(message='add hello', rev_id=b'revid', lossy=True)
126
 
        self.assertEqual(b'revid', revid)
 
116
        revid = wt.commit(message='add hello', rev_id='revid', lossy=True)
 
117
        self.assertEqual('revid', revid)
127
118
 
128
119
    def test_commit_lossy_foreign(self):
129
120
        """Attempt a lossy commit to a foreign branch."""
130
121
        test_foreign.register_dummy_foreign_for_test(self)
131
122
        wt = self.make_branch_and_tree('.',
132
 
                                       format=test_foreign.DummyForeignVcsDirFormat())
 
123
            format=test_foreign.DummyForeignVcsDirFormat())
133
124
        b = wt.branch
134
 
        with open('hello', 'w') as f:
135
 
            f.write('hello world')
 
125
        with file('hello', 'w') as f: f.write('hello world')
136
126
        wt.add('hello')
137
127
        revid = wt.commit(message='add hello', lossy=True,
138
 
                          timestamp=1302659388, timezone=0)
139
 
        self.assertEqual(b'dummy-v1:1302659388-0-UNKNOWN', revid)
 
128
            timestamp=1302659388, timezone=0)
 
129
        self.assertEqual('dummy-v1:1302659388.0-0-UNKNOWN', revid)
140
130
 
141
131
    def test_commit_bound_lossy_foreign(self):
142
132
        """Attempt a lossy commit to a bzr branch bound to a foreign branch."""
143
133
        test_foreign.register_dummy_foreign_for_test(self)
144
134
        foreign_branch = self.make_branch('foreign',
145
 
                                          format=test_foreign.DummyForeignVcsDirFormat())
 
135
            format=test_foreign.DummyForeignVcsDirFormat())
146
136
        wt = foreign_branch.create_checkout("local")
147
137
        b = wt.branch
148
 
        with open('local/hello', 'w') as f:
149
 
            f.write('hello world')
 
138
        with file('local/hello', 'w') as f: f.write('hello world')
150
139
        wt.add('hello')
151
140
        revid = wt.commit(message='add hello', lossy=True,
152
 
                          timestamp=1302659388, timezone=0)
153
 
        self.assertEqual(b'dummy-v1:1302659388-0-0', revid)
154
 
        self.assertEqual(b'dummy-v1:1302659388-0-0',
155
 
                         foreign_branch.last_revision())
156
 
        self.assertEqual(b'dummy-v1:1302659388-0-0',
157
 
                         wt.branch.last_revision())
 
141
            timestamp=1302659388, timezone=0)
 
142
        self.assertEqual('dummy-v1:1302659388.0-0-0', revid)
 
143
        self.assertEqual('dummy-v1:1302659388.0-0-0',
 
144
            foreign_branch.last_revision())
 
145
        self.assertEqual('dummy-v1:1302659388.0-0-0',
 
146
            wt.branch.last_revision())
158
147
 
159
148
    def test_missing_commit(self):
160
149
        """Test a commit with a missing file"""
161
150
        wt = self.make_branch_and_tree('.')
162
151
        b = wt.branch
163
 
        with open('hello', 'w') as f:
164
 
            f.write('hello world')
165
 
        wt.add(['hello'], [b'hello-id'])
 
152
        with file('hello', 'w') as f: f.write('hello world')
 
153
        wt.add(['hello'], ['hello-id'])
166
154
        wt.commit(message='add hello')
167
155
 
168
156
        os.remove('hello')
169
157
        reporter = CapturingReporter()
170
 
        wt.commit('removed hello', rev_id=b'rev2', reporter=reporter)
 
158
        wt.commit('removed hello', rev_id='rev2', reporter=reporter)
171
159
        self.assertEqual(
172
160
            [('missing', u'hello'), ('deleted', u'hello')],
173
161
            reporter.calls)
174
162
 
175
 
        tree = b.repository.revision_tree(b'rev2')
176
 
        self.assertFalse(tree.has_filename('hello'))
 
163
        tree = b.repository.revision_tree('rev2')
 
164
        self.assertFalse(tree.has_id('hello-id'))
177
165
 
178
166
    def test_partial_commit_move(self):
179
167
        """Test a partial commit where a file was renamed but not committed.
188
176
        b = wt.branch
189
177
        self.build_tree(['annotate/', 'annotate/foo.py',
190
178
                         'olive/', 'olive/dialog.py'
191
 
                         ])
 
179
                        ])
192
180
        wt.add(['annotate', 'olive', 'annotate/foo.py', 'olive/dialog.py'])
193
181
        wt.commit(message='add files')
194
182
        wt.rename_one("olive/dialog.py", "aaa")
195
 
        self.build_tree_contents([('annotate/foo.py', b'modified\n')])
 
183
        self.build_tree_contents([('annotate/foo.py', 'modified\n')])
196
184
        wt.commit('renamed hello', specific_files=["annotate"])
197
185
 
198
186
    def test_pointless_commit(self):
199
187
        """Commit refuses unless there are changes or it's forced."""
200
188
        wt = self.make_branch_and_tree('.')
201
189
        b = wt.branch
202
 
        with open('hello', 'w') as f:
203
 
            f.write('hello')
 
190
        with file('hello', 'w') as f: f.write('hello')
204
191
        wt.add(['hello'])
205
192
        wt.commit(message='add hello')
206
193
        self.assertEqual(b.revno(), 1)
226
213
        """Selective commit in tree with deletions"""
227
214
        wt = self.make_branch_and_tree('.')
228
215
        b = wt.branch
229
 
        with open('hello', 'w') as f:
230
 
            f.write('hello')
231
 
        with open('buongia', 'w') as f:
232
 
            f.write('buongia')
 
216
        with file('hello', 'w') as f: f.write('hello')
 
217
        with file('buongia', 'w') as f: f.write('buongia')
233
218
        wt.add(['hello', 'buongia'],
234
 
               [b'hello-id', b'buongia-id'])
 
219
              ['hello-id', 'buongia-id'])
235
220
        wt.commit(message='add files',
236
 
                  rev_id=b'test@rev-1')
 
221
                 rev_id='test@rev-1')
237
222
 
238
223
        os.remove('hello')
239
 
        with open('buongia', 'w') as f:
240
 
            f.write('new text')
 
224
        with file('buongia', 'w') as f: f.write('new text')
241
225
        wt.commit(message='update text',
242
 
                  specific_files=['buongia'],
243
 
                  allow_pointless=False,
244
 
                  rev_id=b'test@rev-2')
 
226
                 specific_files=['buongia'],
 
227
                 allow_pointless=False,
 
228
                 rev_id='test@rev-2')
245
229
 
246
230
        wt.commit(message='remove hello',
247
 
                  specific_files=['hello'],
248
 
                  allow_pointless=False,
249
 
                  rev_id=b'test@rev-3')
 
231
                 specific_files=['hello'],
 
232
                 allow_pointless=False,
 
233
                 rev_id='test@rev-3')
250
234
 
251
235
        eq = self.assertEqual
252
236
        eq(b.revno(), 3)
253
237
 
254
 
        tree2 = b.repository.revision_tree(b'test@rev-2')
 
238
        tree2 = b.repository.revision_tree('test@rev-2')
255
239
        tree2.lock_read()
256
240
        self.addCleanup(tree2.unlock)
257
241
        self.assertTrue(tree2.has_filename('hello'))
258
 
        self.assertEqual(tree2.get_file_text('hello'), b'hello')
259
 
        self.assertEqual(tree2.get_file_text('buongia'), b'new text')
 
242
        self.assertEqual(tree2.get_file_text('hello-id'), 'hello')
 
243
        self.assertEqual(tree2.get_file_text('buongia-id'), 'new text')
260
244
 
261
 
        tree3 = b.repository.revision_tree(b'test@rev-3')
 
245
        tree3 = b.repository.revision_tree('test@rev-3')
262
246
        tree3.lock_read()
263
247
        self.addCleanup(tree3.unlock)
264
248
        self.assertFalse(tree3.has_filename('hello'))
265
 
        self.assertEqual(tree3.get_file_text('buongia'), b'new text')
 
249
        self.assertEqual(tree3.get_file_text('buongia-id'), 'new text')
266
250
 
267
251
    def test_commit_rename(self):
268
252
        """Test commit of a revision where a file is renamed."""
269
253
        tree = self.make_branch_and_tree('.')
270
254
        b = tree.branch
271
255
        self.build_tree(['hello'], line_endings='binary')
272
 
        tree.add(['hello'], [b'hello-id'])
273
 
        tree.commit(message='one', rev_id=b'test@rev-1', allow_pointless=False)
 
256
        tree.add(['hello'], ['hello-id'])
 
257
        tree.commit(message='one', rev_id='test@rev-1', allow_pointless=False)
274
258
 
275
259
        tree.rename_one('hello', 'fruity')
276
 
        tree.commit(message='renamed', rev_id=b'test@rev-2',
277
 
                    allow_pointless=False)
 
260
        tree.commit(message='renamed', rev_id='test@rev-2', allow_pointless=False)
278
261
 
279
262
        eq = self.assertEqual
280
 
        tree1 = b.repository.revision_tree(b'test@rev-1')
 
263
        tree1 = b.repository.revision_tree('test@rev-1')
281
264
        tree1.lock_read()
282
265
        self.addCleanup(tree1.unlock)
283
 
        eq(tree1.id2path(b'hello-id'), 'hello')
284
 
        eq(tree1.get_file_text('hello'), b'contents of hello\n')
 
266
        eq(tree1.id2path('hello-id'), 'hello')
 
267
        eq(tree1.get_file_text('hello-id'), 'contents of hello\n')
285
268
        self.assertFalse(tree1.has_filename('fruity'))
286
269
        self.check_tree_shape(tree1, ['hello'])
287
 
        eq(tree1.get_file_revision('hello'), b'test@rev-1')
 
270
        eq(tree1.get_file_revision('hello-id'), 'test@rev-1')
288
271
 
289
 
        tree2 = b.repository.revision_tree(b'test@rev-2')
 
272
        tree2 = b.repository.revision_tree('test@rev-2')
290
273
        tree2.lock_read()
291
274
        self.addCleanup(tree2.unlock)
292
 
        eq(tree2.id2path(b'hello-id'), 'fruity')
293
 
        eq(tree2.get_file_text('fruity'), b'contents of hello\n')
 
275
        eq(tree2.id2path('hello-id'), 'fruity')
 
276
        eq(tree2.get_file_text('hello-id'), 'contents of hello\n')
294
277
        self.check_tree_shape(tree2, ['fruity'])
295
 
        eq(tree2.get_file_revision('fruity'), b'test@rev-2')
 
278
        eq(tree2.get_file_revision('hello-id'), 'test@rev-2')
296
279
 
297
280
    def test_reused_rev_id(self):
298
281
        """Test that a revision id cannot be reused in a branch"""
299
282
        wt = self.make_branch_and_tree('.')
300
283
        b = wt.branch
301
 
        wt.commit('initial', rev_id=b'test@rev-1', allow_pointless=True)
 
284
        wt.commit('initial', rev_id='test@rev-1', allow_pointless=True)
302
285
        self.assertRaises(Exception,
303
286
                          wt.commit,
304
287
                          message='reused id',
305
 
                          rev_id=b'test@rev-1',
 
288
                          rev_id='test@rev-1',
306
289
                          allow_pointless=True)
307
290
 
308
291
    def test_commit_move(self):
310
293
        eq = self.assertEqual
311
294
        wt = self.make_branch_and_tree('.')
312
295
        b = wt.branch
313
 
        r1 = b'test@rev-1'
 
296
        r1 = 'test@rev-1'
314
297
        self.build_tree(['hello', 'a/', 'b/'])
315
 
        wt.add(['hello', 'a', 'b'], [b'hello-id', b'a-id', b'b-id'])
 
298
        wt.add(['hello', 'a', 'b'], ['hello-id', 'a-id', 'b-id'])
316
299
        wt.commit('initial', rev_id=r1, allow_pointless=False)
317
300
        wt.move(['hello'], 'a')
318
 
        r2 = b'test@rev-2'
 
301
        r2 = 'test@rev-2'
319
302
        wt.commit('two', rev_id=r2, allow_pointless=False)
320
303
        wt.lock_read()
321
304
        try:
324
307
            wt.unlock()
325
308
 
326
309
        wt.move(['b'], 'a')
327
 
        r3 = b'test@rev-3'
 
310
        r3 = 'test@rev-3'
328
311
        wt.commit('three', rev_id=r3, allow_pointless=False)
329
312
        wt.lock_read()
330
313
        try:
331
314
            self.check_tree_shape(wt,
332
 
                                  ['a/', 'a/hello', 'a/b/'])
 
315
                                       ['a/', 'a/hello', 'a/b/'])
333
316
            self.check_tree_shape(b.repository.revision_tree(r3),
334
 
                                  ['a/', 'a/hello', 'a/b/'])
 
317
                                       ['a/', 'a/hello', 'a/b/'])
335
318
        finally:
336
319
            wt.unlock()
337
320
 
338
321
        wt.move(['a/hello'], 'a/b')
339
 
        r4 = b'test@rev-4'
 
322
        r4 = 'test@rev-4'
340
323
        wt.commit('four', rev_id=r4, allow_pointless=False)
341
324
        wt.lock_read()
342
325
        try:
345
328
            wt.unlock()
346
329
 
347
330
        inv = b.repository.get_inventory(r4)
348
 
        eq(inv.get_entry(b'hello-id').revision, r4)
349
 
        eq(inv.get_entry(b'a-id').revision, r1)
350
 
        eq(inv.get_entry(b'b-id').revision, r3)
 
331
        eq(inv['hello-id'].revision, r4)
 
332
        eq(inv['a-id'].revision, r1)
 
333
        eq(inv['b-id'].revision, r3)
351
334
 
352
335
    def test_removed_commit(self):
353
336
        """Commit with a removed file"""
354
337
        wt = self.make_branch_and_tree('.')
355
338
        b = wt.branch
356
 
        with open('hello', 'w') as f:
357
 
            f.write('hello world')
358
 
        wt.add(['hello'], [b'hello-id'])
 
339
        with file('hello', 'w') as f: f.write('hello world')
 
340
        wt.add(['hello'], ['hello-id'])
359
341
        wt.commit(message='add hello')
360
342
        wt.remove('hello')
361
 
        wt.commit('removed hello', rev_id=b'rev2')
 
343
        wt.commit('removed hello', rev_id='rev2')
362
344
 
363
 
        tree = b.repository.revision_tree(b'rev2')
364
 
        self.assertFalse(tree.has_filename('hello'))
 
345
        tree = b.repository.revision_tree('rev2')
 
346
        self.assertFalse(tree.has_id('hello-id'))
365
347
 
366
348
    def test_committed_ancestry(self):
367
349
        """Test commit appends revisions to ancestry."""
369
351
        b = wt.branch
370
352
        rev_ids = []
371
353
        for i in range(4):
372
 
            with open('hello', 'w') as f:
373
 
                f.write((str(i) * 4) + '\n')
 
354
            with file('hello', 'w') as f: f.write((str(i) * 4) + '\n')
374
355
            if i == 0:
375
 
                wt.add(['hello'], [b'hello-id'])
376
 
            rev_id = b'test@rev-%d' % (i + 1)
 
356
                wt.add(['hello'], ['hello-id'])
 
357
            rev_id = 'test@rev-%d' % (i+1)
377
358
            rev_ids.append(rev_id)
378
 
            wt.commit(message='rev %d' % (i + 1),
379
 
                      rev_id=rev_id)
 
359
            wt.commit(message='rev %d' % (i+1),
 
360
                     rev_id=rev_id)
380
361
        for i in range(4):
381
 
            self.assertThat(rev_ids[:i + 1],
382
 
                            MatchesAncestry(b.repository, rev_ids[i]))
 
362
            self.assertThat(rev_ids[:i+1],
 
363
                MatchesAncestry(b.repository, rev_ids[i]))
383
364
 
384
365
    def test_commit_new_subdir_child_selective(self):
385
366
        wt = self.make_branch_and_tree('.')
386
367
        b = wt.branch
387
368
        self.build_tree(['dir/', 'dir/file1', 'dir/file2'])
388
369
        wt.add(['dir', 'dir/file1', 'dir/file2'],
389
 
               [b'dirid', b'file1id', b'file2id'])
390
 
        wt.commit('dir/file1', specific_files=['dir/file1'], rev_id=b'1')
391
 
        inv = b.repository.get_inventory(b'1')
392
 
        self.assertEqual(b'1', inv.get_entry(b'dirid').revision)
393
 
        self.assertEqual(b'1', inv.get_entry(b'file1id').revision)
 
370
              ['dirid', 'file1id', 'file2id'])
 
371
        wt.commit('dir/file1', specific_files=['dir/file1'], rev_id='1')
 
372
        inv = b.repository.get_inventory('1')
 
373
        self.assertEqual('1', inv['dirid'].revision)
 
374
        self.assertEqual('1', inv['file1id'].revision)
394
375
        # FIXME: This should raise a KeyError I think, rbc20051006
395
 
        self.assertRaises(BzrError, inv.get_entry, b'file2id')
 
376
        self.assertRaises(BzrError, inv.__getitem__, 'file2id')
396
377
 
397
378
    def test_strict_commit(self):
398
379
        """Try and commit with unknown files and strict = True, should fail."""
399
 
        from ..errors import StrictCommitFailed
 
380
        from brzlib.errors import StrictCommitFailed
400
381
        wt = self.make_branch_and_tree('.')
401
382
        b = wt.branch
402
 
        with open('hello', 'w') as f:
403
 
            f.write('hello world')
 
383
        with file('hello', 'w') as f: f.write('hello world')
404
384
        wt.add('hello')
405
 
        with open('goodbye', 'w') as f:
406
 
            f.write('goodbye cruel world!')
 
385
        with file('goodbye', 'w') as f: f.write('goodbye cruel world!')
407
386
        self.assertRaises(StrictCommitFailed, wt.commit,
408
 
                          message='add hello but not goodbye', strict=True)
 
387
            message='add hello but not goodbye', strict=True)
409
388
 
410
389
    def test_strict_commit_without_unknowns(self):
411
390
        """Try and commit with no unknown files and strict = True,
412
391
        should work."""
413
392
        wt = self.make_branch_and_tree('.')
414
393
        b = wt.branch
415
 
        with open('hello', 'w') as f:
416
 
            f.write('hello world')
 
394
        with file('hello', 'w') as f: f.write('hello world')
417
395
        wt.add('hello')
418
396
        wt.commit(message='add hello', strict=True)
419
397
 
421
399
        """Try and commit with unknown files and strict = False, should work."""
422
400
        wt = self.make_branch_and_tree('.')
423
401
        b = wt.branch
424
 
        with open('hello', 'w') as f:
425
 
            f.write('hello world')
 
402
        with file('hello', 'w') as f: f.write('hello world')
426
403
        wt.add('hello')
427
 
        with open('goodbye', 'w') as f:
428
 
            f.write('goodbye cruel world!')
 
404
        with file('goodbye', 'w') as f: f.write('goodbye cruel world!')
429
405
        wt.commit(message='add hello but not goodbye', strict=False)
430
406
 
431
407
    def test_nonstrict_commit_without_unknowns(self):
433
409
        should work."""
434
410
        wt = self.make_branch_and_tree('.')
435
411
        b = wt.branch
436
 
        with open('hello', 'w') as f:
437
 
            f.write('hello world')
 
412
        with file('hello', 'w') as f: f.write('hello world')
438
413
        wt.add('hello')
439
414
        wt.commit(message='add hello', strict=False)
440
415
 
441
416
    def test_signed_commit(self):
442
 
        import breezy.gpg
443
 
        import breezy.commit as commit
444
 
        oldstrategy = breezy.gpg.GPGStrategy
 
417
        import brzlib.gpg
 
418
        import brzlib.commit as commit
 
419
        oldstrategy = brzlib.gpg.GPGStrategy
445
420
        wt = self.make_branch_and_tree('.')
446
421
        branch = wt.branch
447
 
        wt.commit("base", allow_pointless=True, rev_id=b'A')
448
 
        self.assertFalse(branch.repository.has_signature_for_revision_id(b'A'))
 
422
        wt.commit("base", allow_pointless=True, rev_id='A')
 
423
        self.assertFalse(branch.repository.has_signature_for_revision_id('A'))
449
424
        try:
450
 
            from ..bzr.testament import Testament
 
425
            from brzlib.testament import Testament
451
426
            # monkey patch gpg signing mechanism
452
 
            breezy.gpg.GPGStrategy = breezy.gpg.LoopbackGPGStrategy
453
 
            conf = config.MemoryStack(b'''
 
427
            brzlib.gpg.GPGStrategy = brzlib.gpg.LoopbackGPGStrategy
 
428
            conf = config.MemoryStack('''
 
429
gpg_signing_command=cat -
454
430
create_signatures=always
455
431
''')
456
432
            commit.Commit(config_stack=conf).commit(
457
 
                message="base", allow_pointless=True, rev_id=b'B',
 
433
                message="base", allow_pointless=True, rev_id='B',
458
434
                working_tree=wt)
459
 
 
460
435
            def sign(text):
461
 
                return breezy.gpg.LoopbackGPGStrategy(None).sign(
462
 
                    text, breezy.gpg.MODE_CLEAR)
 
436
                return brzlib.gpg.LoopbackGPGStrategy(None).sign(text)
463
437
            self.assertEqual(sign(Testament.from_revision(branch.repository,
464
 
                                                          b'B').as_short_text()),
465
 
                             branch.repository.get_signature_text(b'B'))
 
438
                                                          'B').as_short_text()),
 
439
                             branch.repository.get_signature_text('B'))
466
440
        finally:
467
 
            breezy.gpg.GPGStrategy = oldstrategy
 
441
            brzlib.gpg.GPGStrategy = oldstrategy
468
442
 
469
443
    def test_commit_failed_signature(self):
470
 
        import breezy.gpg
471
 
        import breezy.commit as commit
472
 
        oldstrategy = breezy.gpg.GPGStrategy
 
444
        import brzlib.gpg
 
445
        import brzlib.commit as commit
 
446
        oldstrategy = brzlib.gpg.GPGStrategy
473
447
        wt = self.make_branch_and_tree('.')
474
448
        branch = wt.branch
475
 
        wt.commit("base", allow_pointless=True, rev_id=b'A')
476
 
        self.assertFalse(branch.repository.has_signature_for_revision_id(b'A'))
 
449
        wt.commit("base", allow_pointless=True, rev_id='A')
 
450
        self.assertFalse(branch.repository.has_signature_for_revision_id('A'))
477
451
        try:
478
452
            # monkey patch gpg signing mechanism
479
 
            breezy.gpg.GPGStrategy = breezy.gpg.DisabledGPGStrategy
480
 
            conf = config.MemoryStack(b'''
 
453
            brzlib.gpg.GPGStrategy = brzlib.gpg.DisabledGPGStrategy
 
454
            conf = config.MemoryStack('''
 
455
gpg_signing_command=cat -
481
456
create_signatures=always
482
457
''')
483
 
            self.assertRaises(breezy.gpg.SigningFailed,
 
458
            self.assertRaises(SigningFailed,
484
459
                              commit.Commit(config_stack=conf).commit,
485
460
                              message="base",
486
461
                              allow_pointless=True,
487
 
                              rev_id=b'B',
 
462
                              rev_id='B',
488
463
                              working_tree=wt)
489
464
            branch = Branch.open(self.get_url('.'))
490
 
            self.assertEqual(branch.last_revision(), b'A')
491
 
            self.assertFalse(branch.repository.has_revision(b'B'))
 
465
            self.assertEqual(branch.last_revision(), 'A')
 
466
            self.assertFalse(branch.repository.has_revision('B'))
492
467
        finally:
493
 
            breezy.gpg.GPGStrategy = oldstrategy
 
468
            brzlib.gpg.GPGStrategy = oldstrategy
494
469
 
495
470
    def test_commit_invokes_hooks(self):
496
 
        import breezy.commit as commit
 
471
        import brzlib.commit as commit
497
472
        wt = self.make_branch_and_tree('.')
498
473
        branch = wt.branch
499
474
        calls = []
500
 
 
501
475
        def called(branch, rev_id):
502
476
            calls.append('called')
503
 
        breezy.ahook = called
 
477
        brzlib.ahook = called
504
478
        try:
505
 
            conf = config.MemoryStack(b'post_commit=breezy.ahook breezy.ahook')
 
479
            conf = config.MemoryStack('post_commit=brzlib.ahook brzlib.ahook')
506
480
            commit.Commit(config_stack=conf).commit(
507
 
                message="base", allow_pointless=True, rev_id=b'A',
508
 
                working_tree=wt)
 
481
                message = "base", allow_pointless=True, rev_id='A',
 
482
                working_tree = wt)
509
483
            self.assertEqual(['called', 'called'], calls)
510
484
        finally:
511
 
            del breezy.ahook
 
485
            del brzlib.ahook
512
486
 
513
487
    def test_commit_object_doesnt_set_nick(self):
514
488
        # using the Commit object directly does not set the branch nick.
518
492
        self.assertEqual(wt.branch.revno(), 1)
519
493
        self.assertEqual({},
520
494
                         wt.branch.repository.get_revision(
521
 
            wt.branch.last_revision()).properties)
 
495
                            wt.branch.last_revision()).properties)
522
496
 
523
497
    def test_safe_master_lock(self):
524
498
        os.mkdir('master')
542
516
        bound_tree = self.make_branch_and_tree('bound')
543
517
        bound_tree.branch.bind(master_branch)
544
518
 
545
 
        self.build_tree_contents(
546
 
            [('bound/content_file', b'initial contents\n')])
 
519
        self.build_tree_contents([('bound/content_file', 'initial contents\n')])
547
520
        bound_tree.add(['content_file'])
548
521
        bound_tree.commit(message='woo!')
549
522
 
550
 
        other_bzrdir = master_branch.controldir.sprout('other')
 
523
        other_bzrdir = master_branch.bzrdir.sprout('other')
551
524
        other_tree = other_bzrdir.open_workingtree()
552
525
 
553
526
        # do a commit to the other branch changing the content file so
554
527
        # that our commit after merging will have a merged revision in the
555
528
        # content file history.
556
 
        self.build_tree_contents(
557
 
            [('other/content_file', b'change in other\n')])
 
529
        self.build_tree_contents([('other/content_file', 'change in other\n')])
558
530
        other_tree.commit('change in other')
559
531
 
560
532
        # do a merge into the bound branch from other, and then change the
561
533
        # content file locally to force a new revision (rather than using the
562
534
        # revision from other). This forces extra processing in commit.
563
535
        bound_tree.merge_from_branch(other_tree.branch)
564
 
        self.build_tree_contents(
565
 
            [('bound/content_file', b'change in bound\n')])
 
536
        self.build_tree_contents([('bound/content_file', 'change in bound\n')])
566
537
 
567
538
        # before #34959 was fixed, this failed with 'revision not present in
568
539
        # weave' when trying to implicitly push from the bound branch to the master
596
567
            'filetoleave']
597
568
            )
598
569
        this_tree.commit('create_files')
599
 
        other_dir = this_tree.controldir.sprout('other')
 
570
        other_dir = this_tree.bzrdir.sprout('other')
600
571
        other_tree = other_dir.open_workingtree()
601
572
        other_tree.lock_write()
602
573
        # perform the needed actions on the files and dirs.
604
575
            other_tree.rename_one('dirtorename', 'renameddir')
605
576
            other_tree.rename_one('dirtoreparent', 'renameddir/reparenteddir')
606
577
            other_tree.rename_one('filetorename', 'renamedfile')
607
 
            other_tree.rename_one(
608
 
                'filetoreparent', 'renameddir/reparentedfile')
 
578
            other_tree.rename_one('filetoreparent', 'renameddir/reparentedfile')
609
579
            other_tree.remove(['dirtoremove', 'filetoremove'])
610
580
            self.build_tree_contents([
611
581
                ('other/newdir/', ),
612
 
                ('other/filetomodify', b'new content'),
613
 
                ('other/newfile', b'new file content')])
 
582
                ('other/filetomodify', 'new content'),
 
583
                ('other/newfile', 'new file content')])
614
584
            other_tree.add('newfile')
615
585
            other_tree.add('newdir/')
616
586
            other_tree.commit('modify all sample files and dirs.')
619
589
        this_tree.merge_from_branch(other_tree.branch)
620
590
        reporter = CapturingReporter()
621
591
        this_tree.commit('do the commit', reporter=reporter)
622
 
        expected = {
 
592
        expected = set([
623
593
            ('change', 'modified', 'filetomodify'),
624
594
            ('change', 'added', 'newdir'),
625
595
            ('change', 'added', 'newfile'),
629
599
            ('renamed', 'renamed', 'filetoreparent', 'renameddir/reparentedfile'),
630
600
            ('deleted', 'dirtoremove'),
631
601
            ('deleted', 'filetoremove'),
632
 
            }
 
602
            ])
633
603
        result = set(reporter.calls)
634
604
        missing = expected - result
635
605
        new = result - expected
644
614
        tree.remove(['a', 'b'])
645
615
        tree.commit('removed a', specific_files='a')
646
616
        basis = tree.basis_tree()
647
 
        with tree.lock_read():
648
 
            self.assertFalse(basis.is_versioned('a'))
649
 
            self.assertTrue(basis.is_versioned('b'))
 
617
        tree.lock_read()
 
618
        try:
 
619
            self.assertIs(None, basis.path2id('a'))
 
620
            self.assertFalse(basis.path2id('b') is None)
 
621
        finally:
 
622
            tree.unlock()
650
623
 
651
624
    def test_commit_saves_1ms_timestamp(self):
652
625
        """Passing in a timestamp is saved with 1ms resolution"""
654
627
        self.build_tree(['a'])
655
628
        tree.add('a')
656
629
        tree.commit('added a', timestamp=1153248633.4186721, timezone=0,
657
 
                    rev_id=b'a1')
 
630
                    rev_id='a1')
658
631
 
659
 
        rev = tree.branch.repository.get_revision(b'a1')
 
632
        rev = tree.branch.repository.get_revision('a1')
660
633
        self.assertEqual(1153248633.419, rev.timestamp)
661
634
 
662
635
    def test_commit_has_1ms_resolution(self):
664
637
        tree = self.make_branch_and_tree('.')
665
638
        self.build_tree(['a'])
666
639
        tree.add('a')
667
 
        tree.commit('added a', rev_id=b'a1')
 
640
        tree.commit('added a', rev_id='a1')
668
641
 
669
 
        rev = tree.branch.repository.get_revision(b'a1')
 
642
        rev = tree.branch.repository.get_revision('a1')
670
643
        timestamp = rev.timestamp
671
644
        timestamp_1ms = round(timestamp, 3)
672
645
        self.assertEqual(timestamp_1ms, timestamp)
673
646
 
674
 
    def assertBasisTreeKind(self, kind, tree, path):
 
647
    def assertBasisTreeKind(self, kind, tree, file_id):
675
648
        basis = tree.basis_tree()
676
649
        basis.lock_read()
677
650
        try:
678
 
            self.assertEqual(kind, basis.kind(path))
 
651
            self.assertEqual(kind, basis.kind(file_id))
679
652
        finally:
680
653
            basis.unlock()
681
654
 
682
 
    def test_unsupported_symlink_commit(self):
683
 
        self.requireFeature(SymlinkFeature)
684
 
        tree = self.make_branch_and_tree('.')
685
 
        self.build_tree(['hello'])
686
 
        tree.add('hello')
687
 
        tree.commit('added hello', rev_id=b'hello_id')
688
 
        os.symlink('hello', 'foo')
689
 
        tree.add('foo')
690
 
        tree.commit('added foo', rev_id=b'foo_id')
691
 
        log = BytesIO()
692
 
        trace.push_log_file(log)
693
 
        os_symlink = getattr(os, 'symlink', None)
694
 
        os.symlink = None
695
 
        try:
696
 
            # At this point as bzr thinks symlinks are not supported
697
 
            # we should get a warning about symlink foo and bzr should
698
 
            # not think its removed.
699
 
            os.unlink('foo')
700
 
            self.build_tree(['world'])
701
 
            tree.add('world')
702
 
            tree.commit('added world', rev_id=b'world_id')
703
 
        finally:
704
 
            if os_symlink:
705
 
                os.symlink = os_symlink
706
 
        self.assertContainsRe(
707
 
            log.getvalue(),
708
 
            b'Ignoring "foo" as symlinks are not '
709
 
            b'supported on this filesystem\\.')
710
 
 
711
655
    def test_commit_kind_changes(self):
712
656
        self.requireFeature(SymlinkFeature)
713
657
        tree = self.make_branch_and_tree('.')
714
658
        os.symlink('target', 'name')
715
 
        tree.add('name', b'a-file-id')
 
659
        tree.add('name', 'a-file-id')
716
660
        tree.commit('Added a symlink')
717
 
        self.assertBasisTreeKind('symlink', tree, 'name')
 
661
        self.assertBasisTreeKind('symlink', tree, 'a-file-id')
718
662
 
719
663
        os.unlink('name')
720
664
        self.build_tree(['name'])
721
665
        tree.commit('Changed symlink to file')
722
 
        self.assertBasisTreeKind('file', tree, 'name')
 
666
        self.assertBasisTreeKind('file', tree, 'a-file-id')
723
667
 
724
668
        os.unlink('name')
725
669
        os.symlink('target', 'name')
726
670
        tree.commit('file to symlink')
727
 
        self.assertBasisTreeKind('symlink', tree, 'name')
 
671
        self.assertBasisTreeKind('symlink', tree, 'a-file-id')
728
672
 
729
673
        os.unlink('name')
730
674
        os.mkdir('name')
731
675
        tree.commit('symlink to directory')
732
 
        self.assertBasisTreeKind('directory', tree, 'name')
 
676
        self.assertBasisTreeKind('directory', tree, 'a-file-id')
733
677
 
734
678
        os.rmdir('name')
735
679
        os.symlink('target', 'name')
736
680
        tree.commit('directory to symlink')
737
 
        self.assertBasisTreeKind('symlink', tree, 'name')
 
681
        self.assertBasisTreeKind('symlink', tree, 'a-file-id')
738
682
 
739
683
        # prepare for directory <-> file tests
740
684
        os.unlink('name')
741
685
        os.mkdir('name')
742
686
        tree.commit('symlink to directory')
743
 
        self.assertBasisTreeKind('directory', tree, 'name')
 
687
        self.assertBasisTreeKind('directory', tree, 'a-file-id')
744
688
 
745
689
        os.rmdir('name')
746
690
        self.build_tree(['name'])
747
691
        tree.commit('Changed directory to file')
748
 
        self.assertBasisTreeKind('file', tree, 'name')
 
692
        self.assertBasisTreeKind('file', tree, 'a-file-id')
749
693
 
750
694
        os.unlink('name')
751
695
        os.mkdir('name')
752
696
        tree.commit('file to directory')
753
 
        self.assertBasisTreeKind('directory', tree, 'name')
 
697
        self.assertBasisTreeKind('directory', tree, 'a-file-id')
754
698
 
755
699
    def test_commit_unversioned_specified(self):
756
700
        """Commit should raise if specified files isn't in basis or worktree"""
776
720
        tree = self.make_branch_and_tree('.')
777
721
        try:
778
722
            tree.commit()
779
 
        except Exception as e:
 
723
        except Exception, e:
780
724
            self.assertTrue(isinstance(e, BzrError))
781
725
            self.assertEqual('The message or message_callback keyword'
782
726
                             ' parameter is required for commit().', str(e))
803
747
        cb = self.Callback(u'commit 2', self)
804
748
        repository = tree.branch.repository
805
749
        # simulate network failure
806
 
 
807
750
        def raise_(self, arg, arg2, arg3=None, arg4=None):
808
751
            raise errors.NoSuchFile('foo')
809
752
        repository.add_inventory = raise_
816
759
        tree = self.make_branch_and_tree('foo')
817
760
        # pending merge would turn into a left parent
818
761
        tree.commit('commit 1')
819
 
        tree.add_parent_tree_id(b'example')
 
762
        tree.add_parent_tree_id('example')
820
763
        self.build_tree(['foo/bar', 'foo/baz'])
821
764
        tree.add(['bar', 'baz'])
822
 
        err = self.assertRaises(CannotCommitSelectedFileMerge,
823
 
                                tree.commit, 'commit 2', specific_files=['bar', 'baz'])
 
765
        err = self.assertRaises(errors.CannotCommitSelectedFileMerge,
 
766
            tree.commit, 'commit 2', specific_files=['bar', 'baz'])
824
767
        self.assertEqual(['bar', 'baz'], err.files)
825
768
        self.assertEqual('Selected-file commit of merges is not supported'
826
769
                         ' yet: files bar, baz', str(err))
847
790
        self.assertFalse('authors' in rev.properties)
848
791
 
849
792
    def test_commit_author(self):
850
 
        """Passing a non-empty authors kwarg to MutableTree.commit should add
 
793
        """Passing a non-empty author kwarg to MutableTree.commit should add
851
794
        the 'author' revision property.
852
795
        """
853
796
        tree = self.make_branch_and_tree('foo')
854
 
        rev_id = tree.commit(
855
 
            'commit 1',
856
 
            authors=['John Doe <jdoe@example.com>'])
 
797
        rev_id = self.callDeprecated(['The parameter author was '
 
798
                'deprecated in version 1.13. Use authors instead'],
 
799
                tree.commit, 'commit 1', author='John Doe <jdoe@example.com>')
857
800
        rev = tree.branch.repository.get_revision(rev_id)
858
801
        self.assertEqual('John Doe <jdoe@example.com>',
859
802
                         rev.properties['authors'])
870
813
    def test_multiple_authors(self):
871
814
        tree = self.make_branch_and_tree('foo')
872
815
        rev_id = tree.commit('commit 1',
873
 
                             authors=['John Doe <jdoe@example.com>',
874
 
                                      'Jane Rey <jrey@example.com>'])
 
816
                authors=['John Doe <jdoe@example.com>',
 
817
                         'Jane Rey <jrey@example.com>'])
875
818
        rev = tree.branch.repository.get_revision(rev_id)
876
819
        self.assertEqual('John Doe <jdoe@example.com>\n'
877
 
                         'Jane Rey <jrey@example.com>', rev.properties['authors'])
 
820
                'Jane Rey <jrey@example.com>', rev.properties['authors'])
878
821
        self.assertFalse('author' in rev.properties)
879
822
 
 
823
    def test_author_and_authors_incompatible(self):
 
824
        tree = self.make_branch_and_tree('foo')
 
825
        self.assertRaises(AssertionError, tree.commit, 'commit 1',
 
826
                authors=['John Doe <jdoe@example.com>',
 
827
                         'Jane Rey <jrey@example.com>'],
 
828
                author="Jack Me <jme@example.com>")
 
829
 
880
830
    def test_author_with_newline_rejected(self):
881
831
        tree = self.make_branch_and_tree('foo')
882
832
        self.assertRaises(AssertionError, tree.commit, 'commit 1',
883
 
                          authors=['John\nDoe <jdoe@example.com>'])
 
833
                authors=['John\nDoe <jdoe@example.com>'])
884
834
 
885
835
    def test_commit_with_checkout_and_branch_sharing_repo(self):
886
836
        repo = self.make_repository('repo', shared=True)
887
837
        # make_branch_and_tree ignores shared repos
888
838
        branch = controldir.ControlDir.create_branch_convenience('repo/branch')
889
839
        tree2 = branch.create_checkout('repo/tree2')
890
 
        tree2.commit('message', rev_id=b'rev1')
891
 
        self.assertTrue(tree2.branch.repository.has_revision(b'rev1'))
892
 
 
893
 
 
894
 
class FilterExcludedTests(TestCase):
895
 
 
896
 
    def test_add_file_not_excluded(self):
897
 
        changes = [
898
 
            TreeChange(
899
 
                'fid', (None, 'newpath'),
900
 
                0, (False, False), ('pid', 'pid'), ('newpath', 'newpath'),
901
 
                ('file', 'file'), (True, True))]
902
 
        self.assertEqual(changes, list(
903
 
            filter_excluded(changes, ['otherpath'])))
904
 
 
905
 
    def test_add_file_excluded(self):
906
 
        changes = [
907
 
            TreeChange(
908
 
                'fid', (None, 'newpath'),
909
 
                0, (False, False), ('pid', 'pid'), ('newpath', 'newpath'),
910
 
                ('file', 'file'), (True, True))]
911
 
        self.assertEqual([], list(filter_excluded(changes, ['newpath'])))
912
 
 
913
 
    def test_delete_file_excluded(self):
914
 
        changes = [
915
 
            TreeChange(
916
 
                'fid', ('somepath', None),
917
 
                0, (False, None), ('pid', None), ('newpath', None),
918
 
                ('file', None), (True, None))]
919
 
        self.assertEqual([], list(filter_excluded(changes, ['somepath'])))
920
 
 
921
 
    def test_move_from_or_to_excluded(self):
922
 
        changes = [
923
 
            TreeChange(
924
 
                'fid', ('oldpath', 'newpath'),
925
 
                0, (False, False), ('pid', 'pid'), ('oldpath', 'newpath'),
926
 
                ('file', 'file'), (True, True))]
927
 
        self.assertEqual([], list(filter_excluded(changes, ['oldpath'])))
928
 
        self.assertEqual([], list(filter_excluded(changes, ['newpath'])))
 
840
        tree2.commit('message', rev_id='rev1')
 
841
        self.assertTrue(tree2.branch.repository.has_revision('rev1'))