/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 breezy/tests/test_commit.py

  • Committer: Jelmer Vernooij
  • Date: 2020-05-06 02:13:25 UTC
  • mfrom: (7490.7.21 work)
  • mto: This revision was merged to the branch mainline in revision 7501.
  • Revision ID: jelmer@jelmer.uk-20200506021325-awbmmqu1zyorz7sj
Merge 3.1 branch.

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
19
20
 
20
21
import breezy
21
22
from .. import (
22
23
    config,
23
24
    controldir,
24
25
    errors,
 
26
    trace,
25
27
    )
26
28
from ..branch import Branch
27
29
from ..bzr.bzrdir import BzrDirMetaFormat1
36
38
    BzrError,
37
39
    LockContention,
38
40
    )
 
41
from ..tree import TreeChange
39
42
from . import (
40
43
    TestCase,
41
44
    TestCaseWithTransport,
52
55
class MustSignConfig(config.MemoryStack):
53
56
 
54
57
    def __init__(self):
55
 
        super(MustSignConfig, self).__init__('''
 
58
        super(MustSignConfig, self).__init__(b'''
56
59
create_signatures=always
57
60
''')
58
61
 
86
89
        """Commit and check two versions of a single file."""
87
90
        wt = self.make_branch_and_tree('.')
88
91
        b = wt.branch
89
 
        with open('hello', 'w') as f: f.write('hello world')
 
92
        with open('hello', 'w') as f:
 
93
            f.write('hello world')
90
94
        wt.add('hello')
91
95
        rev1 = wt.commit(message='add hello')
92
96
 
93
 
        with open('hello', 'w') as f: f.write('version 2')
 
97
        with open('hello', 'w') as f:
 
98
            f.write('version 2')
94
99
        rev2 = wt.commit(message='commit 2')
95
100
 
96
101
        eq = self.assertEqual
102
107
        tree1.lock_read()
103
108
        text = tree1.get_file_text('hello')
104
109
        tree1.unlock()
105
 
        self.assertEqual('hello world', text)
 
110
        self.assertEqual(b'hello world', text)
106
111
 
107
112
        tree2 = b.repository.revision_tree(rev2)
108
113
        tree2.lock_read()
109
114
        text = tree2.get_file_text('hello')
110
115
        tree2.unlock()
111
 
        self.assertEqual('version 2', text)
 
116
        self.assertEqual(b'version 2', text)
112
117
 
113
118
    def test_commit_lossy_native(self):
114
119
        """Attempt a lossy commit to a native branch."""
115
120
        wt = self.make_branch_and_tree('.')
116
121
        b = wt.branch
117
 
        with open('hello', 'w') as f: f.write('hello world')
 
122
        with open('hello', 'w') as f:
 
123
            f.write('hello world')
118
124
        wt.add('hello')
119
 
        revid = wt.commit(message='add hello', rev_id='revid', lossy=True)
120
 
        self.assertEqual('revid', revid)
 
125
        revid = wt.commit(message='add hello', rev_id=b'revid', lossy=True)
 
126
        self.assertEqual(b'revid', revid)
121
127
 
122
128
    def test_commit_lossy_foreign(self):
123
129
        """Attempt a lossy commit to a foreign branch."""
124
130
        test_foreign.register_dummy_foreign_for_test(self)
125
131
        wt = self.make_branch_and_tree('.',
126
 
            format=test_foreign.DummyForeignVcsDirFormat())
 
132
                                       format=test_foreign.DummyForeignVcsDirFormat())
127
133
        b = wt.branch
128
 
        with open('hello', 'w') as f: f.write('hello world')
 
134
        with open('hello', 'w') as f:
 
135
            f.write('hello world')
129
136
        wt.add('hello')
130
137
        revid = wt.commit(message='add hello', lossy=True,
131
 
            timestamp=1302659388, timezone=0)
132
 
        self.assertEqual('dummy-v1:1302659388.0-0-UNKNOWN', revid)
 
138
                          timestamp=1302659388, timezone=0)
 
139
        self.assertEqual(b'dummy-v1:1302659388-0-UNKNOWN', revid)
133
140
 
134
141
    def test_commit_bound_lossy_foreign(self):
135
142
        """Attempt a lossy commit to a bzr branch bound to a foreign branch."""
136
143
        test_foreign.register_dummy_foreign_for_test(self)
137
144
        foreign_branch = self.make_branch('foreign',
138
 
            format=test_foreign.DummyForeignVcsDirFormat())
 
145
                                          format=test_foreign.DummyForeignVcsDirFormat())
139
146
        wt = foreign_branch.create_checkout("local")
140
147
        b = wt.branch
141
 
        with open('local/hello', 'w') as f: f.write('hello world')
 
148
        with open('local/hello', 'w') as f:
 
149
            f.write('hello world')
142
150
        wt.add('hello')
143
151
        revid = wt.commit(message='add hello', lossy=True,
144
 
            timestamp=1302659388, timezone=0)
145
 
        self.assertEqual('dummy-v1:1302659388.0-0-0', revid)
146
 
        self.assertEqual('dummy-v1:1302659388.0-0-0',
147
 
            foreign_branch.last_revision())
148
 
        self.assertEqual('dummy-v1:1302659388.0-0-0',
149
 
            wt.branch.last_revision())
 
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())
150
158
 
151
159
    def test_missing_commit(self):
152
160
        """Test a commit with a missing file"""
153
161
        wt = self.make_branch_and_tree('.')
154
162
        b = wt.branch
155
 
        with open('hello', 'w') as f: f.write('hello world')
156
 
        wt.add(['hello'], ['hello-id'])
 
163
        with open('hello', 'w') as f:
 
164
            f.write('hello world')
 
165
        wt.add(['hello'], [b'hello-id'])
157
166
        wt.commit(message='add hello')
158
167
 
159
168
        os.remove('hello')
160
169
        reporter = CapturingReporter()
161
 
        wt.commit('removed hello', rev_id='rev2', reporter=reporter)
 
170
        wt.commit('removed hello', rev_id=b'rev2', reporter=reporter)
162
171
        self.assertEqual(
163
172
            [('missing', u'hello'), ('deleted', u'hello')],
164
173
            reporter.calls)
165
174
 
166
 
        tree = b.repository.revision_tree('rev2')
167
 
        self.assertFalse(tree.has_id('hello-id'))
 
175
        tree = b.repository.revision_tree(b'rev2')
 
176
        self.assertFalse(tree.has_filename('hello'))
168
177
 
169
178
    def test_partial_commit_move(self):
170
179
        """Test a partial commit where a file was renamed but not committed.
179
188
        b = wt.branch
180
189
        self.build_tree(['annotate/', 'annotate/foo.py',
181
190
                         'olive/', 'olive/dialog.py'
182
 
                        ])
 
191
                         ])
183
192
        wt.add(['annotate', 'olive', 'annotate/foo.py', 'olive/dialog.py'])
184
193
        wt.commit(message='add files')
185
194
        wt.rename_one("olive/dialog.py", "aaa")
186
 
        self.build_tree_contents([('annotate/foo.py', 'modified\n')])
 
195
        self.build_tree_contents([('annotate/foo.py', b'modified\n')])
187
196
        wt.commit('renamed hello', specific_files=["annotate"])
188
197
 
189
198
    def test_pointless_commit(self):
190
199
        """Commit refuses unless there are changes or it's forced."""
191
200
        wt = self.make_branch_and_tree('.')
192
201
        b = wt.branch
193
 
        with open('hello', 'w') as f: f.write('hello')
 
202
        with open('hello', 'w') as f:
 
203
            f.write('hello')
194
204
        wt.add(['hello'])
195
205
        wt.commit(message='add hello')
196
206
        self.assertEqual(b.revno(), 1)
216
226
        """Selective commit in tree with deletions"""
217
227
        wt = self.make_branch_and_tree('.')
218
228
        b = wt.branch
219
 
        with open('hello', 'w') as f: f.write('hello')
220
 
        with open('buongia', 'w') as f: f.write('buongia')
 
229
        with open('hello', 'w') as f:
 
230
            f.write('hello')
 
231
        with open('buongia', 'w') as f:
 
232
            f.write('buongia')
221
233
        wt.add(['hello', 'buongia'],
222
 
              ['hello-id', 'buongia-id'])
 
234
               [b'hello-id', b'buongia-id'])
223
235
        wt.commit(message='add files',
224
 
                 rev_id='test@rev-1')
 
236
                  rev_id=b'test@rev-1')
225
237
 
226
238
        os.remove('hello')
227
 
        with open('buongia', 'w') as f: f.write('new text')
 
239
        with open('buongia', 'w') as f:
 
240
            f.write('new text')
228
241
        wt.commit(message='update text',
229
 
                 specific_files=['buongia'],
230
 
                 allow_pointless=False,
231
 
                 rev_id='test@rev-2')
 
242
                  specific_files=['buongia'],
 
243
                  allow_pointless=False,
 
244
                  rev_id=b'test@rev-2')
232
245
 
233
246
        wt.commit(message='remove hello',
234
 
                 specific_files=['hello'],
235
 
                 allow_pointless=False,
236
 
                 rev_id='test@rev-3')
 
247
                  specific_files=['hello'],
 
248
                  allow_pointless=False,
 
249
                  rev_id=b'test@rev-3')
237
250
 
238
251
        eq = self.assertEqual
239
252
        eq(b.revno(), 3)
240
253
 
241
 
        tree2 = b.repository.revision_tree('test@rev-2')
 
254
        tree2 = b.repository.revision_tree(b'test@rev-2')
242
255
        tree2.lock_read()
243
256
        self.addCleanup(tree2.unlock)
244
257
        self.assertTrue(tree2.has_filename('hello'))
245
 
        self.assertEqual(tree2.get_file_text('hello'), 'hello')
246
 
        self.assertEqual(tree2.get_file_text('buongia'), 'new text')
 
258
        self.assertEqual(tree2.get_file_text('hello'), b'hello')
 
259
        self.assertEqual(tree2.get_file_text('buongia'), b'new text')
247
260
 
248
 
        tree3 = b.repository.revision_tree('test@rev-3')
 
261
        tree3 = b.repository.revision_tree(b'test@rev-3')
249
262
        tree3.lock_read()
250
263
        self.addCleanup(tree3.unlock)
251
264
        self.assertFalse(tree3.has_filename('hello'))
252
 
        self.assertEqual(tree3.get_file_text('buongia'), 'new text')
 
265
        self.assertEqual(tree3.get_file_text('buongia'), b'new text')
253
266
 
254
267
    def test_commit_rename(self):
255
268
        """Test commit of a revision where a file is renamed."""
256
269
        tree = self.make_branch_and_tree('.')
257
270
        b = tree.branch
258
271
        self.build_tree(['hello'], line_endings='binary')
259
 
        tree.add(['hello'], ['hello-id'])
260
 
        tree.commit(message='one', rev_id='test@rev-1', allow_pointless=False)
 
272
        tree.add(['hello'], [b'hello-id'])
 
273
        tree.commit(message='one', rev_id=b'test@rev-1', allow_pointless=False)
261
274
 
262
275
        tree.rename_one('hello', 'fruity')
263
 
        tree.commit(message='renamed', rev_id='test@rev-2', allow_pointless=False)
 
276
        tree.commit(message='renamed', rev_id=b'test@rev-2',
 
277
                    allow_pointless=False)
264
278
 
265
279
        eq = self.assertEqual
266
 
        tree1 = b.repository.revision_tree('test@rev-1')
 
280
        tree1 = b.repository.revision_tree(b'test@rev-1')
267
281
        tree1.lock_read()
268
282
        self.addCleanup(tree1.unlock)
269
 
        eq(tree1.id2path('hello-id'), 'hello')
270
 
        eq(tree1.get_file_text('hello'), 'contents of hello\n')
 
283
        eq(tree1.id2path(b'hello-id'), 'hello')
 
284
        eq(tree1.get_file_text('hello'), b'contents of hello\n')
271
285
        self.assertFalse(tree1.has_filename('fruity'))
272
286
        self.check_tree_shape(tree1, ['hello'])
273
 
        eq(tree1.get_file_revision('hello'), 'test@rev-1')
 
287
        eq(tree1.get_file_revision('hello'), b'test@rev-1')
274
288
 
275
 
        tree2 = b.repository.revision_tree('test@rev-2')
 
289
        tree2 = b.repository.revision_tree(b'test@rev-2')
276
290
        tree2.lock_read()
277
291
        self.addCleanup(tree2.unlock)
278
 
        eq(tree2.id2path('hello-id'), 'fruity')
279
 
        eq(tree2.get_file_text('fruity'), 'contents of hello\n')
 
292
        eq(tree2.id2path(b'hello-id'), 'fruity')
 
293
        eq(tree2.get_file_text('fruity'), b'contents of hello\n')
280
294
        self.check_tree_shape(tree2, ['fruity'])
281
 
        eq(tree2.get_file_revision('fruity'), 'test@rev-2')
 
295
        eq(tree2.get_file_revision('fruity'), b'test@rev-2')
282
296
 
283
297
    def test_reused_rev_id(self):
284
298
        """Test that a revision id cannot be reused in a branch"""
285
299
        wt = self.make_branch_and_tree('.')
286
300
        b = wt.branch
287
 
        wt.commit('initial', rev_id='test@rev-1', allow_pointless=True)
 
301
        wt.commit('initial', rev_id=b'test@rev-1', allow_pointless=True)
288
302
        self.assertRaises(Exception,
289
303
                          wt.commit,
290
304
                          message='reused id',
291
 
                          rev_id='test@rev-1',
 
305
                          rev_id=b'test@rev-1',
292
306
                          allow_pointless=True)
293
307
 
294
308
    def test_commit_move(self):
296
310
        eq = self.assertEqual
297
311
        wt = self.make_branch_and_tree('.')
298
312
        b = wt.branch
299
 
        r1 = 'test@rev-1'
 
313
        r1 = b'test@rev-1'
300
314
        self.build_tree(['hello', 'a/', 'b/'])
301
 
        wt.add(['hello', 'a', 'b'], ['hello-id', 'a-id', 'b-id'])
 
315
        wt.add(['hello', 'a', 'b'], [b'hello-id', b'a-id', b'b-id'])
302
316
        wt.commit('initial', rev_id=r1, allow_pointless=False)
303
317
        wt.move(['hello'], 'a')
304
 
        r2 = 'test@rev-2'
 
318
        r2 = b'test@rev-2'
305
319
        wt.commit('two', rev_id=r2, allow_pointless=False)
306
320
        wt.lock_read()
307
321
        try:
310
324
            wt.unlock()
311
325
 
312
326
        wt.move(['b'], 'a')
313
 
        r3 = 'test@rev-3'
 
327
        r3 = b'test@rev-3'
314
328
        wt.commit('three', rev_id=r3, allow_pointless=False)
315
329
        wt.lock_read()
316
330
        try:
317
331
            self.check_tree_shape(wt,
318
 
                                       ['a/', 'a/hello', 'a/b/'])
 
332
                                  ['a/', 'a/hello', 'a/b/'])
319
333
            self.check_tree_shape(b.repository.revision_tree(r3),
320
 
                                       ['a/', 'a/hello', 'a/b/'])
 
334
                                  ['a/', 'a/hello', 'a/b/'])
321
335
        finally:
322
336
            wt.unlock()
323
337
 
324
338
        wt.move(['a/hello'], 'a/b')
325
 
        r4 = 'test@rev-4'
 
339
        r4 = b'test@rev-4'
326
340
        wt.commit('four', rev_id=r4, allow_pointless=False)
327
341
        wt.lock_read()
328
342
        try:
331
345
            wt.unlock()
332
346
 
333
347
        inv = b.repository.get_inventory(r4)
334
 
        eq(inv['hello-id'].revision, r4)
335
 
        eq(inv['a-id'].revision, r1)
336
 
        eq(inv['b-id'].revision, r3)
 
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)
337
351
 
338
352
    def test_removed_commit(self):
339
353
        """Commit with a removed file"""
340
354
        wt = self.make_branch_and_tree('.')
341
355
        b = wt.branch
342
 
        with open('hello', 'w') as f: f.write('hello world')
343
 
        wt.add(['hello'], ['hello-id'])
 
356
        with open('hello', 'w') as f:
 
357
            f.write('hello world')
 
358
        wt.add(['hello'], [b'hello-id'])
344
359
        wt.commit(message='add hello')
345
360
        wt.remove('hello')
346
 
        wt.commit('removed hello', rev_id='rev2')
 
361
        wt.commit('removed hello', rev_id=b'rev2')
347
362
 
348
 
        tree = b.repository.revision_tree('rev2')
349
 
        self.assertFalse(tree.has_id('hello-id'))
 
363
        tree = b.repository.revision_tree(b'rev2')
 
364
        self.assertFalse(tree.has_filename('hello'))
350
365
 
351
366
    def test_committed_ancestry(self):
352
367
        """Test commit appends revisions to ancestry."""
354
369
        b = wt.branch
355
370
        rev_ids = []
356
371
        for i in range(4):
357
 
            with open('hello', 'w') as f: f.write((str(i) * 4) + '\n')
 
372
            with open('hello', 'w') as f:
 
373
                f.write((str(i) * 4) + '\n')
358
374
            if i == 0:
359
 
                wt.add(['hello'], ['hello-id'])
360
 
            rev_id = 'test@rev-%d' % (i+1)
 
375
                wt.add(['hello'], [b'hello-id'])
 
376
            rev_id = b'test@rev-%d' % (i + 1)
361
377
            rev_ids.append(rev_id)
362
 
            wt.commit(message='rev %d' % (i+1),
363
 
                     rev_id=rev_id)
 
378
            wt.commit(message='rev %d' % (i + 1),
 
379
                      rev_id=rev_id)
364
380
        for i in range(4):
365
 
            self.assertThat(rev_ids[:i+1],
366
 
                MatchesAncestry(b.repository, rev_ids[i]))
 
381
            self.assertThat(rev_ids[:i + 1],
 
382
                            MatchesAncestry(b.repository, rev_ids[i]))
367
383
 
368
384
    def test_commit_new_subdir_child_selective(self):
369
385
        wt = self.make_branch_and_tree('.')
370
386
        b = wt.branch
371
387
        self.build_tree(['dir/', 'dir/file1', 'dir/file2'])
372
388
        wt.add(['dir', 'dir/file1', 'dir/file2'],
373
 
              ['dirid', 'file1id', 'file2id'])
374
 
        wt.commit('dir/file1', specific_files=['dir/file1'], rev_id='1')
375
 
        inv = b.repository.get_inventory('1')
376
 
        self.assertEqual('1', inv['dirid'].revision)
377
 
        self.assertEqual('1', inv['file1id'].revision)
 
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)
378
394
        # FIXME: This should raise a KeyError I think, rbc20051006
379
 
        self.assertRaises(BzrError, inv.__getitem__, 'file2id')
 
395
        self.assertRaises(BzrError, inv.get_entry, b'file2id')
380
396
 
381
397
    def test_strict_commit(self):
382
398
        """Try and commit with unknown files and strict = True, should fail."""
383
399
        from ..errors import StrictCommitFailed
384
400
        wt = self.make_branch_and_tree('.')
385
401
        b = wt.branch
386
 
        with open('hello', 'w') as f: f.write('hello world')
 
402
        with open('hello', 'w') as f:
 
403
            f.write('hello world')
387
404
        wt.add('hello')
388
 
        with open('goodbye', 'w') as f: f.write('goodbye cruel world!')
 
405
        with open('goodbye', 'w') as f:
 
406
            f.write('goodbye cruel world!')
389
407
        self.assertRaises(StrictCommitFailed, wt.commit,
390
 
            message='add hello but not goodbye', strict=True)
 
408
                          message='add hello but not goodbye', strict=True)
391
409
 
392
410
    def test_strict_commit_without_unknowns(self):
393
411
        """Try and commit with no unknown files and strict = True,
394
412
        should work."""
395
413
        wt = self.make_branch_and_tree('.')
396
414
        b = wt.branch
397
 
        with open('hello', 'w') as f: f.write('hello world')
 
415
        with open('hello', 'w') as f:
 
416
            f.write('hello world')
398
417
        wt.add('hello')
399
418
        wt.commit(message='add hello', strict=True)
400
419
 
402
421
        """Try and commit with unknown files and strict = False, should work."""
403
422
        wt = self.make_branch_and_tree('.')
404
423
        b = wt.branch
405
 
        with open('hello', 'w') as f: f.write('hello world')
 
424
        with open('hello', 'w') as f:
 
425
            f.write('hello world')
406
426
        wt.add('hello')
407
 
        with open('goodbye', 'w') as f: f.write('goodbye cruel world!')
 
427
        with open('goodbye', 'w') as f:
 
428
            f.write('goodbye cruel world!')
408
429
        wt.commit(message='add hello but not goodbye', strict=False)
409
430
 
410
431
    def test_nonstrict_commit_without_unknowns(self):
412
433
        should work."""
413
434
        wt = self.make_branch_and_tree('.')
414
435
        b = wt.branch
415
 
        with open('hello', 'w') as f: f.write('hello world')
 
436
        with open('hello', 'w') as f:
 
437
            f.write('hello world')
416
438
        wt.add('hello')
417
439
        wt.commit(message='add hello', strict=False)
418
440
 
422
444
        oldstrategy = breezy.gpg.GPGStrategy
423
445
        wt = self.make_branch_and_tree('.')
424
446
        branch = wt.branch
425
 
        wt.commit("base", allow_pointless=True, rev_id='A')
426
 
        self.assertFalse(branch.repository.has_signature_for_revision_id('A'))
 
447
        wt.commit("base", allow_pointless=True, rev_id=b'A')
 
448
        self.assertFalse(branch.repository.has_signature_for_revision_id(b'A'))
427
449
        try:
428
 
            from ..testament import Testament
 
450
            from ..bzr.testament import Testament
429
451
            # monkey patch gpg signing mechanism
430
452
            breezy.gpg.GPGStrategy = breezy.gpg.LoopbackGPGStrategy
431
 
            conf = config.MemoryStack('''
 
453
            conf = config.MemoryStack(b'''
432
454
create_signatures=always
433
455
''')
434
456
            commit.Commit(config_stack=conf).commit(
435
 
                message="base", allow_pointless=True, rev_id='B',
 
457
                message="base", allow_pointless=True, rev_id=b'B',
436
458
                working_tree=wt)
 
459
 
437
460
            def sign(text):
438
 
                return breezy.gpg.LoopbackGPGStrategy(None).sign(text)
 
461
                return breezy.gpg.LoopbackGPGStrategy(None).sign(
 
462
                    text, breezy.gpg.MODE_CLEAR)
439
463
            self.assertEqual(sign(Testament.from_revision(branch.repository,
440
 
                                                          'B').as_short_text()),
441
 
                             branch.repository.get_signature_text('B'))
 
464
                                                          b'B').as_short_text()),
 
465
                             branch.repository.get_signature_text(b'B'))
442
466
        finally:
443
467
            breezy.gpg.GPGStrategy = oldstrategy
444
468
 
448
472
        oldstrategy = breezy.gpg.GPGStrategy
449
473
        wt = self.make_branch_and_tree('.')
450
474
        branch = wt.branch
451
 
        wt.commit("base", allow_pointless=True, rev_id='A')
452
 
        self.assertFalse(branch.repository.has_signature_for_revision_id('A'))
 
475
        wt.commit("base", allow_pointless=True, rev_id=b'A')
 
476
        self.assertFalse(branch.repository.has_signature_for_revision_id(b'A'))
453
477
        try:
454
478
            # monkey patch gpg signing mechanism
455
479
            breezy.gpg.GPGStrategy = breezy.gpg.DisabledGPGStrategy
456
 
            conf = config.MemoryStack('''
 
480
            conf = config.MemoryStack(b'''
457
481
create_signatures=always
458
482
''')
459
483
            self.assertRaises(breezy.gpg.SigningFailed,
460
484
                              commit.Commit(config_stack=conf).commit,
461
485
                              message="base",
462
486
                              allow_pointless=True,
463
 
                              rev_id='B',
 
487
                              rev_id=b'B',
464
488
                              working_tree=wt)
465
489
            branch = Branch.open(self.get_url('.'))
466
 
            self.assertEqual(branch.last_revision(), 'A')
467
 
            self.assertFalse(branch.repository.has_revision('B'))
 
490
            self.assertEqual(branch.last_revision(), b'A')
 
491
            self.assertFalse(branch.repository.has_revision(b'B'))
468
492
        finally:
469
493
            breezy.gpg.GPGStrategy = oldstrategy
470
494
 
473
497
        wt = self.make_branch_and_tree('.')
474
498
        branch = wt.branch
475
499
        calls = []
 
500
 
476
501
        def called(branch, rev_id):
477
502
            calls.append('called')
478
503
        breezy.ahook = called
479
504
        try:
480
 
            conf = config.MemoryStack('post_commit=breezy.ahook breezy.ahook')
 
505
            conf = config.MemoryStack(b'post_commit=breezy.ahook breezy.ahook')
481
506
            commit.Commit(config_stack=conf).commit(
482
 
                message = "base", allow_pointless=True, rev_id='A',
483
 
                working_tree = wt)
 
507
                message="base", allow_pointless=True, rev_id=b'A',
 
508
                working_tree=wt)
484
509
            self.assertEqual(['called', 'called'], calls)
485
510
        finally:
486
511
            del breezy.ahook
493
518
        self.assertEqual(wt.branch.revno(), 1)
494
519
        self.assertEqual({},
495
520
                         wt.branch.repository.get_revision(
496
 
                            wt.branch.last_revision()).properties)
 
521
            wt.branch.last_revision()).properties)
497
522
 
498
523
    def test_safe_master_lock(self):
499
524
        os.mkdir('master')
517
542
        bound_tree = self.make_branch_and_tree('bound')
518
543
        bound_tree.branch.bind(master_branch)
519
544
 
520
 
        self.build_tree_contents([('bound/content_file', 'initial contents\n')])
 
545
        self.build_tree_contents(
 
546
            [('bound/content_file', b'initial contents\n')])
521
547
        bound_tree.add(['content_file'])
522
548
        bound_tree.commit(message='woo!')
523
549
 
527
553
        # do a commit to the other branch changing the content file so
528
554
        # that our commit after merging will have a merged revision in the
529
555
        # content file history.
530
 
        self.build_tree_contents([('other/content_file', 'change in other\n')])
 
556
        self.build_tree_contents(
 
557
            [('other/content_file', b'change in other\n')])
531
558
        other_tree.commit('change in other')
532
559
 
533
560
        # do a merge into the bound branch from other, and then change the
534
561
        # content file locally to force a new revision (rather than using the
535
562
        # revision from other). This forces extra processing in commit.
536
563
        bound_tree.merge_from_branch(other_tree.branch)
537
 
        self.build_tree_contents([('bound/content_file', 'change in bound\n')])
 
564
        self.build_tree_contents(
 
565
            [('bound/content_file', b'change in bound\n')])
538
566
 
539
567
        # before #34959 was fixed, this failed with 'revision not present in
540
568
        # weave' when trying to implicitly push from the bound branch to the master
576
604
            other_tree.rename_one('dirtorename', 'renameddir')
577
605
            other_tree.rename_one('dirtoreparent', 'renameddir/reparenteddir')
578
606
            other_tree.rename_one('filetorename', 'renamedfile')
579
 
            other_tree.rename_one('filetoreparent', 'renameddir/reparentedfile')
 
607
            other_tree.rename_one(
 
608
                'filetoreparent', 'renameddir/reparentedfile')
580
609
            other_tree.remove(['dirtoremove', 'filetoremove'])
581
610
            self.build_tree_contents([
582
611
                ('other/newdir/', ),
583
 
                ('other/filetomodify', 'new content'),
584
 
                ('other/newfile', 'new file content')])
 
612
                ('other/filetomodify', b'new content'),
 
613
                ('other/newfile', b'new file content')])
585
614
            other_tree.add('newfile')
586
615
            other_tree.add('newdir/')
587
616
            other_tree.commit('modify all sample files and dirs.')
625
654
        self.build_tree(['a'])
626
655
        tree.add('a')
627
656
        tree.commit('added a', timestamp=1153248633.4186721, timezone=0,
628
 
                    rev_id='a1')
 
657
                    rev_id=b'a1')
629
658
 
630
 
        rev = tree.branch.repository.get_revision('a1')
 
659
        rev = tree.branch.repository.get_revision(b'a1')
631
660
        self.assertEqual(1153248633.419, rev.timestamp)
632
661
 
633
662
    def test_commit_has_1ms_resolution(self):
635
664
        tree = self.make_branch_and_tree('.')
636
665
        self.build_tree(['a'])
637
666
        tree.add('a')
638
 
        tree.commit('added a', rev_id='a1')
 
667
        tree.commit('added a', rev_id=b'a1')
639
668
 
640
 
        rev = tree.branch.repository.get_revision('a1')
 
669
        rev = tree.branch.repository.get_revision(b'a1')
641
670
        timestamp = rev.timestamp
642
671
        timestamp_1ms = round(timestamp, 3)
643
672
        self.assertEqual(timestamp_1ms, timestamp)
650
679
        finally:
651
680
            basis.unlock()
652
681
 
 
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
 
653
711
    def test_commit_kind_changes(self):
654
712
        self.requireFeature(SymlinkFeature)
655
713
        tree = self.make_branch_and_tree('.')
656
714
        os.symlink('target', 'name')
657
 
        tree.add('name', 'a-file-id')
 
715
        tree.add('name', b'a-file-id')
658
716
        tree.commit('Added a symlink')
659
717
        self.assertBasisTreeKind('symlink', tree, 'name')
660
718
 
745
803
        cb = self.Callback(u'commit 2', self)
746
804
        repository = tree.branch.repository
747
805
        # simulate network failure
 
806
 
748
807
        def raise_(self, arg, arg2, arg3=None, arg4=None):
749
808
            raise errors.NoSuchFile('foo')
750
809
        repository.add_inventory = raise_
757
816
        tree = self.make_branch_and_tree('foo')
758
817
        # pending merge would turn into a left parent
759
818
        tree.commit('commit 1')
760
 
        tree.add_parent_tree_id('example')
 
819
        tree.add_parent_tree_id(b'example')
761
820
        self.build_tree(['foo/bar', 'foo/baz'])
762
821
        tree.add(['bar', 'baz'])
763
822
        err = self.assertRaises(CannotCommitSelectedFileMerge,
764
 
            tree.commit, 'commit 2', specific_files=['bar', 'baz'])
 
823
                                tree.commit, 'commit 2', specific_files=['bar', 'baz'])
765
824
        self.assertEqual(['bar', 'baz'], err.files)
766
825
        self.assertEqual('Selected-file commit of merges is not supported'
767
826
                         ' yet: files bar, baz', str(err))
811
870
    def test_multiple_authors(self):
812
871
        tree = self.make_branch_and_tree('foo')
813
872
        rev_id = tree.commit('commit 1',
814
 
                authors=['John Doe <jdoe@example.com>',
815
 
                         'Jane Rey <jrey@example.com>'])
 
873
                             authors=['John Doe <jdoe@example.com>',
 
874
                                      'Jane Rey <jrey@example.com>'])
816
875
        rev = tree.branch.repository.get_revision(rev_id)
817
876
        self.assertEqual('John Doe <jdoe@example.com>\n'
818
 
                'Jane Rey <jrey@example.com>', rev.properties['authors'])
 
877
                         'Jane Rey <jrey@example.com>', rev.properties['authors'])
819
878
        self.assertFalse('author' in rev.properties)
820
879
 
821
880
    def test_author_with_newline_rejected(self):
822
881
        tree = self.make_branch_and_tree('foo')
823
882
        self.assertRaises(AssertionError, tree.commit, 'commit 1',
824
 
                authors=['John\nDoe <jdoe@example.com>'])
 
883
                          authors=['John\nDoe <jdoe@example.com>'])
825
884
 
826
885
    def test_commit_with_checkout_and_branch_sharing_repo(self):
827
886
        repo = self.make_repository('repo', shared=True)
828
887
        # make_branch_and_tree ignores shared repos
829
888
        branch = controldir.ControlDir.create_branch_convenience('repo/branch')
830
889
        tree2 = branch.create_checkout('repo/tree2')
831
 
        tree2.commit('message', rev_id='rev1')
832
 
        self.assertTrue(tree2.branch.repository.has_revision('rev1'))
 
890
        tree2.commit('message', rev_id=b'rev1')
 
891
        self.assertTrue(tree2.branch.repository.has_revision(b'rev1'))
833
892
 
834
893
 
835
894
class FilterExcludedTests(TestCase):
836
895
 
837
896
    def test_add_file_not_excluded(self):
838
897
        changes = [
839
 
            ('fid', (None, 'newpath'),
840
 
             0, (False, False), ('pid', 'pid'), ('newpath', 'newpath'),
841
 
             ('file', 'file'), (True, True))]
842
 
        self.assertEqual(changes, list(filter_excluded(changes, ['otherpath'])))
 
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'])))
843
904
 
844
905
    def test_add_file_excluded(self):
845
906
        changes = [
846
 
            ('fid', (None, 'newpath'),
847
 
             0, (False, False), ('pid', 'pid'), ('newpath', 'newpath'),
848
 
             ('file', 'file'), (True, True))]
 
907
            TreeChange(
 
908
                'fid', (None, 'newpath'),
 
909
                0, (False, False), ('pid', 'pid'), ('newpath', 'newpath'),
 
910
                ('file', 'file'), (True, True))]
849
911
        self.assertEqual([], list(filter_excluded(changes, ['newpath'])))
850
912
 
851
913
    def test_delete_file_excluded(self):
852
914
        changes = [
853
 
            ('fid', ('somepath', None),
854
 
             0, (False, None), ('pid', None), ('newpath', None),
855
 
             ('file', None), (True, None))]
 
915
            TreeChange(
 
916
                'fid', ('somepath', None),
 
917
                0, (False, None), ('pid', None), ('newpath', None),
 
918
                ('file', None), (True, None))]
856
919
        self.assertEqual([], list(filter_excluded(changes, ['somepath'])))
857
920
 
858
921
    def test_move_from_or_to_excluded(self):
859
922
        changes = [
860
 
            ('fid', ('oldpath', 'newpath'),
861
 
             0, (False, False), ('pid', 'pid'), ('oldpath', 'newpath'),
862
 
             ('file', 'file'), (True, True))]
 
923
            TreeChange(
 
924
                'fid', ('oldpath', 'newpath'),
 
925
                0, (False, False), ('pid', 'pid'), ('oldpath', 'newpath'),
 
926
                ('file', 'file'), (True, True))]
863
927
        self.assertEqual([], list(filter_excluded(changes, ['oldpath'])))
864
928
        self.assertEqual([], list(filter_excluded(changes, ['newpath'])))