git-bisect.sh 8.1 KB
Newer Older
1
#!/bin/sh
F
Fredrik Kuivinen 已提交
2

3
USAGE='[help|start|bad|good|new|old|terms|skip|next|reset|visualize|view|replay|log|run]'
4
LONG_USAGE='git bisect help
5
	print this long help message.
6 7
git bisect start [--term-{old,good}=<term> --term-{new,bad}=<term>]
		 [--no-checkout] [<bad> [<good>...]] [--] [<pathspec>...]
8
	reset bisect state and start bisection.
A
Antoine Delaite 已提交
9 10 11 12 13 14
git bisect (bad|new) [<rev>]
	mark <rev> a known-bad revision/
		a revision after change in a given property.
git bisect (good|old) [<rev>...]
	mark <rev>... known-good revisions/
		revisions before change in a given property.
15 16
git bisect terms [--term-good | --term-bad]
	show the terms used for old and new commits (default: bad, good)
17
git bisect skip [(<rev>|<range>)...]
18
	mark <rev>... untestable revisions.
19
git bisect next
20
	find next bisection to test and check it out.
21
git bisect reset [<commit>]
22
	finish bisection search and go back to commit.
23
git bisect (visualize|view)
24
	show bisect status in gitk.
25
git bisect replay <logfile>
26
	replay bisection log.
27
git bisect log
28
	show bisect log.
29
git bisect run <cmd>...
30
	use <cmd>... to automatically bisect.
31 32

Please use "git help bisect" to get the full man page.'
F
Fredrik Kuivinen 已提交
33

34
OPTIONS_SPEC=
35
. git-sh-setup
36

37 38
_x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
_x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
39 40
TERM_BAD=bad
TERM_GOOD=good
41

42 43 44 45 46 47 48 49 50 51
bisect_head()
{
	if test -f "$GIT_DIR/BISECT_HEAD"
	then
		echo BISECT_HEAD
	else
		echo HEAD
	fi
}

52
bisect_autostart() {
53
	test -s "$GIT_DIR/BISECT_START" || {
54
		gettextln "You need to start by \"git bisect start\"" >&2
55 56
		if test -t 0
		then
57 58 59
			# TRANSLATORS: Make sure to include [Y] and [n] in your
			# translation. The program will only accept English input
			# at this point.
60
			gettext "Do you want me to do it for you [Y/n]? " >&2
61 62 63 64 65 66 67 68 69 70 71 72 73
			read yesno
			case "$yesno" in
			[Nn]*)
				exit ;;
			esac
			bisect_start
		else
			exit 1
		fi
	}
}

bisect_start() {
74
	git bisect--helper --bisect-start $@ || exit
75

76 77 78 79 80 81 82
	#
	# Change state.
	# In case of mistaken revs or checkout error, or signals received,
	# "bisect_auto_next" below may exit or misbehave.
	# We have to trap this to be able to clean up using
	# "bisect_clean_state".
	#
83
	trap 'git bisect--helper --bisect-clean-state' 0
84 85 86 87 88
	trap 'exit 255' 1 2 3 15

	#
	# Check if we can proceed to the next bisect state.
	#
89
	get_terms
90
	bisect_auto_next
91 92

	trap '-' 0
93 94
}

95
bisect_skip() {
96
	all=''
97 98
	for arg in "$@"
	do
99 100 101 102 103 104 105 106 107
		case "$arg" in
		*..*)
			revs=$(git rev-list "$arg") || die "$(eval_gettext "Bad rev input: \$arg")" ;;
		*)
			revs=$(git rev-parse --sq-quote "$arg") ;;
		esac
		all="$all $revs"
	done
	eval bisect_state 'skip' $all
108 109
}

110
bisect_state() {
111
	bisect_autostart
112
	state=$1
113 114
	git bisect--helper --check-and-set-terms $state $TERM_GOOD $TERM_BAD || exit
	get_terms
115 116
	case "$#,$state" in
	0,*)
117
		die "Please call 'bisect_state' with at least one argument." ;;
118
	1,"$TERM_BAD"|1,"$TERM_GOOD"|1,skip)
119 120 121
		bisected_head=$(bisect_head)
		rev=$(git rev-parse --verify "$bisected_head") ||
			die "$(eval_gettext "Bad rev input: \$bisected_head")"
122
		git bisect--helper --bisect-write "$state" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit
123
		git bisect--helper --check-expected-revs "$rev" ;;
124
	2,"$TERM_BAD"|*,"$TERM_GOOD"|*,skip)
125
		shift
126
		hash_list=''
127
		for rev in "$@"
128
		do
C
Christian Couder 已提交
129
			sha=$(git rev-parse --verify "$rev^{commit}") ||
130
				die "$(eval_gettext "Bad rev input: \$rev")"
131
			hash_list="$hash_list $sha"
132
		done
133 134
		for rev in $hash_list
		do
135
			git bisect--helper --bisect-write "$state" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit
136
		done
137
		git bisect--helper --check-expected-revs $hash_list ;;
138 139
	*,"$TERM_BAD")
		die "$(eval_gettext "'git bisect \$TERM_BAD' can take only one argument.")" ;;
J
Junio C Hamano 已提交
140 141
	*)
		usage ;;
142
	esac
143 144 145
	bisect_auto_next
}

146
bisect_auto_next() {
147
	git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD && bisect_next || :
148 149 150
}

bisect_next() {
151
	case "$#" in 0) ;; *) usage ;; esac
152
	bisect_autostart
153
	git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD $TERM_GOOD|| exit
154

155
	# Perform all bisection computation, display and checkout
156
	git bisect--helper --next-all $(test -f "$GIT_DIR/BISECT_HEAD" && echo --no-checkout)
157
	res=$?
158

159
	# Check if we should exit because bisection is finished
160 161
	if test $res -eq 10
	then
162
		bad_rev=$(git show-ref --hash --verify refs/bisect/$TERM_BAD)
163
		bad_commit=$(git show-branch $bad_rev)
164
		echo "# first $TERM_BAD commit: $bad_commit" >>"$GIT_DIR/BISECT_LOG"
165
		exit 0
166 167 168
	elif test $res -eq 2
	then
		echo "# only skipped commits left to test" >>"$GIT_DIR/BISECT_LOG"
169 170
		good_revs=$(git for-each-ref --format="%(objectname)" "refs/bisect/$TERM_GOOD-*")
		for skipped in $(git rev-list refs/bisect/$TERM_BAD --not $good_revs)
171 172
		do
			skipped_commit=$(git show-branch $skipped)
173
			echo "# possible first $TERM_BAD commit: $skipped_commit" >>"$GIT_DIR/BISECT_LOG"
174 175
		done
		exit $res
176
	fi
177

178 179 180 181
	# Check for an error in the bisection process
	test $res -ne 0 && exit $res

	return 0
182 183
}

J
Junio C Hamano 已提交
184
bisect_visualize() {
185
	git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit
186 187 188

	if test $# = 0
	then
189
		if test -n "${DISPLAY+set}${SESSIONNAME+set}${MSYSTEM+set}${SECURITYSESSIONID+set}" &&
J
Junio C Hamano 已提交
190
			type gitk >/dev/null 2>&1
191
		then
192 193 194 195
			set gitk
		else
			set git log
		fi
196 197 198 199 200 201 202 203
	else
		case "$1" in
		git*|tig) ;;
		-*)	set git log "$@" ;;
		*)	set git "$@" ;;
		esac
	fi

204
	eval '"$@"' --bisect -- $(cat "$GIT_DIR/BISECT_NAMES")
J
Junio C Hamano 已提交
205 206
}

207
bisect_replay () {
208 209 210
	file="$1"
	test "$#" -eq 1 || die "$(gettext "No logfile given")"
	test -r "$file" || die "$(eval_gettext "cannot read \$file for replaying")"
211
	git bisect--helper --bisect-reset || exit
212
	while read git bisect command rev
213
	do
214
		test "$git $bisect" = "git bisect" || test "$git" = "git-bisect" || continue
215 216
		if test "$git" = "git-bisect"
		then
217 218 219
			rev="$command"
			command="$bisect"
		fi
220
		get_terms
221 222
		git bisect--helper --check-and-set-terms "$command" "$TERM_GOOD" "$TERM_BAD" || exit
		get_terms
223 224
		case "$command" in
		start)
225
			cmd="bisect_start $rev"
226
			eval "$cmd" ;;
227
		"$TERM_GOOD"|"$TERM_BAD"|skip)
228
			git bisect--helper --bisect-write "$command" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit;;
229
		terms)
230
			git bisect--helper --bisect-terms $rev || exit;;
231
		*)
232
			die "$(gettext "?? what are you talking about?")" ;;
233
		esac
234
	done <"$file"
235
	bisect_auto_next
236 237
}

238
bisect_run () {
239
	git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit
240

241 242
	test -n "$*" || die "$(gettext "bisect run failed: no command provided.")"

243 244 245
	while true
	do
		command="$@"
246
		eval_gettextln "running \$command"
247 248 249 250
		"$@"
		res=$?

		# Check for really bad run error.
251 252
		if [ $res -lt 0 -o $res -ge 128 ]
		then
253 254
			eval_gettextln "bisect run failed:
exit code \$res from '\$command' is < 0 or >= 128" >&2
255 256 257 258 259
			exit $res
		fi

		# Find current state depending on run success or failure.
		# A special exit code of 125 means cannot test.
260 261
		if [ $res -eq 125 ]
		then
262
			state='skip'
263 264
		elif [ $res -gt 0 ]
		then
265
			state="$TERM_BAD"
266
		else
267
			state="$TERM_GOOD"
268 269 270
		fi

		# We have to use a subshell because "bisect_state" can exit.
271
		( bisect_state $state >"$GIT_DIR/BISECT_RUN" )
272 273 274 275
		res=$?

		cat "$GIT_DIR/BISECT_RUN"

276
		if sane_grep "first $TERM_BAD commit could be any of" "$GIT_DIR/BISECT_RUN" \
277
			>/dev/null
278
		then
279
			gettextln "bisect run cannot continue any more" >&2
280 281 282
			exit $res
		fi

283 284
		if [ $res -ne 0 ]
		then
285 286
			eval_gettextln "bisect run failed:
'bisect_state \$state' exited with error code \$res" >&2
287 288
			exit $res
		fi
289

290
		if sane_grep "is the first $TERM_BAD commit" "$GIT_DIR/BISECT_RUN" >/dev/null
291
		then
292
			gettextln "bisect run success"
293 294
			exit 0;
		fi
295

296
	done
297 298
}

299
bisect_log () {
300
	test -s "$GIT_DIR/BISECT_LOG" || die "$(gettext "We are not bisecting.")"
301 302
	cat "$GIT_DIR/BISECT_LOG"
}
303

304 305 306 307 308 309 310 311 312 313
get_terms () {
	if test -s "$GIT_DIR/BISECT_TERMS"
	then
		{
		read TERM_BAD
		read TERM_GOOD
		} <"$GIT_DIR/BISECT_TERMS"
	fi
}

314 315
case "$#" in
0)
316
	usage ;;
317
*)
318
	cmd="$1"
319
	get_terms
320 321 322 323 324 325
	shift
	case "$cmd" in
	help)
		git bisect -h ;;
	start)
		bisect_start "$@" ;;
A
Antoine Delaite 已提交
326
	bad|good|new|old|"$TERM_BAD"|"$TERM_GOOD")
327 328 329 330 331 332 333 334 335
		bisect_state "$cmd" "$@" ;;
	skip)
		bisect_skip "$@" ;;
	next)
		# Not sure we want "next" at the UI level anymore.
		bisect_next "$@" ;;
	visualize|view)
		bisect_visualize "$@" ;;
	reset)
336
		git bisect--helper --bisect-reset "$@" ;;
337 338 339 340 341 342
	replay)
		bisect_replay "$@" ;;
	log)
		bisect_log ;;
	run)
		bisect_run "$@" ;;
343
	terms)
344
		git bisect--helper --bisect-terms "$@" || exit;;
345 346 347
	*)
		usage ;;
	esac
348
esac