/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 bzrlib/selftest/test_commit.py

  • Committer: Robert Collins
  • Date: 2005-11-04 23:27:47 UTC
  • Revision ID: robertc@robertcollins.net-20051104232747-5872c68d759bc7be
Bugfix the config test suite to not create .bazaar in the dir where it is run.

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
import os
19
19
 
20
20
import bzrlib
21
 
from bzrlib.tests import TestCaseWithTransport
 
21
from bzrlib.selftest import TestCaseInTempDir
22
22
from bzrlib.branch import Branch
23
 
from bzrlib.bzrdir import BzrDir, BzrDirMetaFormat1
24
23
from bzrlib.workingtree import WorkingTree
25
24
from bzrlib.commit import Commit
26
25
from bzrlib.config import BranchConfig
27
 
from bzrlib.errors import (PointlessCommit, BzrError, SigningFailed, 
28
 
                           LockContention)
 
26
from bzrlib.errors import PointlessCommit, BzrError, SigningFailed
29
27
 
30
28
 
31
29
# TODO: Test commit with some added, and added-but-missing files
45
43
        return "bzrlib.ahook bzrlib.ahook"
46
44
 
47
45
 
48
 
class TestCommit(TestCaseWithTransport):
 
46
class TestCommit(TestCaseInTempDir):
49
47
 
50
48
    def test_simple_commit(self):
51
49
        """Commit and check two versions of a single file."""
52
 
        wt = self.make_branch_and_tree('.')
53
 
        b = wt.branch
 
50
        b = Branch.initialize('.')
54
51
        file('hello', 'w').write('hello world')
55
 
        wt.add('hello')
56
 
        wt.commit(message='add hello')
57
 
        file_id = wt.path2id('hello')
 
52
        b.add('hello')
 
53
        b.commit(message='add hello')
 
54
        file_id = b.working_tree().path2id('hello')
58
55
 
59
56
        file('hello', 'w').write('version 2')
60
 
        wt.commit(message='commit 2')
 
57
        b.commit(message='commit 2')
61
58
 
62
59
        eq = self.assertEquals
63
60
        eq(b.revno(), 2)
64
61
        rh = b.revision_history()
65
 
        rev = b.repository.get_revision(rh[0])
 
62
        rev = b.get_revision(rh[0])
66
63
        eq(rev.message, 'add hello')
67
64
 
68
 
        tree1 = b.repository.revision_tree(rh[0])
 
65
        tree1 = b.revision_tree(rh[0])
69
66
        text = tree1.get_file_text(file_id)
70
67
        eq(text, 'hello world')
71
68
 
72
 
        tree2 = b.repository.revision_tree(rh[1])
 
69
        tree2 = b.revision_tree(rh[1])
73
70
        eq(tree2.get_file_text(file_id), 'version 2')
74
71
 
75
72
    def test_delete_commit(self):
76
73
        """Test a commit with a deleted file"""
77
 
        wt = self.make_branch_and_tree('.')
78
 
        b = wt.branch
 
74
        b = Branch.initialize('.')
79
75
        file('hello', 'w').write('hello world')
80
 
        wt.add(['hello'], ['hello-id'])
81
 
        wt.commit(message='add hello')
 
76
        b.add(['hello'], ['hello-id'])
 
77
        b.commit(message='add hello')
82
78
 
83
79
        os.remove('hello')
84
 
        wt.commit('removed hello', rev_id='rev2')
 
80
        b.commit('removed hello', rev_id='rev2')
85
81
 
86
 
        tree = b.repository.revision_tree('rev2')
 
82
        tree = b.revision_tree('rev2')
87
83
        self.assertFalse(tree.has_id('hello-id'))
88
84
 
 
85
 
89
86
    def test_pointless_commit(self):
90
87
        """Commit refuses unless there are changes or it's forced."""
91
 
        wt = self.make_branch_and_tree('.')
92
 
        b = wt.branch
 
88
        b = Branch.initialize('.')
93
89
        file('hello', 'w').write('hello')
94
 
        wt.add(['hello'])
95
 
        wt.commit(message='add hello')
 
90
        b.add(['hello'])
 
91
        b.commit(message='add hello')
96
92
        self.assertEquals(b.revno(), 1)
97
93
        self.assertRaises(PointlessCommit,
98
 
                          wt.commit,
 
94
                          b.commit,
99
95
                          message='fails',
100
96
                          allow_pointless=False)
101
97
        self.assertEquals(b.revno(), 1)
102
98
        
 
99
 
 
100
 
103
101
    def test_commit_empty(self):
104
102
        """Commiting an empty tree works."""
105
 
        wt = self.make_branch_and_tree('.')
106
 
        b = wt.branch
107
 
        wt.commit(message='empty tree', allow_pointless=True)
 
103
        b = Branch.initialize('.')
 
104
        b.commit(message='empty tree', allow_pointless=True)
108
105
        self.assertRaises(PointlessCommit,
109
 
                          wt.commit,
 
106
                          b.commit,
110
107
                          message='empty tree',
111
108
                          allow_pointless=False)
112
 
        wt.commit(message='empty tree', allow_pointless=True)
 
109
        b.commit(message='empty tree', allow_pointless=True)
113
110
        self.assertEquals(b.revno(), 2)
114
111
 
 
112
 
115
113
    def test_selective_delete(self):
116
114
        """Selective commit in tree with deletions"""
117
 
        wt = self.make_branch_and_tree('.')
118
 
        b = wt.branch
 
115
        b = Branch.initialize('.')
119
116
        file('hello', 'w').write('hello')
120
117
        file('buongia', 'w').write('buongia')
121
 
        wt.add(['hello', 'buongia'],
 
118
        b.add(['hello', 'buongia'],
122
119
              ['hello-id', 'buongia-id'])
123
 
        wt.commit(message='add files',
 
120
        b.commit(message='add files',
124
121
                 rev_id='test@rev-1')
125
122
        
126
123
        os.remove('hello')
127
124
        file('buongia', 'w').write('new text')
128
 
        wt.commit(message='update text',
 
125
        b.commit(message='update text',
129
126
                 specific_files=['buongia'],
130
127
                 allow_pointless=False,
131
128
                 rev_id='test@rev-2')
132
129
 
133
 
        wt.commit(message='remove hello',
 
130
        b.commit(message='remove hello',
134
131
                 specific_files=['hello'],
135
132
                 allow_pointless=False,
136
133
                 rev_id='test@rev-3')
138
135
        eq = self.assertEquals
139
136
        eq(b.revno(), 3)
140
137
 
141
 
        tree2 = b.repository.revision_tree('test@rev-2')
 
138
        tree2 = b.revision_tree('test@rev-2')
142
139
        self.assertTrue(tree2.has_filename('hello'))
143
140
        self.assertEquals(tree2.get_file_text('hello-id'), 'hello')
144
141
        self.assertEquals(tree2.get_file_text('buongia-id'), 'new text')
145
142
        
146
 
        tree3 = b.repository.revision_tree('test@rev-3')
 
143
        tree3 = b.revision_tree('test@rev-3')
147
144
        self.assertFalse(tree3.has_filename('hello'))
148
145
        self.assertEquals(tree3.get_file_text('buongia-id'), 'new text')
149
146
 
 
147
 
150
148
    def test_commit_rename(self):
151
149
        """Test commit of a revision where a file is renamed."""
152
 
        tree = self.make_branch_and_tree('.')
153
 
        b = tree.branch
154
 
        self.build_tree(['hello'], line_endings='binary')
155
 
        tree.add(['hello'], ['hello-id'])
156
 
        tree.commit(message='one', rev_id='test@rev-1', allow_pointless=False)
 
150
        b = Branch.initialize('.')
 
151
        self.build_tree(['hello'])
 
152
        b.add(['hello'], ['hello-id'])
 
153
        b.commit(message='one', rev_id='test@rev-1', allow_pointless=False)
157
154
 
158
 
        tree.rename_one('hello', 'fruity')
159
 
        tree.commit(message='renamed', rev_id='test@rev-2', allow_pointless=False)
 
155
        b.rename_one('hello', 'fruity')
 
156
        b.commit(message='renamed', rev_id='test@rev-2', allow_pointless=False)
160
157
 
161
158
        eq = self.assertEquals
162
 
        tree1 = b.repository.revision_tree('test@rev-1')
 
159
        tree1 = b.revision_tree('test@rev-1')
163
160
        eq(tree1.id2path('hello-id'), 'hello')
164
161
        eq(tree1.get_file_text('hello-id'), 'contents of hello\n')
165
162
        self.assertFalse(tree1.has_filename('fruity'))
167
164
        ie = tree1.inventory['hello-id']
168
165
        eq(ie.revision, 'test@rev-1')
169
166
 
170
 
        tree2 = b.repository.revision_tree('test@rev-2')
 
167
        tree2 = b.revision_tree('test@rev-2')
171
168
        eq(tree2.id2path('hello-id'), 'fruity')
172
169
        eq(tree2.get_file_text('hello-id'), 'contents of hello\n')
173
170
        self.check_inventory_shape(tree2.inventory, ['fruity'])
174
171
        ie = tree2.inventory['hello-id']
175
172
        eq(ie.revision, 'test@rev-2')
176
173
 
 
174
 
177
175
    def test_reused_rev_id(self):
178
176
        """Test that a revision id cannot be reused in a branch"""
179
 
        wt = self.make_branch_and_tree('.')
180
 
        b = wt.branch
181
 
        wt.commit('initial', rev_id='test@rev-1', allow_pointless=True)
 
177
        b = Branch.initialize('.')
 
178
        b.commit('initial', rev_id='test@rev-1', allow_pointless=True)
182
179
        self.assertRaises(Exception,
183
 
                          wt.commit,
 
180
                          b.commit,
184
181
                          message='reused id',
185
182
                          rev_id='test@rev-1',
186
183
                          allow_pointless=True)
 
184
                          
 
185
 
187
186
 
188
187
    def test_commit_move(self):
189
188
        """Test commit of revisions with moved files and directories"""
190
189
        eq = self.assertEquals
191
 
        wt = self.make_branch_and_tree('.')
192
 
        b = wt.branch
 
190
        b = Branch.initialize('.')
193
191
        r1 = 'test@rev-1'
194
192
        self.build_tree(['hello', 'a/', 'b/'])
195
 
        wt.add(['hello', 'a', 'b'], ['hello-id', 'a-id', 'b-id'])
196
 
        wt.commit('initial', rev_id=r1, allow_pointless=False)
197
 
        wt.move(['hello'], 'a')
 
193
        b.add(['hello', 'a', 'b'], ['hello-id', 'a-id', 'b-id'])
 
194
        b.commit('initial', rev_id=r1, allow_pointless=False)
 
195
 
 
196
        b.move(['hello'], 'a')
198
197
        r2 = 'test@rev-2'
199
 
        wt.commit('two', rev_id=r2, allow_pointless=False)
200
 
        self.check_inventory_shape(wt.read_working_inventory(),
 
198
        b.commit('two', rev_id=r2, allow_pointless=False)
 
199
        self.check_inventory_shape(b.working_tree().read_working_inventory(),
201
200
                                   ['a', 'a/hello', 'b'])
202
201
 
203
 
        wt.move(['b'], 'a')
 
202
        b.move(['b'], 'a')
204
203
        r3 = 'test@rev-3'
205
 
        wt.commit('three', rev_id=r3, allow_pointless=False)
206
 
        self.check_inventory_shape(wt.read_working_inventory(),
 
204
        b.commit('three', rev_id=r3, allow_pointless=False)
 
205
        self.check_inventory_shape(b.working_tree().read_working_inventory(),
207
206
                                   ['a', 'a/hello', 'a/b'])
208
 
        self.check_inventory_shape(b.repository.get_revision_inventory(r3),
 
207
        self.check_inventory_shape(b.get_revision_inventory(r3),
209
208
                                   ['a', 'a/hello', 'a/b'])
210
209
 
211
 
        wt.move(['a/hello'], 'a/b')
 
210
        b.move([os.sep.join(['a', 'hello'])],
 
211
               os.sep.join(['a', 'b']))
212
212
        r4 = 'test@rev-4'
213
 
        wt.commit('four', rev_id=r4, allow_pointless=False)
214
 
        self.check_inventory_shape(wt.read_working_inventory(),
 
213
        b.commit('four', rev_id=r4, allow_pointless=False)
 
214
        self.check_inventory_shape(b.working_tree().read_working_inventory(),
215
215
                                   ['a', 'a/b/hello', 'a/b'])
216
216
 
217
 
        inv = b.repository.get_revision_inventory(r4)
 
217
        inv = b.get_revision_inventory(r4)
218
218
        eq(inv['hello-id'].revision, r4)
219
219
        eq(inv['a-id'].revision, r1)
220
220
        eq(inv['b-id'].revision, r3)
 
221
 
221
222
        
222
223
    def test_removed_commit(self):
223
224
        """Commit with a removed file"""
224
 
        wt = self.make_branch_and_tree('.')
225
 
        b = wt.branch
 
225
        b = Branch.initialize('.')
 
226
        wt = b.working_tree()
226
227
        file('hello', 'w').write('hello world')
227
 
        wt.add(['hello'], ['hello-id'])
228
 
        wt.commit(message='add hello')
 
228
        b.add(['hello'], ['hello-id'])
 
229
        b.commit(message='add hello')
 
230
 
 
231
        wt = b.working_tree()  # FIXME: kludge for aliasing of working inventory
229
232
        wt.remove('hello')
230
 
        wt.commit('removed hello', rev_id='rev2')
 
233
        b.commit('removed hello', rev_id='rev2')
231
234
 
232
 
        tree = b.repository.revision_tree('rev2')
 
235
        tree = b.revision_tree('rev2')
233
236
        self.assertFalse(tree.has_id('hello-id'))
234
237
 
 
238
 
235
239
    def test_committed_ancestry(self):
236
240
        """Test commit appends revisions to ancestry."""
237
 
        wt = self.make_branch_and_tree('.')
238
 
        b = wt.branch
 
241
        b = Branch.initialize('.')
239
242
        rev_ids = []
240
243
        for i in range(4):
241
244
            file('hello', 'w').write((str(i) * 4) + '\n')
242
245
            if i == 0:
243
 
                wt.add(['hello'], ['hello-id'])
 
246
                b.add(['hello'], ['hello-id'])
244
247
            rev_id = 'test@rev-%d' % (i+1)
245
248
            rev_ids.append(rev_id)
246
 
            wt.commit(message='rev %d' % (i+1),
 
249
            b.commit(message='rev %d' % (i+1),
247
250
                     rev_id=rev_id)
248
251
        eq = self.assertEquals
249
252
        eq(b.revision_history(), rev_ids)
250
253
        for i in range(4):
251
 
            anc = b.repository.get_ancestry(rev_ids[i])
 
254
            anc = b.get_ancestry(rev_ids[i])
252
255
            eq(anc, [None] + rev_ids[:i+1])
253
256
 
254
257
    def test_commit_new_subdir_child_selective(self):
255
 
        wt = self.make_branch_and_tree('.')
256
 
        b = wt.branch
 
258
        b = Branch.initialize('.')
257
259
        self.build_tree(['dir/', 'dir/file1', 'dir/file2'])
258
 
        wt.add(['dir', 'dir/file1', 'dir/file2'],
 
260
        b.add(['dir', 'dir/file1', 'dir/file2'],
259
261
              ['dirid', 'file1id', 'file2id'])
260
 
        wt.commit('dir/file1', specific_files=['dir/file1'], rev_id='1')
261
 
        inv = b.repository.get_inventory('1')
 
262
        b.commit('dir/file1', specific_files=['dir/file1'], rev_id='1')
 
263
        inv = b.get_inventory('1')
262
264
        self.assertEqual('1', inv['dirid'].revision)
263
265
        self.assertEqual('1', inv['file1id'].revision)
264
266
        # FIXME: This should raise a KeyError I think, rbc20051006
267
269
    def test_strict_commit(self):
268
270
        """Try and commit with unknown files and strict = True, should fail."""
269
271
        from bzrlib.errors import StrictCommitFailed
270
 
        wt = self.make_branch_and_tree('.')
271
 
        b = wt.branch
 
272
        b = Branch.initialize('.')
272
273
        file('hello', 'w').write('hello world')
273
 
        wt.add('hello')
 
274
        b.add('hello')
274
275
        file('goodbye', 'w').write('goodbye cruel world!')
275
 
        self.assertRaises(StrictCommitFailed, wt.commit,
 
276
        self.assertRaises(StrictCommitFailed, b.commit,
276
277
            message='add hello but not goodbye', strict=True)
277
278
 
278
279
    def test_strict_commit_without_unknowns(self):
279
280
        """Try and commit with no unknown files and strict = True,
280
281
        should work."""
281
282
        from bzrlib.errors import StrictCommitFailed
282
 
        wt = self.make_branch_and_tree('.')
283
 
        b = wt.branch
 
283
        b = Branch.initialize('.')
284
284
        file('hello', 'w').write('hello world')
285
 
        wt.add('hello')
286
 
        wt.commit(message='add hello', strict=True)
 
285
        b.add('hello')
 
286
        b.commit(message='add hello', strict=True)
287
287
 
288
288
    def test_nonstrict_commit(self):
289
289
        """Try and commit with unknown files and strict = False, should work."""
290
 
        wt = self.make_branch_and_tree('.')
291
 
        b = wt.branch
 
290
        b = Branch.initialize('.')
292
291
        file('hello', 'w').write('hello world')
293
 
        wt.add('hello')
 
292
        b.add('hello')
294
293
        file('goodbye', 'w').write('goodbye cruel world!')
295
 
        wt.commit(message='add hello but not goodbye', strict=False)
 
294
        b.commit(message='add hello but not goodbye', strict=False)
296
295
 
297
296
    def test_nonstrict_commit_without_unknowns(self):
298
297
        """Try and commit with no unknown files and strict = False,
299
298
        should work."""
300
 
        wt = self.make_branch_and_tree('.')
301
 
        b = wt.branch
 
299
        b = Branch.initialize('.')
302
300
        file('hello', 'w').write('hello world')
303
 
        wt.add('hello')
304
 
        wt.commit(message='add hello', strict=False)
 
301
        b.add('hello')
 
302
        b.commit(message='add hello', strict=False)
305
303
 
306
304
    def test_signed_commit(self):
307
305
        import bzrlib.gpg
308
306
        import bzrlib.commit as commit
309
307
        oldstrategy = bzrlib.gpg.GPGStrategy
310
 
        wt = self.make_branch_and_tree('.')
311
 
        branch = wt.branch
312
 
        wt.commit("base", allow_pointless=True, rev_id='A')
313
 
        self.failIf(branch.repository.has_signature_for_revision_id('A'))
 
308
        branch = Branch.initialize('.')
 
309
        branch.commit("base", allow_pointless=True, rev_id='A')
 
310
        self.failIf(branch.revision_store.has_id('A', 'sig'))
314
311
        try:
315
312
            from bzrlib.testament import Testament
316
313
            # monkey patch gpg signing mechanism
317
314
            bzrlib.gpg.GPGStrategy = bzrlib.gpg.LoopbackGPGStrategy
318
 
            commit.Commit(config=MustSignConfig(branch)).commit(message="base",
 
315
            commit.Commit(config=MustSignConfig(branch)).commit(branch, "base",
319
316
                                                      allow_pointless=True,
320
 
                                                      rev_id='B',
321
 
                                                      working_tree=wt)
322
 
            self.assertEqual(Testament.from_revision(branch.repository,
323
 
                             'B').as_short_text(),
324
 
                             branch.repository.get_signature_text('B'))
 
317
                                                      rev_id='B')
 
318
            self.assertEqual(Testament.from_revision(branch,'B').as_short_text(),
 
319
                             branch.revision_store.get('B', 'sig').read())
325
320
        finally:
326
321
            bzrlib.gpg.GPGStrategy = oldstrategy
327
322
 
329
324
        import bzrlib.gpg
330
325
        import bzrlib.commit as commit
331
326
        oldstrategy = bzrlib.gpg.GPGStrategy
332
 
        wt = self.make_branch_and_tree('.')
333
 
        branch = wt.branch
334
 
        wt.commit("base", allow_pointless=True, rev_id='A')
335
 
        self.failIf(branch.repository.has_signature_for_revision_id('A'))
 
327
        branch = Branch.initialize('.')
 
328
        branch.commit("base", allow_pointless=True, rev_id='A')
 
329
        self.failIf(branch.revision_store.has_id('A', 'sig'))
336
330
        try:
337
331
            from bzrlib.testament import Testament
338
332
            # monkey patch gpg signing mechanism
340
334
            config = MustSignConfig(branch)
341
335
            self.assertRaises(SigningFailed,
342
336
                              commit.Commit(config=config).commit,
343
 
                              message="base",
 
337
                              branch, "base",
344
338
                              allow_pointless=True,
345
 
                              rev_id='B',
346
 
                              working_tree=wt)
347
 
            branch = Branch.open(self.get_url('.'))
 
339
                              rev_id='B')
 
340
            branch = Branch.open('.')
348
341
            self.assertEqual(branch.revision_history(), ['A'])
349
 
            self.failIf(branch.repository.has_revision('B'))
 
342
            self.failIf(branch.revision_store.has_id('B'))
350
343
        finally:
351
344
            bzrlib.gpg.GPGStrategy = oldstrategy
352
345
 
353
346
    def test_commit_invokes_hooks(self):
354
347
        import bzrlib.commit as commit
355
 
        wt = self.make_branch_and_tree('.')
356
 
        branch = wt.branch
 
348
        branch = Branch.initialize('.')
357
349
        calls = []
358
350
        def called(branch, rev_id):
359
351
            calls.append('called')
361
353
        try:
362
354
            config = BranchWithHooks(branch)
363
355
            commit.Commit(config=config).commit(
364
 
                            message = "base",
 
356
                            branch, "base",
365
357
                            allow_pointless=True,
366
 
                            rev_id='A', working_tree = wt)
 
358
                            rev_id='A')
367
359
            self.assertEqual(['called', 'called'], calls)
368
360
        finally:
369
361
            del bzrlib.ahook
370
 
 
371
 
    def test_commit_object_doesnt_set_nick(self):
372
 
        # using the Commit object directly does not set the branch nick.
373
 
        wt = self.make_branch_and_tree('.')
374
 
        c = Commit()
375
 
        c.commit(working_tree=wt, message='empty tree', allow_pointless=True)
376
 
        self.assertEquals(wt.branch.revno(), 1)
377
 
        self.assertEqual({},
378
 
                         wt.branch.repository.get_revision(
379
 
                            wt.branch.last_revision()).properties)
380
 
 
381
 
    def test_safe_master_lock(self):
382
 
        os.mkdir('master')
383
 
        master = BzrDirMetaFormat1().initialize('master')
384
 
        master.create_repository()
385
 
        master_branch = master.create_branch()
386
 
        master.create_workingtree()
387
 
        bound = master.sprout('bound')
388
 
        wt = bound.open_workingtree()
389
 
        wt.branch.set_bound_location(os.path.realpath('master'))
390
 
        master_branch.lock_write()
391
 
        try:
392
 
            self.assertRaises(LockContention, wt.commit, 'silly')
393
 
        finally:
394
 
            master_branch.unlock()