@matheusbsbs / matheus-bagliano / commits / e8e3ce6718
Fork 0

bisect: libify `bisect_checkout`

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. Turn `exit()` to `return` calls in `bisect_checkout()`. Changes related to return values have no bad side effects on the code that calls `bisect_checkout()`. 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 e8e3ce67185fd493a07d84a5ce21b469701f8f64
1 file changed +15 -4
bisect.c
+15 -4
@@ -704,9 +704,10 @@ static int is_expected_rev(const struct object_id *oid)
704 return res;
705 }
706
707 -static int bisect_checkout(const struct object_id *bisect_rev, int no_checkout)
707 +static enum bisect_error bisect_checkout(const struct object_id *bisect_rev, int no_checkout)
708 {
709 char bisect_rev_hex[GIT_MAX_HEXSZ + 1];
710 + enum bisect_error res = BISECT_OK;
711
712 memcpy(bisect_rev_hex, oid_to_hex(bisect_rev), the_hash_algo->hexsz + 1);
713 update_ref(NULL, "BISECT_EXPECTED_REV", bisect_rev, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
@@ -716,14 +717,24 @@ static int bisect_checkout(const struct object_id *bisect_rev, int no_checkout)
717 update_ref(NULL, "BISECT_HEAD", bisect_rev, NULL, 0,
718 UPDATE_REFS_DIE_ON_ERR);
719 } else {
719 - int res;
720 res = run_command_v_opt(argv_checkout, RUN_GIT_CMD);
721 if (res)
722 - exit(res);
722 + /*
723 + * Errors in `run_command()` itself, signaled by res < 0,
724 + * and errors in the child process, signaled by res > 0
725 + * can both be treated as regular BISECT_FAILURE (-1).
726 + */
727 + return -abs(res);
728 }
729
730 argv_show_branch[1] = bisect_rev_hex;
726 - return run_command_v_opt(argv_show_branch, RUN_GIT_CMD);
731 + res = run_command_v_opt(argv_show_branch, RUN_GIT_CMD);
732 + /*
733 + * Errors in `run_command()` itself, signaled by res < 0,
734 + * and errors in the child process, signaled by res > 0
735 + * can both be treated as regular BISECT_FAILURE (-1).
736 + */
737 + return -abs(res);
738 }
739
740 static struct commit *get_commit_reference(struct repository *r,