@matheusbsbs / matheus-bagliano / commits / 6c69f22233
Fork 0

bisect: libify `bisect_next_all`

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. All the functions calling `bisect_next_all()` are already able to handle return values from it. 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 6c69f2223394c200cce1289ac9200f781ef14da7
2 files changed +27 -12
bisect.c
+19 -10
@@ -976,10 +976,10 @@ void read_bisect_terms(const char **read_bad, const char **read_good)
976 }
977
978 /*
979 - * We use the convention that exiting with an exit code 10 means that
979 + * We use the convention that return BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND (-10) means
980 * the bisection process finished successfully.
981 - * In this case the calling shell script should exit 0.
982 - *
981 + * In this case the calling function or command should not turn a
982 + * BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND return code into an error or a non zero exit code.
983 * If no_checkout is non-zero, the bisection process does not
984 * checkout the trial commit but instead simply updates BISECT_HEAD.
985 */
@@ -1010,23 +1010,25 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix, int
1010
1011 if (!revs.commits) {
1012 /*
1013 - * We should exit here only if the "bad"
1013 + * We should return error here only if the "bad"
1014 * commit is also a "skip" commit.
1015 */
1016 res = error_if_skipped_commits(tried, NULL);
1017 if (res < 0)
1018 - exit(-res);
1018 + return res;
1019 printf(_("%s was both %s and %s\n"),
1020 oid_to_hex(current_bad_oid),
1021 term_good,
1022 term_bad);
1023 - exit(1);
1023 +
1024 + return BISECT_FAILED;
1025 }
1026
1027 if (!all) {
1028 fprintf(stderr, _("No testable commit found.\n"
1029 "Maybe you started with bad path parameters?\n"));
1029 - exit(4);
1030 +
1031 + return BISECT_NO_TESTABLE_COMMIT;
1032 }
1033
1034 bisect_rev = &revs.commits->item->object.oid;
@@ -1034,12 +1036,19 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix, int
1036 if (oideq(bisect_rev, current_bad_oid)) {
1037 res = error_if_skipped_commits(tried, current_bad_oid);
1038 if (res)
1037 - exit(-res);
1039 + return res;
1040 printf("%s is the first %s commit\n", oid_to_hex(bisect_rev),
1041 term_bad);
1042 +
1043 show_diff_tree(r, prefix, revs.commits->item);
1041 - /* This means the bisection process succeeded. */
1042 - exit(10);
1044 + /*
1045 + * This means the bisection process succeeded.
1046 + * Using BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND (-10)
1047 + * so that the call chain can simply check
1048 + * for negative return values for early returns up
1049 + * until the cmd_bisect__helper() caller.
1050 + */
1051 + return BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND;
1052 }
1053
1054 nr = all - reaches - 1;
bisect.h
+8 -2
@@ -39,16 +39,22 @@ struct rev_list_info {
39 * BISECT_FAILED error code: default error code.
40 * BISECT_ONLY_SKIPPED_LEFT error code: only skipped
41 * commits left to be tested.
42 + * BISECT_MERGE_BASE_CHECK error code: merge base check failed.
43 + * BISECT_NO_TESTABLE_COMMIT error code: no testable commit found.
44 + * BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND early success code:
45 + * first term_bad commit found.
46 * BISECT_INTERNAL_SUCCESS_MERGE_BASE early success
47 * code: found merge base that should be tested.
44 - * Early success code BISECT_INTERNAL_SUCCESS_MERGE_BASE
45 - * should be only an internal code.
48 + * Early success codes BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND and
49 + * BISECT_INTERNAL_SUCCESS_MERGE_BASE should be only internal codes.
50 */
51 enum bisect_error {
52 BISECT_OK = 0,
53 BISECT_FAILED = -1,
54 BISECT_ONLY_SKIPPED_LEFT = -2,
55 BISECT_MERGE_BASE_CHECK = -3,
56 + BISECT_NO_TESTABLE_COMMIT = -4,
57 + BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND = -10,
58 BISECT_INTERNAL_SUCCESS_MERGE_BASE = -11
59 };
60