blob: 5f282ecf6175d381242b13178b9088a76cd28805 [file] [log] [blame]
Johannes Schindelin150937c2007-07-02 12:14:49 +01001#!/bin/sh
2#
3# Copyright (c) 2007 Johannes E Schindelin
4#
5
Nanako Shiraishi0cb0e142008-09-03 17:59:27 +09006test_description='Test git stash'
Johannes Schindelin150937c2007-07-02 12:14:49 +01007
Johannes Schindelincbc75a12020-11-18 23:44:26 +00008GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
Johannes Schindelin334afbc2020-11-18 23:44:19 +00009export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10
Johannes Schindelin150937c2007-07-02 12:14:49 +010011. ./test-lib.sh
12
brian m. carlsonc7848152019-08-26 01:43:41 +000013diff_cmp () {
14 for i in "$1" "$2"
15 do
16 sed -e 's/^index 0000000\.\.[0-9a-f]*/index 0000000..1234567/' \
17 -e 's/^index [0-9a-f]*\.\.[0-9a-f]*/index 1234567..89abcde/' \
18 -e 's/^index [0-9a-f]*,[0-9a-f]*\.\.[0-9a-f]*/index 1234567,7654321..89abcde/' \
19 "$i" >"$i.compare" || return 1
20 done &&
21 test_cmp "$1.compare" "$2.compare" &&
22 rm -f "$1.compare" "$2.compare"
23}
24
Johannes Schindelin150937c2007-07-02 12:14:49 +010025test_expect_success 'stash some dirty working directory' '
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +000026 echo 1 >file &&
Johannes Schindelin150937c2007-07-02 12:14:49 +010027 git add file &&
Jeff King88bab592015-04-22 15:30:58 -040028 echo unrelated >other-file &&
29 git add other-file &&
Johannes Schindelin150937c2007-07-02 12:14:49 +010030 test_tick &&
31 git commit -m initial &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +000032 echo 2 >file &&
Johannes Schindelin150937c2007-07-02 12:14:49 +010033 git add file &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +000034 echo 3 >file &&
Johannes Schindelin150937c2007-07-02 12:14:49 +010035 test_tick &&
36 git stash &&
37 git diff-files --quiet &&
38 git diff-index --cached --quiet HEAD
39'
40
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +000041cat >expect <<EOF
Johannes Schindelin150937c2007-07-02 12:14:49 +010042diff --git a/file b/file
43index 0cfbf08..00750ed 100644
44--- a/file
45+++ b/file
46@@ -1 +1 @@
47-2
48+3
49EOF
50
51test_expect_success 'parents of stash' '
52 test $(git rev-parse stash^) = $(git rev-parse HEAD) &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +000053 git diff stash^2..stash >output &&
brian m. carlsonc7848152019-08-26 01:43:41 +000054 diff_cmp expect output
Johannes Schindelin150937c2007-07-02 12:14:49 +010055'
56
Jeff King59d418f2011-04-05 17:20:25 -040057test_expect_success 'applying bogus stash does nothing' '
58 test_must_fail git stash apply stash@{1} &&
59 echo 1 >expect &&
60 test_cmp expect file
61'
62
Jeff Kinge0e2a9c2011-04-05 17:23:15 -040063test_expect_success 'apply does not need clean working directory' '
64 echo 4 >other-file &&
Jeff Kinge0e2a9c2011-04-05 17:23:15 -040065 git stash apply &&
66 echo 3 >expect &&
67 test_cmp expect file
68'
69
70test_expect_success 'apply does not clobber working directory changes' '
71 git reset --hard &&
72 echo 4 >file &&
73 test_must_fail git stash apply &&
74 echo 4 >expect &&
75 test_cmp expect file
Johannes Schindelin150937c2007-07-02 12:14:49 +010076'
77
78test_expect_success 'apply stashed changes' '
Jeff Kinge0e2a9c2011-04-05 17:23:15 -040079 git reset --hard &&
80 echo 5 >other-file &&
Johannes Schindelin150937c2007-07-02 12:14:49 +010081 git add other-file &&
82 test_tick &&
83 git commit -m other-file &&
84 git stash apply &&
85 test 3 = $(cat file) &&
86 test 1 = $(git show :file) &&
87 test 1 = $(git show HEAD:file)
88'
89
90test_expect_success 'apply stashed changes (including index)' '
91 git reset --hard HEAD^ &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +000092 echo 6 >other-file &&
Johannes Schindelin150937c2007-07-02 12:14:49 +010093 git add other-file &&
94 test_tick &&
95 git commit -m other-file &&
96 git stash apply --index &&
97 test 3 = $(cat file) &&
98 test 2 = $(git show :file) &&
99 test 1 = $(git show HEAD:file)
100'
101
Junio C Hamanoceff0792007-07-25 15:32:22 -0700102test_expect_success 'unstashing in a subdirectory' '
103 git reset --hard HEAD &&
104 mkdir subdir &&
Jonathan Nieder18a82692010-09-06 20:42:54 -0500105 (
106 cd subdir &&
107 git stash apply
Jens Lehmannfd4ec4f2010-09-06 20:39:54 +0200108 )
Brandon Caseyb683c082008-03-02 14:58:51 -0600109'
110
Jeff Kingd6cc2df2015-05-20 14:01:32 -0400111test_expect_success 'stash drop complains of extra options' '
112 test_must_fail git stash drop --foo
113'
114
Brandon Caseyb683c082008-03-02 14:58:51 -0600115test_expect_success 'drop top stash' '
116 git reset --hard &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000117 git stash list >expected &&
118 echo 7 >file &&
Brandon Caseyb683c082008-03-02 14:58:51 -0600119 git stash &&
120 git stash drop &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000121 git stash list >actual &&
122 test_cmp expected actual &&
Brandon Caseyb683c082008-03-02 14:58:51 -0600123 git stash apply &&
124 test 3 = $(cat file) &&
125 test 1 = $(git show :file) &&
126 test 1 = $(git show HEAD:file)
127'
128
129test_expect_success 'drop middle stash' '
130 git reset --hard &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000131 echo 8 >file &&
Brandon Caseyb683c082008-03-02 14:58:51 -0600132 git stash &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000133 echo 9 >file &&
Brandon Caseyb683c082008-03-02 14:58:51 -0600134 git stash &&
135 git stash drop stash@{1} &&
136 test 2 = $(git stash list | wc -l) &&
137 git stash apply &&
138 test 9 = $(cat file) &&
139 test 1 = $(git show :file) &&
140 test 1 = $(git show HEAD:file) &&
141 git reset --hard &&
142 git stash drop &&
143 git stash apply &&
144 test 3 = $(cat file) &&
145 test 1 = $(git show :file) &&
146 test 1 = $(git show HEAD:file)
147'
148
Aaron M Watsona56c8f52016-10-24 19:40:13 -0400149test_expect_success 'drop middle stash by index' '
150 git reset --hard &&
151 echo 8 >file &&
152 git stash &&
153 echo 9 >file &&
154 git stash &&
155 git stash drop 1 &&
156 test 2 = $(git stash list | wc -l) &&
157 git stash apply &&
158 test 9 = $(cat file) &&
159 test 1 = $(git show :file) &&
160 test 1 = $(git show HEAD:file) &&
161 git reset --hard &&
162 git stash drop &&
163 git stash apply &&
164 test 3 = $(cat file) &&
165 test 1 = $(git show :file) &&
166 test 1 = $(git show HEAD:file)
167'
168
Brandon Caseyb683c082008-03-02 14:58:51 -0600169test_expect_success 'stash pop' '
170 git reset --hard &&
171 git stash pop &&
172 test 3 = $(cat file) &&
173 test 1 = $(git show :file) &&
174 test 1 = $(git show HEAD:file) &&
175 test 0 = $(git stash list | wc -l)
Junio C Hamanoceff0792007-07-25 15:32:22 -0700176'
177
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000178cat >expect <<EOF
Abhijit Menon-Sen4a588072008-07-07 02:50:10 +0530179diff --git a/file2 b/file2
180new file mode 100644
181index 0000000..1fe912c
182--- /dev/null
183+++ b/file2
184@@ -0,0 +1 @@
185+bar2
186EOF
187
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000188cat >expect1 <<EOF
Abhijit Menon-Sen4a588072008-07-07 02:50:10 +0530189diff --git a/file b/file
190index 257cc56..5716ca5 100644
191--- a/file
192+++ b/file
193@@ -1 +1 @@
194-foo
195+bar
196EOF
197
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000198cat >expect2 <<EOF
Abhijit Menon-Sen4a588072008-07-07 02:50:10 +0530199diff --git a/file b/file
200index 7601807..5716ca5 100644
201--- a/file
202+++ b/file
203@@ -1 +1 @@
204-baz
205+bar
206diff --git a/file2 b/file2
207new file mode 100644
208index 0000000..1fe912c
209--- /dev/null
210+++ b/file2
211@@ -0,0 +1 @@
212+bar2
213EOF
214
215test_expect_success 'stash branch' '
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000216 echo foo >file &&
Jonathan Niedera48fcd82010-10-30 20:46:54 -0500217 git commit file -m first &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000218 echo bar >file &&
219 echo bar2 >file2 &&
Abhijit Menon-Sen4a588072008-07-07 02:50:10 +0530220 git add file2 &&
221 git stash &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000222 echo baz >file &&
Abhijit Menon-Sen4a588072008-07-07 02:50:10 +0530223 git commit file -m second &&
224 git stash branch stashbranch &&
225 test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
Johannes Schindelincbc75a12020-11-18 23:44:26 +0000226 test $(git rev-parse HEAD) = $(git rev-parse main^) &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000227 git diff --cached >output &&
brian m. carlsonc7848152019-08-26 01:43:41 +0000228 diff_cmp expect output &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000229 git diff >output &&
brian m. carlsonc7848152019-08-26 01:43:41 +0000230 diff_cmp expect1 output &&
Abhijit Menon-Sen4a588072008-07-07 02:50:10 +0530231 git add file &&
232 git commit -m alternate\ second &&
Johannes Schindelincbc75a12020-11-18 23:44:26 +0000233 git diff main..stashbranch >output &&
brian m. carlsonc7848152019-08-26 01:43:41 +0000234 diff_cmp output expect2 &&
Abhijit Menon-Sen4a588072008-07-07 02:50:10 +0530235 test 0 = $(git stash list | wc -l)
236'
237
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700238test_expect_success 'apply -q is quiet' '
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000239 echo foo >file &&
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700240 git stash &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000241 git stash apply -q >output.out 2>&1 &&
Junio C Hamanoca8d1482013-06-09 11:29:20 -0700242 test_must_be_empty output.out
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700243'
244
245test_expect_success 'save -q is quiet' '
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000246 git stash save --quiet >output.out 2>&1 &&
Junio C Hamanoca8d1482013-06-09 11:29:20 -0700247 test_must_be_empty output.out
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700248'
249
Thomas Gummererdf53c802019-11-13 15:01:36 +0000250test_expect_success 'pop -q works and is quiet' '
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000251 git stash pop -q >output.out 2>&1 &&
Thomas Gummererdf53c802019-11-13 15:01:36 +0000252 echo bar >expect &&
253 git show :file >actual &&
254 test_cmp expect actual &&
Junio C Hamanoca8d1482013-06-09 11:29:20 -0700255 test_must_be_empty output.out
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700256'
257
Thomas Rast460ccd02010-02-15 17:05:46 +0100258test_expect_success 'pop -q --index works and is quiet' '
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000259 echo foo >file &&
Thomas Rast460ccd02010-02-15 17:05:46 +0100260 git add file &&
261 git stash save --quiet &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000262 git stash pop -q --index >output.out 2>&1 &&
Thomas Gummererdf53c802019-11-13 15:01:36 +0000263 git diff-files file2 >file2.diff &&
264 test_must_be_empty file2.diff &&
Thomas Rast460ccd02010-02-15 17:05:46 +0100265 test foo = "$(git show :file)" &&
Junio C Hamanoca8d1482013-06-09 11:29:20 -0700266 test_must_be_empty output.out
Thomas Rast460ccd02010-02-15 17:05:46 +0100267'
268
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700269test_expect_success 'drop -q is quiet' '
270 git stash &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000271 git stash drop -q >output.out 2>&1 &&
Junio C Hamanoca8d1482013-06-09 11:29:20 -0700272 test_must_be_empty output.out
Stephen Boydfcdd0e92009-06-17 18:07:37 -0700273'
274
Johannes Schindelinea41cfc2009-07-27 20:37:10 +0200275test_expect_success 'stash -k' '
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000276 echo bar3 >file &&
277 echo bar4 >file2 &&
Johannes Schindelinea41cfc2009-07-27 20:37:10 +0200278 git add file2 &&
279 git stash -k &&
280 test bar,bar4 = $(cat file),$(cat file2)
281'
282
Dan McGee21ec98a2011-04-07 12:04:19 -0500283test_expect_success 'stash --no-keep-index' '
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000284 echo bar33 >file &&
285 echo bar44 >file2 &&
Dan McGee21ec98a2011-04-07 12:04:19 -0500286 git add file2 &&
287 git stash --no-keep-index &&
288 test bar,bar2 = $(cat file),$(cat file2)
289'
290
Alexandr Miloslavskiy8c3713c2020-02-17 17:25:21 +0000291test_expect_success 'dont assume push with non-option args' '
292 test_must_fail git stash -q drop 2>err &&
293 test_i18ngrep -e "subcommand wasn'\''t specified; '\''push'\'' can'\''t be assumed due to unexpected token '\''drop'\''" err
294'
295
Matthieu Moy3c2eb802009-08-18 23:38:40 +0200296test_expect_success 'stash --invalid-option' '
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000297 echo bar5 >file &&
298 echo bar6 >file2 &&
Matthieu Moy3c2eb802009-08-18 23:38:40 +0200299 git add file2 &&
300 test_must_fail git stash --invalid-option &&
301 test_must_fail git stash save --invalid-option &&
Thomas Gummerer1ada5022017-02-28 20:33:39 +0000302 test bar5,bar6 = $(cat file),$(cat file2)
Matthieu Moy3c2eb802009-08-18 23:38:40 +0200303'
304
Charles Bailey2ba2fe22010-04-18 19:27:49 +0100305test_expect_success 'stash an added file' '
306 git reset --hard &&
307 echo new >file3 &&
308 git add file3 &&
309 git stash save "added file" &&
310 ! test -r file3 &&
311 git stash apply &&
312 test new = "$(cat file3)"
313'
314
Matthew Kraaib6e531e2019-02-25 23:16:11 +0000315test_expect_success 'stash --intent-to-add file' '
316 git reset --hard &&
317 echo new >file4 &&
318 git add --intent-to-add file4 &&
319 test_when_finished "git rm -f file4" &&
320 test_must_fail git stash
321'
322
Charles Bailey2ba2fe22010-04-18 19:27:49 +0100323test_expect_success 'stash rm then recreate' '
324 git reset --hard &&
325 git rm file &&
326 echo bar7 >file &&
327 git stash save "rm then recreate" &&
328 test bar = "$(cat file)" &&
329 git stash apply &&
330 test bar7 = "$(cat file)"
331'
332
333test_expect_success 'stash rm and ignore' '
334 git reset --hard &&
335 git rm file &&
336 echo file >.gitignore &&
337 git stash save "rm and ignore" &&
338 test bar = "$(cat file)" &&
Jonathan Niedera48fcd82010-10-30 20:46:54 -0500339 test file = "$(cat .gitignore)" &&
Charles Bailey2ba2fe22010-04-18 19:27:49 +0100340 git stash apply &&
341 ! test -r file &&
342 test file = "$(cat .gitignore)"
343'
344
345test_expect_success 'stash rm and ignore (stage .gitignore)' '
346 git reset --hard &&
347 git rm file &&
348 echo file >.gitignore &&
349 git add .gitignore &&
350 git stash save "rm and ignore (stage .gitignore)" &&
351 test bar = "$(cat file)" &&
Jonathan Niedera48fcd82010-10-30 20:46:54 -0500352 ! test -r .gitignore &&
Charles Bailey2ba2fe22010-04-18 19:27:49 +0100353 git stash apply &&
354 ! test -r file &&
355 test file = "$(cat .gitignore)"
356'
357
358test_expect_success SYMLINKS 'stash file to symlink' '
359 git reset --hard &&
360 rm file &&
361 ln -s file2 file &&
362 git stash save "file to symlink" &&
363 test -f file &&
364 test bar = "$(cat file)" &&
365 git stash apply &&
366 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
367'
368
369test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
370 git reset --hard &&
371 git rm file &&
372 ln -s file2 file &&
373 git stash save "file to symlink (stage rm)" &&
374 test -f file &&
375 test bar = "$(cat file)" &&
376 git stash apply &&
377 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
378'
379
380test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
381 git reset --hard &&
382 rm file &&
383 ln -s file2 file &&
384 git add file &&
385 git stash save "file to symlink (full stage)" &&
386 test -f file &&
387 test bar = "$(cat file)" &&
388 git stash apply &&
389 case "$(ls -l file)" in *" file -> file2") :;; *) false;; esac
390'
391
392# This test creates a commit with a symlink used for the following tests
393
Johannes Sixt889c6f02013-06-07 22:53:28 +0200394test_expect_success 'stash symlink to file' '
Charles Bailey2ba2fe22010-04-18 19:27:49 +0100395 git reset --hard &&
Johannes Sixt889c6f02013-06-07 22:53:28 +0200396 test_ln_s_add file filelink &&
Charles Bailey2ba2fe22010-04-18 19:27:49 +0100397 git commit -m "Add symlink" &&
398 rm filelink &&
399 cp file filelink &&
Johannes Sixt889c6f02013-06-07 22:53:28 +0200400 git stash save "symlink to file"
401'
402
403test_expect_success SYMLINKS 'this must have re-created the symlink' '
Charles Bailey2ba2fe22010-04-18 19:27:49 +0100404 test -h filelink &&
Johannes Sixt889c6f02013-06-07 22:53:28 +0200405 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
406'
407
408test_expect_success 'unstash must re-create the file' '
Charles Bailey2ba2fe22010-04-18 19:27:49 +0100409 git stash apply &&
410 ! test -h filelink &&
411 test bar = "$(cat file)"
412'
413
Johannes Sixt889c6f02013-06-07 22:53:28 +0200414test_expect_success 'stash symlink to file (stage rm)' '
Charles Bailey2ba2fe22010-04-18 19:27:49 +0100415 git reset --hard &&
416 git rm filelink &&
417 cp file filelink &&
Johannes Sixt889c6f02013-06-07 22:53:28 +0200418 git stash save "symlink to file (stage rm)"
419'
420
421test_expect_success SYMLINKS 'this must have re-created the symlink' '
Charles Bailey2ba2fe22010-04-18 19:27:49 +0100422 test -h filelink &&
Johannes Sixt889c6f02013-06-07 22:53:28 +0200423 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
424'
425
426test_expect_success 'unstash must re-create the file' '
Charles Bailey2ba2fe22010-04-18 19:27:49 +0100427 git stash apply &&
428 ! test -h filelink &&
429 test bar = "$(cat file)"
430'
431
Johannes Sixt889c6f02013-06-07 22:53:28 +0200432test_expect_success 'stash symlink to file (full stage)' '
Charles Bailey2ba2fe22010-04-18 19:27:49 +0100433 git reset --hard &&
434 rm filelink &&
435 cp file filelink &&
436 git add filelink &&
Johannes Sixt889c6f02013-06-07 22:53:28 +0200437 git stash save "symlink to file (full stage)"
438'
439
440test_expect_success SYMLINKS 'this must have re-created the symlink' '
Charles Bailey2ba2fe22010-04-18 19:27:49 +0100441 test -h filelink &&
Johannes Sixt889c6f02013-06-07 22:53:28 +0200442 case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
443'
444
445test_expect_success 'unstash must re-create the file' '
Charles Bailey2ba2fe22010-04-18 19:27:49 +0100446 git stash apply &&
447 ! test -h filelink &&
448 test bar = "$(cat file)"
449'
450
451test_expect_failure 'stash directory to file' '
452 git reset --hard &&
453 mkdir dir &&
454 echo foo >dir/file &&
455 git add dir/file &&
456 git commit -m "Add file in dir" &&
457 rm -fr dir &&
458 echo bar >dir &&
459 git stash save "directory to file" &&
460 test -d dir &&
461 test foo = "$(cat dir/file)" &&
462 test_must_fail git stash apply &&
463 test bar = "$(cat dir)" &&
464 git reset --soft HEAD^
465'
466
467test_expect_failure 'stash file to directory' '
468 git reset --hard &&
469 rm file &&
470 mkdir file &&
471 echo foo >file/file &&
472 git stash save "file to directory" &&
473 test -f file &&
474 test bar = "$(cat file)" &&
475 git stash apply &&
476 test -f file/file &&
477 test foo = "$(cat file/file)"
478'
479
Joel Teichroeb93415f52019-02-25 23:16:09 +0000480test_expect_success 'giving too many ref arguments does not modify files' '
481 git stash clear &&
482 test_when_finished "git reset --hard HEAD" &&
483 echo foo >file2 &&
484 git stash &&
485 echo bar >file2 &&
486 git stash &&
487 test-tool chmtime =123456789 file2 &&
488 for type in apply pop "branch stash-branch"
489 do
490 test_must_fail git stash $type stash@{0} stash@{1} 2>err &&
491 test_i18ngrep "Too many revisions" err &&
492 test 123456789 = $(test-tool chmtime -g file2) || return 1
493 done
494'
495
496test_expect_success 'drop: too many arguments errors out (does nothing)' '
497 git stash list >expect &&
498 test_must_fail git stash drop stash@{0} stash@{1} 2>err &&
499 test_i18ngrep "Too many revisions" err &&
500 git stash list >actual &&
501 test_cmp expect actual
502'
503
504test_expect_success 'show: too many arguments errors out (does nothing)' '
505 test_must_fail git stash show stash@{0} stash@{1} 2>err 1>out &&
506 test_i18ngrep "Too many revisions" err &&
507 test_must_be_empty out
508'
509
Joel Teichroebc95bc222017-08-19 13:13:24 -0700510test_expect_success 'stash create - no changes' '
511 git stash clear &&
512 test_when_finished "git reset --hard HEAD" &&
513 git reset --hard &&
514 git stash create >actual &&
515 test_must_be_empty actual
516'
517
Jon Seymourdaf7a0c2010-08-21 14:09:03 +1000518test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
519 git stash clear &&
520 test_when_finished "git reset --hard HEAD" &&
521 git reset --hard &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000522 echo foo >>file &&
Jon Seymourdaf7a0c2010-08-21 14:09:03 +1000523 STASH_ID=$(git stash create) &&
524 git reset --hard &&
525 git stash branch stash-branch ${STASH_ID} &&
Johannes Schindelincbc75a12020-11-18 23:44:26 +0000526 test_when_finished "git reset --hard HEAD && git checkout main &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000527 git branch -D stash-branch" &&
Jon Seymourdaf7a0c2010-08-21 14:09:03 +1000528 test $(git ls-files --modified | wc -l) -eq 1
529'
530
531test_expect_success 'stash branch - stashes on stack, stash-like argument' '
532 git stash clear &&
533 test_when_finished "git reset --hard HEAD" &&
534 git reset --hard &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000535 echo foo >>file &&
Jon Seymourdaf7a0c2010-08-21 14:09:03 +1000536 git stash &&
537 test_when_finished "git stash drop" &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000538 echo bar >>file &&
Jon Seymourdaf7a0c2010-08-21 14:09:03 +1000539 STASH_ID=$(git stash create) &&
540 git reset --hard &&
541 git stash branch stash-branch ${STASH_ID} &&
Johannes Schindelincbc75a12020-11-18 23:44:26 +0000542 test_when_finished "git reset --hard HEAD && git checkout main &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000543 git branch -D stash-branch" &&
Jon Seymourdaf7a0c2010-08-21 14:09:03 +1000544 test $(git ls-files --modified | wc -l) -eq 1
545'
546
Joel Teichroeb93415f52019-02-25 23:16:09 +0000547test_expect_success 'stash branch complains with no arguments' '
548 test_must_fail git stash branch 2>err &&
549 test_i18ngrep "No branch name specified" err
550'
551
Jonathan Nieder11452112012-03-13 00:01:32 -0500552test_expect_success 'stash show format defaults to --stat' '
Jon Seymourdaf7a0c2010-08-21 14:09:03 +1000553 git stash clear &&
554 test_when_finished "git reset --hard HEAD" &&
555 git reset --hard &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000556 echo foo >>file &&
Jon Seymourdaf7a0c2010-08-21 14:09:03 +1000557 git stash &&
558 test_when_finished "git stash drop" &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000559 echo bar >>file &&
Jon Seymourdaf7a0c2010-08-21 14:09:03 +1000560 STASH_ID=$(git stash create) &&
561 git reset --hard &&
Brandon Casey3fcb8872010-09-24 15:40:38 -0500562 cat >expected <<-EOF &&
Zbigniew Jędrzejewski-Szmekdc801e72012-04-30 22:38:58 +0200563 file | 1 +
Nguyễn Thái Ngọc Duy7f814632012-02-01 19:55:07 +0700564 1 file changed, 1 insertion(+)
Brandon Casey3fcb8872010-09-24 15:40:38 -0500565 EOF
566 git stash show ${STASH_ID} >actual &&
Ævar Arnfjörð Bjarmason1108cea2021-02-11 02:53:53 +0100567 test_cmp expected actual
Jon Seymourdaf7a0c2010-08-21 14:09:03 +1000568'
Brandon Casey3fcb8872010-09-24 15:40:38 -0500569
Jonathan Nieder11452112012-03-13 00:01:32 -0500570test_expect_success 'stash show - stashes on stack, stash-like argument' '
571 git stash clear &&
572 test_when_finished "git reset --hard HEAD" &&
573 git reset --hard &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000574 echo foo >>file &&
Jonathan Nieder11452112012-03-13 00:01:32 -0500575 git stash &&
576 test_when_finished "git stash drop" &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000577 echo bar >>file &&
Jonathan Nieder11452112012-03-13 00:01:32 -0500578 STASH_ID=$(git stash create) &&
579 git reset --hard &&
580 echo "1 0 file" >expected &&
581 git stash show --numstat ${STASH_ID} >actual &&
582 test_cmp expected actual
583'
584
Brian Gernhardt9027fa92010-09-24 18:15:34 -0400585test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
Brandon Casey3fcb8872010-09-24 15:40:38 -0500586 git stash clear &&
587 test_when_finished "git reset --hard HEAD" &&
588 git reset --hard &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000589 echo foo >>file &&
Brandon Casey3fcb8872010-09-24 15:40:38 -0500590 git stash &&
591 test_when_finished "git stash drop" &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000592 echo bar >>file &&
Brandon Casey3fcb8872010-09-24 15:40:38 -0500593 STASH_ID=$(git stash create) &&
594 git reset --hard &&
595 cat >expected <<-EOF &&
596 diff --git a/file b/file
597 index 7601807..935fbd3 100644
598 --- a/file
599 +++ b/file
600 @@ -1 +1,2 @@
601 baz
602 +bar
603 EOF
604 git stash show -p ${STASH_ID} >actual &&
brian m. carlsonc7848152019-08-26 01:43:41 +0000605 diff_cmp expected actual
Brandon Casey3fcb8872010-09-24 15:40:38 -0500606'
607
Brian Gernhardt9027fa92010-09-24 18:15:34 -0400608test_expect_success 'stash show - no stashes on stack, stash-like argument' '
Jon Seymourdaf7a0c2010-08-21 14:09:03 +1000609 git stash clear &&
610 test_when_finished "git reset --hard HEAD" &&
611 git reset --hard &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000612 echo foo >>file &&
Jon Seymourdaf7a0c2010-08-21 14:09:03 +1000613 STASH_ID=$(git stash create) &&
614 git reset --hard &&
Jonathan Nieder11452112012-03-13 00:01:32 -0500615 echo "1 0 file" >expected &&
616 git stash show --numstat ${STASH_ID} >actual &&
617 test_cmp expected actual
Brandon Casey3fcb8872010-09-24 15:40:38 -0500618'
619
Brian Gernhardt9027fa92010-09-24 18:15:34 -0400620test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
Brandon Casey3fcb8872010-09-24 15:40:38 -0500621 git stash clear &&
622 test_when_finished "git reset --hard HEAD" &&
623 git reset --hard &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000624 echo foo >>file &&
Brandon Casey3fcb8872010-09-24 15:40:38 -0500625 STASH_ID=$(git stash create) &&
626 git reset --hard &&
627 cat >expected <<-EOF &&
628 diff --git a/file b/file
629 index 7601807..71b52c4 100644
630 --- a/file
631 +++ b/file
632 @@ -1 +1,2 @@
633 baz
634 +foo
635 EOF
636 git stash show -p ${STASH_ID} >actual &&
brian m. carlsonc7848152019-08-26 01:43:41 +0000637 diff_cmp expected actual
Jon Seymourdaf7a0c2010-08-21 14:09:03 +1000638'
639
Thomas Gummerer8e407bc2019-03-20 22:49:55 +0000640test_expect_success 'stash show --patience shows diff' '
641 git reset --hard &&
642 echo foo >>file &&
643 STASH_ID=$(git stash create) &&
644 git reset --hard &&
645 cat >expected <<-EOF &&
646 diff --git a/file b/file
647 index 7601807..71b52c4 100644
648 --- a/file
649 +++ b/file
650 @@ -1 +1,2 @@
651 baz
652 +foo
653 EOF
654 git stash show --patience ${STASH_ID} >actual &&
brian m. carlsonc7848152019-08-26 01:43:41 +0000655 diff_cmp expected actual
Thomas Gummerer8e407bc2019-03-20 22:49:55 +0000656'
657
Paul-Sebastian Ungureanuab8ad462019-02-25 23:16:12 +0000658test_expect_success 'drop: fail early if specified stash is not a stash ref' '
Jon Seymourdaf7a0c2010-08-21 14:09:03 +1000659 git stash clear &&
660 test_when_finished "git reset --hard HEAD && git stash clear" &&
661 git reset --hard &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000662 echo foo >file &&
Jon Seymourdaf7a0c2010-08-21 14:09:03 +1000663 git stash &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000664 echo bar >file &&
Jon Seymourdaf7a0c2010-08-21 14:09:03 +1000665 git stash &&
Jon Seymour8d66bb02010-09-01 00:49:20 +1000666 test_must_fail git stash drop $(git rev-parse stash@{0}) &&
Jon Seymourdaf7a0c2010-08-21 14:09:03 +1000667 git stash pop &&
668 test bar = "$(cat file)" &&
669 git reset --hard HEAD
670'
671
Paul-Sebastian Ungureanuab8ad462019-02-25 23:16:12 +0000672test_expect_success 'pop: fail early if specified stash is not a stash ref' '
Jon Seymourdaf7a0c2010-08-21 14:09:03 +1000673 git stash clear &&
674 test_when_finished "git reset --hard HEAD && git stash clear" &&
675 git reset --hard &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000676 echo foo >file &&
Jon Seymourdaf7a0c2010-08-21 14:09:03 +1000677 git stash &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000678 echo bar >file &&
Jon Seymourdaf7a0c2010-08-21 14:09:03 +1000679 git stash &&
Jon Seymour8d66bb02010-09-01 00:49:20 +1000680 test_must_fail git stash pop $(git rev-parse stash@{0}) &&
Jon Seymourdaf7a0c2010-08-21 14:09:03 +1000681 git stash pop &&
682 test bar = "$(cat file)" &&
683 git reset --hard HEAD
684'
685
Dmitry Ivankov7be8b3b2011-06-16 19:42:48 +0600686test_expect_success 'ref with non-existent reflog' '
Jon Seymourdaf7a0c2010-08-21 14:09:03 +1000687 git stash clear &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000688 echo bar5 >file &&
689 echo bar6 >file2 &&
Jon Seymourdaf7a0c2010-08-21 14:09:03 +1000690 git add file2 &&
691 git stash &&
Thomas Rast1ae96442011-08-30 02:06:07 +0200692 test_must_fail git rev-parse --quiet --verify does-not-exist &&
Jon Seymour8d66bb02010-09-01 00:49:20 +1000693 test_must_fail git stash drop does-not-exist &&
694 test_must_fail git stash drop does-not-exist@{0} &&
695 test_must_fail git stash pop does-not-exist &&
696 test_must_fail git stash pop does-not-exist@{0} &&
697 test_must_fail git stash apply does-not-exist &&
698 test_must_fail git stash apply does-not-exist@{0} &&
699 test_must_fail git stash show does-not-exist &&
700 test_must_fail git stash show does-not-exist@{0} &&
701 test_must_fail git stash branch tmp does-not-exist &&
702 test_must_fail git stash branch tmp does-not-exist@{0} &&
Jon Seymourdaf7a0c2010-08-21 14:09:03 +1000703 git stash drop
704'
705
706test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
707 git stash clear &&
Jon Seymour8d66bb02010-09-01 00:49:20 +1000708 test_must_fail git stash drop stash@{0} &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000709 echo bar5 >file &&
710 echo bar6 >file2 &&
Jon Seymourdaf7a0c2010-08-21 14:09:03 +1000711 git add file2 &&
712 git stash &&
Jon Seymour9355fc92011-04-06 09:21:13 +1000713 test_must_fail git stash drop stash@{1} &&
714 test_must_fail git stash pop stash@{1} &&
715 test_must_fail git stash apply stash@{1} &&
716 test_must_fail git stash show stash@{1} &&
717 test_must_fail git stash branch tmp stash@{1} &&
Jon Seymourdaf7a0c2010-08-21 14:09:03 +1000718 git stash drop
719'
720
Aaron M Watsona56c8f52016-10-24 19:40:13 -0400721test_expect_success 'invalid ref of the form "n", n >= N' '
722 git stash clear &&
723 test_must_fail git stash drop 0 &&
724 echo bar5 >file &&
725 echo bar6 >file2 &&
726 git add file2 &&
727 git stash &&
728 test_must_fail git stash drop 1 &&
729 test_must_fail git stash pop 1 &&
730 test_must_fail git stash apply 1 &&
731 test_must_fail git stash show 1 &&
732 test_must_fail git stash branch tmp 1 &&
733 git stash drop
734'
735
Thomas Gummerer63b50c82019-06-15 12:26:18 +0100736test_expect_success 'valid ref of the form "n", n < N' '
737 git stash clear &&
738 echo bar5 >file &&
739 echo bar6 >file2 &&
740 git add file2 &&
741 git stash &&
742 git stash show 0 &&
743 git stash branch tmp 0 &&
Johannes Schindelincbc75a12020-11-18 23:44:26 +0000744 git checkout main &&
Thomas Gummerer63b50c82019-06-15 12:26:18 +0100745 git stash &&
746 git stash apply 0 &&
747 git reset --hard &&
748 git stash pop 0 &&
749 git stash &&
750 git stash drop 0 &&
751 test_must_fail git stash drop
752'
753
Paul-Sebastian Ungureanuab8ad462019-02-25 23:16:12 +0000754test_expect_success 'branch: do not drop the stash if the branch exists' '
Tomas Carnecky835d6a12010-09-28 23:19:51 +1000755 git stash clear &&
756 echo foo >file &&
757 git add file &&
758 git commit -m initial &&
759 echo bar >file &&
760 git stash &&
Johannes Schindelincbc75a12020-11-18 23:44:26 +0000761 test_must_fail git stash branch main stash@{0} &&
Tomas Carnecky835d6a12010-09-28 23:19:51 +1000762 git rev-parse stash@{0} --
763'
764
Paul-Sebastian Ungureanuab8ad462019-02-25 23:16:12 +0000765test_expect_success 'branch: should not drop the stash if the apply fails' '
Joel Teichroebb04e6912017-08-19 13:13:25 -0700766 git stash clear &&
767 git reset HEAD~1 --hard &&
768 echo foo >file &&
769 git add file &&
770 git commit -m initial &&
771 echo bar >file &&
772 git stash &&
773 echo baz >file &&
Johannes Schindelincbc75a12020-11-18 23:44:26 +0000774 test_when_finished "git checkout main" &&
Joel Teichroebb04e6912017-08-19 13:13:25 -0700775 test_must_fail git stash branch new_branch stash@{0} &&
776 git rev-parse stash@{0} --
777'
778
Paul-Sebastian Ungureanuab8ad462019-02-25 23:16:12 +0000779test_expect_success 'apply: show same status as git status (relative to ./)' '
Piotr Krukowiecki6eaf92f2011-03-14 20:19:36 +0100780 git stash clear &&
781 echo 1 >subdir/subfile1 &&
782 echo 2 >subdir/subfile2 &&
783 git add subdir/subfile1 &&
784 git commit -m subdir &&
785 (
786 cd subdir &&
787 echo x >subfile1 &&
788 echo x >../file &&
789 git status >../expect &&
790 git stash &&
791 sane_unset GIT_MERGE_VERBOSITY &&
792 git stash apply
793 ) |
Thomas Gummerer1790f4f2017-03-21 22:12:17 +0000794 sed -e 1d >actual && # drop "Saved..."
Ævar Arnfjörð Bjarmason1108cea2021-02-11 02:53:53 +0100795 test_cmp expect actual
Piotr Krukowiecki6eaf92f2011-03-14 20:19:36 +0100796'
797
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000798cat >expect <<EOF
Jonathon Mah44df2e22011-12-30 16:14:01 -0800799diff --git a/HEAD b/HEAD
800new file mode 100644
801index 0000000..fe0cbee
802--- /dev/null
803+++ b/HEAD
804@@ -0,0 +1 @@
805+file-not-a-ref
806EOF
807
808test_expect_success 'stash where working directory contains "HEAD" file' '
809 git stash clear &&
810 git reset --hard &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000811 echo file-not-a-ref >HEAD &&
Jonathon Mah44df2e22011-12-30 16:14:01 -0800812 git add HEAD &&
813 test_tick &&
814 git stash &&
815 git diff-files --quiet &&
816 git diff-index --cached --quiet HEAD &&
817 test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
Paul-Sebastian Ungureanu6a0b88b2019-02-25 23:16:10 +0000818 git diff stash^..stash >output &&
brian m. carlsonc7848152019-08-26 01:43:41 +0000819 diff_cmp expect output
Jonathon Mah44df2e22011-12-30 16:14:01 -0800820'
821
Ramkumar Ramachandrabd514ca2013-06-15 18:43:25 +0530822test_expect_success 'store called with invalid commit' '
823 test_must_fail git stash store foo
824'
825
826test_expect_success 'store updates stash ref and reflog' '
827 git stash clear &&
828 git reset --hard &&
829 echo quux >bazzy &&
830 git add bazzy &&
831 STASH_ID=$(git stash create) &&
832 git reset --hard &&
SZEDER Gábor79b04f92018-08-22 20:13:19 +0200833 test_path_is_missing bazzy &&
Ramkumar Ramachandrabd514ca2013-06-15 18:43:25 +0530834 git stash store -m quuxery $STASH_ID &&
David Turnercbc5cf72018-05-23 07:25:17 +0200835 test $(git rev-parse stash) = $STASH_ID &&
David Turnerd0ab0582015-07-27 18:57:08 -0400836 git reflog --format=%H stash| grep $STASH_ID &&
Ramkumar Ramachandrabd514ca2013-06-15 18:43:25 +0530837 git stash pop &&
838 grep quux bazzy
839'
840
Øystein Walle2a07e432014-01-07 09:22:15 +0100841test_expect_success 'handle stash specification with spaces' '
842 git stash clear &&
843 echo pig >file &&
844 git stash &&
845 stamp=$(git log -g --format="%cd" -1 refs/stash) &&
846 test_tick &&
847 echo cow >file &&
848 git stash &&
849 git stash apply "stash@{$stamp}" &&
850 grep pig file
851'
852
Jeff King288c67c2014-08-06 14:35:25 -0400853test_expect_success 'setup stash with index and worktree changes' '
854 git stash clear &&
855 git reset --hard &&
856 echo index >file &&
857 git add file &&
858 echo working >file &&
859 git stash
860'
861
862test_expect_success 'stash list implies --first-parent -m' '
Jeff Kingf2f3fc92015-04-22 15:30:52 -0400863 cat >expect <<-EOF &&
864 stash@{0}
Jeff King288c67c2014-08-06 14:35:25 -0400865
866 diff --git a/file b/file
867 index 257cc56..d26b33d 100644
868 --- a/file
869 +++ b/file
870 @@ -1 +1 @@
871 -foo
872 +working
873 EOF
Jeff Kingf2f3fc92015-04-22 15:30:52 -0400874 git stash list --format=%gd -p >actual &&
brian m. carlsonc7848152019-08-26 01:43:41 +0000875 diff_cmp expect actual
Jeff King288c67c2014-08-06 14:35:25 -0400876'
877
878test_expect_success 'stash list --cc shows combined diff' '
879 cat >expect <<-\EOF &&
Jeff Kingf2f3fc92015-04-22 15:30:52 -0400880 stash@{0}
Jeff King288c67c2014-08-06 14:35:25 -0400881
882 diff --cc file
883 index 257cc56,9015a7a..d26b33d
884 --- a/file
885 +++ b/file
886 @@@ -1,1 -1,1 +1,1 @@@
887 - foo
888 -index
889 ++working
890 EOF
Jeff Kingf2f3fc92015-04-22 15:30:52 -0400891 git stash list --format=%gd -p --cc >actual &&
brian m. carlsonc7848152019-08-26 01:43:41 +0000892 diff_cmp expect actual
Jeff King288c67c2014-08-06 14:35:25 -0400893'
894
Jeff King9d4e28e2016-12-06 15:25:21 -0500895test_expect_success 'stash is not confused by partial renames' '
896 mv file renamed &&
897 git add renamed &&
898 git stash &&
899 git stash apply &&
900 test_path_is_file renamed &&
901 test_path_is_missing file
902'
903
Thomas Gummererf5727e22017-02-19 11:03:08 +0000904test_expect_success 'push -m shows right message' '
905 >foo &&
906 git add foo &&
907 git stash push -m "test message" &&
Johannes Schindelincbc75a12020-11-18 23:44:26 +0000908 echo "stash@{0}: On main: test message" >expect &&
Thomas Gummererf5727e22017-02-19 11:03:08 +0000909 git stash list -1 >actual &&
910 test_cmp expect actual
911'
912
Phil Hord56754732017-11-22 13:20:30 -0800913test_expect_success 'push -m also works without space' '
914 >foo &&
915 git add foo &&
916 git stash push -m"unspaced test message" &&
Johannes Schindelincbc75a12020-11-18 23:44:26 +0000917 echo "stash@{0}: On main: unspaced test message" >expect &&
Phil Hord56754732017-11-22 13:20:30 -0800918 git stash list -1 >actual &&
919 test_cmp expect actual
920'
921
922test_expect_success 'store -m foo shows right message' '
923 git stash clear &&
924 git reset --hard &&
925 echo quux >bazzy &&
926 git add bazzy &&
927 STASH_ID=$(git stash create) &&
928 git stash store -m "store m" $STASH_ID &&
929 echo "stash@{0}: store m" >expect &&
930 git stash list -1 >actual &&
931 test_cmp expect actual
932'
933
934test_expect_success 'store -mfoo shows right message' '
935 git stash clear &&
936 git reset --hard &&
937 echo quux >bazzy &&
938 git add bazzy &&
939 STASH_ID=$(git stash create) &&
940 git stash store -m"store mfoo" $STASH_ID &&
941 echo "stash@{0}: store mfoo" >expect &&
942 git stash list -1 >actual &&
943 test_cmp expect actual
944'
945
946test_expect_success 'store --message=foo shows right message' '
947 git stash clear &&
948 git reset --hard &&
949 echo quux >bazzy &&
950 git add bazzy &&
951 STASH_ID=$(git stash create) &&
952 git stash store --message="store message=foo" $STASH_ID &&
953 echo "stash@{0}: store message=foo" >expect &&
954 git stash list -1 >actual &&
955 test_cmp expect actual
956'
957
958test_expect_success 'store --message foo shows right message' '
959 git stash clear &&
960 git reset --hard &&
961 echo quux >bazzy &&
962 git add bazzy &&
963 STASH_ID=$(git stash create) &&
964 git stash store --message "store message foo" $STASH_ID &&
965 echo "stash@{0}: store message foo" >expect &&
966 git stash list -1 >actual &&
967 test_cmp expect actual
968'
969
970test_expect_success 'push -mfoo uses right message' '
971 >foo &&
972 git add foo &&
973 git stash push -m"test mfoo" &&
Johannes Schindelincbc75a12020-11-18 23:44:26 +0000974 echo "stash@{0}: On main: test mfoo" >expect &&
Phil Hord56754732017-11-22 13:20:30 -0800975 git stash list -1 >actual &&
976 test_cmp expect actual
977'
978
979test_expect_success 'push --message foo is synonym for -mfoo' '
980 >foo &&
981 git add foo &&
982 git stash push --message "test message foo" &&
Johannes Schindelincbc75a12020-11-18 23:44:26 +0000983 echo "stash@{0}: On main: test message foo" >expect &&
Phil Hord56754732017-11-22 13:20:30 -0800984 git stash list -1 >actual &&
985 test_cmp expect actual
986'
987
988test_expect_success 'push --message=foo is synonym for -mfoo' '
989 >foo &&
990 git add foo &&
991 git stash push --message="test message=foo" &&
Johannes Schindelincbc75a12020-11-18 23:44:26 +0000992 echo "stash@{0}: On main: test message=foo" >expect &&
Phil Hord56754732017-11-22 13:20:30 -0800993 git stash list -1 >actual &&
994 test_cmp expect actual
995'
996
997test_expect_success 'push -m shows right message' '
998 >foo &&
999 git add foo &&
1000 git stash push -m "test m foo" &&
Johannes Schindelincbc75a12020-11-18 23:44:26 +00001001 echo "stash@{0}: On main: test m foo" >expect &&
Phil Hord56754732017-11-22 13:20:30 -08001002 git stash list -1 >actual &&
1003 test_cmp expect actual
1004'
1005
Thomas Gummerer6f5ccd42017-02-19 11:03:09 +00001006test_expect_success 'create stores correct message' '
1007 >foo &&
1008 git add foo &&
1009 STASH_ID=$(git stash create "create test message") &&
Johannes Schindelincbc75a12020-11-18 23:44:26 +00001010 echo "On main: create test message" >expect &&
Thomas Gummerer6f5ccd42017-02-19 11:03:09 +00001011 git show --pretty=%s -s ${STASH_ID} >actual &&
1012 test_cmp expect actual
1013'
1014
1015test_expect_success 'create with multiple arguments for the message' '
1016 >foo &&
1017 git add foo &&
1018 STASH_ID=$(git stash create test untracked) &&
Johannes Schindelincbc75a12020-11-18 23:44:26 +00001019 echo "On main: test untracked" >expect &&
Thomas Gummerer6f5ccd42017-02-19 11:03:09 +00001020 git show --pretty=%s -s ${STASH_ID} >actual &&
1021 test_cmp expect actual
1022'
1023
Joel Teichroeb4e9bf3d2017-08-19 13:13:26 -07001024test_expect_success 'create in a detached state' '
Johannes Schindelincbc75a12020-11-18 23:44:26 +00001025 test_when_finished "git checkout main" &&
Joel Teichroeb4e9bf3d2017-08-19 13:13:26 -07001026 git checkout HEAD~1 &&
1027 >foo &&
1028 git add foo &&
1029 STASH_ID=$(git stash create) &&
1030 HEAD_ID=$(git rev-parse --short HEAD) &&
1031 echo "WIP on (no branch): ${HEAD_ID} initial" >expect &&
1032 git show --pretty=%s -s ${STASH_ID} >actual &&
1033 test_cmp expect actual
1034'
1035
Thomas Gummererdf6bba02017-02-28 20:33:38 +00001036test_expect_success 'stash -- <pathspec> stashes and restores the file' '
1037 >foo &&
1038 >bar &&
1039 git add foo bar &&
1040 git stash push -- foo &&
1041 test_path_is_file bar &&
1042 test_path_is_missing foo &&
1043 git stash pop &&
1044 test_path_is_file foo &&
1045 test_path_is_file bar
1046'
1047
Patrick Steinhardt22fc7032017-06-13 13:38:34 +02001048test_expect_success 'stash -- <pathspec> stashes in subdirectory' '
1049 mkdir sub &&
1050 >foo &&
1051 >bar &&
1052 git add foo bar &&
1053 (
1054 cd sub &&
1055 git stash push -- ../foo
1056 ) &&
1057 test_path_is_file bar &&
1058 test_path_is_missing foo &&
1059 git stash pop &&
1060 test_path_is_file foo &&
1061 test_path_is_file bar
1062'
1063
Thomas Gummererdf6bba02017-02-28 20:33:38 +00001064test_expect_success 'stash with multiple pathspec arguments' '
1065 >foo &&
1066 >bar &&
1067 >extra &&
1068 git add foo bar extra &&
1069 git stash push -- foo bar &&
1070 test_path_is_missing bar &&
1071 test_path_is_missing foo &&
1072 test_path_is_file extra &&
1073 git stash pop &&
1074 test_path_is_file foo &&
1075 test_path_is_file bar &&
1076 test_path_is_file extra
1077'
1078
1079test_expect_success 'stash with file including $IFS character' '
1080 >"foo bar" &&
1081 >foo &&
1082 >bar &&
1083 git add foo* &&
1084 git stash push -- "foo b*" &&
1085 test_path_is_missing "foo bar" &&
1086 test_path_is_file foo &&
1087 test_path_is_file bar &&
1088 git stash pop &&
1089 test_path_is_file "foo bar" &&
1090 test_path_is_file foo &&
1091 test_path_is_file bar
1092'
1093
1094test_expect_success 'stash with pathspec matching multiple paths' '
1095 echo original >file &&
1096 echo original >other-file &&
1097 git commit -m "two" file other-file &&
1098 echo modified >file &&
1099 echo modified >other-file &&
1100 git stash push -- "*file" &&
1101 echo original >expect &&
1102 test_cmp expect file &&
1103 test_cmp expect other-file &&
1104 git stash pop &&
1105 echo modified >expect &&
1106 test_cmp expect file &&
1107 test_cmp expect other-file
1108'
1109
1110test_expect_success 'stash push -p with pathspec shows no changes only once' '
1111 >foo &&
1112 git add foo &&
1113 git commit -m "tmp" &&
1114 git stash push -p foo >actual &&
1115 echo "No local changes to save" >expect &&
1116 git reset --hard HEAD~ &&
Ævar Arnfjörð Bjarmason1108cea2021-02-11 02:53:53 +01001117 test_cmp expect actual
Thomas Gummererdf6bba02017-02-28 20:33:38 +00001118'
1119
Paul-Sebastian Ungureanuab8ad462019-02-25 23:16:12 +00001120test_expect_success 'push <pathspec>: show no changes when there are none' '
Thomas Gummererdf6bba02017-02-28 20:33:38 +00001121 >foo &&
1122 git add foo &&
1123 git commit -m "tmp" &&
1124 git stash push foo >actual &&
1125 echo "No local changes to save" >expect &&
1126 git reset --hard HEAD~ &&
Ævar Arnfjörð Bjarmason1108cea2021-02-11 02:53:53 +01001127 test_cmp expect actual
Thomas Gummererdf6bba02017-02-28 20:33:38 +00001128'
1129
Paul-Sebastian Ungureanuab8ad462019-02-25 23:16:12 +00001130test_expect_success 'push: <pathspec> not in the repository errors out' '
Thomas Gummererdf6bba02017-02-28 20:33:38 +00001131 >untracked &&
1132 test_must_fail git stash push untracked &&
1133 test_path_is_file untracked
1134'
1135
Paul-Sebastian Ungureanu1ac528c2019-02-25 23:16:24 +00001136test_expect_success 'push: -q is quiet with changes' '
1137 >foo &&
1138 git add foo &&
1139 git stash push -q >output 2>&1 &&
1140 test_must_be_empty output
1141'
1142
1143test_expect_success 'push: -q is quiet with no changes' '
1144 git stash push -q >output 2>&1 &&
1145 test_must_be_empty output
1146'
1147
1148test_expect_success 'push: -q is quiet even if there is no initial commit' '
1149 git init foo_dir &&
1150 test_when_finished rm -rf foo_dir &&
1151 (
1152 cd foo_dir &&
1153 >bar &&
1154 test_must_fail git stash push -q >output 2>&1 &&
1155 test_must_be_empty output
1156 )
1157'
1158
Thomas Gummererdf6bba02017-02-28 20:33:38 +00001159test_expect_success 'untracked files are left in place when -u is not given' '
1160 >file &&
1161 git add file &&
1162 >untracked &&
1163 git stash push file &&
1164 test_path_is_file untracked
1165'
1166
Thomas Gummerer9e140902017-02-28 20:33:40 +00001167test_expect_success 'stash without verb with pathspec' '
1168 >"foo bar" &&
1169 >foo &&
1170 >bar &&
1171 git add foo* &&
1172 git stash -- "foo b*" &&
1173 test_path_is_missing "foo bar" &&
1174 test_path_is_file foo &&
1175 test_path_is_file bar &&
1176 git stash pop &&
1177 test_path_is_file "foo bar" &&
1178 test_path_is_file foo &&
1179 test_path_is_file bar
1180'
1181
Thomas Gummerere0e7f992017-03-21 22:12:19 +00001182test_expect_success 'stash -k -- <pathspec> leaves unstaged files intact' '
1183 git reset &&
1184 >foo &&
1185 >bar &&
1186 git add foo bar &&
1187 git commit -m "test" &&
1188 echo "foo" >foo &&
1189 echo "bar" >bar &&
1190 git stash -k -- foo &&
1191 test "",bar = $(cat foo),$(cat bar) &&
1192 git stash pop &&
1193 test foo,bar = $(cat foo),$(cat bar)
1194'
1195
Thomas Gummererbba067d2018-01-06 00:24:20 +00001196test_expect_success 'stash -- <subdir> leaves untracked files in subdir intact' '
1197 git reset &&
1198 >subdir/untracked &&
1199 >subdir/tracked1 &&
1200 >subdir/tracked2 &&
1201 git add subdir/tracked* &&
1202 git stash -- subdir/ &&
1203 test_path_is_missing subdir/tracked1 &&
1204 test_path_is_missing subdir/tracked2 &&
1205 test_path_is_file subdir/untracked &&
1206 git stash pop &&
1207 test_path_is_file subdir/tracked1 &&
1208 test_path_is_file subdir/tracked2 &&
1209 test_path_is_file subdir/untracked
1210'
1211
1212test_expect_success 'stash -- <subdir> works with binary files' '
1213 git reset &&
1214 >subdir/untracked &&
1215 >subdir/tracked &&
1216 cp "$TEST_DIRECTORY"/test-binary-1.png subdir/tracked-binary &&
1217 git add subdir/tracked* &&
1218 git stash -- subdir/ &&
1219 test_path_is_missing subdir/tracked &&
1220 test_path_is_missing subdir/tracked-binary &&
1221 test_path_is_file subdir/untracked &&
1222 git stash pop &&
1223 test_path_is_file subdir/tracked &&
1224 test_path_is_file subdir/tracked-binary &&
1225 test_path_is_file subdir/untracked
1226'
1227
Thomas Gummerer06408972019-03-06 22:09:11 +00001228test_expect_success 'stash with user.name and user.email set works' '
1229 test_config user.name "A U Thor" &&
1230 test_config user.email "a.u@thor" &&
1231 git stash
1232'
1233
Slavica Djukic3bc21112018-11-18 14:44:07 +01001234test_expect_success 'stash works when user.name and user.email are not set' '
1235 git reset &&
1236 >1 &&
1237 git add 1 &&
1238 echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>" >expect &&
1239 git stash &&
1240 git show -s --format="%an <%ae>" refs/stash >actual &&
1241 test_cmp expect actual &&
1242 >2 &&
1243 git add 2 &&
1244 test_config user.useconfigonly true &&
1245 test_config stash.usebuiltin true &&
1246 (
1247 sane_unset GIT_AUTHOR_NAME &&
1248 sane_unset GIT_AUTHOR_EMAIL &&
1249 sane_unset GIT_COMMITTER_NAME &&
1250 sane_unset GIT_COMMITTER_EMAIL &&
1251 test_unconfig user.email &&
1252 test_unconfig user.name &&
1253 test_must_fail git commit -m "should fail" &&
1254 echo "git stash <git@stash>" >expect &&
1255 >2 &&
1256 git stash &&
1257 git show -s --format="%an <%ae>" refs/stash >actual &&
1258 test_cmp expect actual
1259 )
1260'
1261
Thomas Gummererb932f6a2019-07-16 15:23:22 +01001262test_expect_success 'stash --keep-index with file deleted in index does not resurrect it on disk' '
1263 test_commit to-remove to-remove &&
1264 git rm to-remove &&
1265 git stash --keep-index &&
1266 test_path_is_missing to-remove
1267'
1268
Thomas Gummerer34933d02019-09-11 19:20:27 +01001269test_expect_success 'stash apply should succeed with unmodified file' '
1270 echo base >file &&
1271 git add file &&
1272 git commit -m base &&
1273
1274 # now stash a modification
1275 echo modified >file &&
1276 git stash &&
1277
1278 # make the file stat dirty
1279 cp file other &&
1280 mv other file &&
1281
1282 git stash apply
1283'
1284
Johannes Schindelin4a58c3d2019-10-30 10:49:38 +00001285test_expect_success 'stash handles skip-worktree entries nicely' '
1286 test_commit A &&
1287 echo changed >A.t &&
1288 git add A.t &&
1289 git update-index --skip-worktree A.t &&
1290 rm A.t &&
1291 git stash &&
1292
1293 git rev-parse --verify refs/stash:A.t
1294'
1295
Thomas Gummerer8a2cd3f2020-03-03 17:46:13 +00001296test_expect_success 'stash -c stash.useBuiltin=false warning ' '
1297 expected="stash.useBuiltin support has been removed" &&
1298
1299 git -c stash.useBuiltin=false stash 2>err &&
1300 test_i18ngrep "$expected" err &&
1301 env GIT_TEST_STASH_USE_BUILTIN=false git stash 2>err &&
1302 test_i18ngrep "$expected" err &&
1303
1304 git -c stash.useBuiltin=true stash 2>err &&
1305 test_must_be_empty err &&
1306 env GIT_TEST_STASH_USE_BUILTIN=true git stash 2>err &&
1307 test_must_be_empty err
1308'
1309
Johannes Schindelin150937c2007-07-02 12:14:49 +01001310test_done