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

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

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

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

ref_stash=refs/stash

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

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

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

しらいしななこ 已提交
42 43 44 45
	if no_changes
	then
		exit 0
	fi
46

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

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

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

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

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

J
Junio C Hamano 已提交
94
save_stash () {
95 96 97 98 99 100 101
	keep_index=
	case "$1" in
	--keep-index)
		keep_index=t
		shift
	esac

102
	stash_msg="$*"
J
Junio C Hamano 已提交
103 104 105

	if no_changes
	then
106
		echo 'No local changes to save'
J
Junio C Hamano 已提交
107 108 109 110 111 112
		exit 0
	fi
	test -f "$GIT_DIR/logs/$ref_stash" ||
		clear_stash || die "Cannot initialize stash"

	create_stash "$stash_msg"
E
Emil Medve 已提交
113 114 115

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

117
	git update-ref -m "$stash_msg" $ref_stash $w_commit ||
しらいしななこ 已提交
118
		die "Cannot save the current status"
119
	printf 'Saved working directory and index state "%s"\n' "$stash_msg"
120 121 122 123 124 125 126

	git reset --hard

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

129
have_stash () {
130
	git rev-parse --verify $ref_stash >/dev/null 2>&1
131 132
}

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

show_stash () {
140
	flags=$(git rev-parse --no-revs --flags "$@")
しらいしななこ 已提交
141 142 143 144
	if test -z "$flags"
	then
		flags=--stat
	fi
145
	s=$(git rev-parse --revs-only --no-flags --default $ref_stash "$@")
しらいしななこ 已提交
146

147 148
	w_commit=$(git rev-parse --verify "$s") &&
	b_commit=$(git rev-parse --verify "$s^") &&
149
	git diff $flags $b_commit $w_commit
しらいしななこ 已提交
150 151 152
}

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

156 157 158 159 160 161 162
	unstash_index=
	case "$1" in
	--index)
		unstash_index=t
		shift
	esac

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

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

J
Junio C Hamano 已提交
175
	unstashed_index_tree=
176 177
	if test -n "$unstash_index" && test "$b_tree" != "$i_tree" &&
			test "$c_tree" != "$i_tree"
J
Junio C Hamano 已提交
178
	then
179
		git diff-tree --binary $s^2^..$s^2 | git apply --cached
180 181 182 183 184
		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 已提交
185
	fi
186

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

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
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
}

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
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
}

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