Skip to content

Commit bfacfce

Browse files
tanushree27gitster
authored andcommitted
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 <[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 b8e3b2f commit bfacfce

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

builtin/bisect--helper.c

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -291,26 +291,14 @@ static const char need_bisect_start_warning[] =
291291
"You then need to give me at least one %s and %s revision.\n"
292292
"You can use \"git bisect %s\" and \"git bisect %s\" for that.");
293293

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)
296297
{
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-
307298
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;
314302

315303
if (missing_good && !missing_bad &&
316304
!strcmp(current_term, terms->term_good)) {
@@ -321,29 +309,43 @@ static int bisect_next_check(const struct bisect_terms *terms,
321309
*/
322310
warning(_("bisecting only with a %s commit"), terms->term_bad);
323311
if (!isatty(0))
324-
goto finish;
312+
return 0;
325313
/*
326314
* TRANSLATORS: Make sure to include [Y] and [n] in your
327315
* translation. The program will only accept English input
328316
* at this point.
329317
*/
330318
yesno = git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO);
331319
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;
341322
}
342323

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);
347349
}
348350

349351
static int get_terms(struct bisect_terms *terms)

0 commit comments

Comments
 (0)