]> git.ipfire.org Git - thirdparty/git.git/blame - git-stash.sh
Add urls.txt to git-clone man page
[thirdparty/git.git] / git-stash.sh
CommitLineData
f2c66ed1
NS
1#!/bin/sh
2# Copyright (c) 2007, Nanako Shiraishi
3
4USAGE='[ | list | show | apply | clear]'
5
6. git-sh-setup
7require_work_tree
8
9TMP="$GIT_DIR/.git-stash.$$"
10trap 'rm -f "$TMP-*"' 0
11
12ref_stash=refs/stash
13
14no_changes () {
5be60078
JH
15 git diff-index --quiet --cached HEAD &&
16 git diff-files --quiet
f2c66ed1
NS
17}
18
19clear_stash () {
20 logfile="$GIT_DIR/logs/$ref_stash" &&
21 mkdir -p "$(dirname "$logfile")" &&
22 : >"$logfile"
23}
24
25save_stash () {
26 if no_changes
27 then
28 echo >&2 'No local changes to save'
29 exit 0
30 fi
31 test -f "$GIT_DIR/logs/$ref_stash" ||
32 clear_stash || die "Cannot initialize stash"
33
34 # state of the base commit
5be60078 35 if b_commit=$(git rev-parse --verify HEAD)
f2c66ed1 36 then
5be60078 37 head=$(git log --abbrev-commit --pretty=oneline -n 1 HEAD)
f2c66ed1
NS
38 else
39 die "You do not have the initial commit yet"
40 fi
41
5be60078 42 if branch=$(git symbolic-ref -q HEAD)
f2c66ed1
NS
43 then
44 branch=${branch#refs/heads/}
45 else
46 branch='(no branch)'
47 fi
48 msg=$(printf '%s: %s' "$branch" "$head")
49
50 # state of the index
5be60078 51 i_tree=$(git write-tree) &&
f2c66ed1 52 i_commit=$(printf 'index on %s' "$msg" |
5be60078 53 git commit-tree $i_tree -p $b_commit) ||
f2c66ed1
NS
54 die "Cannot save the current index state"
55
56 # state of the working tree
57 w_tree=$( (
58 GIT_INDEX_FILE="$TMP-index" &&
59 export GIT_INDEX_FILE &&
60
61 rm -f "$TMP-index" &&
5be60078
JH
62 git read-tree $i_tree &&
63 git add -u &&
64 git write-tree &&
f2c66ed1
NS
65 rm -f "$TMP-index"
66 ) ) ||
67 die "Cannot save the current worktree state"
68
69 # create the stash
843103d6 70 w_commit=$(printf 'WIP on %s\n' "$msg" |
5be60078 71 git commit-tree $w_tree -p $b_commit -p $i_commit) ||
f2c66ed1
NS
72 die "Cannot record working tree state"
73
e754e990 74 git update-ref -m "WIP on $msg" $ref_stash $w_commit ||
f2c66ed1
NS
75 die "Cannot save the current status"
76 printf >&2 'Saved WIP on %s\n' "$msg"
77}
78
401de405 79have_stash () {
5be60078 80 git rev-parse --verify $ref_stash >/dev/null 2>&1
401de405
JK
81}
82
f2c66ed1 83list_stash () {
401de405 84 have_stash || return 0
5be60078 85 git log --pretty=oneline -g "$@" $ref_stash |
f2c66ed1
NS
86 sed -n -e 's/^[.0-9a-f]* refs\///p'
87}
88
89show_stash () {
5be60078 90 flags=$(git rev-parse --no-revs --flags "$@")
f2c66ed1
NS
91 if test -z "$flags"
92 then
93 flags=--stat
94 fi
5be60078 95 s=$(git rev-parse --revs-only --no-flags --default $ref_stash "$@")
f2c66ed1 96
5be60078
JH
97 w_commit=$(git rev-parse --verify "$s") &&
98 b_commit=$(git rev-parse --verify "$s^") &&
99 git diff $flags $b_commit $w_commit
f2c66ed1
NS
100}
101
102apply_stash () {
5be60078 103 git diff-files --quiet ||
f2c66ed1
NS
104 die 'Cannot restore on top of a dirty state'
105
106 # current index state
5be60078 107 c_tree=$(git write-tree) ||
f2c66ed1
NS
108 die 'Cannot apply a stash in the middle of a merge'
109
5be60078
JH
110 s=$(git rev-parse --revs-only --no-flags --default $ref_stash "$@") &&
111 w_tree=$(git rev-parse --verify "$s:") &&
112 b_tree=$(git rev-parse --verify "$s^:") ||
f2c66ed1
NS
113 die "$*: no valid stashed state found"
114
115 eval "
116 GITHEAD_$w_tree='Stashed changes' &&
117 GITHEAD_$c_tree='Updated upstream' &&
118 GITHEAD_$b_tree='Version stash was based on' &&
119 export GITHEAD_$w_tree GITHEAD_$c_tree GITHEAD_$b_tree
120 "
121
122 if git-merge-recursive $b_tree -- $c_tree $w_tree
123 then
124 # No conflict
125 a="$TMP-added" &&
5be60078
JH
126 git diff --cached --name-only --diff-filter=A $c_tree >"$a" &&
127 git read-tree --reset $c_tree &&
128 git update-index --add --stdin <"$a" ||
f2c66ed1
NS
129 die "Cannot unstage modified files"
130 git-status
131 rm -f "$a"
132 else
133 # Merge conflict; keep the exit status from merge-recursive
134 exit
135 fi
136}
137
138# Main command set
139case "$1" in
fcb10a96
JH
140list)
141 shift
f2c66ed1
NS
142 if test $# = 0
143 then
144 set x -n 10
145 shift
146 fi
147 list_stash "$@"
148 ;;
149show)
150 shift
151 show_stash "$@"
152 ;;
153apply)
154 shift
155 apply_stash "$@"
156 ;;
157clear)
158 clear_stash
159 ;;
fcb10a96 160save | '')
f2c66ed1
NS
161 save_stash && git-reset --hard
162 ;;
163*)
164 usage
165esac