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

4 5
dashless=$(basename "$0" | sed -e 's/-/ /')
USAGE="list [<options>]
S
Stephen Boyd 已提交
6 7 8
   or: $dashless show [<stash>]
   or: $dashless drop [-q|--quiet] [<stash>]
   or: $dashless ( pop | apply ) [--index] [-q|--quiet] [<stash>]
9
   or: $dashless branch <branchname> [<stash>]
S
Stephen Boyd 已提交
10
   or: $dashless [save [--keep-index] [-q|--quiet] [<message>]]
11
   or: $dashless clear"
しらいしななこ 已提交
12

13
SUBDIRECTORY_OK=Yes
14
OPTIONS_SPEC=
しらいしななこ 已提交
15 16
. git-sh-setup
require_work_tree
17
cd_to_toplevel
しらいしななこ 已提交
18 19 20 21 22 23 24

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

ref_stash=refs/stash

no_changes () {
25 26
	git diff-index --quiet --cached HEAD --ignore-submodules -- &&
	git diff-files --quiet --ignore-submodules
しらいしななこ 已提交
27 28 29
}

clear_stash () {
30 31 32 33
	if test $# != 0
	then
		die "git stash clear with parameters is unimplemented"
	fi
34
	if current=$(git rev-parse --verify $ref_stash 2>/dev/null)
35
	then
E
Emil Medve 已提交
36
		git update-ref -d $ref_stash $current
37
	fi
しらいしななこ 已提交
38 39
}

J
Junio C Hamano 已提交
40
create_stash () {
41 42
	stash_msg="$1"

43
	git update-index -q --refresh
しらいしななこ 已提交
44 45 46 47
	if no_changes
	then
		exit 0
	fi
48

しらいしななこ 已提交
49
	# state of the base commit
50
	if b_commit=$(git rev-parse --verify HEAD)
しらいしななこ 已提交
51
	then
52
		head=$(git log --no-color --abbrev-commit --pretty=oneline -n 1 HEAD --)
しらいしななこ 已提交
53 54 55 56
	else
		die "You do not have the initial commit yet"
	fi

57
	if branch=$(git symbolic-ref -q HEAD)
しらいしななこ 已提交
58 59 60 61 62 63 64 65
	then
		branch=${branch#refs/heads/}
	else
		branch='(no branch)'
	fi
	msg=$(printf '%s: %s' "$branch" "$head")

	# state of the index
66
	i_tree=$(git write-tree) &&
67
	i_commit=$(printf 'index on %s\n' "$msg" |
68
		git commit-tree $i_tree -p $b_commit) ||
しらいしななこ 已提交
69 70 71 72
		die "Cannot save the current index state"

	# state of the working tree
	w_tree=$( (
73 74
		rm -f "$TMP-index" &&
		cp -p ${GIT_INDEX_FILE-"$GIT_DIR/index"} "$TMP-index" &&
しらいしななこ 已提交
75 76
		GIT_INDEX_FILE="$TMP-index" &&
		export GIT_INDEX_FILE &&
77
		git read-tree -m $i_tree &&
78 79
		git add -u &&
		git write-tree &&
しらいしななこ 已提交
80 81 82 83 84
		rm -f "$TMP-index"
	) ) ||
		die "Cannot save the current worktree state"

	# create the stash
85 86 87 88 89 90 91
	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" |
92
		git commit-tree $w_tree -p $b_commit -p $i_commit) ||
しらいしななこ 已提交
93
		die "Cannot record working tree state"
J
Junio C Hamano 已提交
94
}
しらいしななこ 已提交
95

J
Junio C Hamano 已提交
96
save_stash () {
97
	keep_index=
S
Stephen Boyd 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110
	while test $# != 0
	do
		case "$1" in
		--keep-index)
			keep_index=t
			;;
		-q|--quiet)
			GIT_QUIET=t
			;;
		*)
			break
			;;
		esac
111
		shift
S
Stephen Boyd 已提交
112
	done
113

114
	stash_msg="$*"
J
Junio C Hamano 已提交
115

116
	git update-index -q --refresh
J
Junio C Hamano 已提交
117 118
	if no_changes
	then
S
Stephen Boyd 已提交
119
		say 'No local changes to save'
J
Junio C Hamano 已提交
120 121 122 123 124 125
		exit 0
	fi
	test -f "$GIT_DIR/logs/$ref_stash" ||
		clear_stash || die "Cannot initialize stash"

	create_stash "$stash_msg"
E
Emil Medve 已提交
126 127 128

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

130
	git update-ref -m "$stash_msg" $ref_stash $w_commit ||
しらいしななこ 已提交
131
		die "Cannot save the current status"
S
Stephen Boyd 已提交
132
	say Saved working directory and index state "$stash_msg"
133

S
Stephen Boyd 已提交
134
	git reset --hard ${GIT_QUIET:+-q}
135 136 137 138 139

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

142
have_stash () {
143
	git rev-parse --verify $ref_stash >/dev/null 2>&1
144 145
}

しらいしななこ 已提交
146
list_stash () {
147
	have_stash || return 0
148
	git log --no-color --pretty=oneline -g "$@" $ref_stash -- |
しらいしななこ 已提交
149 150 151 152
	sed -n -e 's/^[.0-9a-f]* refs\///p'
}

show_stash () {
153
	flags=$(git rev-parse --no-revs --flags "$@")
しらいしななこ 已提交
154 155 156 157
	if test -z "$flags"
	then
		flags=--stat
	fi
158

159 160
	w_commit=$(git rev-parse --verify --default $ref_stash "$@") &&
	b_commit=$(git rev-parse --verify "$w_commit^") &&
161
	git diff $flags $b_commit $w_commit
しらいしななこ 已提交
162 163 164
}

apply_stash () {
165
	git update-index -q --refresh &&
166
	git diff-files --quiet --ignore-submodules ||
167
		die 'Cannot apply to a dirty working tree, please stage your changes'
しらいしななこ 已提交
168

169
	unstash_index=
S
Stephen Boyd 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183

	while test $# != 0
	do
		case "$1" in
		--index)
			unstash_index=t
			;;
		-q|--quiet)
			GIT_QUIET=t
			;;
		*)
			break
			;;
		esac
184
		shift
S
Stephen Boyd 已提交
185
	done
186

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

J
Junio C Hamano 已提交
191 192
	# stash records the work tree, and is a merge between the
	# base commit (first parent) and the index tree (second parent).
193
	s=$(git rev-parse --verify --default $ref_stash "$@") &&
194
	w_tree=$(git rev-parse --verify "$s:") &&
J
Junio C Hamano 已提交
195 196
	b_tree=$(git rev-parse --verify "$s^1:") &&
	i_tree=$(git rev-parse --verify "$s^2:") ||
しらいしななこ 已提交
197 198
		die "$*: no valid stashed state found"

J
Junio C Hamano 已提交
199
	unstashed_index_tree=
200 201
	if test -n "$unstash_index" && test "$b_tree" != "$i_tree" &&
			test "$c_tree" != "$i_tree"
J
Junio C Hamano 已提交
202
	then
203
		git diff-tree --binary $s^2^..$s^2 | git apply --cached
204 205
		test $? -ne 0 &&
			die 'Conflicts in index. Try without --index.'
206
		unstashed_index_tree=$(git write-tree) ||
207 208
			die 'Could not save index tree'
		git reset
J
Junio C Hamano 已提交
209
	fi
210

しらいしななこ 已提交
211 212 213 214 215 216 217
	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
	"

S
Stephen Boyd 已提交
218 219 220 221
	if test -n "$GIT_QUIET"
	then
		export GIT_MERGE_VERBOSITY=0
	fi
222
	if git merge-recursive $b_tree -- $c_tree $w_tree
しらいしななこ 已提交
223 224
	then
		# No conflict
J
Junio C Hamano 已提交
225 226 227
		if test -n "$unstashed_index_tree"
		then
			git read-tree "$unstashed_index_tree"
228 229
		else
			a="$TMP-added" &&
230
			git diff-index --cached --name-only --diff-filter=A $c_tree >"$a" &&
231 232 233 234
			git read-tree --reset $c_tree &&
			git update-index --add --stdin <"$a" ||
				die "Cannot unstage modified files"
			rm -f "$a"
J
Junio C Hamano 已提交
235
		fi
S
Stephen Boyd 已提交
236 237 238 239 240 241
		squelch=
		if test -n "$GIT_QUIET"
		then
			squelch='>/dev/null 2>&1'
		fi
		eval "git status $squelch" || :
しらいしななこ 已提交
242 243
	else
		# Merge conflict; keep the exit status from merge-recursive
244
		status=$?
J
Junio C Hamano 已提交
245 246 247 248
		if test -n "$unstash_index"
		then
			echo >&2 'Index was not unstashed.'
		fi
249
		exit $status
しらいしななこ 已提交
250 251 252
	fi
}

253 254 255
drop_stash () {
	have_stash || die 'No stash entries to drop'

S
Stephen Boyd 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268
	while test $# != 0
	do
		case "$1" in
		-q|--quiet)
			GIT_QUIET=t
			;;
		*)
			break
			;;
		esac
		shift
	done

269 270 271 272 273 274
	if test $# = 0
	then
		set x "$ref_stash@{0}"
		shift
	fi
	# Verify supplied argument looks like a stash entry
275
	s=$(git rev-parse --verify "$@") &&
276 277 278
	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 ||
279 280 281
		die "$*: not a valid stashed state"

	git reflog delete --updateref --rewrite "$@" &&
S
Stephen Boyd 已提交
282
		say "Dropped $* ($s)" || die "$*: Could not drop stash entry"
283 284

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

288 289 290 291 292 293 294 295 296 297 298 299
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

300
	git checkout -b $branch $stash^ &&
301 302 303 304
	apply_stash --index $stash &&
	drop_stash $stash
}

しらいしななこ 已提交
305 306
# Main command set
case "$1" in
307 308
list)
	shift
しらいしななこ 已提交
309 310 311 312 313 314 315 316 317 318 319
	if test $# = 0
	then
		set x -n 10
		shift
	fi
	list_stash "$@"
	;;
show)
	shift
	show_stash "$@"
	;;
320 321
save)
	shift
322
	save_stash "$@"
323
	;;
しらいしななこ 已提交
324 325 326 327 328
apply)
	shift
	apply_stash "$@"
	;;
clear)
329 330
	shift
	clear_stash "$@"
しらいしななこ 已提交
331
	;;
J
Junio C Hamano 已提交
332 333 334 335 336 337 338
create)
	if test $# -gt 0 && test "$1" = create
	then
		shift
	fi
	create_stash "$*" && echo "$w_commit"
	;;
339 340 341 342
drop)
	shift
	drop_stash "$@"
	;;
343 344 345 346 347 348 349 350
pop)
	shift
	if apply_stash "$@"
	then
		test -z "$unstash_index" || shift
		drop_stash "$@"
	fi
	;;
351 352 353 354
branch)
	shift
	apply_to_branch "$@"
	;;
しらいしななこ 已提交
355
*)
356
	if test $# -eq 0
357
	then
358
		save_stash &&
S
Stephen Boyd 已提交
359
		say '(To restore them type "git stash apply")'
360 361
	else
		usage
362 363
	fi
	;;
しらいしななこ 已提交
364
esac