git-stash.sh 6.3 KB
Newer Older
しらいしななこ 已提交
1 2 3
#!/bin/sh
# Copyright (c) 2007, Nanako Shiraishi

4
USAGE='[  | save | list | show | apply | clear | drop | pop | create | branch ]'
しらいしななこ 已提交
5

6
SUBDIRECTORY_OK=Yes
7
OPTIONS_SPEC=
しらいしななこ 已提交
8 9
. git-sh-setup
require_work_tree
10
cd_to_toplevel
しらいしななこ 已提交
11 12 13 14 15 16 17

TMP="$GIT_DIR/.git-stash.$$"
trap 'rm -f "$TMP-*"' 0

ref_stash=refs/stash

no_changes () {
18 19
	git diff-index --quiet --cached HEAD --ignore-submodules -- &&
	git diff-files --quiet --ignore-submodules
しらいしななこ 已提交
20 21 22
}

clear_stash () {
23 24 25 26
	if test $# != 0
	then
		die "git stash clear with parameters is unimplemented"
	fi
27 28
	if current=$(git rev-parse --verify $ref_stash 2>/dev/null)
	then
E
Emil Medve 已提交
29
		git update-ref -d $ref_stash $current
30
	fi
しらいしななこ 已提交
31 32
}

J
Junio C Hamano 已提交
33
create_stash () {
34 35
	stash_msg="$1"

しらいしななこ 已提交
36 37 38 39
	if no_changes
	then
		exit 0
	fi
40

しらいしななこ 已提交
41
	# state of the base commit
42
	if b_commit=$(git rev-parse --verify HEAD)
しらいしななこ 已提交
43
	then
44
		head=$(git log --no-color --abbrev-commit --pretty=oneline -n 1 HEAD --)
しらいしななこ 已提交
45 46 47 48
	else
		die "You do not have the initial commit yet"
	fi

49
	if branch=$(git symbolic-ref -q HEAD)
しらいしななこ 已提交
50 51 52 53 54 55 56 57
	then
		branch=${branch#refs/heads/}
	else
		branch='(no branch)'
	fi
	msg=$(printf '%s: %s' "$branch" "$head")

	# state of the index
58
	i_tree=$(git write-tree) &&
59
	i_commit=$(printf 'index on %s\n' "$msg" |
60
		git commit-tree $i_tree -p $b_commit) ||
しらいしななこ 已提交
61 62 63 64
		die "Cannot save the current index state"

	# state of the working tree
	w_tree=$( (
65 66
		rm -f "$TMP-index" &&
		cp -p ${GIT_INDEX_FILE-"$GIT_DIR/index"} "$TMP-index" &&
しらいしななこ 已提交
67 68
		GIT_INDEX_FILE="$TMP-index" &&
		export GIT_INDEX_FILE &&
69
		git read-tree -m $i_tree &&
70 71
		git add -u &&
		git write-tree &&
しらいしななこ 已提交
72 73 74 75 76
		rm -f "$TMP-index"
	) ) ||
		die "Cannot save the current worktree state"

	# create the stash
77 78 79 80 81 82 83
	if test -z "$stash_msg"
	then
		stash_msg=$(printf 'WIP on %s' "$msg")
	else
		stash_msg=$(printf 'On %s: %s' "$branch" "$stash_msg")
	fi
	w_commit=$(printf '%s\n' "$stash_msg" |
84
		git commit-tree $w_tree -p $b_commit -p $i_commit) ||
しらいしななこ 已提交
85
		die "Cannot record working tree state"
J
Junio C Hamano 已提交
86
}
しらいしななこ 已提交
87

J
Junio C Hamano 已提交
88
save_stash () {
89 90 91 92 93 94 95
	keep_index=
	case "$1" in
	--keep-index)
		keep_index=t
		shift
	esac

96
	stash_msg="$*"
J
Junio C Hamano 已提交
97 98 99

	if no_changes
	then
100
		echo 'No local changes to save'
J
Junio C Hamano 已提交
101 102 103 104 105 106
		exit 0
	fi
	test -f "$GIT_DIR/logs/$ref_stash" ||
		clear_stash || die "Cannot initialize stash"

	create_stash "$stash_msg"
E
Emil Medve 已提交
107 108 109

	# Make sure the reflog for stash is kept.
	: >>"$GIT_DIR/logs/$ref_stash"
しらいしななこ 已提交
110

111
	git update-ref -m "$stash_msg" $ref_stash $w_commit ||
しらいしななこ 已提交
112
		die "Cannot save the current status"
113
	printf 'Saved working directory and index state "%s"\n' "$stash_msg"
114 115 116 117 118 119 120

	git reset --hard

	if test -n "$keep_index" && test -n $i_tree
	then
		git read-tree --reset -u $i_tree
	fi
しらいしななこ 已提交
121 122
}

123
have_stash () {
124
	git rev-parse --verify $ref_stash >/dev/null 2>&1
125 126
}

しらいしななこ 已提交
127
list_stash () {
128
	have_stash || return 0
129
	git log --no-color --pretty=oneline -g "$@" $ref_stash -- |
しらいしななこ 已提交
130 131 132 133
	sed -n -e 's/^[.0-9a-f]* refs\///p'
}

show_stash () {
134
	flags=$(git rev-parse --no-revs --flags "$@")
しらいしななこ 已提交
135 136 137 138
	if test -z "$flags"
	then
		flags=--stat
	fi
139
	s=$(git rev-parse --revs-only --no-flags --default $ref_stash "$@")
しらいしななこ 已提交
140

141 142
	w_commit=$(git rev-parse --verify "$s") &&
	b_commit=$(git rev-parse --verify "$s^") &&
143
	git diff $flags $b_commit $w_commit
しらいしななこ 已提交
144 145 146
}

apply_stash () {
147
	git diff-files --quiet --ignore-submodules ||
しらいしななこ 已提交
148 149
		die 'Cannot restore on top of a dirty state'

150 151 152 153 154 155 156
	unstash_index=
	case "$1" in
	--index)
		unstash_index=t
		shift
	esac

しらいしななこ 已提交
157
	# current index state
158
	c_tree=$(git write-tree) ||
しらいしななこ 已提交
159 160
		die 'Cannot apply a stash in the middle of a merge'

J
Junio C Hamano 已提交
161 162
	# stash records the work tree, and is a merge between the
	# base commit (first parent) and the index tree (second parent).
163 164
	s=$(git rev-parse --revs-only --no-flags --default $ref_stash "$@") &&
	w_tree=$(git rev-parse --verify "$s:") &&
J
Junio C Hamano 已提交
165 166
	b_tree=$(git rev-parse --verify "$s^1:") &&
	i_tree=$(git rev-parse --verify "$s^2:") ||
しらいしななこ 已提交
167 168
		die "$*: no valid stashed state found"

J
Junio C Hamano 已提交
169
	unstashed_index_tree=
170 171
	if test -n "$unstash_index" && test "$b_tree" != "$i_tree" &&
			test "$c_tree" != "$i_tree"
J
Junio C Hamano 已提交
172
	then
173
		git diff-tree --binary $s^2^..$s^2 | git apply --cached
174 175 176 177 178
		test $? -ne 0 &&
			die 'Conflicts in index. Try without --index.'
		unstashed_index_tree=$(git-write-tree) ||
			die 'Could not save index tree'
		git reset
J
Junio C Hamano 已提交
179
	fi
180

しらいしななこ 已提交
181 182 183 184 185 186 187 188 189 190
	eval "
		GITHEAD_$w_tree='Stashed changes' &&
		GITHEAD_$c_tree='Updated upstream' &&
		GITHEAD_$b_tree='Version stash was based on' &&
		export GITHEAD_$w_tree GITHEAD_$c_tree GITHEAD_$b_tree
	"

	if git-merge-recursive $b_tree -- $c_tree $w_tree
	then
		# No conflict
J
Junio C Hamano 已提交
191 192 193
		if test -n "$unstashed_index_tree"
		then
			git read-tree "$unstashed_index_tree"
194 195
		else
			a="$TMP-added" &&
196
			git diff-index --cached --name-only --diff-filter=A $c_tree >"$a" &&
197 198 199 200
			git read-tree --reset $c_tree &&
			git update-index --add --stdin <"$a" ||
				die "Cannot unstage modified files"
			rm -f "$a"
J
Junio C Hamano 已提交
201 202
		fi
		git status || :
しらいしななこ 已提交
203 204
	else
		# Merge conflict; keep the exit status from merge-recursive
205
		status=$?
J
Junio C Hamano 已提交
206 207 208 209
		if test -n "$unstash_index"
		then
			echo >&2 'Index was not unstashed.'
		fi
210
		exit $status
しらいしななこ 已提交
211 212 213
	fi
}

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
drop_stash () {
	have_stash || die 'No stash entries to drop'

	if test $# = 0
	then
		set x "$ref_stash@{0}"
		shift
	fi
	# Verify supplied argument looks like a stash entry
	s=$(git rev-parse --revs-only --no-flags "$@") &&
	git rev-parse --verify "$s:"   > /dev/null 2>&1 &&
	git rev-parse --verify "$s^1:" > /dev/null 2>&1 &&
	git rev-parse --verify "$s^2:" > /dev/null 2>&1 ||
		die "$*: not a valid stashed state"

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

	# clear_stash if we just dropped the last stash entry
	git rev-parse --verify "$ref_stash@{0}" > /dev/null 2>&1 || clear_stash
}

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
apply_to_branch () {
	have_stash || die 'Nothing to apply'

	test -n "$1" || die 'No branch name specified'
	branch=$1

	if test -z "$2"
	then
		set x "$ref_stash@{0}"
	fi
	stash=$2

	git-checkout -b $branch $stash^ &&
	apply_stash --index $stash &&
	drop_stash $stash
}

しらいしななこ 已提交
253 254
# Main command set
case "$1" in
255 256
list)
	shift
しらいしななこ 已提交
257 258 259 260 261 262 263 264 265 266 267
	if test $# = 0
	then
		set x -n 10
		shift
	fi
	list_stash "$@"
	;;
show)
	shift
	show_stash "$@"
	;;
268 269
save)
	shift
270
	save_stash "$@"
271
	;;
しらいしななこ 已提交
272 273 274 275 276
apply)
	shift
	apply_stash "$@"
	;;
clear)
277 278
	shift
	clear_stash "$@"
しらいしななこ 已提交
279
	;;
J
Junio C Hamano 已提交
280 281 282 283 284 285 286
create)
	if test $# -gt 0 && test "$1" = create
	then
		shift
	fi
	create_stash "$*" && echo "$w_commit"
	;;
287 288 289 290
drop)
	shift
	drop_stash "$@"
	;;
291 292 293 294 295 296 297 298
pop)
	shift
	if apply_stash "$@"
	then
		test -z "$unstash_index" || shift
		drop_stash "$@"
	fi
	;;
299 300 301 302
branch)
	shift
	apply_to_branch "$@"
	;;
しらいしななこ 已提交
303
*)
304
	if test $# -eq 0
305
	then
306
		save_stash &&
307
		echo '(To restore them type "git stash apply")'
308 309
	else
		usage
310 311
	fi
	;;
しらいしななこ 已提交
312
esac