1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
|
#!/bin/sh
#
# Copyright (c) 2007 Johannes E Schindelin
#
test_description='Test git stash'
. ./test-lib.sh
test_expect_success 'stash some dirty working directory' '
echo 1 > file &&
git add file &&
test_tick &&
git commit -m initial &&
echo 2 > file &&
git add file &&
echo 3 > file &&
test_tick &&
git stash &&
git diff-files --quiet &&
git diff-index --cached --quiet HEAD
'
cat > expect << EOF
diff --git a/file b/file
index 0cfbf08..00750ed 100644
--- a/file
+++ b/file
@@ -1 +1 @@
-2
+3
EOF
test_expect_success 'parents of stash' '
test $(git rev-parse stash^) = $(git rev-parse HEAD) &&
git diff stash^2..stash > output &&
test_cmp output expect
'
test_expect_success 'applying bogus stash does nothing' '
test_must_fail git stash apply stash@{1} &&
echo 1 >expect &&
test_cmp expect file
'
test_expect_success 'apply needs clean working directory' '
echo 4 > other-file &&
git add other-file &&
echo 5 > other-file &&
test_must_fail git stash apply
'
test_expect_success 'apply stashed changes' '
git add other-file &&
test_tick &&
git commit -m other-file &&
git stash apply &&
test 3 = $(cat file) &&
test 1 = $(git show :file) &&
test 1 = $(git show HEAD:file)
'
test_expect_success 'apply stashed changes (including index)' '
git reset --hard HEAD^ &&
echo 6 > other-file &&
git add other-file &&
test_tick &&
git commit -m other-file &&
git stash apply --index &&
test 3 = $(cat file) &&
test 2 = $(git show :file) &&
test 1 = $(git show HEAD:file)
'
test_expect_success 'unstashing in a subdirectory' '
git reset --hard HEAD &&
mkdir subdir &&
(
cd subdir &&
git stash apply
)
'
test_expect_success 'drop top stash' '
git reset --hard &&
git stash list > stashlist1 &&
echo 7 > file &&
git stash &&
git stash drop &&
git stash list > stashlist2 &&
test_cmp stashlist1 stashlist2 &&
git stash apply &&
test 3 = $(cat file) &&
test 1 = $(git show :file) &&
test 1 = $(git show HEAD:file)
'
test_expect_success 'drop middle stash' '
git reset --hard &&
echo 8 > file &&
git stash &&
echo 9 > file &&
git stash &&
git stash drop stash@{1} &&
test 2 = $(git stash list | wc -l) &&
git stash apply &&
test 9 = $(cat file) &&
test 1 = $(git show :file) &&
test 1 = $(git show HEAD:file) &&
git reset --hard &&
git stash drop &&
git stash apply &&
test 3 = $(cat file) &&
test 1 = $(git show :file) &&
test 1 = $(git show HEAD:file)
'
test_expect_success 'stash pop' '
git reset --hard &&
git stash pop &&
test 3 = $(cat file) &&
test 1 = $(git show :file) &&
test 1 = $(git show HEAD:file) &&
test 0 = $(git stash list | wc -l)
'
cat > expect << EOF
diff --git a/file2 b/file2
new file mode 100644
index 0000000..1fe912c
--- /dev/null
+++ b/file2
@@ -0,0 +1 @@
+bar2
EOF
cat > expect1 << EOF
diff --git a/file b/file
index 257cc56..5716ca5 100644
--- a/file
+++ b/file
@@ -1 +1 @@
-foo
+bar
EOF
cat > expect2 << EOF
diff --git a/file b/file
index 7601807..5716ca5 100644
--- a/file
+++ b/file
@@ -1 +1 @@
-baz
+bar
diff --git a/file2 b/file2
new file mode 100644
index 0000000..1fe912c
--- /dev/null
+++ b/file2
@@ -0,0 +1 @@
+bar2
EOF
test_expect_success 'stash branch' '
echo foo > file &&
git commit file -m first &&
echo bar > file &&
echo bar2 > file2 &&
git add file2 &&
git stash &&
echo baz > file &&
git commit file -m second &&
git stash branch stashbranch &&
test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
test $(git rev-parse HEAD) = $(git rev-parse master^) &&
git diff --cached > output &&
test_cmp output expect &&
git diff > output &&
test_cmp output expect1 &&
git add file &&
git commit -m alternate\ second &&
git diff master..stashbranch > output &&
test_cmp output expect2 &&
test 0 = $(git stash list | wc -l)
'
test_expect_success 'apply -q is quiet' '
echo foo > file &&
git stash &&
git stash apply -q > output.out 2>&1 &&
test ! -s output.out
'
test_expect_success 'save -q is quiet' '
git stash save --quiet > output.out 2>&1 &&
test ! -s output.out
'
test_expect_success 'pop -q is quiet' '
git stash pop -q > output.out 2>&1 &&
test ! -s output.out
'
test_expect_success 'pop -q --index works and is quiet' '
echo foo > file &&
git add file &&
git stash save --quiet &&
git stash pop -q --index > output.out 2>&1 &&
test foo = "$(git show :file)" &&
test ! -s output.out
'
test_expect_success 'drop -q is quiet' '
git stash &&
git stash drop -q > output.out 2>&1 &&
test ! -s output.out
'
test_expect_success 'stash -k' '
echo bar3 > file &&
echo bar4 > file2 &&
git add file2 &&
git stash -k &&
test bar,bar4 = $(cat file),$(cat file2)
'
test_expect_success 'stash --invalid-option' '
echo bar5 > file &&
echo bar6 > file2 &&
git add file2 &&
test_must_fail git stash --invalid-option &&
test_must_fail git stash save --invalid-option &&
test bar5,bar6 = $(cat file),$(cat file2) &&
git stash -- -message-starting-with-dash &&
test bar,bar2 = $(cat file),$(cat file2)
'
test_expect_success 'stash an added file' '
git reset --hard &&
echo new >file3 &&
git add file3 &&
git stash save "added file" &&
! test -r file3 &&
git stash apply &&
test new = "$(cat file3)"
'
test_expect_success 'stash rm then recreate' '
git reset --hard &&
git rm file &&
echo bar7 >file &&
git stash save "rm then recreate" &&
test bar = "$(cat file)" &&
git stash apply &&
test bar7 = "$(cat file)"
'
test_expect_success 'stash rm and ignore' '
git reset --hard &&
git rm file &&
echo file >.gitignore &&
git stash save "rm and ignore" &&
test bar = "$(cat file)" &&
test file = "$(cat .gitignore)" &&
git stash apply &&
! test -r file &&
test file = "$(cat .gitignore)"
'
test_expect_success 'stash rm and ignore (stage .gitignore)' '
git reset --hard &&
git rm file &&
echo file >.gitignore &&
git add .gitignore &&
git stash save "rm and ignore (stage .gitignore)" &&
test bar = "$(cat file)" &&
! test -r .gitignore &&
git stash apply &&
! test -r file &&
test file = "$(cat .gitignore)"
'
test_expect_success SYMLINKS 'stash file to symlink' '
git reset --hard &&
rm file &&
ln -s file2 file &&
git stash save "file to symlink" &&
test -f file &&
test bar = "$(cat file)" &&
git stash apply &&
case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
'
test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
git reset --hard &&
git rm file &&
ln -s file2 file &&
git stash save "file to symlink (stage rm)" &&
test -f file &&
test bar = "$(cat file)" &&
git stash apply &&
case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
'
test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
git reset --hard &&
rm file &&
ln -s file2 file &&
git add file &&
git stash save "file to symlink (full stage)" &&
test -f file &&
test bar = "$(cat file)" &&
git stash apply &&
case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
'
# This test creates a commit with a symlink used for the following tests
test_expect_success SYMLINKS 'stash symlink to file' '
git reset --hard &&
ln -s file filelink &&
git add filelink &&
git commit -m "Add symlink" &&
rm filelink &&
cp file filelink &&
git stash save "symlink to file" &&
test -h filelink &&
case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
git stash apply &&
! test -h filelink &&
test bar = "$(cat file)"
'
test_expect_success SYMLINKS 'stash symlink to file (stage rm)' '
git reset --hard &&
git rm filelink &&
cp file filelink &&
git stash save "symlink to file (stage rm)" &&
test -h filelink &&
case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
git stash apply &&
! test -h filelink &&
test bar = "$(cat file)"
'
test_expect_success SYMLINKS 'stash symlink to file (full stage)' '
git reset --hard &&
rm filelink &&
cp file filelink &&
git add filelink &&
git stash save "symlink to file (full stage)" &&
test -h filelink &&
case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac &&
git stash apply &&
! test -h filelink &&
test bar = "$(cat file)"
'
test_expect_failure 'stash directory to file' '
git reset --hard &&
mkdir dir &&
echo foo >dir/file &&
git add dir/file &&
git commit -m "Add file in dir" &&
rm -fr dir &&
echo bar >dir &&
git stash save "directory to file" &&
test -d dir &&
test foo = "$(cat dir/file)" &&
test_must_fail git stash apply &&
test bar = "$(cat dir)" &&
git reset --soft HEAD^
'
test_expect_failure 'stash file to directory' '
git reset --hard &&
rm file &&
mkdir file &&
echo foo >file/file &&
git stash save "file to directory" &&
test -f file &&
test bar = "$(cat file)" &&
git stash apply &&
test -f file/file &&
test foo = "$(cat file/file)"
'
test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
git stash clear &&
test_when_finished "git reset --hard HEAD" &&
git reset --hard &&
echo foo >> file &&
STASH_ID=$(git stash create) &&
git reset --hard &&
git stash branch stash-branch ${STASH_ID} &&
test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
test $(git ls-files --modified | wc -l) -eq 1
'
test_expect_success 'stash branch - stashes on stack, stash-like argument' '
git stash clear &&
test_when_finished "git reset --hard HEAD" &&
git reset --hard &&
echo foo >> file &&
git stash &&
test_when_finished "git stash drop" &&
echo bar >> file &&
STASH_ID=$(git stash create) &&
git reset --hard &&
git stash branch stash-branch ${STASH_ID} &&
test_when_finished "git reset --hard HEAD && git checkout master && git branch -D stash-branch" &&
test $(git ls-files --modified | wc -l) -eq 1
'
test_expect_success 'stash show - stashes on stack, stash-like argument' '
git stash clear &&
test_when_finished "git reset --hard HEAD" &&
git reset --hard &&
echo foo >> file &&
git stash &&
test_when_finished "git stash drop" &&
echo bar >> file &&
STASH_ID=$(git stash create) &&
git reset --hard &&
cat >expected <<-EOF &&
file | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
EOF
git stash show ${STASH_ID} >actual &&
test_cmp expected actual
'
test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
git stash clear &&
test_when_finished "git reset --hard HEAD" &&
git reset --hard &&
echo foo >> file &&
git stash &&
test_when_finished "git stash drop" &&
echo bar >> file &&
STASH_ID=$(git stash create) &&
git reset --hard &&
cat >expected <<-EOF &&
diff --git a/file b/file
index 7601807..935fbd3 100644
--- a/file
+++ b/file
@@ -1 +1,2 @@
baz
+bar
EOF
git stash show -p ${STASH_ID} >actual &&
test_cmp expected actual
'
test_expect_success 'stash show - no stashes on stack, stash-like argument' '
git stash clear &&
test_when_finished "git reset --hard HEAD" &&
git reset --hard &&
echo foo >> file &&
STASH_ID=$(git stash create) &&
git reset --hard &&
cat >expected <<-EOF &&
file | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
EOF
git stash show ${STASH_ID} >actual &&
test_cmp expected actual
'
test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
git stash clear &&
test_when_finished "git reset --hard HEAD" &&
git reset --hard &&
echo foo >> file &&
STASH_ID=$(git stash create) &&
git reset --hard &&
cat >expected <<-EOF &&
diff --git a/file b/file
index 7601807..71b52c4 100644
--- a/file
+++ b/file
@@ -1 +1,2 @@
baz
+foo
EOF
git stash show -p ${STASH_ID} >actual &&
test_cmp expected actual
'
test_expect_success 'stash drop - fail early if specified stash is not a stash reference' '
git stash clear &&
test_when_finished "git reset --hard HEAD && git stash clear" &&
git reset --hard &&
echo foo > file &&
git stash &&
echo bar > file &&
git stash &&
test_must_fail git stash drop $(git rev-parse stash@{0}) &&
git stash pop &&
test bar = "$(cat file)" &&
git reset --hard HEAD
'
test_expect_success 'stash pop - fail early if specified stash is not a stash reference' '
git stash clear &&
test_when_finished "git reset --hard HEAD && git stash clear" &&
git reset --hard &&
echo foo > file &&
git stash &&
echo bar > file &&
git stash &&
test_must_fail git stash pop $(git rev-parse stash@{0}) &&
git stash pop &&
test bar = "$(cat file)" &&
git reset --hard HEAD
'
test_expect_success 'ref with non-existant reflog' '
git stash clear &&
echo bar5 > file &&
echo bar6 > file2 &&
git add file2 &&
git stash &&
! "git rev-parse --quiet --verify does-not-exist" &&
test_must_fail git stash drop does-not-exist &&
test_must_fail git stash drop does-not-exist@{0} &&
test_must_fail git stash pop does-not-exist &&
test_must_fail git stash pop does-not-exist@{0} &&
test_must_fail git stash apply does-not-exist &&
test_must_fail git stash apply does-not-exist@{0} &&
test_must_fail git stash show does-not-exist &&
test_must_fail git stash show does-not-exist@{0} &&
test_must_fail git stash branch tmp does-not-exist &&
test_must_fail git stash branch tmp does-not-exist@{0} &&
git stash drop
'
test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
git stash clear &&
test_must_fail git stash drop stash@{0} &&
echo bar5 > file &&
echo bar6 > file2 &&
git add file2 &&
git stash &&
test_must_fail git drop stash@{1} &&
test_must_fail git pop stash@{1} &&
test_must_fail git apply stash@{1} &&
test_must_fail git show stash@{1} &&
test_must_fail git branch tmp stash@{1} &&
git stash drop
'
test_expect_success 'stash branch should not drop the stash if the branch exists' '
git stash clear &&
echo foo >file &&
git add file &&
git commit -m initial &&
echo bar >file &&
git stash &&
test_must_fail git stash branch master stash@{0} &&
git rev-parse stash@{0} --
'
test_done
|