1
2
from bzrlib.diff import _patch_header_date
6
from bzrlib.plugins.bzrtools.hunk_selector import (
10
from bzrlib.plugins.bzrtools.errors import NoColor
12
from bzrtools.hunk_selector import (
16
from bzrtools.errors import NoColor
5
from bzrlib.plugins.bzrtools.hunk_selector import (
9
from bzrlib.plugins.bzrtools.errors import NoColor
10
from bzrlib.plugins.bzrtools import cmd_shelf
19
13
class ShelfTests(bzrlib.tests.TestCaseWithTransport):
41
35
def _check_diff(self, diff=DIFF_1, filename='test_file'):
42
36
old_tree = self.tree.basis_tree()
43
old_date = _patch_header_date(old_tree,
44
old_tree.inventory.path2id(filename),
46
new_date = _patch_header_date(self.tree,
47
self.tree.inventory.path2id(filename),
49
keys = { 'filename' : filename , 'old_date': old_date,
40
old_date = _patch_header_date(old_tree,
41
old_tree.inventory.path2id(filename),
43
new_date = _patch_header_date(self.tree,
44
self.tree.inventory.path2id(filename), filename)
48
keys = { 'filename' : filename , 'old_date': old_date,
50
49
'new_date': new_date}
51
50
hdr = self.DIFF_HEADER % keys
53
self.assertEqual(self.capture('diff', retcode=1), hdr + diff + '\n')
52
self.assertEqual(self.run_bzr('diff', retcode=1)[0], hdr + diff + '\n')
55
54
def _check_shelf(self, idx, diff=DIFF_1, filename='test_file',
57
56
old_tree = self.tree.basis_tree()
58
old_date = _patch_header_date(old_tree,
59
old_tree.inventory.path2id(filename),
59
old_date = _patch_header_date(old_tree,
60
old_tree.inventory.path2id(filename),
61
64
diff = diff % { 'filename' : filename, 'old_date': old_date,
62
65
'new_date': new_date}
63
66
shelf = open(os.path.join(self.tree.basedir,
74
77
def __test_loop(self, count):
75
78
self.tree = self.make_branch_and_tree('.')
76
79
self.__create_and_add_test_file()
80
for counter in range(count):
81
81
# Modify the test file
82
# write in binary mode because on win32 line-endings should be LF
82
# write in binary mode because on win32 line-endings should be
83
84
f = file('test_file', 'wb')
84
85
f.write(self.MODIFIED)
89
new_date = _patch_header_date(self.tree,
90
self.tree.inventory.path2id('test_file'), 'test_file')
90
self.tree.lock_write()
92
new_date = _patch_header_date(self.tree,
93
self.tree.inventory.path2id('test_file'), 'test_file')
92
97
# Shelve the changes
93
self.run_bzr('shelve', '--all', retcode=0)
98
self.run_bzr('shelve --all', retcode=0)
95
100
# Make sure there is no diff anymore
96
self.assertEqual(self.capture('diff', retcode=0), '')
101
self.assertEqual(self.run_bzr('diff', retcode=0)[0], '')
98
103
# Make sure the file is actually back the way it was
99
104
self.assertEqual(file('test_file').read(), self.ORIGINAL)
157
162
def test_shelf_with_two_revisions(self):
158
163
self.tree = self.make_branch_and_tree('.')
160
cmd = 'shelve --all -r 1..2'
161
(stdout, stderr) = self.run_bzr_captured(cmd.split(), retcode=None)
165
stdout, stderr = self.run_bzr('shelve --all -r 1..2', retcode=None)
163
167
self.assertEqual(stderr.split('\n')[0],
164
168
'bzr: ERROR: shelve only accepts a single revision parameter.')
173
177
self.build_tree_contents([('test_file', 'patch %s\n' % patch)])
175
179
# Shelve the changes
176
self.run_bzr('shelve', '--all', retcode=0)
180
self.run_bzr('shelve --all', retcode=0)
178
182
# Make sure there is no diff anymore
179
self.assertEqual(self.capture('diff', retcode=0), '')
183
self.assertEqual(self.run_bzr('diff', retcode=0)[0], '')
181
185
# Check the shelf is right
182
186
shelf = open(os.path.join(self.tree.basedir,
216
220
self.assertTrue('patch 02' in shelf)
218
222
# Check the shown output is right
219
shown = self.capture('shelf show', retcode=0)
223
shown = self.run_bzr('shelf show', retcode=0)[0]
220
224
self.assertEqual(shown, shelf)
222
226
def test_shelf_show_with_no_patch(self):
223
227
self.tree = self.make_branch_and_tree('.')
224
stderr = self.run_bzr_captured(['shelf', 'show', '00'], retcode=None)[1]
228
stderr = self.run_bzr('shelf show 00', retcode=None)[1]
225
229
self.assertTrue("Patch '00' doesn't exist on shelf default!" in stderr)
227
231
def test_shelf_unshelve_failure(self):
264
268
file('test_file2', 'w').write(self.MODIFIED)
266
270
# Shelve the changes
267
self.run_bzr('shelve', '--all', retcode=0)
271
self.run_bzr('shelve --all', retcode=0)
269
273
# Put the changes to test_file back, the shelved patch won't apply now
270
274
file('test_file', 'w').write(self.MODIFIED)
271
275
self.tree.commit(message='screw up test_file')
273
277
# Unshelve, should fail
274
self.run_bzr('unshelve', '--all', retcode=3)
278
self.run_bzr('unshelve --all', retcode=3)
276
280
# Working tree should be unchanged
277
diff = self.capture('diff', retcode=0)
281
diff = self.run_bzr('diff', retcode=0)[0]
278
282
self.assertEqual(diff, '')
280
284
# Force should succeed and modify test_file2, but leave shelf
281
self.run_bzr('unshelve', '--force', '--all', retcode=0)
285
self.run_bzr('unshelve --force --all', retcode=0)
282
286
self.assertEqual(open('test_file2').read(), self.MODIFIED)
283
287
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
293
297
file('test_file2', 'w').write(self.MODIFIED)
295
299
# Shelve the changes
296
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
297
self.run_bzr('shelve', '--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)
300
self.run_bzr('unshelve', '--all', retcode=0)
304
self.run_bzr('unshelve --all', retcode=0)
302
306
# We should now have 00 and 01~
303
307
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
304
308
self.assertTrue(os.path.exists('.shelf/shelves/default/01~'))
307
lines = self.capture('shelf ls', retcode=0).split('\n')
311
lines = self.run_bzr('shelf ls', retcode=0)[0].split('\n')
308
312
for line in lines:
309
313
self.assertFalse(line.startswith(' 01'))
311
315
# Unshelve, if unshelve is confused by the backup it will fail
312
self.run_bzr('unshelve', '--all', retcode=0)
316
self.run_bzr('unshelve --all', retcode=0)
314
318
def test_shelf_delete(self):
315
319
self.tree = self.make_branch_and_tree('.')
317
self.__create_and_add_test_file()
318
self.__create_and_add_test_file(filename='test_file2')
320
# Modify the test files
321
# write in binary mode because on win32 line-endings should be LF
322
f = file('test_file', 'wb')
323
f.write(self.MODIFIED)
325
f = file('test_file2', 'wb')
326
f.write(self.MODIFIED)
328
new_date = _patch_header_date(self.tree,
329
self.tree.inventory.path2id('test_file'), 'test_file')
320
self.tree.lock_write()
322
self.__create_and_add_test_file()
323
self.__create_and_add_test_file(filename='test_file2')
325
# Modify the test files
326
# write in binary mode because on win32 line-endings should be LF
327
f = file('test_file', 'wb')
328
f.write(self.MODIFIED)
330
f = file('test_file2', 'wb')
331
f.write(self.MODIFIED)
333
new_date = _patch_header_date(self.tree,
334
self.tree.inventory.path2id('test_file'), 'test_file')
331
338
# Shelve the changes
332
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
333
self.run_bzr('shelve', '--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)
335
342
self._check_shelf('00', new_date=new_date)
338
self.run_bzr('shelf', 'delete', '00', retcode=0)
345
self.run_bzr('shelf delete 00', retcode=0)
340
347
# We should now have 01 but not 00, but we should have 00~
341
348
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
346
353
self._check_shelf('00~', new_date=new_date)
349
lines = self.capture('shelf ls', retcode=0).split('\n')
356
lines = self.run_bzr('shelf ls', retcode=0)[0].split('\n')
350
357
for line in lines:
351
358
self.assertFalse(line.startswith(' 00'))
353
360
# Unshelve should unshelve 01
354
self.run_bzr('unshelve', '--all', retcode=0)
361
self.run_bzr('unshelve --all', retcode=0)
355
362
self.assertEqual(file('test_file2').read(), self.MODIFIED)
357
364
def test_shelf_gaps(self):
358
365
self.tree = self.make_branch_and_tree('.')
359
366
self.__create_and_add_test_file()
360
367
file('test_file', 'w').write(self.MODIFIED)
361
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
368
self.run_bzr('shelve --all test_file', retcode=0)
362
369
file('test_file', 'w').write(self.MODIFIED)
363
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
370
self.run_bzr('shelve --all test_file', retcode=0)
365
372
# Now delete 00, leaving 01, next shelve should go into 02
366
self.run_bzr('shelf', 'delete', '0', retcode=0)
373
self.run_bzr('shelf delete 0', retcode=0)
367
374
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
368
375
self.assertFalse(os.path.exists('.shelf/shelves/default/02'))
369
376
file('test_file', 'w').write(self.MODIFIED)
370
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
377
self.run_bzr('shelve --all test_file', retcode=0)
371
378
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
372
379
self.assertTrue(os.path.exists('.shelf/shelves/default/02'))
379
386
# Modify then shelve, so we're not upgrading to 00, just for kicks
380
387
file('test_file', 'w').write(self.MODIFIED)
381
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
388
self.run_bzr('shelve --all test_file', retcode=0)
383
390
open('.bzr-shelf', 'w').write('First old shelf')
384
391
open('.bzr-shelf-1', 'w').write('Second old shelf')
387
394
# shelve and unshelve should bitch and do nothing
388
395
file('test_file', 'w').write('blah blah blah')
389
self.run_bzr('shelve', '--all', retcode=3)
396
self.run_bzr('shelve --all', retcode=3)
390
397
self.assertFalse(os.path.exists('.shelf/shelves/default/01'))
391
398
self.assertEqual(file('test_file').read(), 'blah blah blah')
392
self.run_bzr('unshelve', '--all', retcode=3)
399
self.run_bzr('unshelve --all', retcode=3)
393
400
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
395
402
# Upgrade, make sure it worked
396
self.run_bzr('shelf', 'upgrade', retcode=0)
403
self.run_bzr('shelf upgrade', retcode=0)
397
404
self.assertEqual(open('.shelf/shelves/default/01').read(),
398
405
'First old shelf')
399
406
self.assertEqual(open('.shelf/shelves/default/02').read(),
418
425
self.__create_and_add_test_file()
420
427
# Run a benign shelf command to setup .shelf for us
421
self.run_bzr('shelf', 'ls', retcode=0)
428
self.run_bzr('shelf ls', retcode=0)
423
430
old_tree = self.tree.basis_tree()
424
old_date = _patch_header_date(old_tree,
425
old_tree.inventory.path2id('test_file'),
427
new_date = _patch_header_date(self.tree,
428
self.tree.inventory.path2id('test_file'),
432
self.tree.lock_read()
434
old_date = _patch_header_date(old_tree,
435
old_tree.inventory.path2id('test_file'),
437
new_date = _patch_header_date(self.tree,
438
self.tree.inventory.path2id('test_file'), 'test_file')
430
442
# Fake a -p0 shelved patch
431
443
diff = self.DIFF_1 % { 'filename' : 'test_file', 'old_date': old_date,
432
444
'new_date' : new_date}
458
470
# Shelve the changes
459
self.run_bzr('shelve', '--all', retcode=0)
471
self.run_bzr('shelve --all', retcode=0)
461
473
# Working tree should be unchanged
462
diff = self.capture('diff', retcode=0)
474
diff = self.run_bzr('diff', retcode=0)[0]
463
475
self.assertEqual(diff, '')
465
477
# Unshelve, should succeed
466
self.run_bzr('unshelve', '--all', retcode=0)
478
self.run_bzr('unshelve --all', retcode=0)
468
480
self._check_diff(filename='subdir/test_file')
470
482
# Make sure relative filenames work ok
471
self.run_bzr('shelve', 'test_file', '--all', retcode=0)
483
self.run_bzr('shelve test_file --all', retcode=0)
473
485
def test_shelf_shelf_bogus_subcommand(self):
474
486
self.tree = self.make_branch_and_tree('.')
475
self.run_bzr('shelf', 'foo', retcode=3) # <- retcode == 3
487
self.run_bzr('shelf foo', retcode=3) # <- retcode == 3
477
489
def test_shelf_OOO_unshelve(self):
478
490
self.tree = self.make_branch_and_tree('.')
497
509
self.assertTrue(os.path.exists('.shelf/shelves/default/0%d' % i))
500
self.run_bzr('unshelve', '--all', '00', retcode=0)
512
self.run_bzr('unshelve --all 00', retcode=0)
501
513
self.assertEqual(file('test_file1').read(), self.MODIFIED)
504
lines = self.capture('shelf ls', retcode=0).split('\n')
516
lines = self.run_bzr('shelf ls', retcode=0)[0].split('\n')
505
517
for line in lines:
506
518
self.assertFalse(line.startswith(' 00'))
508
520
# Check we can reshelve once we've unshelved out of order, should be 04
509
521
self.assertFalse(os.path.exists('.shelf/shelves/default/04'))
510
self.run_bzr('shelve', '--all')
522
self.run_bzr('shelve --all')
511
523
self.assertTrue(os.path.exists('.shelf/shelves/default/04'))
514
text = self.capture('shelf ls', retcode=0)
526
text = self.run_bzr('shelf ls', retcode=0)[0]
515
527
for line in text.split('\n'):
516
528
self.assertFalse(line.startswith(' 00'))
518
530
# We now have 01,02,03,04
520
self.run_bzr('unshelve', '--all', '02', retcode=0)
532
self.run_bzr('unshelve --all 02', retcode=0)
521
533
self.assertEqual(file('test_file3').read(), self.MODIFIED)
523
535
# Unshelve the default, this is the reshelved 00, hence modifies file 1
524
self.run_bzr('unshelve', '--all', retcode=0)
536
self.run_bzr('unshelve --all', retcode=0)
525
537
self.assertEqual(file('test_file1').read(), self.MODIFIED)
527
539
def test_shelf_switch_basic(self):
531
543
# This should go to "default"
532
544
file('test_file', 'w').write(self.MODIFIED)
533
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
545
self.run_bzr('shelve --all test_file', retcode=0)
535
547
# Switch to "other"
536
self.run_bzr('shelf', 'switch', 'other', retcode=0)
548
self.run_bzr('shelf switch other', retcode=0)
537
549
file('test_file', 'w').write(self.MODIFIED)
538
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
550
self.run_bzr('shelve --all test_file', retcode=0)
540
552
# Check it worked
541
553
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
543
555
self.assertTrue(os.path.exists('.shelf/shelves/other/00'))
546
self.run_bzr('shelf', 'switch', 'default', retcode=0)
558
self.run_bzr('shelf switch default', retcode=0)
547
559
file('test_file', 'w').write(self.MODIFIED)
548
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
560
self.run_bzr('shelve --all test_file', retcode=0)
550
562
# Check that worked
551
563
self.assertTrue(os.path.exists('.shelf/shelves/default/01'))
555
567
self.tree = self.make_branch_and_tree('.')
557
569
# Check the bad arg handling
558
stdout, error = self.run_bzr_captured(['unshelve', '01'], retcode=3)
570
stdout, error = self.run_bzr('unshelve 01', retcode=3)
559
571
self.assertTrue("Patch '01' doesn't exist on shelf" in error)
561
stdout, error = self.run_bzr_captured(['unshelve', 'foo'], retcode=3)
573
stdout, error = self.run_bzr('unshelve foo', retcode=3)
562
574
self.assertTrue("Invalid patch name 'foo'" in error)
564
576
# Hex and is cracky, so it shouldn't work
565
stdout, error = self.run_bzr_captured(['unshelve', '0x00'], retcode=3)
577
stdout, error = self.run_bzr(['unshelve', '0x00'], retcode=3)
566
578
self.assertTrue("Invalid patch name '0x00'" in error)
568
580
def test_color_hunk_selector(self):
600
612
f = file('test_file', 'wb')
601
613
f.write(self.MODIFIED)
603
stdout, error = self.run_bzr_captured(['shelve', '--all',
605
stdout, error = self.run_bzr_captured(['unshelve', '--all',
615
stdout, error = self.run_bzr('shelve --all --no-color')
616
stdout, error = self.run_bzr('unshelve --all --no-color')
618
def test_shelf_help(self):
619
self.assertContainsRe(cmd_shelf().help(),
620
'list\n.*List the patches on the current shelf')