Skip to content

Commit bc9e739

Browse files
committed
stash: implement "stash create"
This subcommand creates a stash from the current state and writes out the resulting commit object ID to the standard output, without updating the stash ref nor resetting the tree. It is intended to be used by scripts to temporarily rewind the working tree to a clean state. Signed-off-by: Junio C Hamano <[email protected]>
1 parent d3392f7 commit bc9e739

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

git-stash.sh

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,13 @@ clear_stash () {
2525
fi
2626
}
2727

28-
save_stash () {
28+
create_stash () {
2929
stash_msg="$1"
3030

3131
if no_changes
3232
then
33-
echo >&2 'No local changes to save'
3433
exit 0
3534
fi
36-
test -f "$GIT_DIR/logs/$ref_stash" ||
37-
clear_stash || die "Cannot initialize stash"
38-
39-
# Make sure the reflog for stash is kept.
40-
: >>"$GIT_DIR/logs/$ref_stash"
4135

4236
# state of the base commit
4337
if b_commit=$(git rev-parse --verify HEAD)
@@ -84,7 +78,20 @@ save_stash () {
8478
w_commit=$(printf '%s\n' "$stash_msg" |
8579
git commit-tree $w_tree -p $b_commit -p $i_commit) ||
8680
die "Cannot record working tree state"
81+
}
8782

83+
save_stash () {
84+
stash_msg="$1"
85+
86+
if no_changes
87+
then
88+
echo >&2 'No local changes to save'
89+
exit 0
90+
fi
91+
test -f "$GIT_DIR/logs/$ref_stash" ||
92+
clear_stash || die "Cannot initialize stash"
93+
94+
create_stash "$stash_msg"
8895
git update-ref -m "$stash_msg" $ref_stash $w_commit ||
8996
die "Cannot save the current status"
9097
printf >&2 'Saved "%s"\n' "$stash_msg"
@@ -202,6 +209,13 @@ apply)
202209
clear)
203210
clear_stash
204211
;;
212+
create)
213+
if test $# -gt 0 && test "$1" = create
214+
then
215+
shift
216+
fi
217+
create_stash "$*" && echo "$w_commit"
218+
;;
205219
help | usage)
206220
usage
207221
;;

0 commit comments

Comments
 (0)