bisect--helper: `is_expected_rev` & `check_expected_revs` shell function in C

Reimplement `is_expected_rev` & `check_expected_revs` shell function in C and add a `--check-expected-revs` subcommand to `git bisect--helper` to call it from git-bisect.sh . Using `--check-expected-revs` subcommand is a temporary measure to port shell functions to C so as to use the existing test suite. As more functions are ported, this subcommand would be retired but its implementation will be called by some other method. Helped-by: Eric Sunshine <sunshine@sunshineco.com> Mentored-by: Lars Schneider <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Pranit Bauva committed Sep 29, 2017 at 06:49 UTC b903674b354bf1538a89833dbee27fe3a7105e48
2 files changed +35 -19
builtin/bisect--helper.c
+33 -1
@@ -5,6 +5,8 @@
5 #include "refs.h"
6
7 static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
8 +static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV")
9 +static GIT_PATH_FUNC(git_path_bisect_ancestors_ok, "BISECT_ANCESTORS_OK")
10
11 static const char * const git_bisect_helper_usage[] = {
12 N_("git bisect--helper --next-all [--no-checkout]"),
@@ -80,12 +82,37 @@ static int write_terms(const char *bad, const char *good)
82 return (res < 0) ? -1 : 0;
83 }
84
85 +static int is_expected_rev(const char *expected_hex)
86 +{
87 + struct strbuf actual_hex = STRBUF_INIT;
88 + int res = 0;
89 + if (strbuf_read_file(&actual_hex, git_path_bisect_expected_rev(), 0) >= 40) {
90 + strbuf_trim(&actual_hex);
91 + res = !strcmp(actual_hex.buf, expected_hex);
92 + }
93 + strbuf_release(&actual_hex);
94 + return res;
95 +}
96 +
97 +static void check_expected_revs(const char **revs, int rev_nr)
98 +{
99 + int i;
100 +
101 + for (i = 0; i < rev_nr; i++) {
102 + if (!is_expected_rev(revs[i])) {
103 + unlink_or_warn(git_path_bisect_ancestors_ok());
104 + unlink_or_warn(git_path_bisect_expected_rev());
105 + }
106 + }
107 +}
108 +
109 int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
110 {
111 enum {
112 NEXT_ALL = 1,
113 WRITE_TERMS,
88 - BISECT_CLEAN_STATE
114 + BISECT_CLEAN_STATE,
115 + CHECK_EXPECTED_REVS
116 } cmdmode = 0;
117 int no_checkout = 0;
118 struct option options[] = {
@@ -95,6 +122,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
122 N_("write the terms to .git/BISECT_TERMS"), WRITE_TERMS),
123 OPT_CMDMODE(0, "bisect-clean-state", &cmdmode,
124 N_("cleanup the bisection state"), BISECT_CLEAN_STATE),
125 + OPT_CMDMODE(0, "check-expected-revs", &cmdmode,
126 + N_("check for expected revs"), CHECK_EXPECTED_REVS),
127 OPT_BOOL(0, "no-checkout", &no_checkout,
128 N_("update BISECT_HEAD instead of checking out the current commit")),
129 OPT_END()
@@ -117,6 +146,9 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
146 if (argc != 0)
147 return error(_("--bisect-clean-state requires no arguments"));
148 return bisect_clean_state();
149 + case CHECK_EXPECTED_REVS:
150 + check_expected_revs(argv, argc);
151 + return 0;
152 default:
153 return error("BUG: unknown subcommand '%d'", cmdmode);
154 }
git-bisect.sh
+2 -18
@@ -237,22 +237,6 @@ bisect_write() {
237 test -n "$nolog" || echo "git bisect $state $rev" >>"$GIT_DIR/BISECT_LOG"
238 }
239
240 -is_expected_rev() {
241 - test -f "$GIT_DIR/BISECT_EXPECTED_REV" &&
242 - test "$1" = $(cat "$GIT_DIR/BISECT_EXPECTED_REV")
243 -}
244 -
245 -check_expected_revs() {
246 - for _rev in "$@"; do
247 - if ! is_expected_rev "$_rev"
248 - then
249 - rm -f "$GIT_DIR/BISECT_ANCESTORS_OK"
250 - rm -f "$GIT_DIR/BISECT_EXPECTED_REV"
251 - return
252 - fi
253 - done
254 -}
255 -
240 bisect_skip() {
241 all=''
242 for arg in "$@"
@@ -280,7 +264,7 @@ bisect_state() {
264 rev=$(git rev-parse --verify "$bisected_head") ||
265 die "$(eval_gettext "Bad rev input: \$bisected_head")"
266 bisect_write "$state" "$rev"
283 - check_expected_revs "$rev" ;;
267 + git bisect--helper --check-expected-revs "$rev" ;;
268 2,"$TERM_BAD"|*,"$TERM_GOOD"|*,skip)
269 shift
270 hash_list=''
@@ -294,7 +278,7 @@ bisect_state() {
278 do
279 bisect_write "$state" "$rev"
280 done
297 - check_expected_revs $hash_list ;;
281 + git bisect--helper --check-expected-revs $hash_list ;;
282 *,"$TERM_BAD")
283 die "$(eval_gettext "'git bisect \$TERM_BAD' can take only one argument.")" ;;
284 *)