-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbisect
executable file
·79 lines (70 loc) · 1.43 KB
/
bisect
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
# print the first version where script exits non-zero
usage() {
cat <<EOF 1>&2
Usage: $scriptname <script> <good> <bad>
EOF
}
error() {
echo "$scriptname: $*" >&2
}
scriptname=bisect
while getopts ":h" option; do
case $option in
h)
usage
exit 0
;;
':')
echo "Missing argument to -$OPTARG" 1>&2
usage
exit 2
;;
'?')
echo "Invalid option -$OPTARG" 1>&2
usage
exit 2
;;
*)
echo "Program does not support -$option yet" 1>&2
usage
exit 2
;;
esac
done
shift $((OPTIND - 1))
script=$1
good=$2
bad=$3
if [[ -z $script ]] || [[ -z $good ]] || [[ -z $bad ]]; then
usage
exit 2
fi
if ! [[ -x $script ]]; then
error "$script is not executable"
exit 1
fi
if ((bad <= good)); then
error "bad version must be higher than good version"
exit 1
fi
logfile="$HOME/bisect.$(date +%Y%m%d.%H%M%S)"
log() {
echo "$(date) $*" >> "$logfile"
echo "$*" >&2
}
log "Given $good is good and $bad is bad..."
while ((bad-good > 1)); do
version=$((good + (bad-good)/2))
echo "Testing $script at $version..." >> "$logfile"
echo "==========" >> "$logfile"
if "$script" "$version" >>"$logfile" 2>&1; then
log "$version is good"
good=$version
else
log "$version is bad"
bad=$version
fi
echo "==========" >> "$logfile"
done
log "$bad is bad"