Skip to content

Commit b903674

Browse files
pranitbauva1997gitster
authored andcommitted
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 <[email protected]> Mentored-by: Lars Schneider <[email protected]> Mentored-by: Christian Couder <[email protected]> Signed-off-by: Pranit Bauva <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ba7eafe commit b903674

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

builtin/bisect--helper.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "refs.h"
66

77
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")
810

911
static const char * const git_bisect_helper_usage[] = {
1012
N_("git bisect--helper --next-all [--no-checkout]"),
@@ -80,12 +82,37 @@ static int write_terms(const char *bad, const char *good)
8082
return (res < 0) ? -1 : 0;
8183
}
8284

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+
83109
int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
84110
{
85111
enum {
86112
NEXT_ALL = 1,
87113
WRITE_TERMS,
88-
BISECT_CLEAN_STATE
114+
BISECT_CLEAN_STATE,
115+
CHECK_EXPECTED_REVS
89116
} cmdmode = 0;
90117
int no_checkout = 0;
91118
struct option options[] = {
@@ -95,6 +122,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
95122
N_("write the terms to .git/BISECT_TERMS"), WRITE_TERMS),
96123
OPT_CMDMODE(0, "bisect-clean-state", &cmdmode,
97124
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),
98127
OPT_BOOL(0, "no-checkout", &no_checkout,
99128
N_("update BISECT_HEAD instead of checking out the current commit")),
100129
OPT_END()
@@ -117,6 +146,9 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
117146
if (argc != 0)
118147
return error(_("--bisect-clean-state requires no arguments"));
119148
return bisect_clean_state();
149+
case CHECK_EXPECTED_REVS:
150+
check_expected_revs(argv, argc);
151+
return 0;
120152
default:
121153
return error("BUG: unknown subcommand '%d'", cmdmode);
122154
}

git-bisect.sh

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -237,22 +237,6 @@ bisect_write() {
237237
test -n "$nolog" || echo "git bisect $state $rev" >>"$GIT_DIR/BISECT_LOG"
238238
}
239239

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-
256240
bisect_skip() {
257241
all=''
258242
for arg in "$@"
@@ -280,7 +264,7 @@ bisect_state() {
280264
rev=$(git rev-parse --verify "$bisected_head") ||
281265
die "$(eval_gettext "Bad rev input: \$bisected_head")"
282266
bisect_write "$state" "$rev"
283-
check_expected_revs "$rev" ;;
267+
git bisect--helper --check-expected-revs "$rev" ;;
284268
2,"$TERM_BAD"|*,"$TERM_GOOD"|*,skip)
285269
shift
286270
hash_list=''
@@ -294,7 +278,7 @@ bisect_state() {
294278
do
295279
bisect_write "$state" "$rev"
296280
done
297-
check_expected_revs $hash_list ;;
281+
git bisect--helper --check-expected-revs $hash_list ;;
298282
*,"$TERM_BAD")
299283
die "$(eval_gettext "'git bisect \$TERM_BAD' can take only one argument.")" ;;
300284
*)

0 commit comments

Comments
 (0)