7
7
UnshelveHunkSelector,
9
9
from bzrlib.plugins.bzrtools.errors import NoColor
10
from bzrlib.plugins.bzrtools import cmd_shelf
10
from bzrlib.plugins.bzrtools import command
11
from bzrlib.plugins.bzrtools.command_classes import cmd_shelf1
13
14
class ShelfTests(bzrlib.tests.TestCaseWithTransport):
37
bzrlib.tests.TestCaseWithTransport.setUp(self)
38
command._testing = True
39
self.addCleanup(command._stop_testing)
35
41
def _check_diff(self, diff=DIFF_1, filename='test_file'):
36
42
old_tree = self.tree.basis_tree()
37
43
old_tree.lock_read()
48
keys = { 'filename' : filename , 'old_date': old_date,
54
keys = { 'filename' : filename , 'old_date': old_date,
49
55
'new_date': new_date}
50
56
hdr = self.DIFF_HEADER % keys
52
self.assertEqual(self.capture('diff', retcode=1), hdr + diff + '\n')
58
self.assertEqual(self.run_bzr('diff', retcode=1)[0], hdr + diff + '\n')
54
60
def _check_shelf(self, idx, diff=DIFF_1, filename='test_file',
95
101
self.tree.unlock()
97
103
# Shelve the changes
98
self.run_bzr('shelve', '--all', retcode=0)
104
self.run_bzr('shelve1 --all', retcode=0)
100
106
# Make sure there is no diff anymore
101
self.assertEqual(self.capture('diff', retcode=0), '')
107
self.assertEqual(self.run_bzr('diff', retcode=0)[0], '')
103
109
# Make sure the file is actually back the way it was
104
110
self.assertEqual(file('test_file').read(), self.ORIGINAL)
106
112
self._check_shelf('00', new_date=new_date)
109
self.run_bzr('unshelve', '--all', retcode=0)
115
self.run_bzr('unshelve1 --all', retcode=0)
111
117
self._check_diff()
122
128
self.__create_and_add_test_file()
124
130
# Shelve the changes
125
self.run_bzr('shelve', '--all', retcode=3)
131
self.run_bzr('shelve1 --all', retcode=3)
127
133
if os.path.exists(os.path.join(self.tree.branch.base,
128
134
'.shelf/shelves/default/00')):
146
152
self.tree.commit(message='update test_file')
148
154
# Shelve the changes
149
self.run_bzr('shelve', '--all', '-r', '1', retcode=0)
155
self.run_bzr('shelve1 --all -r 1', retcode=0)
151
157
self._check_diff(self.DIFF_2)
154
160
self.assertEqual(file('test_file').read(), self.ORIGINAL)
157
self.run_bzr('unshelve', '--all', retcode=0)
163
self.run_bzr('unshelve1 --all', retcode=0)
159
165
# Make sure the file is back the way it should be
160
166
self.assertEqual(file('test_file').read(), self.MODIFIED)
162
168
def test_shelf_with_two_revisions(self):
163
169
self.tree = self.make_branch_and_tree('.')
165
cmd = 'shelve --all -r 1..2'
166
(stdout, stderr) = self.run_bzr_captured(cmd.split(), retcode=None)
171
stdout, stderr = self.run_bzr('shelve1 --all -r 1..2', retcode=None)
168
173
self.assertEqual(stderr.split('\n')[0],
169
174
'bzr: ERROR: shelve only accepts a single revision parameter.')
178
183
self.build_tree_contents([('test_file', 'patch %s\n' % patch)])
180
185
# Shelve the changes
181
self.run_bzr('shelve', '--all', retcode=0)
186
self.run_bzr('shelve1 --all', retcode=0)
183
188
# Make sure there is no diff anymore
184
self.assertEqual(self.capture('diff', retcode=0), '')
189
self.assertEqual(self.run_bzr('diff', retcode=0)[0], '')
186
191
# Check the shelf is right
187
192
shelf = open(os.path.join(self.tree.basedir,
189
194
self.assertTrue('patch %s' % patch in shelf)
191
196
# Check the shown output is right
192
shown = self.capture('shelf show %s' % patch, retcode=0)
197
shown = self.run_bzr('shelf1 show %s' % patch, retcode=0)[0]
193
198
self.assertEqual(shown, shelf)
195
200
def test_shelf_show_multi(self):
205
210
self.assertTrue('patch 00' in shelf)
207
212
# Check the shown output is right
208
shown = self.capture('shelf show 00', retcode=0)
213
shown = self.run_bzr('shelf1 show 00', retcode=0)[0]
209
214
self.assertEqual(shown, shelf)
211
216
def test_shelf_show_unspecified(self):
221
226
self.assertTrue('patch 02' in shelf)
223
228
# Check the shown output is right
224
shown = self.capture('shelf show', retcode=0)
229
shown = self.run_bzr('shelf1 show', retcode=0)[0]
225
230
self.assertEqual(shown, shelf)
227
232
def test_shelf_show_with_no_patch(self):
228
233
self.tree = self.make_branch_and_tree('.')
229
stderr = self.run_bzr_captured(['shelf', 'show', '00'], retcode=None)[1]
234
stderr = self.run_bzr('shelf1 show 00', retcode=None)[1]
230
235
self.assertTrue("Patch '00' doesn't exist on shelf default!" in stderr)
232
237
def test_shelf_unshelve_failure(self):
238
243
file('test_file', 'w').write(self.MODIFIED)
240
245
# Shelve the changes
241
self.run_bzr('shelve', '--all', retcode=0)
246
self.run_bzr('shelve1 --all', retcode=0)
243
248
# Write an unapplyable patch into the shelf
244
249
shelf = open(os.path.join(self.tree.basedir,
249
254
# Unshelve, should fail
250
self.run_bzr('unshelve', '--all', retcode=3)
255
self.run_bzr('unshelve1 --all', retcode=3)
252
257
# Make sure the patch is still there, eventhough it's broken
253
258
shelf = open(os.path.join(self.tree.basedir,
255
260
self.assertEqual(shelf, self.DIFF_2)
257
262
# Working tree should be unchanged
258
diff = self.capture('diff', retcode=0)
263
diff = self.run_bzr('diff', retcode=0)[0]
259
264
self.assertEqual(diff, '')
261
266
def test_shelf_unshelve_failure_two_hunks(self):
269
274
file('test_file2', 'w').write(self.MODIFIED)
271
276
# Shelve the changes
272
self.run_bzr('shelve', '--all', retcode=0)
277
self.run_bzr('shelve1 --all', retcode=0)
274
279
# Put the changes to test_file back, the shelved patch won't apply now
275
280
file('test_file', 'w').write(self.MODIFIED)
276
281
self.tree.commit(message='screw up test_file')
278
283
# Unshelve, should fail
279
self.run_bzr('unshelve', '--all', retcode=3)
284
self.run_bzr('unshelve1 --all', retcode=3)
281
286
# Working tree should be unchanged
282
diff = self.capture('diff', retcode=0)
287
diff = self.run_bzr('diff', retcode=0)[0]
283
288
self.assertEqual(diff, '')
285
290
# Force should succeed and modify test_file2, but leave shelf
286
self.run_bzr('unshelve', '--force', '--all', retcode=0)
291
self.run_bzr('unshelve1 --force --all', retcode=0)
287
292
self.assertEqual(open('test_file2').read(), self.MODIFIED)
288
293
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
298
303
file('test_file2', 'w').write(self.MODIFIED)
300
305
# Shelve the changes
301
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
302
self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
306
self.run_bzr('shelve1 --all test_file', retcode=0)
307
self.run_bzr('shelve1 --all test_file2', retcode=0)
305
self.run_bzr('unshelve', '--all', retcode=0)
310
self.run_bzr('unshelve1 --all', retcode=0)
307
312
# We should now have 00 and 01~
308
313
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
309
314
self.assertTrue(os.path.exists('.shelf/shelves/default/01~'))
312
lines = self.capture('shelf ls', retcode=0).split('\n')
317
lines = self.run_bzr('shelf1 ls', retcode=0)[0].split('\n')
313
318
for line in lines:
314
319
self.assertFalse(line.startswith(' 01'))
316
321
# Unshelve, if unshelve is confused by the backup it will fail
317
self.run_bzr('unshelve', '--all', retcode=0)
322
self.run_bzr('unshelve1 --all', retcode=0)
319
324
def test_shelf_delete(self):
320
325
self.tree = self.make_branch_and_tree('.')
337
342
self.tree.unlock()
339
344
# Shelve the changes
340
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
341
self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
345
self.run_bzr('shelve1 --all test_file', retcode=0)
346
self.run_bzr('shelve1 --all test_file2', retcode=0)
343
348
self._check_shelf('00', new_date=new_date)
346
self.run_bzr('shelf', 'delete', '00', retcode=0)
351
self.run_bzr('shelf1 delete 00', retcode=0)
348
353
# We should now have 01 but not 00, but we should have 00~
349
354
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
354
359
self._check_shelf('00~', new_date=new_date)
357
lines = self.capture('shelf ls', retcode=0).split('\n')
362
lines = self.run_bzr('shelf1 ls', retcode=0)[0].split('\n')
358
363
for line in lines:
359
364
self.assertFalse(line.startswith(' 00'))
361
366
# Unshelve should unshelve 01
362
self.run_bzr('unshelve', '--all', retcode=0)
367
self.run_bzr('unshelve1 --all', retcode=0)
363
368
self.assertEqual(file('test_file2').read(), self.MODIFIED)
365
370
def test_shelf_gaps(self):
366
371
self.tree = self.make_branch_and_tree('.')
367
372
self.__create_and_add_test_file()
368
373
file('test_file', 'w').write(self.MODIFIED)
369
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
374
self.run_bzr('shelve1 --all test_file', retcode=0)
370
375
file('test_file', 'w').write(self.MODIFIED)
371
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
376
self.run_bzr('shelve1 --all test_file', retcode=0)
373
378
# Now delete 00, leaving 01, next shelve should go into 02
374
self.run_bzr('shelf', 'delete', '0', retcode=0)
379
self.run_bzr('shelf1 delete 0', retcode=0)
375
380
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
376
381
self.assertFalse(os.path.exists('.shelf/shelves/default/02'))
377
382
file('test_file', 'w').write(self.MODIFIED)
378
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
383
self.run_bzr('shelve1 --all test_file', retcode=0)
379
384
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
380
385
self.assertTrue(os.path.exists('.shelf/shelves/default/02'))
387
392
# Modify then shelve, so we're not upgrading to 00, just for kicks
388
393
file('test_file', 'w').write(self.MODIFIED)
389
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
394
self.run_bzr('shelve1 --all test_file', retcode=0)
391
396
open('.bzr-shelf', 'w').write('First old shelf')
392
397
open('.bzr-shelf-1', 'w').write('Second old shelf')
395
400
# shelve and unshelve should bitch and do nothing
396
401
file('test_file', 'w').write('blah blah blah')
397
self.run_bzr('shelve', '--all', retcode=3)
402
self.run_bzr('shelve1 --all', retcode=3)
398
403
self.assertFalse(os.path.exists('.shelf/shelves/default/01'))
399
404
self.assertEqual(file('test_file').read(), 'blah blah blah')
400
self.run_bzr('unshelve', '--all', retcode=3)
405
self.run_bzr('unshelve1 --all', retcode=3)
401
406
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
403
408
# Upgrade, make sure it worked
404
self.run_bzr('shelf', 'upgrade', retcode=0)
409
self.run_bzr('shelf1 upgrade', retcode=0)
405
410
self.assertEqual(open('.shelf/shelves/default/01').read(),
406
411
'First old shelf')
407
412
self.assertEqual(open('.shelf/shelves/default/02').read(),
418
423
self.assertFalse(os.path.exists('.bzr-shelf-3'))
420
425
# Shelve should work now
421
self.run_bzr('shelve', '--all', retcode=0)
426
self.run_bzr('shelve1 --all', retcode=0)
423
428
def test_shelf_p1_patch(self):
424
429
self.tree = self.make_branch_and_tree('.')
426
431
self.__create_and_add_test_file()
428
433
# Run a benign shelf command to setup .shelf for us
429
self.run_bzr('shelf', 'ls', retcode=0)
434
self.run_bzr('shelf1 ls', retcode=0)
431
436
old_tree = self.tree.basis_tree()
432
437
old_tree.lock_read()
448
453
open('.shelf/shelves/default/00', 'w').write(diff)
450
455
# This should work
451
self.run_bzr('unshelve', '--all', retcode=0)
456
self.run_bzr('unshelve1 --all', retcode=0)
453
458
self._check_diff()
471
476
# Shelve the changes
472
self.run_bzr('shelve', '--all', retcode=0)
477
self.run_bzr('shelve1 --all', retcode=0)
474
479
# Working tree should be unchanged
475
diff = self.capture('diff', retcode=0)
480
diff = self.run_bzr('diff', retcode=0)[0]
476
481
self.assertEqual(diff, '')
478
483
# Unshelve, should succeed
479
self.run_bzr('unshelve', '--all', retcode=0)
484
self.run_bzr('unshelve1 --all', retcode=0)
481
486
self._check_diff(filename='subdir/test_file')
483
488
# Make sure relative filenames work ok
484
self.run_bzr('shelve', 'test_file', '--all', retcode=0)
489
self.run_bzr('shelve1 test_file --all', retcode=0)
486
491
def test_shelf_shelf_bogus_subcommand(self):
487
492
self.tree = self.make_branch_and_tree('.')
488
self.run_bzr('shelf', 'foo', retcode=3) # <- retcode == 3
493
self.run_bzr('shelf1 foo', retcode=3) # <- retcode == 3
490
495
def test_shelf_OOO_unshelve(self):
491
496
self.tree = self.make_branch_and_tree('.')
500
505
# Shelve the changes
501
506
for i in range(1, 5):
502
self.run_bzr('shelve', '--all', 'test_file%d' % i, retcode=0)
507
self.run_bzr(['shelve1', '--all', 'test_file%d' % i], retcode=0)
504
509
# Check shelving worked
505
510
for i in range(1, 5):
510
515
self.assertTrue(os.path.exists('.shelf/shelves/default/0%d' % i))
513
self.run_bzr('unshelve', '--all', '00', retcode=0)
518
self.run_bzr('unshelve1 --all 00', retcode=0)
514
519
self.assertEqual(file('test_file1').read(), self.MODIFIED)
517
lines = self.capture('shelf ls', retcode=0).split('\n')
522
lines = self.run_bzr('shelf1 ls', retcode=0)[0].split('\n')
518
523
for line in lines:
519
524
self.assertFalse(line.startswith(' 00'))
521
526
# Check we can reshelve once we've unshelved out of order, should be 04
522
527
self.assertFalse(os.path.exists('.shelf/shelves/default/04'))
523
self.run_bzr('shelve', '--all')
528
self.run_bzr('shelve1 --all')
524
529
self.assertTrue(os.path.exists('.shelf/shelves/default/04'))
527
text = self.capture('shelf ls', retcode=0)
532
text = self.run_bzr('shelf1 ls', retcode=0)[0]
528
533
for line in text.split('\n'):
529
534
self.assertFalse(line.startswith(' 00'))
531
536
# We now have 01,02,03,04
533
self.run_bzr('unshelve', '--all', '02', retcode=0)
538
self.run_bzr('unshelve1 --all 02', retcode=0)
534
539
self.assertEqual(file('test_file3').read(), self.MODIFIED)
536
541
# Unshelve the default, this is the reshelved 00, hence modifies file 1
537
self.run_bzr('unshelve', '--all', retcode=0)
542
self.run_bzr('unshelve1 --all', retcode=0)
538
543
self.assertEqual(file('test_file1').read(), self.MODIFIED)
540
545
def test_shelf_switch_basic(self):
544
549
# This should go to "default"
545
550
file('test_file', 'w').write(self.MODIFIED)
546
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
551
self.run_bzr('shelve1 --all test_file', retcode=0)
548
553
# Switch to "other"
549
self.run_bzr('shelf', 'switch', 'other', retcode=0)
554
self.run_bzr('shelf1 switch other', retcode=0)
550
555
file('test_file', 'w').write(self.MODIFIED)
551
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
556
self.run_bzr('shelve1 --all test_file', retcode=0)
553
558
# Check it worked
554
559
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
556
561
self.assertTrue(os.path.exists('.shelf/shelves/other/00'))
559
self.run_bzr('shelf', 'switch', 'default', retcode=0)
564
self.run_bzr('shelf1 switch default', retcode=0)
560
565
file('test_file', 'w').write(self.MODIFIED)
561
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
566
self.run_bzr('shelve1 --all test_file', retcode=0)
563
568
# Check that worked
564
569
self.assertTrue(os.path.exists('.shelf/shelves/default/01'))
565
570
self.assertFalse(os.path.exists('.shelf/shelves/other/01'))
572
def test_shelf_binary(self):
573
self.tree = self.make_branch_and_tree('.')
574
self.build_tree_contents([('file', '\x00')])
575
self.tree.add('file')
576
self.run_bzr_error(['Changes involve binary files.'], 'shelve1 --all')
567
578
def test_shelf_bad_patch_arg(self):
568
579
self.tree = self.make_branch_and_tree('.')
570
581
# Check the bad arg handling
571
stdout, error = self.run_bzr_captured(['unshelve', '01'], retcode=3)
582
stdout, error = self.run_bzr('unshelve1 01', retcode=3)
572
583
self.assertTrue("Patch '01' doesn't exist on shelf" in error)
574
stdout, error = self.run_bzr_captured(['unshelve', 'foo'], retcode=3)
585
stdout, error = self.run_bzr('unshelve1 foo', retcode=3)
575
586
self.assertTrue("Invalid patch name 'foo'" in error)
577
588
# Hex and is cracky, so it shouldn't work
578
stdout, error = self.run_bzr_captured(['unshelve', '0x00'], retcode=3)
589
stdout, error = self.run_bzr(['unshelve1', '0x00'], retcode=3)
579
590
self.assertTrue("Invalid patch name '0x00'" in error)
581
592
def test_color_hunk_selector(self):
613
624
f = file('test_file', 'wb')
614
625
f.write(self.MODIFIED)
616
stdout, error = self.run_bzr_captured(['shelve', '--all',
618
stdout, error = self.run_bzr_captured(['unshelve', '--all',
627
stdout, error = self.run_bzr('shelve1 --all --no-color')
628
stdout, error = self.run_bzr('unshelve1 --all --no-color')
621
630
def test_shelf_help(self):
622
self.assertContainsRe(cmd_shelf().help(),
631
self.assertContainsRe(cmd_shelf1().help(),
623
632
'list\n.*List the patches on the current shelf')
634
def test_show_empty_shelf(self):
635
self.tree = self.make_branch_and_tree('.')
636
self.run_bzr_error(('No patches on shelf.',), 'shelf1 show')