@matheusbsbs / matheus-bagliano / commits / 45b6370812
Fork 0

bisect: libify `check_good_are_ancestors_of_bad` and its dependents

Since we want to get rid of git-bisect.sh, it would be necessary to convert those exit() calls to return statements so that errors can be reported. Emulate try catch in C by converting `exit(<positive-value>)` to `return <negative-value>`. Follow POSIX conventions to return <negative-value> to indicate error. Code that turns BISECT_INTERNAL_SUCCESS_MERGE_BASE (-11) to BISECT_OK (0) from `check_good_are_ancestors_of_bad()` has been moved to `cmd_bisect__helper()`. Update all callers to handle the error returns. 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 Feb 17, 2020 at 09:40 UTC 45b6370812cb9bc50212d50f071c8ae5dd851698
2 files changed +37 -15
bisect.c
+27 -14
@@ -872,20 +872,23 @@ static int check_ancestors(struct repository *r, int rev_nr,
872 *
873 * If that's not the case, we need to check the merge bases.
874 * If a merge base must be tested by the user, its source code will be
875 - * checked out to be tested by the user and we will exit.
875 + * checked out to be tested by the user and we will return.
876 */
877 -static void check_good_are_ancestors_of_bad(struct repository *r,
877 +
878 +static enum bisect_error check_good_are_ancestors_of_bad(struct repository *r,
879 const char *prefix,
880 int no_checkout)
881 {
881 - char *filename = git_pathdup("BISECT_ANCESTORS_OK");
882 + char *filename;
883 struct stat st;
884 int fd, rev_nr;
885 enum bisect_error res = BISECT_OK;
886 struct commit **rev;
887
888 if (!current_bad_oid)
888 - die(_("a %s revision is needed"), term_bad);
889 + return error(_("a %s revision is needed"), term_bad);
890 +
891 + filename = git_pathdup("BISECT_ANCESTORS_OK");
892
893 /* Check if file BISECT_ANCESTORS_OK exists. */
894 if (!stat(filename, &st) && S_ISREG(st.st_mode))
@@ -901,18 +904,26 @@ static void check_good_are_ancestors_of_bad(struct repository *r,
904 if (check_ancestors(r, rev_nr, rev, prefix))
905 res = check_merge_bases(rev_nr, rev, no_checkout);
906 free(rev);
904 - if (res)
905 - exit(res == BISECT_INTERNAL_SUCCESS_MERGE_BASE ? BISECT_OK : -res);
907
907 - /* Create file BISECT_ANCESTORS_OK. */
908 - fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600);
909 - if (fd < 0)
910 - warning_errno(_("could not create file '%s'"),
911 - filename);
912 - else
913 - close(fd);
908 + if (!res) {
909 + /* Create file BISECT_ANCESTORS_OK. */
910 + fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0600);
911 + if (fd < 0)
912 + /*
913 + * BISECT_ANCESTORS_OK file is not absolutely necessary,
914 + * the bisection process will continue at the next
915 + * bisection step.
916 + * So, just signal with a warning that something
917 + * might be wrong.
918 + */
919 + warning_errno(_("could not create file '%s'"),
920 + filename);
921 + else
922 + close(fd);
923 + }
924 done:
925 free(filename);
926 + return res;
927 }
928
929 /*
@@ -984,7 +995,9 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix, int
995 if (read_bisect_refs())
996 die(_("reading bisect refs failed"));
997
987 - check_good_are_ancestors_of_bad(r, prefix, no_checkout);
998 + res = check_good_are_ancestors_of_bad(r, prefix, no_checkout);
999 + if (res)
1000 + return res;
1001
1002 bisect_rev_setup(r, &revs, prefix, "%s", "^%s", 1);
1003 revs.limited = 1;
builtin/bisect--helper.c
+10 -1
@@ -666,7 +666,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
666
667 switch (cmdmode) {
668 case NEXT_ALL:
669 - return bisect_next_all(the_repository, prefix, no_checkout);
669 + res = bisect_next_all(the_repository, prefix, no_checkout);
670 + break;
671 case WRITE_TERMS:
672 if (argc != 2)
673 return error(_("--write-terms requires two arguments"));
@@ -713,5 +714,13 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
714 return error("BUG: unknown subcommand '%d'", cmdmode);
715 }
716 free_terms(&terms);
717 +
718 + /*
719 + * Handle early success
720 + * From check_merge_bases > check_good_are_ancestors_of_bad > bisect_next_all
721 + */
722 + if (res == BISECT_INTERNAL_SUCCESS_MERGE_BASE)
723 + res = BISECT_OK;
724 +
725 return abs(res);
726 }