58
65
# Make sure the file is actually back the way it was
59
66
self.assertEqual(file('test_file').read(), self.ORIGINAL)
61
# Check the shelf is right
62
shelf = b._transport.get('.bzr/x-shelf/default/00').read()
63
self.assertEqual(shelf, self.DIFF_1)
68
self._check_shelf('00')
66
self.run_bzr('unshelve', retcode=True)
68
# Check the diff is right again
69
self.assertEqual(self.capture('diff', retcode=1),
70
self.DIFF_HEADER + self.DIFF_1 + '\n')
71
self.run_bzr('unshelve', '--all', retcode=0)
75
# Check the shelved patch was backed up
76
self._check_shelf('00~')
72
78
# Make sure the file is back the way it should be
73
79
self.assertEqual(file('test_file').read(), self.MODIFIED)
75
81
def test_shelf_nothing_to_shelve(self):
77
from bzrlib.branch import Branch
78
b = Branch.initialize('.')
80
# Create a test file and commit it
81
file('test_file', 'w').write(self.ORIGINAL)
82
b.working_tree().add('test_file')
83
b.working_tree().commit(message='add test_file')
83
self.tree = self.make_branch_and_tree('.')
84
self.__create_and_add_test_file()
85
86
# Shelve the changes
86
self.run_bzr('shelve', retcode=True)
87
self.run_bzr('shelve', '--all', retcode=3)
88
if b._transport.has('.bzr/x-shelf/default/00'):
89
if os.path.exists(os.path.join(self.tree.branch.base,
90
'.shelf/shelves/default/00')):
89
91
self.fail("Shelf exists, but it shouldn't")
93
def __create_and_add_test_file(self, filename='test_file'):
94
f = open(filename, 'w')
95
f.write(self.ORIGINAL)
97
self.tree.add(self.tree.relpath(os.path.join(os.getcwd(), filename)))
98
self.tree.commit(message='add %s' % filename)
91
100
def test_shelf_with_revision(self):
92
from bzrlib.branch import Branch
93
b = Branch.initialize('.')
101
self.tree = self.make_branch_and_tree('.')
95
# Create a test file and commit it
96
file('test_file', 'w').write(self.ORIGINAL)
97
b.working_tree().add('test_file')
98
b.working_tree().commit(message='add test_file')
103
self.__create_and_add_test_file()
100
105
# Modify the test file and commit it
101
file('test_file', 'w').write(self.MODIFIED)
102
b.working_tree().commit(message='update test_file')
106
self.build_tree_contents([('test_file', self.MODIFIED)])
107
self.tree.commit(message='update test_file')
104
109
# Shelve the changes
105
self.run_bzr('shelve', '-r', '1', retcode=True)
110
self.run_bzr('shelve', '--all', '-r', '1', retcode=0)
107
# Check the diff is right
108
self.assertEqual(self.capture('diff', retcode=1),
109
self.DIFF_HEADER + self.DIFF_2 + '\n')
112
self._check_diff(self.DIFF_2)
111
114
# Make sure the file is the way it should be
112
115
self.assertEqual(file('test_file').read(), self.ORIGINAL)
115
self.run_bzr('unshelve', retcode=True)
118
self.run_bzr('unshelve', '--all', retcode=0)
117
120
# Make sure the file is back the way it should be
118
121
self.assertEqual(file('test_file').read(), self.MODIFIED)
120
123
def test_shelf_with_two_revisions(self):
121
from bzrlib.branch import Branch
122
b = Branch.initialize('.')
124
self.tree = self.make_branch_and_tree('.')
124
cmd = 'shelve -r 1..2'
126
cmd = 'shelve --all -r 1..2'
125
127
(stdout, stderr) = self.run_bzr_captured(cmd.split(), retcode=None)
127
129
self.assertEqual(stderr.split('\n')[0],
128
'bzr: ERROR: bzrlib.errors.BzrCommandError: shelve only ' \
129
'accepts a single revision parameter.')
130
'bzr: ERROR: shelve only accepts a single revision parameter.')
132
def test_shelf_show_basic(self):
133
self.tree = self.make_branch_and_tree('.')
134
self.__create_and_add_test_file()
135
self.__test_show(self.tree, '00')
137
def __test_show(self, tree, patch):
138
# Modify the test file
139
self.build_tree_contents([('test_file', 'patch %s\n' % patch)])
142
self.run_bzr('shelve', '--all', retcode=0)
144
# Make sure there is no diff anymore
145
self.assertEqual(self.capture('diff', retcode=0), '')
147
# Check the shelf is right
148
shelf = open(os.path.join(self.tree.branch.base,
149
'.shelf/shelves/default', patch)).read()
150
self.assertTrue('patch %s' % patch in shelf)
152
# Check the shown output is right
153
shown = self.capture('shelf show %s' % patch, retcode=0)
154
self.assertEqual(shown, shelf)
156
def test_shelf_show_multi(self):
157
self.tree = self.make_branch_and_tree('.')
158
self.__create_and_add_test_file()
159
self.__test_show(self.tree, '00')
160
self.__test_show(self.tree, '01')
161
self.__test_show(self.tree, '02')
163
# Now check we can show a previously shelved patch
164
shelf = open(os.path.join(self.tree.branch.base,
165
'.shelf/shelves/default/00')).read()
166
self.assertTrue('patch 00' in shelf)
168
# Check the shown output is right
169
shown = self.capture('shelf show 00', retcode=0)
170
self.assertEqual(shown, shelf)
172
def test_shelf_show_unspecified(self):
173
self.tree = self.make_branch_and_tree('.')
174
self.__create_and_add_test_file()
175
self.__test_show(self.tree, '00')
176
self.__test_show(self.tree, '01')
177
self.__test_show(self.tree, '02')
179
# Check that not specifying at patch gets us the most recent
180
shelf = open(os.path.join(self.tree.branch.base,
181
'.shelf/shelves/default/02')).read()
182
self.assertTrue('patch 02' in shelf)
184
# Check the shown output is right
185
shown = self.capture('shelf show', retcode=0)
186
self.assertEqual(shown, shelf)
188
def test_shelf_show_with_no_patch(self):
189
self.tree = self.make_branch_and_tree('.')
190
stderr = self.run_bzr_captured(['shelf', 'show', '00'], retcode=None)[1]
191
self.assertTrue("Patch '00' doesn't exist on shelf default!" in stderr)
193
def test_shelf_unshelve_failure(self):
194
self.tree = self.make_branch_and_tree('.')
196
self.__create_and_add_test_file()
198
# Modify the test file
199
file('test_file', 'w').write(self.MODIFIED)
202
self.run_bzr('shelve', '--all', retcode=0)
204
# Write an unapplyable patch into the shelf
205
shelf = open(os.path.join(self.tree.branch.base,
206
'.shelf/shelves/default/00'), 'w')
207
shelf.write(self.DIFF_2)
210
# Unshelve, should fail
211
self.run_bzr('unshelve', '--all', retcode=3)
213
# Make sure the patch is still there, eventhough it's broken
214
shelf = open(os.path.join(self.tree.branch.base,
215
'.shelf/shelves/default/00')).read()
216
self.assertEqual(shelf, self.DIFF_2)
218
# Working tree should be unchanged
219
diff = self.capture('diff', retcode=0)
220
self.assertEqual(diff, '')
222
def test_shelf_unshelve_failure_two_hunks(self):
223
self.tree = self.make_branch_and_tree('.')
225
self.__create_and_add_test_file()
226
self.__create_and_add_test_file(filename='test_file2')
228
# Modify the test files
229
file('test_file', 'w').write(self.MODIFIED)
230
file('test_file2', 'w').write(self.MODIFIED)
233
self.run_bzr('shelve', '--all', retcode=0)
235
# Put the changes to test_file back, the shelved patch won't apply now
236
file('test_file', 'w').write(self.MODIFIED)
237
self.tree.commit(message='screw up test_file')
239
# Unshelve, should fail
240
self.run_bzr('unshelve', '--all', retcode=3)
242
# Working tree should be unchanged
243
diff = self.capture('diff', retcode=0)
244
self.assertEqual(diff, '')
246
# Force should succeed and modify test_file2, but leave shelf
247
self.run_bzr('unshelve', '--force', '--all', retcode=0)
248
self.assertEqual(open('test_file2').read(), self.MODIFIED)
249
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
251
def test_shelf_after_unshelve(self):
252
self.tree = self.make_branch_and_tree('.')
254
self.__create_and_add_test_file()
255
self.__create_and_add_test_file(filename='test_file2')
257
# Modify the test files
258
file('test_file', 'w').write(self.MODIFIED)
259
file('test_file2', 'w').write(self.MODIFIED)
262
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
263
self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
266
self.run_bzr('unshelve', '--all', retcode=0)
268
# We should now have 00 and 01~
269
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
270
self.assertTrue(os.path.exists('.shelf/shelves/default/01~'))
273
lines = self.capture('shelf ls', retcode=0).split('\n')
275
self.assertFalse(line.startswith(' 01'))
277
# Unshelve, if unshelve is confused by the backup it will fail
278
self.run_bzr('unshelve', '--all', retcode=0)
280
def test_shelf_delete(self):
281
self.tree = self.make_branch_and_tree('.')
283
self.__create_and_add_test_file()
284
self.__create_and_add_test_file(filename='test_file2')
286
# Modify the test files
287
file('test_file', 'w').write(self.MODIFIED)
288
file('test_file2', 'w').write(self.MODIFIED)
291
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
292
self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
294
self._check_shelf('00')
297
self.run_bzr('shelf', 'delete', '00', retcode=0)
299
# We should now have 01 but not 00, but we should have 00~
300
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
301
self.assertTrue(os.path.exists('.shelf/shelves/default/00~'))
302
self.assertTrue(os.path.exists('.shelf/shelves/default/01'))
304
# Check the backup is right
305
self._check_shelf('00~')
308
lines = self.capture('shelf ls', retcode=0).split('\n')
310
self.assertFalse(line.startswith(' 00'))
312
# Unshelve should unshelve 01
313
self.run_bzr('unshelve', '--all', retcode=0)
314
self.assertEqual(file('test_file2').read(), self.MODIFIED)
316
def test_shelf_gaps(self):
317
self.tree = self.make_branch_and_tree('.')
318
self.__create_and_add_test_file()
319
file('test_file', 'w').write(self.MODIFIED)
320
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
321
file('test_file', 'w').write(self.MODIFIED)
322
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
324
# Now delete 00, leaving 01, next shelve should go into 02
325
self.run_bzr('shelf', 'delete', '0', retcode=0)
326
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
327
self.assertFalse(os.path.exists('.shelf/shelves/default/02'))
328
file('test_file', 'w').write(self.MODIFIED)
329
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
330
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
331
self.assertTrue(os.path.exists('.shelf/shelves/default/02'))
333
def test_shelf_upgrade(self):
334
self.tree = self.make_branch_and_tree('.')
336
self.__create_and_add_test_file()
338
# Modify then shelve, so we're not upgrading to 00, just for kicks
339
file('test_file', 'w').write(self.MODIFIED)
340
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
342
open('.bzr-shelf', 'w').write('First old shelf')
343
open('.bzr-shelf-1', 'w').write('Second old shelf')
344
open('.bzr-shelf-3', 'w').write('Fourth old shelf')
346
# shelve and unshelve should bitch and do nothing
347
file('test_file', 'w').write('blah blah blah')
348
self.run_bzr('shelve', '--all', retcode=3)
349
self.assertFalse(os.path.exists('.shelf/shelves/default/01'))
350
self.assertEqual(file('test_file').read(), 'blah blah blah')
351
self.run_bzr('unshelve', '--all', retcode=3)
352
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
354
# Upgrade, make sure it worked
355
self.run_bzr('shelf', 'upgrade', retcode=0)
356
self.assertEqual(open('.shelf/shelves/default/01').read(),
358
self.assertEqual(open('.shelf/shelves/default/02').read(),
360
self.assertEqual(open('.shelf/shelves/default/03').read(),
363
# Check the old shelves got backed up right
364
self.assertTrue(os.path.exists('.bzr-shelf~'))
365
self.assertTrue(os.path.exists('.bzr-shelf-1~'))
366
self.assertTrue(os.path.exists('.bzr-shelf-3~'))
367
self.assertFalse(os.path.exists('.bzr-shelf'))
368
self.assertFalse(os.path.exists('.bzr-shelf-1'))
369
self.assertFalse(os.path.exists('.bzr-shelf-3'))
371
# Shelve should work now
372
self.run_bzr('shelve', '--all', retcode=0)
374
def test_shelf_p1_patch(self):
375
self.tree = self.make_branch_and_tree('.')
377
self.__create_and_add_test_file()
379
# Run a benign shelf command to setup .shelf for us
380
self.run_bzr('shelf', 'ls', retcode=0)
382
# Fake a -p0 shelved patch
383
diff = self.DIFF_1 % { 'filename' : 'test_file' }
384
diff = diff.replace('--- ', '--- a/')
385
diff = diff.replace('+++ ', '+++ b/')
386
open('.shelf/shelves/default/00', 'w').write(diff)
389
self.run_bzr('unshelve', '--all', retcode=0)
393
def test_shelf_shelve_in_subdir(self):
394
self.tree = self.make_branch_and_tree('.')
398
self.tree.add(subdir)
401
self.__create_and_add_test_file()
403
# Modify the test file
404
file('test_file', 'w').write(self.MODIFIED)
407
self.run_bzr('shelve', '--all', retcode=0)
409
# Working tree should be unchanged
410
diff = self.capture('diff', retcode=0)
411
self.assertEqual(diff, '')
413
# Unshelve, should succeed
414
self.run_bzr('unshelve', '--all', retcode=0)
416
self._check_diff(filename='subdir/test_file')
418
# Make sure relative filenames work ok
419
self.run_bzr('shelve', 'test_file', '--all', retcode=0)
421
def test_shelf_shelf_bogus_subcommand(self):
422
self.tree = self.make_branch_and_tree('.')
423
self.run_bzr('shelf', 'foo', retcode=3) # <- retcode == 3
425
def test_shelf_OOO_unshelve(self):
426
self.tree = self.make_branch_and_tree('.')
428
for i in range(1, 5):
429
self.__create_and_add_test_file(filename='test_file%d' % i)
431
# Modify the test files
432
for i in range(1, 5):
433
file('test_file%d' % i, 'w').write(self.MODIFIED)
436
for i in range(1, 5):
437
self.run_bzr('shelve', '--all', 'test_file%d' % i, retcode=0)
439
# Check shelving worked
440
for i in range(1, 5):
441
self.assertEqual(file('test_file%d' % i).read(), self.ORIGINAL)
443
# We should now have 00-03
444
for i in range(0, 4):
445
self.assertTrue(os.path.exists('.shelf/shelves/default/0%d' % i))
448
self.run_bzr('unshelve', '--all', '00', retcode=0)
449
self.assertEqual(file('test_file1').read(), self.MODIFIED)
452
lines = self.capture('shelf ls', retcode=0).split('\n')
454
self.assertFalse(line.startswith(' 00'))
456
# Check we can reshelve once we've unshelved out of order, should be 04
457
self.assertFalse(os.path.exists('.shelf/shelves/default/04'))
458
self.run_bzr('shelve', '--all')
459
self.assertTrue(os.path.exists('.shelf/shelves/default/04'))
462
text = self.capture('shelf ls', retcode=0)
463
for line in text.split('\n'):
464
self.assertFalse(line.startswith(' 00'))
466
# We now have 01,02,03,04
468
self.run_bzr('unshelve', '--all', '02', retcode=0)
469
self.assertEqual(file('test_file3').read(), self.MODIFIED)
471
# Unshelve the default, this is the reshelved 00, hence modifies file 1
472
self.run_bzr('unshelve', '--all', retcode=0)
473
self.assertEqual(file('test_file1').read(), self.MODIFIED)
475
def test_shelf_switch_basic(self):
476
self.tree = self.make_branch_and_tree('.')
477
self.__create_and_add_test_file()
479
# This should go to "default"
480
file('test_file', 'w').write(self.MODIFIED)
481
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
484
self.run_bzr('shelf', 'switch', 'other', retcode=0)
485
file('test_file', 'w').write(self.MODIFIED)
486
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
489
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
490
self.assertFalse(os.path.exists('.shelf/shelves/default/01'))
491
self.assertTrue(os.path.exists('.shelf/shelves/other/00'))
494
self.run_bzr('shelf', 'switch', 'default', retcode=0)
495
file('test_file', 'w').write(self.MODIFIED)
496
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
499
self.assertTrue(os.path.exists('.shelf/shelves/default/01'))
500
self.assertFalse(os.path.exists('.shelf/shelves/other/01'))