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 <larsxschneider@gmail.com> Mentored-by: Christian Couder <chriscool@tuxfamily.org> Mentored by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com> Signed-off-by: Tanushree Tumane <tanushreetumane@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Pranit Bauva committed Jan 2, 2019 at 07:38 UTC 5e82c3dd22aa804ba0ae571bf903b387b0f766a6
2 files changed +52 -27
builtin/bisect--helper.c
+50 -1
@@ -3,15 +3,21 @@
3 #include "parse-options.h"
4 #include "bisect.h"
5 #include "refs.h"
6 +#include "dir.h"
7 +#include "argv-array.h"
8 +#include "run-command.h"
9
10 static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
11 static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV")
12 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")
15
16 static const char * const git_bisect_helper_usage[] = {
17 N_("git bisect--helper --next-all [--no-checkout]"),
18 N_("git bisect--helper --write-terms <bad_term> <good_term>"),
19 N_("git bisect--helper --bisect-clean-state"),
20 + N_("git bisect--helper --bisect-reset [<commit>]"),
21 NULL
22 };
23
@@ -106,13 +112,50 @@ static void check_expected_revs(const char **revs, int rev_nr)
112 }
113 }
114
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 +
151 int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
152 {
153 enum {
154 NEXT_ALL = 1,
155 WRITE_TERMS,
156 BISECT_CLEAN_STATE,
115 - CHECK_EXPECTED_REVS
157 + CHECK_EXPECTED_REVS,
158 + BISECT_RESET
159 } cmdmode = 0;
160 int no_checkout = 0;
161 struct option options[] = {
@@ -124,6 +167,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
167 N_("cleanup the bisection state"), BISECT_CLEAN_STATE),
168 OPT_CMDMODE(0, "check-expected-revs", &cmdmode,
169 N_("check for expected revs"), CHECK_EXPECTED_REVS),
170 + OPT_CMDMODE(0, "bisect-reset", &cmdmode,
171 + N_("reset the bisection state"), BISECT_RESET),
172 OPT_BOOL(0, "no-checkout", &no_checkout,
173 N_("update BISECT_HEAD instead of checking out the current commit")),
174 OPT_END()
@@ -149,6 +194,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
194 case CHECK_EXPECTED_REVS:
195 check_expected_revs(argv, argc);
196 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);
201 default:
202 return error("BUG: unknown subcommand '%d'", cmdmode);
203 }
git-bisect.sh
+2 -26
@@ -393,35 +393,11 @@ bisect_visualize() {
393 eval '"$@"' --bisect -- $(cat "$GIT_DIR/BISECT_NAMES")
394 }
395
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 -
396 bisect_replay () {
397 file="$1"
398 test "$#" -eq 1 || die "$(gettext "No logfile given")"
399 test -r "$file" || die "$(eval_gettext "cannot read \$file for replaying")"
424 - bisect_reset
400 + git bisect--helper --bisect-reset || exit
401 while read git bisect command rev
402 do
403 test "$git $bisect" = "git bisect" || test "$git" = "git-bisect" || continue
@@ -613,7 +589,7 @@ case "$#" in
589 visualize|view)
590 bisect_visualize "$@" ;;
591 reset)
616 - bisect_reset "$@" ;;
592 + git bisect--helper --bisect-reset "$@" ;;
593 replay)
594 bisect_replay "$@" ;;
595 log)