Skip to content

Commit fcdd0e9

Browse files
bebarinogitster
authored andcommitted
stash: teach quiet option
Teach stash pop, apply, save, and drop to be quiet when told. By using the quiet option (-q), these actions will be silent unless errors are encountered. Signed-off-by: Stephen Boyd <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0e987a1 commit fcdd0e9

File tree

3 files changed

+92
-24
lines changed

3 files changed

+92
-24
lines changed

Documentation/git-stash.txt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@ SYNOPSIS
99
--------
1010
[verse]
1111
'git stash' list [<options>]
12-
'git stash' ( show | drop ) [<stash>]
13-
'git stash' ( pop | apply ) [--index] [<stash>]
12+
'git stash' show [<stash>]
13+
'git stash' drop [-q|--quiet] [<stash>]
14+
'git stash' ( pop | apply ) [--index] [-q|--quiet] [<stash>]
1415
'git stash' branch <branchname> [<stash>]
15-
'git stash' [save [--keep-index] [<message>]]
16+
'git stash' [save [--keep-index] [-q|--quiet] [<message>]]
1617
'git stash' clear
1718
'git stash' create
1819

@@ -41,7 +42,7 @@ is also possible).
4142
OPTIONS
4243
-------
4344

44-
save [--keep-index] [<message>]::
45+
save [--keep-index] [-q|--quiet] [<message>]::
4546

4647
Save your local modifications to a new 'stash', and run `git reset
4748
--hard` to revert them. This is the default action when no
@@ -75,7 +76,7 @@ show [<stash>]::
7576
it will accept any format known to 'git-diff' (e.g., `git stash show
7677
-p stash@\{1}` to view the second most recent stash in patch form).
7778

78-
pop [<stash>]::
79+
pop [--index] [-q|--quiet] [<stash>]::
7980

8081
Remove a single stashed state from the stash list and apply it
8182
on top of the current working tree state, i.e., do the inverse
@@ -93,7 +94,7 @@ longer apply the changes as they were originally).
9394
+
9495
When no `<stash>` is given, `stash@\{0}` is assumed.
9596

96-
apply [--index] [<stash>]::
97+
apply [--index] [-q|--quiet] [<stash>]::
9798

9899
Like `pop`, but do not remove the state from the stash list.
99100

@@ -115,7 +116,7 @@ clear::
115116
Remove all the stashed states. Note that those states will then
116117
be subject to pruning, and may be difficult or impossible to recover.
117118

118-
drop [<stash>]::
119+
drop [-q|--quiet] [<stash>]::
119120

120121
Remove a single stashed state from the stash list. When no `<stash>`
121122
is given, it removes the latest one. i.e. `stash@\{0}`

git-stash.sh

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33

44
dashless=$(basename "$0" | sed -e 's/-/ /')
55
USAGE="list [<options>]
6-
or: $dashless ( show | drop ) [<stash>]
7-
or: $dashless ( pop | apply ) [--index] [<stash>]
6+
or: $dashless show [<stash>]
7+
or: $dashless drop [-q|--quiet] [<stash>]
8+
or: $dashless ( pop | apply ) [--index] [-q|--quiet] [<stash>]
89
or: $dashless branch <branchname> [<stash>]
9-
or: $dashless [save [--keep-index] [<message>]]
10+
or: $dashless [save [--keep-index] [-q|--quiet] [<message>]]
1011
or: $dashless clear"
1112

1213
SUBDIRECTORY_OK=Yes
@@ -94,18 +95,28 @@ create_stash () {
9495

9596
save_stash () {
9697
keep_index=
97-
case "$1" in
98-
--keep-index)
99-
keep_index=t
98+
while test $# != 0
99+
do
100+
case "$1" in
101+
--keep-index)
102+
keep_index=t
103+
;;
104+
-q|--quiet)
105+
GIT_QUIET=t
106+
;;
107+
*)
108+
break
109+
;;
110+
esac
100111
shift
101-
esac
112+
done
102113

103114
stash_msg="$*"
104115

105116
git update-index -q --refresh
106117
if no_changes
107118
then
108-
echo 'No local changes to save'
119+
say 'No local changes to save'
109120
exit 0
110121
fi
111122
test -f "$GIT_DIR/logs/$ref_stash" ||
@@ -118,9 +129,9 @@ save_stash () {
118129

119130
git update-ref -m "$stash_msg" $ref_stash $w_commit ||
120131
die "Cannot save the current status"
121-
printf 'Saved working directory and index state "%s"\n' "$stash_msg"
132+
say Saved working directory and index state "$stash_msg"
122133

123-
git reset --hard
134+
git reset --hard ${GIT_QUIET:+-q}
124135

125136
if test -n "$keep_index" && test -n $i_tree
126137
then
@@ -156,11 +167,22 @@ apply_stash () {
156167
die 'Cannot apply to a dirty working tree, please stage your changes'
157168

158169
unstash_index=
159-
case "$1" in
160-
--index)
161-
unstash_index=t
170+
171+
while test $# != 0
172+
do
173+
case "$1" in
174+
--index)
175+
unstash_index=t
176+
;;
177+
-q|--quiet)
178+
GIT_QUIET=t
179+
;;
180+
*)
181+
break
182+
;;
183+
esac
162184
shift
163-
esac
185+
done
164186

165187
# current index state
166188
c_tree=$(git write-tree) ||
@@ -193,6 +215,10 @@ apply_stash () {
193215
export GITHEAD_$w_tree GITHEAD_$c_tree GITHEAD_$b_tree
194216
"
195217

218+
if test -n "$GIT_QUIET"
219+
then
220+
export GIT_MERGE_VERBOSITY=0
221+
fi
196222
if git-merge-recursive $b_tree -- $c_tree $w_tree
197223
then
198224
# No conflict
@@ -207,7 +233,12 @@ apply_stash () {
207233
die "Cannot unstage modified files"
208234
rm -f "$a"
209235
fi
210-
git status || :
236+
squelch=
237+
if test -n "$GIT_QUIET"
238+
then
239+
squelch='>/dev/null 2>&1'
240+
fi
241+
eval "git status $squelch" || :
211242
else
212243
# Merge conflict; keep the exit status from merge-recursive
213244
status=$?
@@ -222,6 +253,19 @@ apply_stash () {
222253
drop_stash () {
223254
have_stash || die 'No stash entries to drop'
224255

256+
while test $# != 0
257+
do
258+
case "$1" in
259+
-q|--quiet)
260+
GIT_QUIET=t
261+
;;
262+
*)
263+
break
264+
;;
265+
esac
266+
shift
267+
done
268+
225269
if test $# = 0
226270
then
227271
set x "$ref_stash@{0}"
@@ -235,7 +279,7 @@ drop_stash () {
235279
die "$*: not a valid stashed state"
236280

237281
git reflog delete --updateref --rewrite "$@" &&
238-
echo "Dropped $* ($s)" || die "$*: Could not drop stash entry"
282+
say "Dropped $* ($s)" || die "$*: Could not drop stash entry"
239283

240284
# clear_stash if we just dropped the last stash entry
241285
git rev-parse --verify "$ref_stash@{0}" > /dev/null 2>&1 || clear_stash
@@ -312,7 +356,7 @@ branch)
312356
if test $# -eq 0
313357
then
314358
save_stash &&
315-
echo '(To restore them type "git stash apply")'
359+
say '(To restore them type "git stash apply")'
316360
else
317361
usage
318362
fi

t/t3903-stash.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,27 @@ test_expect_success 'stash branch' '
177177
test 0 = $(git stash list | wc -l)
178178
'
179179

180+
test_expect_success 'apply -q is quiet' '
181+
echo foo > file &&
182+
git stash &&
183+
git stash apply -q > output.out 2>&1 &&
184+
test ! -s output.out
185+
'
186+
187+
test_expect_success 'save -q is quiet' '
188+
git stash save --quiet > output.out 2>&1 &&
189+
test ! -s output.out
190+
'
191+
192+
test_expect_success 'pop -q is quiet' '
193+
git stash pop -q > output.out 2>&1 &&
194+
test ! -s output.out
195+
'
196+
197+
test_expect_success 'drop -q is quiet' '
198+
git stash &&
199+
git stash drop -q > output.out 2>&1 &&
200+
test ! -s output.out
201+
'
202+
180203
test_done

0 commit comments

Comments
 (0)