Skip to content

Commit 517ecb3

Browse files
pranitbauva1997gitster
authored andcommitted
bisect--helper: reimplement bisect_next and bisect_auto_next shell functions in C
Reimplement the `bisect_next()` and the `bisect_auto_next()` shell functions in C and add the subcommands to `git bisect--helper` to call them from git-bisect.sh . bisect_auto_next() function returns an enum bisect_error type as whole `git bisect` can exit with an error code when bisect_next() does. Return an error when `bisect_next()` fails, that fix a bug on shell script version. Using `--bisect-next` and `--bisect-auto-next` subcommands is a temporary measure to port shell function to C so as to use the existing test suite. As more functions are ported, `--bisect-auto-next` subcommand will be retired and will be called by some other methods. 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: Miriam Rubio <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c7a7f48 commit 517ecb3

File tree

3 files changed

+191
-47
lines changed

3 files changed

+191
-47
lines changed

bisect.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -988,8 +988,11 @@ void read_bisect_terms(const char **read_bad, const char **read_good)
988988
* the bisection process finished successfully.
989989
* In this case the calling function or command should not turn a
990990
* BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND return code into an error or a non zero exit code.
991-
* If no_checkout is non-zero, the bisection process does not
992-
* checkout the trial commit but instead simply updates BISECT_HEAD.
991+
*
992+
* Checking BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND
993+
* in bisect_helper::bisect_next() and only transforming it to 0 at
994+
* the end of bisect_helper::cmd_bisect__helper() helps bypassing
995+
* all the code related to finding a commit to test.
993996
*/
994997
enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
995998
{
@@ -999,6 +1002,10 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
9991002
enum bisect_error res = BISECT_OK;
10001003
struct object_id *bisect_rev;
10011004
char *steps_msg;
1005+
/*
1006+
* If no_checkout is non-zero, the bisection process does not
1007+
* checkout the trial commit but instead simply updates BISECT_HEAD.
1008+
*/
10021009
int no_checkout = ref_exists("BISECT_HEAD");
10031010
unsigned bisect_flags = 0;
10041011

builtin/bisect--helper.c

Lines changed: 178 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "run-command.h"
99
#include "prompt.h"
1010
#include "quote.h"
11+
#include "revision.h"
1112

1213
static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
1314
static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV")
@@ -29,10 +30,17 @@ static const char * const git_bisect_helper_usage[] = {
2930
N_("git bisect--helper --bisect-terms [--term-good | --term-old | --term-bad | --term-new]"),
3031
N_("git bisect--helper --bisect-start [--term-{old,good}=<term> --term-{new,bad}=<term>]"
3132
" [--no-checkout] [--first-parent] [<bad> [<good>...]] [--] [<paths>...]"),
33+
N_("git bisect--helper --bisect-next"),
34+
N_("git bisect--helper --bisect-auto-next"),
3235
N_("git bisect--helper --bisect-autostart"),
3336
NULL
3437
};
3538

39+
struct add_bisect_ref_data {
40+
struct rev_info *revs;
41+
unsigned int object_flags;
42+
};
43+
3644
struct bisect_terms {
3745
char *term_good;
3846
char *term_bad;
@@ -56,6 +64,8 @@ static void set_terms(struct bisect_terms *terms, const char *bad,
5664
static const char vocab_bad[] = "bad|new";
5765
static const char vocab_good[] = "good|old";
5866

67+
static int bisect_autostart(struct bisect_terms *terms);
68+
5969
/*
6070
* Check whether the string `term` belongs to the set of strings
6171
* included in the variable arguments.
@@ -80,7 +90,7 @@ static int write_in_file(const char *path, const char *mode, const char *format,
8090
FILE *fp = NULL;
8191
int res = 0;
8292

83-
if (strcmp(mode, "w"))
93+
if (strcmp(mode, "w") && strcmp(mode, "a"))
8494
BUG("write-in-file does not support '%s' mode", mode);
8595
fp = fopen(path, mode);
8696
if (!fp)
@@ -109,6 +119,18 @@ static int write_to_file(const char *path, const char *format, ...)
109119
return res;
110120
}
111121

122+
static int append_to_file(const char *path, const char *format, ...)
123+
{
124+
int res;
125+
va_list args;
126+
127+
va_start(args, format);
128+
res = write_in_file(path, "a", format, args);
129+
va_end(args);
130+
131+
return res;
132+
}
133+
112134
static int check_term_format(const char *term, const char *orig_term)
113135
{
114136
int res;
@@ -451,6 +473,142 @@ static int bisect_append_log_quoted(const char **argv)
451473
return res;
452474
}
453475

476+
static int add_bisect_ref(const char *refname, const struct object_id *oid,
477+
int flags, void *cb)
478+
{
479+
struct add_bisect_ref_data *data = cb;
480+
481+
add_pending_oid(data->revs, refname, oid, data->object_flags);
482+
483+
return 0;
484+
}
485+
486+
static int prepare_revs(struct bisect_terms *terms, struct rev_info *revs)
487+
{
488+
int res = 0;
489+
struct add_bisect_ref_data cb = { revs };
490+
char *good = xstrfmt("%s-*", terms->term_good);
491+
492+
/*
493+
* We cannot use terms->term_bad directly in
494+
* for_each_glob_ref_in() and we have to append a '*' to it,
495+
* otherwise for_each_glob_ref_in() will append '/' and '*'.
496+
*/
497+
char *bad = xstrfmt("%s*", terms->term_bad);
498+
499+
/*
500+
* It is important to reset the flags used by revision walks
501+
* as the previous call to bisect_next_all() in turn
502+
* sets up a revision walk.
503+
*/
504+
reset_revision_walk();
505+
init_revisions(revs, NULL);
506+
setup_revisions(0, NULL, revs, NULL);
507+
for_each_glob_ref_in(add_bisect_ref, bad, "refs/bisect/", &cb);
508+
cb.object_flags = UNINTERESTING;
509+
for_each_glob_ref_in(add_bisect_ref, good, "refs/bisect/", &cb);
510+
if (prepare_revision_walk(revs))
511+
res = error(_("revision walk setup failed\n"));
512+
513+
free(good);
514+
free(bad);
515+
return res;
516+
}
517+
518+
static int bisect_skipped_commits(struct bisect_terms *terms)
519+
{
520+
int res;
521+
FILE *fp = NULL;
522+
struct rev_info revs;
523+
struct commit *commit;
524+
struct pretty_print_context pp = {0};
525+
struct strbuf commit_name = STRBUF_INIT;
526+
527+
res = prepare_revs(terms, &revs);
528+
if (res)
529+
return res;
530+
531+
fp = fopen(git_path_bisect_log(), "a");
532+
if (!fp)
533+
return error_errno(_("could not open '%s' for appending"),
534+
git_path_bisect_log());
535+
536+
if (fprintf(fp, "# only skipped commits left to test\n") < 0)
537+
return error_errno(_("failed to write to '%s'"), git_path_bisect_log());
538+
539+
while ((commit = get_revision(&revs)) != NULL) {
540+
strbuf_reset(&commit_name);
541+
format_commit_message(commit, "%s",
542+
&commit_name, &pp);
543+
fprintf(fp, "# possible first %s commit: [%s] %s\n",
544+
terms->term_bad, oid_to_hex(&commit->object.oid),
545+
commit_name.buf);
546+
}
547+
548+
/*
549+
* Reset the flags used by revision walks in case
550+
* there is another revision walk after this one.
551+
*/
552+
reset_revision_walk();
553+
554+
strbuf_release(&commit_name);
555+
fclose(fp);
556+
return 0;
557+
}
558+
559+
static int bisect_successful(struct bisect_terms *terms)
560+
{
561+
struct object_id oid;
562+
struct commit *commit;
563+
struct pretty_print_context pp = {0};
564+
struct strbuf commit_name = STRBUF_INIT;
565+
char *bad_ref = xstrfmt("refs/bisect/%s",terms->term_bad);
566+
int res;
567+
568+
read_ref(bad_ref, &oid);
569+
commit = lookup_commit_reference_by_name(bad_ref);
570+
format_commit_message(commit, "%s", &commit_name, &pp);
571+
572+
res = append_to_file(git_path_bisect_log(), "# first %s commit: [%s] %s\n",
573+
terms->term_bad, oid_to_hex(&commit->object.oid),
574+
commit_name.buf);
575+
576+
strbuf_release(&commit_name);
577+
free(bad_ref);
578+
return res;
579+
}
580+
581+
static enum bisect_error bisect_next(struct bisect_terms *terms, const char *prefix)
582+
{
583+
enum bisect_error res;
584+
585+
if (bisect_autostart(terms))
586+
return BISECT_FAILED;
587+
588+
if (bisect_next_check(terms, terms->term_good))
589+
return BISECT_FAILED;
590+
591+
/* Perform all bisection computation */
592+
res = bisect_next_all(the_repository, prefix);
593+
594+
if (res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND) {
595+
res = bisect_successful(terms);
596+
return res ? res : BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND;
597+
} else if (res == BISECT_ONLY_SKIPPED_LEFT) {
598+
res = bisect_skipped_commits(terms);
599+
return res ? res : BISECT_ONLY_SKIPPED_LEFT;
600+
}
601+
return res;
602+
}
603+
604+
static enum bisect_error bisect_auto_next(struct bisect_terms *terms, const char *prefix)
605+
{
606+
if (bisect_next_check(terms, NULL))
607+
return BISECT_OK;
608+
609+
return bisect_next(terms, prefix);
610+
}
611+
454612
static int bisect_start(struct bisect_terms *terms, const char **argv, int argc)
455613
{
456614
int no_checkout = 0;
@@ -700,6 +858,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
700858
BISECT_TERMS,
701859
BISECT_START,
702860
BISECT_AUTOSTART,
861+
BISECT_NEXT,
862+
BISECT_AUTO_NEXT
703863
} cmdmode = 0;
704864
int res = 0, nolog = 0;
705865
struct option options[] = {
@@ -723,6 +883,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
723883
N_("print out the bisect terms"), BISECT_TERMS),
724884
OPT_CMDMODE(0, "bisect-start", &cmdmode,
725885
N_("start the bisect session"), BISECT_START),
886+
OPT_CMDMODE(0, "bisect-next", &cmdmode,
887+
N_("find the next bisection commit"), BISECT_NEXT),
888+
OPT_CMDMODE(0, "bisect-auto-next", &cmdmode,
889+
N_("verify the next bisection state then checkout the next bisection commit"), BISECT_AUTO_NEXT),
726890
OPT_CMDMODE(0, "bisect-autostart", &cmdmode,
727891
N_("start the bisection if it has not yet been started"), BISECT_AUTOSTART),
728892
OPT_BOOL(0, "no-log", &nolog,
@@ -784,6 +948,18 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
784948
set_terms(&terms, "bad", "good");
785949
res = bisect_start(&terms, argv, argc);
786950
break;
951+
case BISECT_NEXT:
952+
if (argc)
953+
return error(_("--bisect-next requires 0 arguments"));
954+
get_terms(&terms);
955+
res = bisect_next(&terms, prefix);
956+
break;
957+
case BISECT_AUTO_NEXT:
958+
if (argc)
959+
return error(_("--bisect-auto-next requires 0 arguments"));
960+
get_terms(&terms);
961+
res = bisect_auto_next(&terms, prefix);
962+
break;
787963
case BISECT_AUTOSTART:
788964
if (argc)
789965
return error(_("--bisect-autostart does not accept arguments"));
@@ -799,7 +975,7 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
799975
* Handle early success
800976
* From check_merge_bases > check_good_are_ancestors_of_bad > bisect_next_all
801977
*/
802-
if (res == BISECT_INTERNAL_SUCCESS_MERGE_BASE)
978+
if ((res == BISECT_INTERNAL_SUCCESS_MERGE_BASE) || (res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND))
803979
res = BISECT_OK;
804980

805981
return -res;

git-bisect.sh

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ bisect_start() {
6565
#
6666
# Check if we can proceed to the next bisect state.
6767
#
68-
get_terms
69-
bisect_auto_next
68+
git bisect--helper --bisect-auto-next || exit
7069

7170
trap '-' 0
7271
}
@@ -119,45 +118,7 @@ bisect_state() {
119118
*)
120119
usage ;;
121120
esac
122-
bisect_auto_next
123-
}
124-
125-
bisect_auto_next() {
126-
git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD && bisect_next || :
127-
}
128-
129-
bisect_next() {
130-
case "$#" in 0) ;; *) usage ;; esac
131-
git bisect--helper --bisect-autostart || exit
132-
git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD $TERM_GOOD|| exit
133-
134-
# Perform all bisection computation, display and checkout
135-
git bisect--helper --next-all
136-
res=$?
137-
138-
# Check if we should exit because bisection is finished
139-
if test $res -eq 10
140-
then
141-
bad_rev=$(git show-ref --hash --verify refs/bisect/$TERM_BAD)
142-
bad_commit=$(git show-branch $bad_rev)
143-
echo "# first $TERM_BAD commit: $bad_commit" >>"$GIT_DIR/BISECT_LOG"
144-
exit 0
145-
elif test $res -eq 2
146-
then
147-
echo "# only skipped commits left to test" >>"$GIT_DIR/BISECT_LOG"
148-
good_revs=$(git for-each-ref --format="%(objectname)" "refs/bisect/$TERM_GOOD-*")
149-
for skipped in $(git rev-list refs/bisect/$TERM_BAD --not $good_revs)
150-
do
151-
skipped_commit=$(git show-branch $skipped)
152-
echo "# possible first $TERM_BAD commit: $skipped_commit" >>"$GIT_DIR/BISECT_LOG"
153-
done
154-
exit $res
155-
fi
156-
157-
# Check for an error in the bisection process
158-
test $res -ne 0 && exit $res
159-
160-
return 0
121+
git bisect--helper --bisect-auto-next
161122
}
162123

163124
bisect_visualize() {
@@ -213,7 +174,7 @@ bisect_replay () {
213174
esac
214175
done <"$file"
215176
IFS="$oIFS"
216-
bisect_auto_next
177+
git bisect--helper --bisect-auto-next || exit
217178
}
218179

219180
bisect_run () {
@@ -310,7 +271,7 @@ case "$#" in
310271
bisect_skip "$@" ;;
311272
next)
312273
# Not sure we want "next" at the UI level anymore.
313-
bisect_next "$@" ;;
274+
git bisect--helper --bisect-next "$@" || exit ;;
314275
visualize|view)
315276
bisect_visualize "$@" ;;
316277
reset)

0 commit comments

Comments
 (0)