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

bisect--helper: introduce new `decide_next()` function

Let's refactor code from bisect_next_check() into a new decide_next() helper function. This removes some goto statements and makes the code simpler, clearer and easier to understand. While at it `bad_ref` and `good_glob` are not const any more to void casting them inside `free()`. Mentored-by: Christian Couder <chriscool@tuxfamily.org> 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>

Tanushree Tumane committed Feb 17, 2020 at 09:40 UTC bfacfce7d94951fe5b4e70bc2431dae5506b474b
1 file changed +34 -32
builtin/bisect--helper.c
+34 -32
@@ -291,26 +291,14 @@ static const char need_bisect_start_warning[] =
291 "You then need to give me at least one %s and %s revision.\n"
292 "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
293
294 -static int bisect_next_check(const struct bisect_terms *terms,
295 - const char *current_term)
294 +static int decide_next(const struct bisect_terms *terms,
295 + const char *current_term, int missing_good,
296 + int missing_bad)
297 {
297 - int missing_good = 1, missing_bad = 1, res = 0;
298 - const char *bad_ref = xstrfmt("refs/bisect/%s", terms->term_bad);
299 - const char *good_glob = xstrfmt("%s-*", terms->term_good);
300 -
301 - if (ref_exists(bad_ref))
302 - missing_bad = 0;
303 -
304 - for_each_glob_ref_in(mark_good, good_glob, "refs/bisect/",
305 - (void *) &missing_good);
306 -
298 if (!missing_good && !missing_bad)
308 - goto finish;
309 -
310 - if (!current_term) {
311 - res = -1;
312 - goto finish;
313 - }
299 + return 0;
300 + if (!current_term)
301 + return -1;
302
303 if (missing_good && !missing_bad &&
304 !strcmp(current_term, terms->term_good)) {
@@ -321,7 +309,7 @@ static int bisect_next_check(const struct bisect_terms *terms,
309 */
310 warning(_("bisecting only with a %s commit"), terms->term_bad);
311 if (!isatty(0))
324 - goto finish;
312 + return 0;
313 /*
314 * TRANSLATORS: Make sure to include [Y] and [n] in your
315 * translation. The program will only accept English input
@@ -329,21 +317,35 @@ static int bisect_next_check(const struct bisect_terms *terms,
317 */
318 yesno = git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO);
319 if (starts_with(yesno, "N") || starts_with(yesno, "n"))
332 - res = -1;
333 - goto finish;
334 - }
335 - if (!is_empty_or_missing_file(git_path_bisect_start())) {
336 - res = error(_(need_bad_and_good_revision_warning),
337 - vocab_bad, vocab_good, vocab_bad, vocab_good);
338 - } else {
339 - res = error(_(need_bisect_start_warning),
340 - vocab_good, vocab_bad, vocab_good, vocab_bad);
320 + return -1;
321 + return 0;
322 }
323
343 -finish:
344 - free((void *) good_glob);
345 - free((void *) bad_ref);
346 - return res;
324 + if (!is_empty_or_missing_file(git_path_bisect_start()))
325 + return error(_(need_bad_and_good_revision_warning),
326 + vocab_bad, vocab_good, vocab_bad, vocab_good);
327 + else
328 + return error(_(need_bisect_start_warning),
329 + vocab_good, vocab_bad, vocab_good, vocab_bad);
330 +}
331 +
332 +static int bisect_next_check(const struct bisect_terms *terms,
333 + const char *current_term)
334 +{
335 + int missing_good = 1, missing_bad = 1;
336 + char *bad_ref = xstrfmt("refs/bisect/%s", terms->term_bad);
337 + char *good_glob = xstrfmt("%s-*", terms->term_good);
338 +
339 + if (ref_exists(bad_ref))
340 + missing_bad = 0;
341 +
342 + for_each_glob_ref_in(mark_good, good_glob, "refs/bisect/",
343 + (void *) &missing_good);
344 +
345 + free(good_glob);
346 + free(bad_ref);
347 +
348 + return decide_next(terms, current_term, missing_good, missing_bad);
349 }
350
351 static int get_terms(struct bisect_terms *terms)