Skip to content

Commit 6c69f22

Browse files
pranitbauva1997gitster
authored andcommittedFeb 19, 2020
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 <[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 9ec598e commit 6c69f22

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed
 

Diff for: ‎bisect.c

+19-10
Original file line numberDiff line numberDiff line change
@@ -976,10 +976,10 @@ void read_bisect_terms(const char **read_bad, const char **read_good)
976976
}
977977

978978
/*
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
980980
* 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.
983983
* If no_checkout is non-zero, the bisection process does not
984984
* checkout the trial commit but instead simply updates BISECT_HEAD.
985985
*/
@@ -1010,36 +1010,45 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix, int
10101010

10111011
if (!revs.commits) {
10121012
/*
1013-
* We should exit here only if the "bad"
1013+
* We should return error here only if the "bad"
10141014
* commit is also a "skip" commit.
10151015
*/
10161016
res = error_if_skipped_commits(tried, NULL);
10171017
if (res < 0)
1018-
exit(-res);
1018+
return res;
10191019
printf(_("%s was both %s and %s\n"),
10201020
oid_to_hex(current_bad_oid),
10211021
term_good,
10221022
term_bad);
1023-
exit(1);
1023+
1024+
return BISECT_FAILED;
10241025
}
10251026

10261027
if (!all) {
10271028
fprintf(stderr, _("No testable commit found.\n"
10281029
"Maybe you started with bad path parameters?\n"));
1029-
exit(4);
1030+
1031+
return BISECT_NO_TESTABLE_COMMIT;
10301032
}
10311033

10321034
bisect_rev = &revs.commits->item->object.oid;
10331035

10341036
if (oideq(bisect_rev, current_bad_oid)) {
10351037
res = error_if_skipped_commits(tried, current_bad_oid);
10361038
if (res)
1037-
exit(-res);
1039+
return res;
10381040
printf("%s is the first %s commit\n", oid_to_hex(bisect_rev),
10391041
term_bad);
1042+
10401043
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;
10431052
}
10441053

10451054
nr = all - reaches - 1;

Diff for: ‎bisect.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,22 @@ struct rev_list_info {
3939
* BISECT_FAILED error code: default error code.
4040
* BISECT_ONLY_SKIPPED_LEFT error code: only skipped
4141
* 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.
4246
* BISECT_INTERNAL_SUCCESS_MERGE_BASE early success
4347
* 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.
4650
*/
4751
enum bisect_error {
4852
BISECT_OK = 0,
4953
BISECT_FAILED = -1,
5054
BISECT_ONLY_SKIPPED_LEFT = -2,
5155
BISECT_MERGE_BASE_CHECK = -3,
56+
BISECT_NO_TESTABLE_COMMIT = -4,
57+
BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND = -10,
5258
BISECT_INTERNAL_SUCCESS_MERGE_BASE = -11
5359
};
5460

0 commit comments

Comments
 (0)
Please sign in to comment.