43
42
new_date = _patch_header_date(self.tree,
44
43
self.tree.inventory.path2id(filename), filename)
48
keys = { 'filename' : filename , 'old_date': old_date,
46
keys = { 'filename' : filename , 'old_date': old_date,
49
47
'new_date': new_date}
50
48
hdr = self.DIFF_HEADER % keys
52
self.assertEqual(self.run_bzr('diff', retcode=1)[0], hdr + diff + '\n')
50
self.assertEqual(self.capture('diff', retcode=1), hdr + diff + '\n')
54
52
def _check_shelf(self, idx, diff=DIFF_1, filename='test_file',
77
75
def __test_loop(self, count):
78
76
self.tree = self.make_branch_and_tree('.')
79
77
self.__create_and_add_test_file()
80
for counter in range(count):
81
82
# Modify the test file
82
# write in binary mode because on win32 line-endings should be
83
# write in binary mode because on win32 line-endings should be LF
84
84
f = file('test_file', 'wb')
85
85
f.write(self.MODIFIED)
90
self.tree.lock_write()
92
new_date = _patch_header_date(self.tree,
93
self.tree.inventory.path2id('test_file'), 'test_file')
90
new_date = _patch_header_date(self.tree,
91
self.tree.inventory.path2id('test_file'), 'test_file')
97
93
# Shelve the changes
98
self.run_bzr('shelve1 --all', retcode=0)
94
self.run_bzr('shelve', '--all', retcode=0)
100
96
# Make sure there is no diff anymore
101
self.assertEqual(self.run_bzr('diff', retcode=0)[0], '')
97
self.assertEqual(self.capture('diff', retcode=0), '')
103
99
# Make sure the file is actually back the way it was
104
100
self.assertEqual(file('test_file').read(), self.ORIGINAL)
106
102
self._check_shelf('00', new_date=new_date)
109
self.run_bzr('unshelve1 --all', retcode=0)
105
self.run_bzr('unshelve', '--all', retcode=0)
111
107
self._check_diff()
122
118
self.__create_and_add_test_file()
124
120
# Shelve the changes
125
self.run_bzr('shelve1 --all', retcode=3)
121
self.run_bzr('shelve', '--all', retcode=3)
127
123
if os.path.exists(os.path.join(self.tree.branch.base,
128
124
'.shelf/shelves/default/00')):
146
142
self.tree.commit(message='update test_file')
148
144
# Shelve the changes
149
self.run_bzr('shelve1 --all -r 1', retcode=0)
145
self.run_bzr('shelve', '--all', '-r', '1', retcode=0)
151
147
self._check_diff(self.DIFF_2)
154
150
self.assertEqual(file('test_file').read(), self.ORIGINAL)
157
self.run_bzr('unshelve1 --all', retcode=0)
153
self.run_bzr('unshelve', '--all', retcode=0)
159
155
# Make sure the file is back the way it should be
160
156
self.assertEqual(file('test_file').read(), self.MODIFIED)
162
158
def test_shelf_with_two_revisions(self):
163
159
self.tree = self.make_branch_and_tree('.')
165
stdout, stderr = self.run_bzr('shelve1 --all -r 1..2', retcode=None)
161
cmd = 'shelve --all -r 1..2'
162
(stdout, stderr) = self.run_bzr_captured(cmd.split(), retcode=None)
167
164
self.assertEqual(stderr.split('\n')[0],
168
165
'bzr: ERROR: shelve only accepts a single revision parameter.')
177
174
self.build_tree_contents([('test_file', 'patch %s\n' % patch)])
179
176
# Shelve the changes
180
self.run_bzr('shelve1 --all', retcode=0)
177
self.run_bzr('shelve', '--all', retcode=0)
182
179
# Make sure there is no diff anymore
183
self.assertEqual(self.run_bzr('diff', retcode=0)[0], '')
180
self.assertEqual(self.capture('diff', retcode=0), '')
185
182
# Check the shelf is right
186
183
shelf = open(os.path.join(self.tree.basedir,
188
185
self.assertTrue('patch %s' % patch in shelf)
190
187
# Check the shown output is right
191
shown = self.run_bzr('shelf1 show %s' % patch, retcode=0)[0]
188
shown = self.capture('shelf show %s' % patch, retcode=0)
192
189
self.assertEqual(shown, shelf)
194
191
def test_shelf_show_multi(self):
204
201
self.assertTrue('patch 00' in shelf)
206
203
# Check the shown output is right
207
shown = self.run_bzr('shelf1 show 00', retcode=0)[0]
204
shown = self.capture('shelf show 00', retcode=0)
208
205
self.assertEqual(shown, shelf)
210
207
def test_shelf_show_unspecified(self):
220
217
self.assertTrue('patch 02' in shelf)
222
219
# Check the shown output is right
223
shown = self.run_bzr('shelf1 show', retcode=0)[0]
220
shown = self.capture('shelf show', retcode=0)
224
221
self.assertEqual(shown, shelf)
226
223
def test_shelf_show_with_no_patch(self):
227
224
self.tree = self.make_branch_and_tree('.')
228
stderr = self.run_bzr('shelf1 show 00', retcode=None)[1]
225
stderr = self.run_bzr_captured(['shelf', 'show', '00'], retcode=None)[1]
229
226
self.assertTrue("Patch '00' doesn't exist on shelf default!" in stderr)
231
228
def test_shelf_unshelve_failure(self):
237
234
file('test_file', 'w').write(self.MODIFIED)
239
236
# Shelve the changes
240
self.run_bzr('shelve1 --all', retcode=0)
237
self.run_bzr('shelve', '--all', retcode=0)
242
239
# Write an unapplyable patch into the shelf
243
240
shelf = open(os.path.join(self.tree.basedir,
248
245
# Unshelve, should fail
249
self.run_bzr('unshelve1 --all', retcode=3)
246
self.run_bzr('unshelve', '--all', retcode=3)
251
248
# Make sure the patch is still there, eventhough it's broken
252
249
shelf = open(os.path.join(self.tree.basedir,
254
251
self.assertEqual(shelf, self.DIFF_2)
256
253
# Working tree should be unchanged
257
diff = self.run_bzr('diff', retcode=0)[0]
254
diff = self.capture('diff', retcode=0)
258
255
self.assertEqual(diff, '')
260
257
def test_shelf_unshelve_failure_two_hunks(self):
268
265
file('test_file2', 'w').write(self.MODIFIED)
270
267
# Shelve the changes
271
self.run_bzr('shelve1 --all', retcode=0)
268
self.run_bzr('shelve', '--all', retcode=0)
273
270
# Put the changes to test_file back, the shelved patch won't apply now
274
271
file('test_file', 'w').write(self.MODIFIED)
275
272
self.tree.commit(message='screw up test_file')
277
274
# Unshelve, should fail
278
self.run_bzr('unshelve1 --all', retcode=3)
275
self.run_bzr('unshelve', '--all', retcode=3)
280
277
# Working tree should be unchanged
281
diff = self.run_bzr('diff', retcode=0)[0]
278
diff = self.capture('diff', retcode=0)
282
279
self.assertEqual(diff, '')
284
281
# Force should succeed and modify test_file2, but leave shelf
285
self.run_bzr('unshelve1 --force --all', retcode=0)
282
self.run_bzr('unshelve', '--force', '--all', retcode=0)
286
283
self.assertEqual(open('test_file2').read(), self.MODIFIED)
287
284
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
297
294
file('test_file2', 'w').write(self.MODIFIED)
299
296
# Shelve the changes
300
self.run_bzr('shelve1 --all test_file', retcode=0)
301
self.run_bzr('shelve1 --all test_file2', retcode=0)
297
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
298
self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
304
self.run_bzr('unshelve1 --all', retcode=0)
301
self.run_bzr('unshelve', '--all', retcode=0)
306
303
# We should now have 00 and 01~
307
304
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
308
305
self.assertTrue(os.path.exists('.shelf/shelves/default/01~'))
311
lines = self.run_bzr('shelf1 ls', retcode=0)[0].split('\n')
308
lines = self.capture('shelf ls', retcode=0).split('\n')
312
309
for line in lines:
313
310
self.assertFalse(line.startswith(' 01'))
315
312
# Unshelve, if unshelve is confused by the backup it will fail
316
self.run_bzr('unshelve1 --all', retcode=0)
313
self.run_bzr('unshelve', '--all', retcode=0)
318
315
def test_shelf_delete(self):
319
316
self.tree = self.make_branch_and_tree('.')
336
333
self.tree.unlock()
338
335
# Shelve the changes
339
self.run_bzr('shelve1 --all test_file', retcode=0)
340
self.run_bzr('shelve1 --all test_file2', retcode=0)
336
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
337
self.run_bzr('shelve', '--all', 'test_file2', retcode=0)
342
339
self._check_shelf('00', new_date=new_date)
345
self.run_bzr('shelf1 delete 00', retcode=0)
342
self.run_bzr('shelf', 'delete', '00', retcode=0)
347
344
# We should now have 01 but not 00, but we should have 00~
348
345
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
353
350
self._check_shelf('00~', new_date=new_date)
356
lines = self.run_bzr('shelf1 ls', retcode=0)[0].split('\n')
353
lines = self.capture('shelf ls', retcode=0).split('\n')
357
354
for line in lines:
358
355
self.assertFalse(line.startswith(' 00'))
360
357
# Unshelve should unshelve 01
361
self.run_bzr('unshelve1 --all', retcode=0)
358
self.run_bzr('unshelve', '--all', retcode=0)
362
359
self.assertEqual(file('test_file2').read(), self.MODIFIED)
364
361
def test_shelf_gaps(self):
365
362
self.tree = self.make_branch_and_tree('.')
366
363
self.__create_and_add_test_file()
367
364
file('test_file', 'w').write(self.MODIFIED)
368
self.run_bzr('shelve1 --all test_file', retcode=0)
365
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
369
366
file('test_file', 'w').write(self.MODIFIED)
370
self.run_bzr('shelve1 --all test_file', retcode=0)
367
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
372
369
# Now delete 00, leaving 01, next shelve should go into 02
373
self.run_bzr('shelf1 delete 0', retcode=0)
370
self.run_bzr('shelf', 'delete', '0', retcode=0)
374
371
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
375
372
self.assertFalse(os.path.exists('.shelf/shelves/default/02'))
376
373
file('test_file', 'w').write(self.MODIFIED)
377
self.run_bzr('shelve1 --all test_file', retcode=0)
374
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
378
375
self.assertFalse(os.path.exists('.shelf/shelves/default/00'))
379
376
self.assertTrue(os.path.exists('.shelf/shelves/default/02'))
386
383
# Modify then shelve, so we're not upgrading to 00, just for kicks
387
384
file('test_file', 'w').write(self.MODIFIED)
388
self.run_bzr('shelve1 --all test_file', retcode=0)
385
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
390
387
open('.bzr-shelf', 'w').write('First old shelf')
391
388
open('.bzr-shelf-1', 'w').write('Second old shelf')
394
391
# shelve and unshelve should bitch and do nothing
395
392
file('test_file', 'w').write('blah blah blah')
396
self.run_bzr('shelve1 --all', retcode=3)
393
self.run_bzr('shelve', '--all', retcode=3)
397
394
self.assertFalse(os.path.exists('.shelf/shelves/default/01'))
398
395
self.assertEqual(file('test_file').read(), 'blah blah blah')
399
self.run_bzr('unshelve1 --all', retcode=3)
396
self.run_bzr('unshelve', '--all', retcode=3)
400
397
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
402
399
# Upgrade, make sure it worked
403
self.run_bzr('shelf1 upgrade', retcode=0)
400
self.run_bzr('shelf', 'upgrade', retcode=0)
404
401
self.assertEqual(open('.shelf/shelves/default/01').read(),
405
402
'First old shelf')
406
403
self.assertEqual(open('.shelf/shelves/default/02').read(),
417
414
self.assertFalse(os.path.exists('.bzr-shelf-3'))
419
416
# Shelve should work now
420
self.run_bzr('shelve1 --all', retcode=0)
417
self.run_bzr('shelve', '--all', retcode=0)
422
419
def test_shelf_p1_patch(self):
423
420
self.tree = self.make_branch_and_tree('.')
425
422
self.__create_and_add_test_file()
427
424
# Run a benign shelf command to setup .shelf for us
428
self.run_bzr('shelf1 ls', retcode=0)
425
self.run_bzr('shelf', 'ls', retcode=0)
430
427
old_tree = self.tree.basis_tree()
431
428
old_tree.lock_read()
447
444
open('.shelf/shelves/default/00', 'w').write(diff)
449
446
# This should work
450
self.run_bzr('unshelve1 --all', retcode=0)
447
self.run_bzr('unshelve', '--all', retcode=0)
452
449
self._check_diff()
470
467
# Shelve the changes
471
self.run_bzr('shelve1 --all', retcode=0)
468
self.run_bzr('shelve', '--all', retcode=0)
473
470
# Working tree should be unchanged
474
diff = self.run_bzr('diff', retcode=0)[0]
471
diff = self.capture('diff', retcode=0)
475
472
self.assertEqual(diff, '')
477
474
# Unshelve, should succeed
478
self.run_bzr('unshelve1 --all', retcode=0)
475
self.run_bzr('unshelve', '--all', retcode=0)
480
477
self._check_diff(filename='subdir/test_file')
482
479
# Make sure relative filenames work ok
483
self.run_bzr('shelve1 test_file --all', retcode=0)
480
self.run_bzr('shelve', 'test_file', '--all', retcode=0)
485
482
def test_shelf_shelf_bogus_subcommand(self):
486
483
self.tree = self.make_branch_and_tree('.')
487
self.run_bzr('shelf1 foo', retcode=3) # <- retcode == 3
484
self.run_bzr('shelf', 'foo', retcode=3) # <- retcode == 3
489
486
def test_shelf_OOO_unshelve(self):
490
487
self.tree = self.make_branch_and_tree('.')
499
496
# Shelve the changes
500
497
for i in range(1, 5):
501
self.run_bzr(['shelve1', '--all', 'test_file%d' % i], retcode=0)
498
self.run_bzr('shelve', '--all', 'test_file%d' % i, retcode=0)
503
500
# Check shelving worked
504
501
for i in range(1, 5):
509
506
self.assertTrue(os.path.exists('.shelf/shelves/default/0%d' % i))
512
self.run_bzr('unshelve1 --all 00', retcode=0)
509
self.run_bzr('unshelve', '--all', '00', retcode=0)
513
510
self.assertEqual(file('test_file1').read(), self.MODIFIED)
516
lines = self.run_bzr('shelf1 ls', retcode=0)[0].split('\n')
513
lines = self.capture('shelf ls', retcode=0).split('\n')
517
514
for line in lines:
518
515
self.assertFalse(line.startswith(' 00'))
520
517
# Check we can reshelve once we've unshelved out of order, should be 04
521
518
self.assertFalse(os.path.exists('.shelf/shelves/default/04'))
522
self.run_bzr('shelve1 --all')
519
self.run_bzr('shelve', '--all')
523
520
self.assertTrue(os.path.exists('.shelf/shelves/default/04'))
526
text = self.run_bzr('shelf1 ls', retcode=0)[0]
523
text = self.capture('shelf ls', retcode=0)
527
524
for line in text.split('\n'):
528
525
self.assertFalse(line.startswith(' 00'))
530
527
# We now have 01,02,03,04
532
self.run_bzr('unshelve1 --all 02', retcode=0)
529
self.run_bzr('unshelve', '--all', '02', retcode=0)
533
530
self.assertEqual(file('test_file3').read(), self.MODIFIED)
535
532
# Unshelve the default, this is the reshelved 00, hence modifies file 1
536
self.run_bzr('unshelve1 --all', retcode=0)
533
self.run_bzr('unshelve', '--all', retcode=0)
537
534
self.assertEqual(file('test_file1').read(), self.MODIFIED)
539
536
def test_shelf_switch_basic(self):
543
540
# This should go to "default"
544
541
file('test_file', 'w').write(self.MODIFIED)
545
self.run_bzr('shelve1 --all test_file', retcode=0)
542
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
547
544
# Switch to "other"
548
self.run_bzr('shelf1 switch other', retcode=0)
545
self.run_bzr('shelf', 'switch', 'other', retcode=0)
549
546
file('test_file', 'w').write(self.MODIFIED)
550
self.run_bzr('shelve1 --all test_file', retcode=0)
547
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
552
549
# Check it worked
553
550
self.assertTrue(os.path.exists('.shelf/shelves/default/00'))
555
552
self.assertTrue(os.path.exists('.shelf/shelves/other/00'))
558
self.run_bzr('shelf1 switch default', retcode=0)
555
self.run_bzr('shelf', 'switch', 'default', retcode=0)
559
556
file('test_file', 'w').write(self.MODIFIED)
560
self.run_bzr('shelve1 --all test_file', retcode=0)
557
self.run_bzr('shelve', '--all', 'test_file', retcode=0)
562
559
# Check that worked
563
560
self.assertTrue(os.path.exists('.shelf/shelves/default/01'))
564
561
self.assertFalse(os.path.exists('.shelf/shelves/other/01'))
566
def test_shelf_binary(self):
567
self.tree = self.make_branch_and_tree('.')
568
self.build_tree_contents([('file', '\x00')])
569
self.tree.add('file')
570
self.run_bzr_error(['Changes involve binary files.'], 'shelve1 --all')
572
563
def test_shelf_bad_patch_arg(self):
573
564
self.tree = self.make_branch_and_tree('.')
575
566
# Check the bad arg handling
576
stdout, error = self.run_bzr('unshelve1 01', retcode=3)
567
stdout, error = self.run_bzr_captured(['unshelve', '01'], retcode=3)
577
568
self.assertTrue("Patch '01' doesn't exist on shelf" in error)
579
stdout, error = self.run_bzr('unshelve1 foo', retcode=3)
570
stdout, error = self.run_bzr_captured(['unshelve', 'foo'], retcode=3)
580
571
self.assertTrue("Invalid patch name 'foo'" in error)
582
573
# Hex and is cracky, so it shouldn't work
583
stdout, error = self.run_bzr(['unshelve1', '0x00'], retcode=3)
574
stdout, error = self.run_bzr_captured(['unshelve', '0x00'], retcode=3)
584
575
self.assertTrue("Invalid patch name '0x00'" in error)
586
577
def test_color_hunk_selector(self):
618
609
f = file('test_file', 'wb')
619
610
f.write(self.MODIFIED)
621
stdout, error = self.run_bzr('shelve1 --all --no-color')
622
stdout, error = self.run_bzr('unshelve1 --all --no-color')
624
def test_shelf_help(self):
625
self.assertContainsRe(cmd_shelf1().help(),
626
'list\n.*List the patches on the current shelf')
628
def test_show_empty_shelf(self):
629
self.tree = self.make_branch_and_tree('.')
630
self.run_bzr_error(('No patches on shelf.',), 'shelf1 show')
612
stdout, error = self.run_bzr_captured(['shelve', '--all',
614
stdout, error = self.run_bzr_captured(['unshelve', '--all',