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

[PATCH v17 6/7] bisect--helper: `get_terms` & `bisect_terms` shell function in C

From
Pranit Bauva via GitGitGadget <gitgitgadget@gmail.com>
Date
Jan 2, 2019, 15:38 UTC
Message-ID
<f2b1706e5be93d8481dfd99fcb4c0a705fd36784.1546443509.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.101.v17.git.gitgitgadget@gmail.com>
From: Pranit Bauva <pranit.bauva@gmail.com>

Reimplement the `get_terms` and `bisect_terms` shell function in C and add `bisect-terms` subcommand to `git bisect--helper` to call it from git-bisect.sh .

Using `--bisect-terms` subcommand is a temporary measure to port shell function in C so as to use the existing test suite. As more functions are ported, this subcommand will be retired but its implementation will be called by some other methods.

Also use error() to report "no terms defined" and accordingly change the test in t6030.

We need to use PARSE_OPT_KEEP_UNKNOWN here to allow for parameters that look like options (e.g --term-good) but should not be parsed by cmd_bisect__helper(). This change is safe because all other cmdmodes have strict argc checks already.

Mentored-by: Lars Schneider <larsxschneider@gmail.com>
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>
---
 builtin/bisect--helper.c    | 62 +++++++++++++++++++++++++++++++++++--
 git-bisect.sh               | 35 ++-------------------
 t/t6030-bisect-porcelain.sh |  2 +-
 3 files changed, 63 insertions(+), 36 deletions(-)
diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c
index 38ae825b9a..d61edd3c91 100644
--- a/builtin/bisect--helper.c
+++ b/builtin/bisect--helper.c
@@ -23,6 +23,7 @@ static const char * const git_bisect_helper_usage[] = {
 	N_("git bisect--helper --bisect-write [--no-log] <state> <revision> <good_term> <bad_term>"),
 	N_("git bisect--helper --bisect-check-and-set-terms <command> <good_term> <bad_term>"),
 	N_("git bisect--helper --bisect-next-check <good_term> <bad_term> [<term>]"),
+	N_("git bisect--helper --bisect-terms [--term-good | --term-old | --term-bad | --term-new]"),
 	NULL
 };
 
@@ -339,6 +340,55 @@ static int bisect_next_check(const struct bisect_terms *terms,
 	return retval;
 }
 
+static int get_terms(struct bisect_terms *terms)
+{
+	struct strbuf str = STRBUF_INIT;
+	FILE *fp = NULL;
+	int res = 0;
+
+	fp = fopen(git_path_bisect_terms(), "r");
+	if (!fp) {
+		res = -1;
+		goto finish;
+	}
+
+	free_terms(terms);
+	strbuf_getline_lf(&str, fp);
+	terms->term_bad = strbuf_detach(&str, NULL);
+	strbuf_getline_lf(&str, fp);
+	terms->term_good = strbuf_detach(&str, NULL);
+
+finish:
+	if (fp)
+		fclose(fp);
+	strbuf_release(&str);
+	return res;
+}
+
+static int bisect_terms(struct bisect_terms *terms, const char *option)
+{
+	if (get_terms(terms))
+		return error(_("no terms defined"));
+
+	if (option == NULL) {
+		printf(_("Your current terms are %s for the old state\n"
+			 "and %s for the new state.\n"),
+		       terms->term_good, terms->term_bad);
+		return 0;
+	}
+	if (one_of(option, "--term-good", "--term-old", NULL))
+		printf("%s\n", terms->term_good);
+	else if (one_of(option, "--term-bad", "--term-new", NULL))
+		printf("%s\n", terms->term_bad);
+	else
+		return error(_("invalid argument %s for 'git bisect terms'.\n"
+			       "Supported options are: "
+			       "--term-good|--term-old and "
+			       "--term-bad|--term-new."), option);
+
+	return 0;
+}
+
 int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 {
 	enum {
@@ -349,7 +399,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 		BISECT_RESET,
 		BISECT_WRITE,
 		CHECK_AND_SET_TERMS,
-		BISECT_NEXT_CHECK
+		BISECT_NEXT_CHECK,
+		BISECT_TERMS
 	} cmdmode = 0;
 	int no_checkout = 0, res = 0, nolog = 0;
 	struct option options[] = {
@@ -369,6 +420,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 			 N_("check and set terms in a bisection state"), CHECK_AND_SET_TERMS),
 		OPT_CMDMODE(0, "bisect-next-check", &cmdmode,
 			 N_("check whether bad or good terms exist"), BISECT_NEXT_CHECK),
+		OPT_CMDMODE(0, "bisect-terms", &cmdmode,
+			 N_("print out the bisect terms"), BISECT_TERMS),
 		OPT_BOOL(0, "no-checkout", &no_checkout,
 			 N_("update BISECT_HEAD instead of checking out the current commit")),
 		OPT_BOOL(0, "no-log", &nolog,
@@ -378,7 +431,7 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 	struct bisect_terms terms = { .term_good = NULL, .term_bad = NULL };
 
 	argc = parse_options(argc, argv, prefix, options,
-			     git_bisect_helper_usage, 0);
+			     git_bisect_helper_usage, PARSE_OPT_KEEP_UNKNOWN);
 
 	if (!cmdmode)
 		usage_with_options(git_bisect_helper_usage, options);
@@ -419,6 +472,11 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 		set_terms(&terms, argv[1], argv[0]);
 		res = bisect_next_check(&terms, argc == 3 ? argv[2] : NULL);
 		break;
+	case BISECT_TERMS:
+		if (argc > 1)
+			return error(_("--bisect-terms requires 0 or 1 argument"));
+		res = bisect_terms(&terms, argc == 1 ? argv[0] : NULL);
+		break;
 	default:
 		return error("BUG: unknown subcommand '%d'", cmdmode);
 	}
diff --git a/git-bisect.sh b/git-bisect.sh
index 5ef3e25621..bdb614e3c2 100755
--- a/git-bisect.sh
+++ b/git-bisect.sh
@@ -355,7 +355,7 @@ bisect_replay () {
 		"$TERM_GOOD"|"$TERM_BAD"|skip)
 			git bisect--helper --bisect-write "$command" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit;;
 		terms)
-			bisect_terms $rev ;;
+			git bisect--helper --bisect-terms $rev || exit;;
 		*)
 			die "$(gettext "?? what are you talking about?")" ;;
 		esac
@@ -439,37 +439,6 @@ get_terms () {
 	fi
 }
 
-bisect_terms () {
-	get_terms
-	if ! test -s "$GIT_DIR/BISECT_TERMS"
-	then
-		die "$(gettext "no terms defined")"
-	fi
-	case "$#" in
-	0)
-		gettextln "Your current terms are $TERM_GOOD for the old state
-and $TERM_BAD for the new state."
-		;;
-	1)
-		arg=$1
-		case "$arg" in
-			--term-good|--term-old)
-				printf '%s\n' "$TERM_GOOD"
-				;;
-			--term-bad|--term-new)
-				printf '%s\n' "$TERM_BAD"
-				;;
-			*)
-				die "$(eval_gettext "invalid argument \$arg for 'git bisect terms'.
-Supported options are: --term-good|--term-old and --term-bad|--term-new.")"
-				;;
-		esac
-		;;
-	*)
-		usage ;;
-	esac
-}
-
 case "$#" in
 0)
 	usage ;;
@@ -500,7 +469,7 @@ case "$#" in
 	run)
 		bisect_run "$@" ;;
 	terms)
-		bisect_terms "$@" ;;
+		git bisect--helper --bisect-terms "$@" || exit;;
 	*)
 		usage ;;
 	esac
diff --git a/t/t6030-bisect-porcelain.sh b/t/t6030-bisect-porcelain.sh
index f84ff941c3..55835ee4a4 100755
--- a/t/t6030-bisect-porcelain.sh
+++ b/t/t6030-bisect-porcelain.sh
@@ -802,7 +802,7 @@ test_expect_success 'bisect terms needs 0 or 1 argument' '
 	test_must_fail git bisect terms only-one &&
 	test_must_fail git bisect terms 1 2 &&
 	test_must_fail git bisect terms 2>actual &&
-	echo "no terms defined" >expected &&
+	echo "error: no terms defined" >expected &&
 	test_i18ncmp expected actual
 '
 
-- 
gitgitgadget
Previous: Pranit Bauva via GitGitGadgetNext: Ramsay Jones
Message 48 of 50 in “bisect--helper: `bisect_reset` shell function in C”
  1. 1/8 bisect--helper: `bisect_reset` shell function in CPranit Bauva, Oct 27, 2017
  2. 7/8 bisect--helper: `bisect_start` shell function partially in CPranit Bauva, Oct 27, 2017
  3. Stephan BeyerOct 30, 2017
  4. Pranit BauvaOct 30, 2017
  5. SZEDER GáborFeb 16, 2018
  6. 2/8 bisect--helper: `bisect_write` shell function in CPranit Bauva, Oct 27, 2017
  7. Martin ÅgrenOct 27, 2017
  8. Pranit BauvaOct 30, 2017
  9. Johannes SchindelinNov 23, 2018
  10. Martin ÅgrenNov 23, 2018
  11. Johannes SchindelinNov 26, 2018
  12. Junio C HamanoOct 27, 2017
  13. Pranit BauvaOct 30, 2017
  14. Stephan BeyerOct 30, 2017
  15. Stephan BeyerOct 30, 2017
  16. Ramsay JonesNov 8, 2017
  17. 3/8 wrapper: move is_empty_file() and rename it as is_empty_or_missing_file()Pranit Bauva, Oct 27, 2017
  18. 5/8 bisect--helper: `bisect_next_check` shell function in CPranit Bauva, Oct 27, 2017
  19. Martin ÅgrenOct 27, 2017
  20. Pranit BauvaOct 30, 2017
  21. Ramsay JonesNov 8, 2017
  22. Stephan BeyerNov 12, 2017
  23. Stephan BeyerNov 12, 2017
  24. Stephan BeyerNov 12, 2017
  25. Junio C HamanoNov 13, 2017
  26. 8/8 t6030: make various test to pass GETTEXT_POISON testsPranit Bauva, Oct 27, 2017
  27. 4/8 bisect--helper: `check_and_set_terms` shell function in CPranit Bauva, Oct 27, 2017
  28. Ramsay JonesNov 8, 2017
  29. 6/8 bisect--helper: `get_terms` & `bisect_terms` shell function in CPranit Bauva, Oct 27, 2017
  30. Martin ÅgrenOct 27, 2017
  31. Pranit BauvaOct 30, 2017
  32. Stephan BeyerOct 30, 2017
  33. Pranit BauvaOct 30, 2017
  34. Ramsay JonesNov 8, 2017
  35. Pranit BauvaOct 27, 2017
  36. Junio C HamanoOct 27, 2017
  37. Pranit BauvaOct 30, 2017
  38. Stephan BeyerOct 30, 2017
  39. Pranit BauvaOct 30, 2017
  40. Ramsay JonesNov 8, 2017
  41. 0/7 git bisect: convert from shell to CTanushree Tumane via GitGitGadget, Jan 2, 2019
  42. 1/7 bisect--helper: `bisect_reset` shell function in CPranit Bauva via GitGitGadget, Jan 2, 2019
  43. 2/7 bisect--helper: `bisect_write` shell function in CPranit Bauva via GitGitGadget, Jan 2, 2019
  44. 4/7 bisect--helper: `check_and_set_terms` shell function in CPranit Bauva via GitGitGadget, Jan 2, 2019
  45. 5/7 bisect--helper: `bisect_next_check` shell function in CPranit Bauva via GitGitGadget, Jan 2, 2019
  46. 7/7 bisect--helper: `bisect_start` shell function partially in CPranit Bauva via GitGitGadget, Jan 2, 2019
  47. 3/7 wrapper: move is_empty_file() and rename it as is_empty_or_missing_file()Pranit Bauva via GitGitGadget, Jan 2, 2019
  48. 6/7 bisect--helper: `get_terms` & `bisect_terms` shell function in CPranit Bauva via GitGitGadget, Jan 2, 2019
  49. Ramsay JonesJan 3, 2019
  50. TANUSHREE TUMANEJan 7, 2019

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.