bisect: move argument parsing before state modification.

Currently 'git bisect start' modifies some state prior to checking that its arguments are valid. This change moves argument validation before state modification with the effect that state modification does not occur unless argument validations succeeds. An existing test is changed to check that new bisect state is not created if arguments are invalid. A new test is added to check that existing bisect state is not modified if arguments are invalid. Signed-off-by: Jon Seymour <jon.seymour@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jon Seymour committed Aug 4, 2011 at 22:00 UTC 4764f46492b0849ce3cf2f527978acd9c2d5f7e6
2 files changed +44 -36
git-bisect.sh
+33 -33
@@ -59,6 +59,39 @@ bisect_autostart() {
59 }
60
61 bisect_start() {
62 + #
63 + # Check for one bad and then some good revisions.
64 + #
65 + has_double_dash=0
66 + for arg; do
67 + case "$arg" in --) has_double_dash=1; break ;; esac
68 + done
69 + orig_args=$(git rev-parse --sq-quote "$@")
70 + bad_seen=0
71 + eval=''
72 + while [ $# -gt 0 ]; do
73 + arg="$1"
74 + case "$arg" in
75 + --)
76 + shift
77 + break
78 + ;;
79 + *)
80 + rev=$(git rev-parse -q --verify "$arg^{commit}") || {
81 + test $has_double_dash -eq 1 &&
82 + die "$(eval_gettext "'\$arg' does not appear to be a valid revision")"
83 + break
84 + }
85 + case $bad_seen in
86 + 0) state='bad' ; bad_seen=1 ;;
87 + *) state='good' ;;
88 + esac
89 + eval="$eval bisect_write '$state' '$rev' 'nolog'; "
90 + shift
91 + ;;
92 + esac
93 + done
94 +
95 #
96 # Verify HEAD.
97 #
@@ -97,39 +130,6 @@ bisect_start() {
130 #
131 bisect_clean_state || exit
132
100 - #
101 - # Check for one bad and then some good revisions.
102 - #
103 - has_double_dash=0
104 - for arg; do
105 - case "$arg" in --) has_double_dash=1; break ;; esac
106 - done
107 - orig_args=$(git rev-parse --sq-quote "$@")
108 - bad_seen=0
109 - eval=''
110 - while [ $# -gt 0 ]; do
111 - arg="$1"
112 - case "$arg" in
113 - --)
114 - shift
115 - break
116 - ;;
117 - *)
118 - rev=$(git rev-parse -q --verify "$arg^{commit}") || {
119 - test $has_double_dash -eq 1 &&
120 - die "$(eval_gettext "'\$arg' does not appear to be a valid revision")"
121 - break
122 - }
123 - case $bad_seen in
124 - 0) state='bad' ; bad_seen=1 ;;
125 - *) state='good' ;;
126 - esac
127 - eval="$eval bisect_write '$state' '$rev' 'nolog'; "
128 - shift
129 - ;;
130 - esac
131 - done
132 -
133 #
134 # Change state.
135 # In case of mistaken revs or checkout error, or signals received,
t/t6030-bisect-porcelain.sh
+11 -3
@@ -138,15 +138,23 @@ test_expect_success 'bisect start: back in good branch' '
138 grep "* other" branch.output > /dev/null
139 '
140
141 -test_expect_success 'bisect start: no ".git/BISECT_START" if junk rev' '
142 - git bisect start $HASH4 $HASH1 -- &&
143 - git bisect good &&
141 +test_expect_success 'bisect start: no ".git/BISECT_START" created if junk rev' '
142 + git bisect reset &&
143 test_must_fail git bisect start $HASH4 foo -- &&
144 git branch > branch.output &&
145 grep "* other" branch.output > /dev/null &&
146 test_must_fail test -e .git/BISECT_START
147 '
148
149 +test_expect_success 'bisect start: existing ".git/BISECT_START" not modified if junk rev' '
150 + git bisect start $HASH4 $HASH1 -- &&
151 + git bisect good &&
152 + cp .git/BISECT_START saved &&
153 + test_must_fail git bisect start $HASH4 foo -- &&
154 + git branch > branch.output &&
155 + grep "* (no branch)" branch.output > /dev/null &&
156 + test_cmp saved .git/BISECT_START
157 +'
158 test_expect_success 'bisect start: no ".git/BISECT_START" if mistaken rev' '
159 git bisect start $HASH4 $HASH1 -- &&
160 git bisect good &&