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