~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_commit.py

Merge repository so I dont trample on myself.

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
 
                          b.working_tree().commit,
 
181
                          wt.commit,
178
182
                          message='reused id',
179
183
                          rev_id='test@rev-1',
180
184
                          allow_pointless=True)
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)
193
 
        self.check_inventory_shape(b.working_tree().read_working_inventory(),
 
197
        wt.commit('two', rev_id=r2, allow_pointless=False)
 
198
        self.check_inventory_shape(wt.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([os.sep.join(['a', 'hello'])],
205
 
               os.sep.join(['a', 'b']))
 
209
        wt.move(['a/hello'], 'a/b')
206
210
        r4 = 'test@rev-4'
207
 
        b.working_tree().commit('four', rev_id=r4, allow_pointless=False)
208
 
        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(),
209
213
                                   ['a', 'a/b/hello', 'a/b'])
210
214
 
211
 
        inv = b.get_revision_inventory(r4)
 
215
        inv = b.repository.get_revision_inventory(r4)
212
216
        eq(inv['hello-id'].revision, r4)
213
217
        eq(inv['a-id'].revision, r1)
214
218
        eq(inv['b-id'].revision, r3)
215
219
        
216
220
    def test_removed_commit(self):
217
221
        """Commit with a removed file"""
218
 
        b = Branch.initialize(u'.')
219
 
        wt = b.working_tree()
 
222
        wt = self.make_branch_and_tree('.')
 
223
        b = wt.branch
220
224
        file('hello', 'w').write('hello world')
221
 
        b.working_tree().add(['hello'], ['hello-id'])
222
 
        b.working_tree().commit(message='add hello')
223
 
 
224
 
        wt = b.working_tree()  # FIXME: kludge for aliasing of working inventory
 
225
        wt.add(['hello'], ['hello-id'])
 
226
        wt.commit(message='add hello')
225
227
        wt.remove('hello')
226
 
        b.working_tree().commit('removed hello', rev_id='rev2')
 
228
        wt.commit('removed hello', rev_id='rev2')
227
229
 
228
 
        tree = b.revision_tree('rev2')
 
230
        tree = b.repository.revision_tree('rev2')
229
231
        self.assertFalse(tree.has_id('hello-id'))
230
232
 
231
 
 
232
233
    def test_committed_ancestry(self):
233
234
        """Test commit appends revisions to ancestry."""
234
 
        b = Branch.initialize(u'.')
 
235
        wt = self.make_branch_and_tree('.')
 
236
        b = wt.branch
235
237
        rev_ids = []
236
238
        for i in range(4):
237
239
            file('hello', 'w').write((str(i) * 4) + '\n')
238
240
            if i == 0:
239
 
                b.working_tree().add(['hello'], ['hello-id'])
 
241
                wt.add(['hello'], ['hello-id'])
240
242
            rev_id = 'test@rev-%d' % (i+1)
241
243
            rev_ids.append(rev_id)
242
 
            b.working_tree().commit(message='rev %d' % (i+1),
 
244
            wt.commit(message='rev %d' % (i+1),
243
245
                     rev_id=rev_id)
244
246
        eq = self.assertEquals
245
247
        eq(b.revision_history(), rev_ids)
246
248
        for i in range(4):
247
 
            anc = b.get_ancestry(rev_ids[i])
 
249
            anc = b.repository.get_ancestry(rev_ids[i])
248
250
            eq(anc, [None] + rev_ids[:i+1])
249
251
 
250
252
    def test_commit_new_subdir_child_selective(self):
251
 
        b = Branch.initialize(u'.')
 
253
        wt = self.make_branch_and_tree('.')
 
254
        b = wt.branch
252
255
        self.build_tree(['dir/', 'dir/file1', 'dir/file2'])
253
 
        b.working_tree().add(['dir', 'dir/file1', 'dir/file2'],
 
256
        wt.add(['dir', 'dir/file1', 'dir/file2'],
254
257
              ['dirid', 'file1id', 'file2id'])
255
 
        b.working_tree().commit('dir/file1', specific_files=['dir/file1'], rev_id='1')
256
 
        inv = b.get_inventory('1')
 
258
        wt.commit('dir/file1', specific_files=['dir/file1'], rev_id='1')
 
259
        inv = b.repository.get_inventory('1')
257
260
        self.assertEqual('1', inv['dirid'].revision)
258
261
        self.assertEqual('1', inv['file1id'].revision)
259
262
        # FIXME: This should raise a KeyError I think, rbc20051006
262
265
    def test_strict_commit(self):
263
266
        """Try and commit with unknown files and strict = True, should fail."""
264
267
        from bzrlib.errors import StrictCommitFailed
265
 
        b = Branch.initialize(u'.')
 
268
        wt = self.make_branch_and_tree('.')
 
269
        b = wt.branch
266
270
        file('hello', 'w').write('hello world')
267
 
        b.working_tree().add('hello')
 
271
        wt.add('hello')
268
272
        file('goodbye', 'w').write('goodbye cruel world!')
269
 
        self.assertRaises(StrictCommitFailed, b.working_tree().commit,
 
273
        self.assertRaises(StrictCommitFailed, wt.commit,
270
274
            message='add hello but not goodbye', strict=True)
271
275
 
272
276
    def test_strict_commit_without_unknowns(self):
273
277
        """Try and commit with no unknown files and strict = True,
274
278
        should work."""
275
279
        from bzrlib.errors import StrictCommitFailed
276
 
        b = Branch.initialize(u'.')
 
280
        wt = self.make_branch_and_tree('.')
 
281
        b = wt.branch
277
282
        file('hello', 'w').write('hello world')
278
 
        b.working_tree().add('hello')
279
 
        b.working_tree().commit(message='add hello', strict=True)
 
283
        wt.add('hello')
 
284
        wt.commit(message='add hello', strict=True)
280
285
 
281
286
    def test_nonstrict_commit(self):
282
287
        """Try and commit with unknown files and strict = False, should work."""
283
 
        b = Branch.initialize(u'.')
 
288
        wt = self.make_branch_and_tree('.')
 
289
        b = wt.branch
284
290
        file('hello', 'w').write('hello world')
285
 
        b.working_tree().add('hello')
 
291
        wt.add('hello')
286
292
        file('goodbye', 'w').write('goodbye cruel world!')
287
 
        b.working_tree().commit(message='add hello but not goodbye', strict=False)
 
293
        wt.commit(message='add hello but not goodbye', strict=False)
288
294
 
289
295
    def test_nonstrict_commit_without_unknowns(self):
290
296
        """Try and commit with no unknown files and strict = False,
291
297
        should work."""
292
 
        b = Branch.initialize(u'.')
 
298
        wt = self.make_branch_and_tree('.')
 
299
        b = wt.branch
293
300
        file('hello', 'w').write('hello world')
294
 
        b.working_tree().add('hello')
295
 
        b.working_tree().commit(message='add hello', strict=False)
 
301
        wt.add('hello')
 
302
        wt.commit(message='add hello', strict=False)
296
303
 
297
304
    def test_signed_commit(self):
298
305
        import bzrlib.gpg
299
306
        import bzrlib.commit as commit
300
307
        oldstrategy = bzrlib.gpg.GPGStrategy
301
 
        branch = Branch.initialize(u'.')
302
 
        branch.working_tree().commit("base", allow_pointless=True, rev_id='A')
303
 
        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'))
304
312
        try:
305
313
            from bzrlib.testament import Testament
306
314
            # monkey patch gpg signing mechanism
307
315
            bzrlib.gpg.GPGStrategy = bzrlib.gpg.LoopbackGPGStrategy
308
 
            commit.Commit(config=MustSignConfig(branch)).commit(branch, "base",
 
316
            commit.Commit(config=MustSignConfig(branch)).commit(message="base",
309
317
                                                      allow_pointless=True,
310
 
                                                      rev_id='B')
311
 
            self.assertEqual(Testament.from_revision(branch,'B').as_short_text(),
312
 
                             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())
313
324
        finally:
314
325
            bzrlib.gpg.GPGStrategy = oldstrategy
315
326
 
317
328
        import bzrlib.gpg
318
329
        import bzrlib.commit as commit
319
330
        oldstrategy = bzrlib.gpg.GPGStrategy
320
 
        branch = Branch.initialize(u'.')
321
 
        branch.working_tree().commit("base", allow_pointless=True, rev_id='A')
322
 
        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'))
323
335
        try:
324
336
            from bzrlib.testament import Testament
325
337
            # monkey patch gpg signing mechanism
327
339
            config = MustSignConfig(branch)
328
340
            self.assertRaises(SigningFailed,
329
341
                              commit.Commit(config=config).commit,
330
 
                              branch, "base",
 
342
                              message="base",
331
343
                              allow_pointless=True,
332
 
                              rev_id='B')
333
 
            branch = Branch.open(u'.')
 
344
                              rev_id='B',
 
345
                              working_tree=wt)
 
346
            branch = Branch.open(self.get_url('.'))
334
347
            self.assertEqual(branch.revision_history(), ['A'])
335
 
            self.failIf(branch.revision_store.has_id('B'))
 
348
            self.failIf(branch.repository.revision_store.has_id('B'))
336
349
        finally:
337
350
            bzrlib.gpg.GPGStrategy = oldstrategy
338
351
 
339
352
    def test_commit_invokes_hooks(self):
340
353
        import bzrlib.commit as commit
341
 
        branch = Branch.initialize(u'.')
 
354
        wt = self.make_branch_and_tree('.')
 
355
        branch = wt.branch
342
356
        calls = []
343
357
        def called(branch, rev_id):
344
358
            calls.append('called')
346
360
        try:
347
361
            config = BranchWithHooks(branch)
348
362
            commit.Commit(config=config).commit(
349
 
                            branch, "base",
 
363
                            message = "base",
350
364
                            allow_pointless=True,
351
 
                            rev_id='A')
 
365
                            rev_id='A', working_tree = wt)
352
366
            self.assertEqual(['called', 'called'], calls)
353
367
        finally:
354
368
            del bzrlib.ahook