bisect--helper: reimplement `bisect_state` & `bisect_head` shell functions in C

Reimplement the `bisect_state()` shell functions in C and also add a subcommand `--bisect-state` to `git-bisect--helper` to call them from git-bisect.sh . Using `--bisect-state` subcommand is a temporary measure to port shell function to C so as to use the existing test suite. As more functions are ported, this subcommand will be retired and will be called by some other methods. `bisect_head()` is only called from `bisect_state()`, thus it is not required to introduce another subcommand. Note that the `eval` in the changed line of `git-bisect.sh` cannot be dropped: it is necessary because the `rev` and the `tail` variables may contain multiple, quoted arguments that need to be passed to `bisect--helper` (without the quotes, naturally). 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> Reviewed-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Pranit Bauva committed Oct 15, 2020 at 15:38 UTC 27257bc4661ed7ffb715922693c7a2bee136824c
2 files changed +89 -52
builtin/bisect--helper.c
+85 -1
@@ -31,6 +31,8 @@ static const char * const git_bisect_helper_usage[] = {
31 N_("git bisect--helper --bisect-next"),
32 N_("git bisect--helper --bisect-auto-next"),
33 N_("git bisect--helper --bisect-autostart"),
34 + N_("git bisect--helper --bisect-state (bad|new) [<rev>]"),
35 + N_("git bisect--helper --bisect-state (good|old) [<rev>...]"),
36 NULL
37 };
38
@@ -855,6 +857,80 @@ static int bisect_autostart(struct bisect_terms *terms)
857 return res;
858 }
859
860 +static enum bisect_error bisect_state(struct bisect_terms *terms, const char **argv,
861 + int argc)
862 +{
863 + const char *state;
864 + int i, verify_expected = 1;
865 + struct object_id oid, expected;
866 + struct strbuf buf = STRBUF_INIT;
867 + struct oid_array revs = OID_ARRAY_INIT;
868 +
869 + if (!argc)
870 + return error(_("Please call `--bisect-state` with at least one argument"));
871 +
872 + if (bisect_autostart(terms))
873 + return BISECT_FAILED;
874 +
875 + state = argv[0];
876 + if (check_and_set_terms(terms, state) ||
877 + !one_of(state, terms->term_good, terms->term_bad, "skip", NULL))
878 + return BISECT_FAILED;
879 +
880 + argv++;
881 + argc--;
882 + if (argc > 1 && !strcmp(state, terms->term_bad))
883 + return error(_("'git bisect %s' can take only one argument."), terms->term_bad);
884 +
885 + if (argc == 0) {
886 + const char *head = "BISECT_HEAD";
887 + enum get_oid_result res_head = get_oid(head, &oid);
888 +
889 + if (res_head == MISSING_OBJECT) {
890 + head = "HEAD";
891 + res_head = get_oid(head, &oid);
892 + }
893 +
894 + if (res_head)
895 + error(_("Bad rev input: %s"), head);
896 + oid_array_append(&revs, &oid);
897 + }
898 +
899 + /*
900 + * All input revs must be checked before executing bisect_write()
901 + * to discard junk revs.
902 + */
903 +
904 + for (; argc; argc--, argv++) {
905 + if (get_oid(*argv, &oid)){
906 + error(_("Bad rev input: %s"), *argv);
907 + oid_array_clear(&revs);
908 + return BISECT_FAILED;
909 + }
910 + oid_array_append(&revs, &oid);
911 + }
912 +
913 + if (strbuf_read_file(&buf, git_path_bisect_expected_rev(), 0) < the_hash_algo->hexsz ||
914 + get_oid_hex(buf.buf, &expected) < 0)
915 + verify_expected = 0; /* Ignore invalid file contents */
916 + strbuf_release(&buf);
917 +
918 + for (i = 0; i < revs.nr; i++) {
919 + if (bisect_write(state, oid_to_hex(&revs.oid[i]), terms, 0)) {
920 + oid_array_clear(&revs);
921 + return BISECT_FAILED;
922 + }
923 + if (verify_expected && !oideq(&revs.oid[i], &expected)) {
924 + unlink_or_warn(git_path_bisect_ancestors_ok());
925 + unlink_or_warn(git_path_bisect_expected_rev());
926 + verify_expected = 0;
927 + }
928 + }
929 +
930 + oid_array_clear(&revs);
931 + return bisect_auto_next(terms, NULL);
932 +}
933 +
934 int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
935 {
936 enum {
@@ -868,7 +944,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
944 BISECT_START,
945 BISECT_AUTOSTART,
946 BISECT_NEXT,
871 - BISECT_AUTO_NEXT
947 + BISECT_AUTO_NEXT,
948 + BISECT_STATE
949 } cmdmode = 0;
950 int res = 0, nolog = 0;
951 struct option options[] = {
@@ -894,6 +971,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
971 N_("verify the next bisection state then checkout the next bisection commit"), BISECT_AUTO_NEXT),
972 OPT_CMDMODE(0, "bisect-autostart", &cmdmode,
973 N_("start the bisection if it has not yet been started"), BISECT_AUTOSTART),
974 + OPT_CMDMODE(0, "bisect-state", &cmdmode,
975 + N_("mark the state of ref (or refs)"), BISECT_STATE),
976 OPT_BOOL(0, "no-log", &nolog,
977 N_("no log for BISECT_WRITE")),
978 OPT_END()
@@ -964,6 +1043,11 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
1043 set_terms(&terms, "bad", "good");
1044 res = bisect_autostart(&terms);
1045 break;
1046 + case BISECT_STATE:
1047 + set_terms(&terms, "bad", "good");
1048 + get_terms(&terms);
1049 + res = bisect_state(&terms, argv, argc);
1050 + break;
1051 default:
1052 BUG("unknown subcommand %d", cmdmode);
1053 }
git-bisect.sh
+4 -51
@@ -39,16 +39,6 @@ _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
39 TERM_BAD=bad
40 TERM_GOOD=good
41
42 -bisect_head()
43 -{
44 - if git rev-parse --verify -q BISECT_HEAD > /dev/null
45 - then
46 - echo BISECT_HEAD
47 - else
48 - echo HEAD
49 - fi
50 -}
51 -
42 bisect_skip() {
43 all=''
44 for arg in "$@"
@@ -61,43 +51,7 @@ bisect_skip() {
51 esac
52 all="$all $revs"
53 done
64 - eval bisect_state 'skip' $all
65 -}
66 -
67 -bisect_state() {
68 - git bisect--helper --bisect-autostart || exit
69 - state=$1
70 - git bisect--helper --check-and-set-terms $state $TERM_GOOD $TERM_BAD || exit
71 - get_terms
72 - case "$#,$state" in
73 - 0,*)
74 - die "Please call 'bisect_state' with at least one argument." ;;
75 - 1,"$TERM_BAD"|1,"$TERM_GOOD"|1,skip)
76 - bisected_head=$(bisect_head)
77 - rev=$(git rev-parse --verify "$bisected_head") ||
78 - die "$(eval_gettext "Bad rev input: \$bisected_head")"
79 - git bisect--helper --bisect-write "$state" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit
80 - git bisect--helper --check-expected-revs "$rev" ;;
81 - 2,"$TERM_BAD"|*,"$TERM_GOOD"|*,skip)
82 - shift
83 - hash_list=''
84 - for rev in "$@"
85 - do
86 - sha=$(git rev-parse --verify "$rev^{commit}") ||
87 - die "$(eval_gettext "Bad rev input: \$rev")"
88 - hash_list="$hash_list $sha"
89 - done
90 - for rev in $hash_list
91 - do
92 - git bisect--helper --bisect-write "$state" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit
93 - done
94 - git bisect--helper --check-expected-revs $hash_list ;;
95 - *,"$TERM_BAD")
96 - die "$(eval_gettext "'git bisect \$TERM_BAD' can take only one argument.")" ;;
97 - *)
98 - usage ;;
99 - esac
100 - git bisect--helper --bisect-auto-next
54 + eval git bisect--helper --bisect-state 'skip' $all
55 }
56
57 bisect_visualize() {
@@ -187,8 +141,7 @@ exit code \$res from '\$command' is < 0 or >= 128" >&2
141 state="$TERM_GOOD"
142 fi
143
190 - # We have to use a subshell because "bisect_state" can exit.
191 - ( bisect_state $state >"$GIT_DIR/BISECT_RUN" )
144 + git bisect--helper --bisect-state $state >"$GIT_DIR/BISECT_RUN"
145 res=$?
146
147 cat "$GIT_DIR/BISECT_RUN"
@@ -203,7 +156,7 @@ exit code \$res from '\$command' is < 0 or >= 128" >&2
156 if [ $res -ne 0 ]
157 then
158 eval_gettextln "bisect run failed:
206 -'bisect_state \$state' exited with error code \$res" >&2
159 +'bisect-state \$state' exited with error code \$res" >&2
160 exit $res
161 fi
162
@@ -244,7 +197,7 @@ case "$#" in
197 start)
198 git bisect--helper --bisect-start "$@" ;;
199 bad|good|new|old|"$TERM_BAD"|"$TERM_GOOD")
247 - bisect_state "$cmd" "$@" ;;
200 + git bisect--helper --bisect-state "$cmd" "$@" ;;
201 skip)
202 bisect_skip "$@" ;;
203 next)