~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_commit.py

[merge] update from bzr.dev

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