Skip to content

Commit 5e82c3d

Browse files
pranitbauva1997gitster
authored andcommitted
bisect--helper: bisect_reset shell function in C
Reimplement `bisect_reset` shell function in C and add a `--bisect-reset` subcommand to `git bisect--helper` to call it from git-bisect.sh . Using `bisect_reset` 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. Note: --bisect-clean-state subcommand has not been retired as there are still a function namely `bisect_start()` which still uses this subcommand. Mentored-by: Lars Schneider <[email protected]> Mentored-by: Christian Couder <[email protected]> Mentored by: Johannes Schindelin <[email protected]> Signed-off-by: Pranit Bauva <[email protected]> Signed-off-by: Tanushree Tumane <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b21ebb6 commit 5e82c3d

File tree

2 files changed

+52
-27
lines changed

2 files changed

+52
-27
lines changed

builtin/bisect--helper.c

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@
33
#include "parse-options.h"
44
#include "bisect.h"
55
#include "refs.h"
6+
#include "dir.h"
7+
#include "argv-array.h"
8+
#include "run-command.h"
69

710
static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
811
static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV")
912
static GIT_PATH_FUNC(git_path_bisect_ancestors_ok, "BISECT_ANCESTORS_OK")
13+
static GIT_PATH_FUNC(git_path_bisect_start, "BISECT_START")
14+
static GIT_PATH_FUNC(git_path_bisect_head, "BISECT_HEAD")
1015

1116
static const char * const git_bisect_helper_usage[] = {
1217
N_("git bisect--helper --next-all [--no-checkout]"),
1318
N_("git bisect--helper --write-terms <bad_term> <good_term>"),
1419
N_("git bisect--helper --bisect-clean-state"),
20+
N_("git bisect--helper --bisect-reset [<commit>]"),
1521
NULL
1622
};
1723

@@ -106,13 +112,50 @@ static void check_expected_revs(const char **revs, int rev_nr)
106112
}
107113
}
108114

115+
static int bisect_reset(const char *commit)
116+
{
117+
struct strbuf branch = STRBUF_INIT;
118+
119+
if (!commit) {
120+
if (strbuf_read_file(&branch, git_path_bisect_start(), 0) < 1) {
121+
printf(_("We are not bisecting.\n"));
122+
return 0;
123+
}
124+
strbuf_rtrim(&branch);
125+
} else {
126+
struct object_id oid;
127+
128+
if (get_oid_commit(commit, &oid))
129+
return error(_("'%s' is not a valid commit"), commit);
130+
strbuf_addstr(&branch, commit);
131+
}
132+
133+
if (!file_exists(git_path_bisect_head())) {
134+
struct argv_array argv = ARGV_ARRAY_INIT;
135+
136+
argv_array_pushl(&argv, "checkout", branch.buf, "--", NULL);
137+
if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
138+
strbuf_release(&branch);
139+
argv_array_clear(&argv);
140+
return error(_("could not check out original"
141+
" HEAD '%s'. Try 'git bisect"
142+
"reset <commit>'."), branch.buf);
143+
}
144+
argv_array_clear(&argv);
145+
}
146+
147+
strbuf_release(&branch);
148+
return bisect_clean_state();
149+
}
150+
109151
int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
110152
{
111153
enum {
112154
NEXT_ALL = 1,
113155
WRITE_TERMS,
114156
BISECT_CLEAN_STATE,
115-
CHECK_EXPECTED_REVS
157+
CHECK_EXPECTED_REVS,
158+
BISECT_RESET
116159
} cmdmode = 0;
117160
int no_checkout = 0;
118161
struct option options[] = {
@@ -124,6 +167,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
124167
N_("cleanup the bisection state"), BISECT_CLEAN_STATE),
125168
OPT_CMDMODE(0, "check-expected-revs", &cmdmode,
126169
N_("check for expected revs"), CHECK_EXPECTED_REVS),
170+
OPT_CMDMODE(0, "bisect-reset", &cmdmode,
171+
N_("reset the bisection state"), BISECT_RESET),
127172
OPT_BOOL(0, "no-checkout", &no_checkout,
128173
N_("update BISECT_HEAD instead of checking out the current commit")),
129174
OPT_END()
@@ -149,6 +194,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
149194
case CHECK_EXPECTED_REVS:
150195
check_expected_revs(argv, argc);
151196
return 0;
197+
case BISECT_RESET:
198+
if (argc > 1)
199+
return error(_("--bisect-reset requires either no argument or a commit"));
200+
return !!bisect_reset(argc ? argv[0] : NULL);
152201
default:
153202
return error("BUG: unknown subcommand '%d'", cmdmode);
154203
}

git-bisect.sh

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -393,35 +393,11 @@ bisect_visualize() {
393393
eval '"$@"' --bisect -- $(cat "$GIT_DIR/BISECT_NAMES")
394394
}
395395

396-
bisect_reset() {
397-
test -s "$GIT_DIR/BISECT_START" || {
398-
gettextln "We are not bisecting."
399-
return
400-
}
401-
case "$#" in
402-
0) branch=$(cat "$GIT_DIR/BISECT_START") ;;
403-
1) git rev-parse --quiet --verify "$1^{commit}" >/dev/null || {
404-
invalid="$1"
405-
die "$(eval_gettext "'\$invalid' is not a valid commit")"
406-
}
407-
branch="$1" ;;
408-
*)
409-
usage ;;
410-
esac
411-
412-
if ! test -f "$GIT_DIR/BISECT_HEAD" && ! git checkout "$branch" --
413-
then
414-
die "$(eval_gettext "Could not check out original HEAD '\$branch'.
415-
Try 'git bisect reset <commit>'.")"
416-
fi
417-
git bisect--helper --bisect-clean-state || exit
418-
}
419-
420396
bisect_replay () {
421397
file="$1"
422398
test "$#" -eq 1 || die "$(gettext "No logfile given")"
423399
test -r "$file" || die "$(eval_gettext "cannot read \$file for replaying")"
424-
bisect_reset
400+
git bisect--helper --bisect-reset || exit
425401
while read git bisect command rev
426402
do
427403
test "$git $bisect" = "git bisect" || test "$git" = "git-bisect" || continue
@@ -613,7 +589,7 @@ case "$#" in
613589
visualize|view)
614590
bisect_visualize "$@" ;;
615591
reset)
616-
bisect_reset "$@" ;;
592+
git bisect--helper --bisect-reset "$@" ;;
617593
replay)
618594
bisect_replay "$@" ;;
619595
log)

0 commit comments

Comments
 (0)