Skip to content

Commit 45b6370

Browse files
pranitbauva1997gitster
authored andcommitted
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 <[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 cdd4dc2 commit 45b6370

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

bisect.c

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -872,20 +872,23 @@ static int check_ancestors(struct repository *r, int rev_nr,
872872
*
873873
* If that's not the case, we need to check the merge bases.
874874
* 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.
876876
*/
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,
878879
const char *prefix,
879880
int no_checkout)
880881
{
881-
char *filename = git_pathdup("BISECT_ANCESTORS_OK");
882+
char *filename;
882883
struct stat st;
883884
int fd, rev_nr;
884885
enum bisect_error res = BISECT_OK;
885886
struct commit **rev;
886887

887888
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");
889892

890893
/* Check if file BISECT_ANCESTORS_OK exists. */
891894
if (!stat(filename, &st) && S_ISREG(st.st_mode))
@@ -901,18 +904,26 @@ static void check_good_are_ancestors_of_bad(struct repository *r,
901904
if (check_ancestors(r, rev_nr, rev, prefix))
902905
res = check_merge_bases(rev_nr, rev, no_checkout);
903906
free(rev);
904-
if (res)
905-
exit(res == BISECT_INTERNAL_SUCCESS_MERGE_BASE ? BISECT_OK : -res);
906907

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+
}
914924
done:
915925
free(filename);
926+
return res;
916927
}
917928

918929
/*
@@ -984,7 +995,9 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix, int
984995
if (read_bisect_refs())
985996
die(_("reading bisect refs failed"));
986997

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;
9881001

9891002
bisect_rev_setup(r, &revs, prefix, "%s", "^%s", 1);
9901003
revs.limited = 1;

builtin/bisect--helper.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
666666

667667
switch (cmdmode) {
668668
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;
670671
case WRITE_TERMS:
671672
if (argc != 2)
672673
return error(_("--write-terms requires two arguments"));
@@ -713,5 +714,13 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
713714
return error("BUG: unknown subcommand '%d'", cmdmode);
714715
}
715716
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+
716725
return abs(res);
717726
}

0 commit comments

Comments
 (0)