@matheusbsbs / matheus-bagliano / commits / 517ecb3161
Fork 0

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 <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: Miriam Rubio <mirucam@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Pranit Bauva committed Sep 24, 2020 at 14:33 UTC 517ecb3161daa4503f7638489fd44177b3659913
3 files changed +191 -47
bisect.c
+9 -2
@@ -988,8 +988,11 @@ void read_bisect_terms(const char **read_bad, const char **read_good)
988 * the bisection process finished successfully.
989 * In this case the calling function or command should not turn a
990 * 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.
996 */
997 enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
998 {
@@ -999,6 +1002,10 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
1002 enum bisect_error res = BISECT_OK;
1003 struct object_id *bisect_rev;
1004 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 + */
1009 int no_checkout = ref_exists("BISECT_HEAD");
1010 unsigned bisect_flags = 0;
1011
builtin/bisect--helper.c
+178 -2
@@ -8,6 +8,7 @@
8 #include "run-command.h"
9 #include "prompt.h"
10 #include "quote.h"
11 +#include "revision.h"
12
13 static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
14 static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV")
@@ -29,10 +30,17 @@ static const char * const git_bisect_helper_usage[] = {
30 N_("git bisect--helper --bisect-terms [--term-good | --term-old | --term-bad | --term-new]"),
31 N_("git bisect--helper --bisect-start [--term-{old,good}=<term> --term-{new,bad}=<term>]"
32 " [--no-checkout] [--first-parent] [<bad> [<good>...]] [--] [<paths>...]"),
33 + N_("git bisect--helper --bisect-next"),
34 + N_("git bisect--helper --bisect-auto-next"),
35 N_("git bisect--helper --bisect-autostart"),
36 NULL
37 };
38
39 +struct add_bisect_ref_data {
40 + struct rev_info *revs;
41 + unsigned int object_flags;
42 +};
43 +
44 struct bisect_terms {
45 char *term_good;
46 char *term_bad;
@@ -56,6 +64,8 @@ static void set_terms(struct bisect_terms *terms, const char *bad,
64 static const char vocab_bad[] = "bad|new";
65 static const char vocab_good[] = "good|old";
66
67 +static int bisect_autostart(struct bisect_terms *terms);
68 +
69 /*
70 * Check whether the string `term` belongs to the set of strings
71 * included in the variable arguments.
@@ -80,7 +90,7 @@ static int write_in_file(const char *path, const char *mode, const char *format,
90 FILE *fp = NULL;
91 int res = 0;
92
83 - if (strcmp(mode, "w"))
93 + if (strcmp(mode, "w") && strcmp(mode, "a"))
94 BUG("write-in-file does not support '%s' mode", mode);
95 fp = fopen(path, mode);
96 if (!fp)
@@ -109,6 +119,18 @@ static int write_to_file(const char *path, const char *format, ...)
119 return res;
120 }
121
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 +
134 static int check_term_format(const char *term, const char *orig_term)
135 {
136 int res;
@@ -451,6 +473,142 @@ finish:
473 return res;
474 }
475
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 +
612 static int bisect_start(struct bisect_terms *terms, const char **argv, int argc)
613 {
614 int no_checkout = 0;
@@ -700,6 +858,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
858 BISECT_TERMS,
859 BISECT_START,
860 BISECT_AUTOSTART,
861 + BISECT_NEXT,
862 + BISECT_AUTO_NEXT
863 } cmdmode = 0;
864 int res = 0, nolog = 0;
865 struct option options[] = {
@@ -723,6 +883,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
883 N_("print out the bisect terms"), BISECT_TERMS),
884 OPT_CMDMODE(0, "bisect-start", &cmdmode,
885 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),
890 OPT_CMDMODE(0, "bisect-autostart", &cmdmode,
891 N_("start the bisection if it has not yet been started"), BISECT_AUTOSTART),
892 OPT_BOOL(0, "no-log", &nolog,
@@ -784,6 +948,18 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
948 set_terms(&terms, "bad", "good");
949 res = bisect_start(&terms, argv, argc);
950 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;
963 case BISECT_AUTOSTART:
964 if (argc)
965 return error(_("--bisect-autostart does not accept arguments"));
@@ -799,7 +975,7 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
975 * Handle early success
976 * From check_merge_bases > check_good_are_ancestors_of_bad > bisect_next_all
977 */
802 - if (res == BISECT_INTERNAL_SUCCESS_MERGE_BASE)
978 + if ((res == BISECT_INTERNAL_SUCCESS_MERGE_BASE) || (res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND))
979 res = BISECT_OK;
980
981 return -res;
git-bisect.sh
+4 -43
@@ -65,8 +65,7 @@ bisect_start() {
65 #
66 # Check if we can proceed to the next bisect state.
67 #
68 - get_terms
69 - bisect_auto_next
68 + git bisect--helper --bisect-auto-next || exit
69
70 trap '-' 0
71 }
@@ -119,45 +118,7 @@ bisect_state() {
118 *)
119 usage ;;
120 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
122 }
123
124 bisect_visualize() {
@@ -213,7 +174,7 @@ bisect_replay () {
174 esac
175 done <"$file"
176 IFS="$oIFS"
216 - bisect_auto_next
177 + git bisect--helper --bisect-auto-next || exit
178 }
179
180 bisect_run () {
@@ -310,7 +271,7 @@ case "$#" in
271 bisect_skip "$@" ;;
272 next)
273 # Not sure we want "next" at the UI level anymore.
313 - bisect_next "$@" ;;
274 + git bisect--helper --bisect-next "$@" || exit ;;
275 visualize|view)
276 bisect_visualize "$@" ;;
277 reset)