git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v4 04/12] bisect--helper: introduce new `decide_next()` function

From
Miriam Rubio <mirucam@gmail.com>
Date
Feb 17, 2020, 08:40 UTC
Message-ID
<20200217084039.78215-5-mirucam@gmail.com>
In-Reply-To
<20200217084039.78215-1-mirucam@gmail.com>
From: Tanushree Tumane <tanushreetumane@gmail.com>

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>
---
 builtin/bisect--helper.c | 66 +++++++++++++++++++++-------------------
 1 file changed, 34 insertions(+), 32 deletions(-)
diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c
index 21de5c096c..e21d3d1a4c 100644
--- a/builtin/bisect--helper.c
+++ b/builtin/bisect--helper.c
@@ -291,26 +291,14 @@ static const char need_bisect_start_warning[] =
 	   "You then need to give me at least one %s and %s revision.\n"
 	   "You can use \"git bisect %s\" and \"git bisect %s\" for that.");
 
-static int bisect_next_check(const struct bisect_terms *terms,
-			     const char *current_term)
+static int decide_next(const struct bisect_terms *terms,
+		       const char *current_term, int missing_good,
+		       int missing_bad)
 {
-	int missing_good = 1, missing_bad = 1, res = 0;
-	const char *bad_ref = xstrfmt("refs/bisect/%s", terms->term_bad);
-	const char *good_glob = xstrfmt("%s-*", terms->term_good);
-
-	if (ref_exists(bad_ref))
-		missing_bad = 0;
-
-	for_each_glob_ref_in(mark_good, good_glob, "refs/bisect/",
-			     (void *) &missing_good);
-
 	if (!missing_good && !missing_bad)
-		goto finish;
-
-	if (!current_term) {
-		res = -1;
-		goto finish;
-	}
+		return 0;
+	if (!current_term)
+		return -1;
 
 	if (missing_good && !missing_bad &&
 	    !strcmp(current_term, terms->term_good)) {
@@ -321,7 +309,7 @@ static int bisect_next_check(const struct bisect_terms *terms,
 		 */
 		warning(_("bisecting only with a %s commit"), terms->term_bad);
 		if (!isatty(0))
-			goto finish;
+			return 0;
 		/*
 		 * TRANSLATORS: Make sure to include [Y] and [n] in your
 		 * translation. The program will only accept English input
@@ -329,21 +317,35 @@ static int bisect_next_check(const struct bisect_terms *terms,
 		 */
 		yesno = git_prompt(_("Are you sure [Y/n]? "), PROMPT_ECHO);
 		if (starts_with(yesno, "N") || starts_with(yesno, "n"))
-			res = -1;
-		goto finish;
-	}
-	if (!is_empty_or_missing_file(git_path_bisect_start())) {
-		res = error(_(need_bad_and_good_revision_warning),
-			       vocab_bad, vocab_good, vocab_bad, vocab_good);
-	} else {
-		res = error(_(need_bisect_start_warning),
-			       vocab_good, vocab_bad, vocab_good, vocab_bad);
+			return -1;
+		return 0;
 	}
 
-finish:
-	free((void *) good_glob);
-	free((void *) bad_ref);
-	return res;
+	if (!is_empty_or_missing_file(git_path_bisect_start()))
+		return error(_(need_bad_and_good_revision_warning),
+			     vocab_bad, vocab_good, vocab_bad, vocab_good);
+	else
+		return error(_(need_bisect_start_warning),
+			     vocab_good, vocab_bad, vocab_good, vocab_bad);
+}
+
+static int bisect_next_check(const struct bisect_terms *terms,
+			     const char *current_term)
+{
+	int missing_good = 1, missing_bad = 1;
+	char *bad_ref = xstrfmt("refs/bisect/%s", terms->term_bad);
+	char *good_glob = xstrfmt("%s-*", terms->term_good);
+
+	if (ref_exists(bad_ref))
+		missing_bad = 0;
+
+	for_each_glob_ref_in(mark_good, good_glob, "refs/bisect/",
+			     (void *) &missing_good);
+
+	free(good_glob);
+	free(bad_ref);
+
+	return decide_next(terms, current_term, missing_good, missing_bad);
 }
 
 static int get_terms(struct bisect_terms *terms)
-- 
2.25.0
Previous: Miriam RubioNext: Miriam Rubio
Message 6 of 14 in “Finish converting git bisect to C part 1”
  1. Miriam RubioFeb 17, 2020
  2. 01/12 bisect--helper: convert `vocab_*` char pointers to char arraysMiriam Rubio, Feb 17, 2020
  3. 03/12 bisect: use the standard 'if (!var)' way to check for 0Miriam Rubio, Feb 17, 2020
  4. 02/12 bisect--helper: change `retval` to `res`Miriam Rubio, Feb 17, 2020
  5. 05/12 bisect: add enum to represent bisect returning codesMiriam Rubio, Feb 17, 2020
  6. 04/12 bisect--helper: introduce new `decide_next()` functionMiriam Rubio, Feb 17, 2020
  7. 06/12 bisect--helper: return error codes from `cmd_bisect__helper()`Miriam Rubio, Feb 17, 2020
  8. 08/12 bisect: libify `bisect_checkout`Miriam Rubio, Feb 17, 2020
  9. 07/12 bisect: libify `exit_if_skipped_commits` to `error_if_skipped*` and its dependentsMiriam Rubio, Feb 17, 2020
  10. 10/12 bisect: libify `check_good_are_ancestors_of_bad` and its dependentsMiriam Rubio, Feb 17, 2020
  11. 09/12 bisect: libify `check_merge_bases` and its dependentsMiriam Rubio, Feb 17, 2020
  12. 11/12 bisect: libify `handle_bad_merge_base` and its dependentsMiriam Rubio, Feb 17, 2020
  13. 12/12 bisect: libify `bisect_next_all`Miriam Rubio, Feb 17, 2020
  14. Junio C HamanoFeb 19, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.