Skip to contentSkip to conversation

Finish converting git bisect to C part 4

PATCH Superseded Miriam Rubio 4 participants 20 messages Aug 17, 2021 – Aug 18, 2021

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v4 cover Finish converting git bisect to C part 4

These patches correspond to a fourth part of patch series of Outreachy project "Finish converting git bisect from shell to C" started by Pranit Bauva and Tanushree Tumane
(https://public-inbox.org/git/[email protected]) and continued by me.

This fourth part is formed by reimplementations of some git bisect subcommands, addition of tests and removal of some temporary subcommands.

These patch series emails were generated from:
https://gitlab.com/mirucam/git/commits/git-bisect-work-part4-v4.1.

I would like to thank Junio Hamano, Andrzej Hunt and Christian Couder for reviewing this patch series.

General changes
---------------
* Rebase on master branch: 5d213e46bb (Git 2.33-rc2, 2021-08-11) to include latest updates in bisect-helper.c file.
* Add three tests requested by reviewers in v3 patch series in t6030-bisect-porcelain.sh file.

Specific changes
----------------

[5/6] bisect--helper: reimplement bisect_run shell function in C
* Content of the BISECT_RUN file is shown to the user.
* Use strvec_push() instead of xstrdup().
* Fix a bug on previous patch series regarding to BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND (-10) return code.

---

Miriam Rubio (3):
t6030-bisect-porcelain: add tests to control bisect run exit cases
t6030-bisect-porcelain: add test for bisect visualize
bisect--helper: retire --bisect-next-check subcommand

Pranit Bauva (2):
run-command: make exists_in_PATH() non-static
bisect--helper: reimplement bisect_visualize()shell function in C

Tanushree Tumane (1):
bisect--helper: reimplement bisect_run shell function in C

 builtin/bisect--helper.c    | 130 +++++++++++++++++++++++++++++++++---
 git-bisect.sh               |  87 +-----------------------
 run-command.c               |   2 +-
 run-command.h               |  12 ++++
 t/t6030-bisect-porcelain.sh |  21 ++++++
 5 files changed, 158 insertions(+), 94 deletions(-)
Signature · 1 line
2.29.2
#1 lore raw

PATCH v4 1/6 t6030-bisect-porcelain: add tests to control bisect run exit cases

There is a gap on bisect run test coverage related with error exits. Add two tests to control these error cases.

---
 t/t6030-bisect-porcelain.sh | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
t/t6030-bisect-porcelain.sh +14 -0
index a1baf4e451..f41453cc97 100755
@@ -962,4 +962,18 @@ test_expect_success 'bisect handles annotated tags' '
 	grep "$bad is the first bad commit" output
 '
 
+test_expect_success 'bisect run fails with exit code equals or greater than 128' '
+	write_script test_script.sh <<-\EOF &&
+	exit 128 >/dev/null
+	EOF
+	test_must_fail git bisect run ./test_script.sh > my_bisect_log.txt
+'
+
+test_expect_success 'bisect run fails with exit code smaller than 0' '
+	write_script test_script.sh <<-\EOF &&
+	exit -1 >/dev/null
+	EOF
+	test_must_fail git bisect run ./test_script.sh > my_bisect_log.txt
+'
+
 test_done
Signature · 1 line
2.29.2
#5 lore raw

PATCH v4 2/6 t6030-bisect-porcelain: add test for bisect visualize

In reply to Miriam Rubio

Add a test to control breakages in bisect visualize command.

---
 t/t6030-bisect-porcelain.sh | 7 +++++++
 1 file changed, 7 insertions(+)
t/t6030-bisect-porcelain.sh +7 -0
index f41453cc97..99b7517400 100755
@@ -976,4 +976,11 @@ test_expect_success 'bisect run fails with exit code smaller than 0' '
 	test_must_fail git bisect run ./test_script.sh > my_bisect_log.txt
 '
 
+test_expect_success 'bisect visualize with a filename with dash and space' '
+	echo "My test line" >> -hello\ 2 &&
+	git add -- -hello\ 2 &&
+	git commit --quiet -m "Add test line" -- -hello\ 2 &&
+	git bisect visualize -p -- -hello\ 2 > my_bisect_log.txt
+'
+
 test_done
Signature · 1 line
2.29.2
#8 lore raw

PATCH v4 3/6 run-command: make `exists_in_PATH()` non-static

In reply to Miriam Rubio

From: Pranit Bauva <[email protected]>

Removes the static keyword from exists_in_PATH() function and declares the function in run-command.h file. The function will be used in bisect_visualize() in a later commit.

Mentored by: Christian Couder <[email protected]> Mentored by: Johannes Schindelin <[email protected]>

---
 run-command.c |  2 +-
 run-command.h | 12 ++++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)
run-command.c +1 -1
index f72e72cce7..390f46819f 100644
@@ -210,7 +210,7 @@ static char *locate_in_PATH(const char *file)
 	return NULL;
 }
 
-static int exists_in_PATH(const char *file)
+int exists_in_PATH(const char *file)
 {
 	char *r = locate_in_PATH(file);
 	int found = r != NULL;
run-command.h +12 -0
index af1296769f..54d74b706f 100644
@@ -182,6 +182,18 @@ void child_process_clear(struct child_process *);
 
 int is_executable(const char *name);
 
+/**
+ * Search if a $PATH for a command exists.  This emulates the path search that
+ * execvp would perform, without actually executing the command so it
+ * can be used before fork() to prepare to run a command using
+ * execve() or after execvp() to diagnose why it failed.
+ *
+ * The caller should ensure that file contains no directory separators.
+ *
+ * Returns 1 if it is found in $PATH or 0 if the command could not be found.
+ */
+int exists_in_PATH(const char *file);
+
 /**
  * Start a sub-process. Takes a pointer to a `struct child_process`
  * that specifies the details and returns pipe FDs (if requested).
Signature · 1 line
2.29.2
#9 lore raw

PATCH v4 4/6 bisect--helper: reimplement `bisect_visualize()`shell function in C

In reply to Miriam Rubio

From: Pranit Bauva <[email protected]>

Reimplement the bisect_visualize() shell function in C and also add --bisect-visualize subcommand to git bisect--helper to call it from git-bisect.sh.

---
 builtin/bisect--helper.c | 48 +++++++++++++++++++++++++++++++++++++++-
 git-bisect.sh            | 25 +--------------------
 2 files changed, 48 insertions(+), 25 deletions(-)
builtin/bisect--helper.c +47 -1
index f184eaeac6..4258429c1c 100644
@@ -30,6 +30,7 @@ static const char * const git_bisect_helper_usage[] = {
 	N_("git bisect--helper --bisect-state (good|old) [<rev>...]"),
 	N_("git bisect--helper --bisect-replay <filename>"),
 	N_("git bisect--helper --bisect-skip [(<rev>|<range>)...]"),
+	N_("git bisect--helper --bisect-visualize"),
 	NULL
 };
 
@@ -1036,6 +1037,44 @@ static enum bisect_error bisect_skip(struct bisect_terms *terms, const char **ar
 	return res;
 }
 
+static int bisect_visualize(struct bisect_terms *terms, const char **argv, int argc)
+{
+	struct strvec args = STRVEC_INIT;
+	int flags = RUN_COMMAND_NO_STDIN, res = 0;
+	struct strbuf sb = STRBUF_INIT;
+
+	if (bisect_next_check(terms, NULL) != 0)
+		return BISECT_FAILED;
+
+	if (!argc) {
+		if ((getenv("DISPLAY") || getenv("SESSIONNAME") || getenv("MSYSTEM") ||
+		     getenv("SECURITYSESSIONID")) && exists_in_PATH("gitk"))
+			strvec_push(&args, "gitk");
+		else {
+			strvec_pushl(&args, "log", NULL);
+			flags |= RUN_GIT_CMD;
+		}
+	} else {
+		if (argv[0][0] == '-') {
+			strvec_pushl(&args, "log", NULL);
+			flags |= RUN_GIT_CMD;
+		} else if (strcmp(argv[0], "tig") && !starts_with(argv[0], "git"))
+			flags |= RUN_GIT_CMD;
+
+		strvec_pushv(&args, argv);
+	}
+
+	strvec_pushl(&args, "--bisect", "--", NULL);
+
+	strbuf_read_file(&sb, git_path_bisect_names(), 0);
+	sq_dequote_to_strvec(sb.buf, &args);
+	strbuf_release(&sb);
+
+	res = run_command_v_opt(args.v, flags);
+	strvec_clear(&args);
+	return res;
+}
+
 int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 {
 	enum {
@@ -1048,7 +1087,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 		BISECT_STATE,
 		BISECT_LOG,
 		BISECT_REPLAY,
-		BISECT_SKIP
+		BISECT_SKIP,
+		BISECT_VISUALIZE,
 	} cmdmode = 0;
 	int res = 0, nolog = 0;
 	struct option options[] = {
@@ -1070,6 +1110,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 			 N_("replay the bisection process from the given file"), BISECT_REPLAY),
 		OPT_CMDMODE(0, "bisect-skip", &cmdmode,
 			 N_("skip some commits for checkout"), BISECT_SKIP),
+		OPT_CMDMODE(0, "bisect-visualize", &cmdmode,
+			 N_("visualize the bisection"), BISECT_VISUALIZE),
 		OPT_BOOL(0, "no-log", &nolog,
 			 N_("no log for BISECT_WRITE")),
 		OPT_END()
@@ -1131,6 +1173,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 		get_terms(&terms);
 		res = bisect_skip(&terms, argv, argc);
 		break;
+	case BISECT_VISUALIZE:
+		get_terms(&terms);
+		res = bisect_visualize(&terms, argv, argc);
+		break;
 	default:
 		BUG("unknown subcommand %d", cmdmode);
 	}
git-bisect.sh +1 -24
index 6a7afaea8d..95f7f3fb8c 100755
@@ -39,29 +39,6 @@ _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
 TERM_BAD=bad
 TERM_GOOD=good
 
-bisect_visualize() {
-	git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit
-
-	if test $# = 0
-	then
-		if test -n "${DISPLAY+set}${SESSIONNAME+set}${MSYSTEM+set}${SECURITYSESSIONID+set}" &&
-			type gitk >/dev/null 2>&1
-		then
-			set gitk
-		else
-			set git log
-		fi
-	else
-		case "$1" in
-		git*|tig) ;;
-		-*)	set git log "$@" ;;
-		*)	set git "$@" ;;
-		esac
-	fi
-
-	eval '"$@"' --bisect -- $(cat "$GIT_DIR/BISECT_NAMES")
-}
-
 bisect_run () {
 	git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit
 
@@ -152,7 +129,7 @@ case "$#" in
 		# Not sure we want "next" at the UI level anymore.
 		git bisect--helper --bisect-next "$@" || exit ;;
 	visualize|view)
-		bisect_visualize "$@" ;;
+		git bisect--helper --bisect-visualize "$@" || exit;;
 	reset)
 		git bisect--helper --bisect-reset "$@" ;;
 	replay)
Signature · 1 line
2.29.2
#12 lore raw

PATCH v4 5/6 bisect--helper: reimplement `bisect_run` shell function in C

In reply to Miriam Rubio

From: Tanushree Tumane <[email protected]>

Reimplement the bisect_run() shell function in C and also add --bisect-run subcommand to git bisect--helper to call it from git-bisect.sh.

---
 builtin/bisect--helper.c | 75 ++++++++++++++++++++++++++++++++++++++++
 git-bisect.sh            | 62 +--------------------------------
 2 files changed, 76 insertions(+), 61 deletions(-)
builtin/bisect--helper.c +75 -0
index 4258429c1c..852e0a30fb 100644
@@ -31,6 +31,7 @@ static const char * const git_bisect_helper_usage[] = {
 	N_("git bisect--helper --bisect-replay <filename>"),
 	N_("git bisect--helper --bisect-skip [(<rev>|<range>)...]"),
 	N_("git bisect--helper --bisect-visualize"),
+	N_("git bisect--helper --bisect-run <cmd>..."),
 	NULL
 };
 
@@ -1075,6 +1076,71 @@ static int bisect_visualize(struct bisect_terms *terms, const char **argv, int a
 	return res;
 }
 
+static int bisect_run(struct bisect_terms *terms, const char **argv, int argc)
+{
+	int res = BISECT_OK;
+	struct strbuf command = STRBUF_INIT;
+	struct strvec args = STRVEC_INIT;
+	struct strvec run_args = STRVEC_INIT;
+	int exit = 0;
+
+	if (bisect_next_check(terms, NULL))
+		return BISECT_FAILED;
+
+	if (argc)
+		sq_quote_argv(&command, argv);
+	else
+		return BISECT_FAILED;
+
+	strvec_push(&run_args, command.buf);
+
+	while (1) {
+		strvec_clear(&args);
+		exit = 1;
+
+		printf(_("running %s"), command.buf);
+		res = run_command_v_opt(run_args.v, RUN_USING_SHELL);
+
+		if (res < 0 || 128 <= res) {
+			error(_("bisect run failed: exit code %d from"
+				" '%s' is < 0 or >= 128"), res, command.buf);
+			strbuf_release(&command);
+			return res;
+		}
+
+		if (res == 125)
+			strvec_push(&args, "skip");
+		else if (res > 0)
+			strvec_push(&args, terms->term_bad);
+		else
+			strvec_push(&args, terms->term_good);
+
+		res = bisect_state(terms, args.v, args.nr);
+
+		if (res == BISECT_ONLY_SKIPPED_LEFT)
+			error(_("bisect run cannot continue any more"));
+		else if (res == BISECT_INTERNAL_SUCCESS_MERGE_BASE) {
+			printf(_("bisect run success"));
+			res = BISECT_OK;
+		} else if (res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND) {
+			printf(_("bisect found first bad commit"));
+			res = BISECT_OK;
+		} else if (res) {
+			error(_("bisect run failed:'git bisect--helper --bisect-state"
+			" %s' exited with error code %d"), args.v[0], res);
+		} else {
+			exit = 0;
+		}
+
+		if (exit) {
+			strbuf_release(&command);
+			strvec_clear(&args);
+			strvec_clear(&run_args);
+			return res;
+		}
+	}
+}
+
 int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 {
 	enum {
@@ -1089,6 +1155,7 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 		BISECT_REPLAY,
 		BISECT_SKIP,
 		BISECT_VISUALIZE,
+		BISECT_RUN,
 	} cmdmode = 0;
 	int res = 0, nolog = 0;
 	struct option options[] = {
@@ -1112,6 +1179,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 			 N_("skip some commits for checkout"), BISECT_SKIP),
 		OPT_CMDMODE(0, "bisect-visualize", &cmdmode,
 			 N_("visualize the bisection"), BISECT_VISUALIZE),
+		OPT_CMDMODE(0, "bisect-run", &cmdmode,
+			 N_("use <cmd>... to automatically bisect."), BISECT_RUN),
 		OPT_BOOL(0, "no-log", &nolog,
 			 N_("no log for BISECT_WRITE")),
 		OPT_END()
@@ -1177,6 +1246,12 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 		get_terms(&terms);
 		res = bisect_visualize(&terms, argv, argc);
 		break;
+	case BISECT_RUN:
+		if (!argc)
+			return error(_("bisect run failed: no command provided."));
+		get_terms(&terms);
+		res = bisect_run(&terms, argv, argc);
+		break;
 	default:
 		BUG("unknown subcommand %d", cmdmode);
 	}
git-bisect.sh +1 -61

74 lines not on this page · raw message

Signature · 1 line
2.29.2
#18 lore raw

PATCH v4 6/6 bisect--helper: retire `--bisect-next-check` subcommand

In reply to Miriam Rubio

After reimplementation of git bisect run in C, --bisect-next-check subcommand is not needed anymore.

Let's remove it from options list and code.

Mentored by: Christian Couder <[email protected]>

---
 builtin/bisect--helper.c | 7 -------
 1 file changed, 7 deletions(-)
builtin/bisect--helper.c +0 -7
index 852e0a30fb..d749747639 100644
@@ -21,7 +21,6 @@ static GIT_PATH_FUNC(git_path_bisect_first_parent, "BISECT_FIRST_PARENT")
 
 static const char * const git_bisect_helper_usage[] = {
 	N_("git bisect--helper --bisect-reset [<commit>]"),
-	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]"),
 	N_("git bisect--helper --bisect-start [--term-{new,bad}=<term> --term-{old,good}=<term>]"
 					    " [--no-checkout] [--first-parent] [<bad> [<good>...]] [--] [<paths>...]"),
@@ -1200,12 +1199,6 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 			return error(_("--bisect-reset requires either no argument or a commit"));
 		res = bisect_reset(argc ? argv[0] : NULL);
 		break;
-	case BISECT_NEXT_CHECK:
-		if (argc != 2 && argc != 3)
-			return error(_("--bisect-next-check requires 2 or 3 arguments"));
-		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"));
Signature · 1 line
2.29.2
#2 lore raw

Re: [PATCH v4 1/6] t6030-bisect-porcelain: add tests to control bisect run exit cases

In reply to Miriam Rubio

t/t6030-bisect-porcelain.sh test_expect_success 'bisect handles annotated tags' ' · line 970

Quoted 6 lines · Miriam Rubio
t/t6030-bisect-porcelain.sh +6 -0
+test_expect_success 'bisect run fails with exit code equals or greater than 128' '
+	write_script test_script.sh <<-\EOF &&
+	exit 128 >/dev/null
+	EOF
+	test_must_fail git bisect run ./test_script.sh > my_bisect_log.txt
+'

This only checks for exit code equals to 128. You should also check for exit code greater than 128, for example 255.

t/t6030-bisect-porcelain.sh test_expect_success 'bisect handles annotated tags' ' · line 977

Quoted 7 lines
t/t6030-bisect-porcelain.sh +7 -0
+
+test_expect_success 'bisect run fails with exit code smaller than 0' '
+	write_script test_script.sh <<-\EOF &&
+	exit -1 >/dev/null
+	EOF
+	test_must_fail git bisect run ./test_script.sh > my_bisect_log.txt
+'

This test looks OK, using -1 as representative of negative exit code. However, wording of test name can also be 'bisect run fails with negative exit code'.

Thanks for reviewing.

Signature · 1 line
An old man doll... just what I always wanted! - Clara
#6 lore raw

Re: [PATCH v4 2/6] t6030-bisect-porcelain: add test for bisect visualize

In reply to Miriam Rubio
Quoted 14 lines · Miriam Rubio

Add a test to control breakages in bisect visualize command.

---
  t/t6030-bisect-porcelain.sh | 7 +++++++
  1 file changed, 7 insertions(+)
t/t6030-bisect-porcelain.sh +0 -0
index f41453cc97..99b7517400 100755
@@ -976,4 +976,11 @@ test_expect_success 'bisect run fails with exit code smaller than 0' '
  	test_must_fail git bisect run ./test_script.sh > my_bisect_log.txt

t/t6030-bisect-porcelain.sh test_expect_success 'bisect run fails with exit code smaller than 0' ' · line 986

Quoted 10 lines
t/t6030-bisect-porcelain.sh +7 -0
  '
 
+test_expect_success 'bisect visualize with a filename with dash and space' '
+	echo "My test line" >> -hello\ 2 &&
+	git add -- -hello\ 2 &&
+	git commit --quiet -m "Add test line" -- -hello\ 2 &&
+	git bisect visualize -p -- -hello\ 2 > my_bisect_log.txt
+'
+
  test_done

Seems like you're testing with filename with dash and space. Does git bisect visualize have any problems handling such filenames?

Signature · 1 line
An old man doll... just what I always wanted! - Clara
#3 lore raw

Re: [PATCH v4 1/6] t6030-bisect-porcelain: add tests to control bisect run exit cases

In reply to Bagas Sanjaya
Quoted 13 lines · Bagas Sanjaya

t/t6030-bisect-porcelain.sh test_expect_success 'bisect handles annotated tags' ' · line 970

Quoted 6 lines · Miriam Rubio
t/t6030-bisect-porcelain.sh +6 -0
+test_expect_success 'bisect run fails with exit code equals or greater than 128' '
+     write_script test_script.sh <<-\EOF &&
+     exit 128 >/dev/null
+     EOF
+     test_must_fail git bisect run ./test_script.sh > my_bisect_log.txt
+'

This only checks for exit code equals to 128. You should also check for exit code greater than 128, for example 255.

Quoted 11 lines

t/t6030-bisect-porcelain.sh test_expect_success 'bisect handles annotated tags' ' · line 977

Quoted 7 lines
t/t6030-bisect-porcelain.sh +7 -0
+
+test_expect_success 'bisect run fails with exit code smaller than 0' '
+     write_script test_script.sh <<-\EOF &&
+     exit -1 >/dev/null
+     EOF
+     test_must_fail git bisect run ./test_script.sh > my_bisect_log.txt
+'

This test looks OK, using -1 as representative of negative exit code. However, wording of test name can also be 'bisect run fails with negative exit code'.

Actually I am not sure that it makes sense to test an exit code smaller than 0, as POSIX exit codes are between 0 and 255 (included).

For example:

$ bash -c 'exit -1'; echo $?
255
$ dash -c 'exit -1'; echo $?
dash: 1: exit: Illegal number: -1
2
#10 lore raw

Re: [PATCH v4 4/6] bisect--helper: reimplement `bisect_visualize()`shell function in C

In reply to Miriam Rubio

Hi Miriam,

this looks good!

Just one suggestion (but I won't insist on it):

Quoted 9 lines · Miriam Rubio
builtin/bisect--helper.c +5 -0
@@ -1036,6 +1037,44 @@ static enum bisect_error bisect_skip(struct bisect_terms *terms, const char **ar
 	return res;
 }
 
+static int bisect_visualize(struct bisect_terms *terms, const char **argv, int argc)
+{
+	struct strvec args = STRVEC_INIT;
+	int flags = RUN_COMMAND_NO_STDIN, res = 0;
+	struct strbuf sb = STRBUF_INIT;

builtin/bisect--helper.c static enum bisect_error bisect_skip(struct bisect_terms *terms, const char **ar · line 1054

Quoted 10 lines
builtin/bisect--helper.c +10 -0
+
+	if (bisect_next_check(terms, NULL) != 0)
+		return BISECT_FAILED;
+
+	if (!argc) {
+		if ((getenv("DISPLAY") || getenv("SESSIONNAME") || getenv("MSYSTEM") ||
+		     getenv("SECURITYSESSIONID")) && exists_in_PATH("gitk"))
+			strvec_push(&args, "gitk");
+		else {
+			strvec_pushl(&args, "log", NULL);

This could be written more concisely as strvec_push(&args, "log").

builtin/bisect--helper.c static enum bisect_error bisect_skip(struct bisect_terms *terms, const char **ar · line 1059

Quoted 5 lines
builtin/bisect--helper.c +5 -0
+			flags |= RUN_GIT_CMD;
+		}
+	} else {
+		if (argv[0][0] == '-') {
+			strvec_pushl(&args, "log", NULL);

Same here.

Otherwise, it looks good to me!

Thank you,
Dscho

Quoted 98 lines
builtin/bisect--helper.c +26 -1
+			flags |= RUN_GIT_CMD;
+		} else if (strcmp(argv[0], "tig") && !starts_with(argv[0], "git"))
+			flags |= RUN_GIT_CMD;
+
+		strvec_pushv(&args, argv);
+	}
+
+	strvec_pushl(&args, "--bisect", "--", NULL);
+
+	strbuf_read_file(&sb, git_path_bisect_names(), 0);
+	sq_dequote_to_strvec(sb.buf, &args);
+	strbuf_release(&sb);
+
+	res = run_command_v_opt(args.v, flags);
+	strvec_clear(&args);
+	return res;
+}
+
 int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 {
 	enum {
@@ -1048,7 +1087,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 		BISECT_STATE,
 		BISECT_LOG,
 		BISECT_REPLAY,
-		BISECT_SKIP
+		BISECT_SKIP,
+		BISECT_VISUALIZE,
 	} cmdmode = 0;
 	int res = 0, nolog = 0;
 	struct option options[] = {
@@ -1070,6 +1110,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 			 N_("replay the bisection process from the given file"), BISECT_REPLAY),
 		OPT_CMDMODE(0, "bisect-skip", &cmdmode,
 			 N_("skip some commits for checkout"), BISECT_SKIP),
+		OPT_CMDMODE(0, "bisect-visualize", &cmdmode,
+			 N_("visualize the bisection"), BISECT_VISUALIZE),
 		OPT_BOOL(0, "no-log", &nolog,
 			 N_("no log for BISECT_WRITE")),
 		OPT_END()
@@ -1131,6 +1173,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 		get_terms(&terms);
 		res = bisect_skip(&terms, argv, argc);
 		break;
+	case BISECT_VISUALIZE:
+		get_terms(&terms);
+		res = bisect_visualize(&terms, argv, argc);
+		break;
 	default:
 		BUG("unknown subcommand %d", cmdmode);
 	}
git-bisect.sh +1 -25
index 6a7afaea8d..95f7f3fb8c 100755
@@ -39,29 +39,6 @@ _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
 TERM_BAD=bad
 TERM_GOOD=good
 
-bisect_visualize() {
-	git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit
-
-	if test $# = 0
-	then
-		if test -n "${DISPLAY+set}${SESSIONNAME+set}${MSYSTEM+set}${SECURITYSESSIONID+set}" &&
-			type gitk >/dev/null 2>&1
-		then
-			set gitk
-		else
-			set git log
-		fi
-	else
-		case "$1" in
-		git*|tig) ;;
-		-*)	set git log "$@" ;;
-		*)	set git "$@" ;;
-		esac
-	fi
-
-	eval '"$@"' --bisect -- $(cat "$GIT_DIR/BISECT_NAMES")
-}
-
 bisect_run () {
 	git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit
 
@@ -152,7 +129,7 @@ case "$#" in
 		# Not sure we want "next" at the UI level anymore.
 		git bisect--helper --bisect-next "$@" || exit ;;
 	visualize|view)
-		bisect_visualize "$@" ;;
+		git bisect--helper --bisect-visualize "$@" || exit;;
 	reset)
 		git bisect--helper --bisect-reset "$@" ;;
 	replay)
--

2.29.2

#13 lore raw

Re: [PATCH v4 5/6] bisect--helper: reimplement `bisect_run` shell function in C

In reply to Miriam Rubio

Hi Miriam,

Quoted 35 lines · Miriam Rubio

From: Tanushree Tumane <[email protected]>

Reimplement the bisect_run() shell function in C and also add --bisect-run subcommand to git bisect--helper to call it from git-bisect.sh.

---
 builtin/bisect--helper.c | 75 ++++++++++++++++++++++++++++++++++++++++
 git-bisect.sh            | 62 +--------------------------------
 2 files changed, 76 insertions(+), 61 deletions(-)
builtin/bisect--helper.c +6 -0
index 4258429c1c..852e0a30fb 100644
@@ -31,6 +31,7 @@ static const char * const git_bisect_helper_usage[] = {
 	N_("git bisect--helper --bisect-replay <filename>"),
 	N_("git bisect--helper --bisect-skip [(<rev>|<range>)...]"),
 	N_("git bisect--helper --bisect-visualize"),
+	N_("git bisect--helper --bisect-run <cmd>..."),
 	NULL
 };
 
@@ -1075,6 +1076,71 @@ static int bisect_visualize(struct bisect_terms *terms, const char **argv, int a
 	return res;
 }
 
+static int bisect_run(struct bisect_terms *terms, const char **argv, int argc)
+{
+	int res = BISECT_OK;
+	struct strbuf command = STRBUF_INIT;
+	struct strvec args = STRVEC_INIT;

builtin/bisect--helper.c static int bisect_visualize(struct bisect_terms *terms, const char **argv, int a · line 1093

Quoted 10 lines
builtin/bisect--helper.c +10 -0
+	struct strvec run_args = STRVEC_INIT;
+	int exit = 0;
+
+	if (bisect_next_check(terms, NULL))
+		return BISECT_FAILED;
+
+	if (argc)
+		sq_quote_argv(&command, argv);
+	else
+		return BISECT_FAILED;

Do we want to say something helpful here, e.g. _("bisect run failed: no command provided.")?

Quoted 15 lines
builtin/bisect--helper.c +15 -0
+
+	strvec_push(&run_args, command.buf);
+
+	while (1) {
+		strvec_clear(&args);
+		exit = 1;
+
+		printf(_("running %s"), command.buf);
+		res = run_command_v_opt(run_args.v, RUN_USING_SHELL);
+
+		if (res < 0 || 128 <= res) {
+			error(_("bisect run failed: exit code %d from"
+				" '%s' is < 0 or >= 128"), res, command.buf);
+			strbuf_release(&command);
+			return res;

builtin/bisect--helper.c static int bisect_visualize(struct bisect_terms *terms, const char **argv, int a · line 1118

Quoted 10 lines
builtin/bisect--helper.c +10 -0
+		}
+
+		if (res == 125)
+			strvec_push(&args, "skip");
+		else if (res > 0)
+			strvec_push(&args, terms->term_bad);
+		else
+			strvec_push(&args, terms->term_good);
+
+		res = bisect_state(terms, args.v, args.nr);

Since args.nr will always be 1, it would probably be better to use something like this:

		const char *new_state;

		[...]
		if (res == 125)
			new_state = "skip";
		else
			new_state = res > 0 ?
				terms->term_bad : terms->term_good;

		res = bisect_state(terms, &new_state, 1);

Also: I think at this stage, an equivalent to cat "$GIT_DIR/BISECT_RUN" is missing.

Quoted 4 lines
builtin/bisect--helper.c +4 -0
+
+		if (res == BISECT_ONLY_SKIPPED_LEFT)
+			error(_("bisect run cannot continue any more"));
+		else if (res == BISECT_INTERNAL_SUCCESS_MERGE_BASE) {

builtin/bisect--helper.c static int bisect_visualize(struct bisect_terms *terms, const char **argv, int a · line 1132

Quoted 10 lines
builtin/bisect--helper.c +10 -0
+			printf(_("bisect run success"));
+			res = BISECT_OK;
+		} else if (res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND) {
+			printf(_("bisect found first bad commit"));
+			res = BISECT_OK;
+		} else if (res) {
+			error(_("bisect run failed:'git bisect--helper --bisect-state"
+			" %s' exited with error code %d"), args.v[0], res);
+		} else {
+			exit = 0;

Since the only purpose of exit seems to be that the loop should continue if exit is set to 0, and it is only set here, how about doing away with the variable altogether and writing continue; instead of exit = 0;? Then the conditional block below does not need to be conditional.

Other than that: well done!

Ciao,
Dscho

Quoted 128 lines
builtin/bisect--helper.c +20 -0
+		}
+
+		if (exit) {
+			strbuf_release(&command);
+			strvec_clear(&args);
+			strvec_clear(&run_args);
+			return res;
+		}
+	}
+}
+
 int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 {
 	enum {
@@ -1089,6 +1155,7 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 		BISECT_REPLAY,
 		BISECT_SKIP,
 		BISECT_VISUALIZE,
+		BISECT_RUN,
 	} cmdmode = 0;
 	int res = 0, nolog = 0;
 	struct option options[] = {
@@ -1112,6 +1179,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 			 N_("skip some commits for checkout"), BISECT_SKIP),
 		OPT_CMDMODE(0, "bisect-visualize", &cmdmode,
 			 N_("visualize the bisection"), BISECT_VISUALIZE),
+		OPT_CMDMODE(0, "bisect-run", &cmdmode,
+			 N_("use <cmd>... to automatically bisect."), BISECT_RUN),
 		OPT_BOOL(0, "no-log", &nolog,
 			 N_("no log for BISECT_WRITE")),
 		OPT_END()
@@ -1177,6 +1246,12 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 		get_terms(&terms);
 		res = bisect_visualize(&terms, argv, argc);
 		break;
+	case BISECT_RUN:
+		if (!argc)
+			return error(_("bisect run failed: no command provided."));
+		get_terms(&terms);
+		res = bisect_run(&terms, argv, argc);
+		break;
 	default:
 		BUG("unknown subcommand %d", cmdmode);
 	}
git-bisect.sh +1 -62
index 95f7f3fb8c..e83d011e17 100755
@@ -39,66 +39,6 @@ _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
 TERM_BAD=bad
 TERM_GOOD=good
 
-bisect_run () {
-	git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit
-
-	test -n "$*" || die "$(gettext "bisect run failed: no command provided.")"
-
-	while true
-	do
-		command="$@"
-		eval_gettextln "running \$command"
-		"$@"
-		res=$?
-
-		# Check for really bad run error.
-		if [ $res -lt 0 -o $res -ge 128 ]
-		then
-			eval_gettextln "bisect run failed:
-exit code \$res from '\$command' is < 0 or >= 128" >&2
-			exit $res
-		fi
-
-		# Find current state depending on run success or failure.
-		# A special exit code of 125 means cannot test.
-		if [ $res -eq 125 ]
-		then
-			state='skip'
-		elif [ $res -gt 0 ]
-		then
-			state="$TERM_BAD"
-		else
-			state="$TERM_GOOD"
-		fi
-
-		git bisect--helper --bisect-state $state >"$GIT_DIR/BISECT_RUN"
-		res=$?
-
-		cat "$GIT_DIR/BISECT_RUN"
-
-		if sane_grep "first $TERM_BAD commit could be any of" "$GIT_DIR/BISECT_RUN" \
-			>/dev/null
-		then
-			gettextln "bisect run cannot continue any more" >&2
-			exit $res
-		fi
-
-		if [ $res -ne 0 ]
-		then
-			eval_gettextln "bisect run failed:
-'bisect-state \$state' exited with error code \$res" >&2
-			exit $res
-		fi
-
-		if sane_grep "is the first $TERM_BAD commit" "$GIT_DIR/BISECT_RUN" >/dev/null
-		then
-			gettextln "bisect run success"
-			exit 0;
-		fi
-
-	done
-}
-
 get_terms () {
 	if test -s "$GIT_DIR/BISECT_TERMS"
 	then
@@ -137,7 +77,7 @@ case "$#" in
 	log)
 		git bisect--helper --bisect-log || exit ;;
 	run)
-		bisect_run "$@" ;;
+		git bisect--helper --bisect-run "$@" || exit;;
 	terms)
 		git bisect--helper --bisect-terms "$@" || exit;;
 	*)
--

2.29.2

#19 lore raw

Re: [PATCH v4 6/6] bisect--helper: retire `--bisect-next-check` subcommand

In reply to Miriam Rubio

Hi Miriam,

Quoted 10 lines · Miriam Rubio

After reimplementation of git bisect run in C, --bisect-next-check subcommand is not needed anymore.

Let's remove it from options list and code.

Mentored by: Christian Couder <[email protected]>

---
 builtin/bisect--helper.c | 7 -------
 1 file changed, 7 deletions(-)

Exciting! This is inching closer and closer to a fully-built-in git bisect.

Thank you so much!
Dscho

Quoted 30 lines
builtin/bisect--helper.c +0 -8
index 852e0a30fb..d749747639 100644
@@ -21,7 +21,6 @@ static GIT_PATH_FUNC(git_path_bisect_first_parent, "BISECT_FIRST_PARENT")
 
 static const char * const git_bisect_helper_usage[] = {
 	N_("git bisect--helper --bisect-reset [<commit>]"),
-	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]"),
 	N_("git bisect--helper --bisect-start [--term-{new,bad}=<term> --term-{old,good}=<term>]"
 					    " [--no-checkout] [--first-parent] [<bad> [<good>...]] [--] [<paths>...]"),
@@ -1200,12 +1199,6 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 			return error(_("--bisect-reset requires either no argument or a commit"));
 		res = bisect_reset(argc ? argv[0] : NULL);
 		break;
-	case BISECT_NEXT_CHECK:
-		if (argc != 2 && argc != 3)
-			return error(_("--bisect-next-check requires 2 or 3 arguments"));
-		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"));
--

2.29.2

author
#7 lore raw

Re: [PATCH v4 2/6] t6030-bisect-porcelain: add test for bisect visualize

In reply to Bagas Sanjaya

Hi Bagas,

Quoted 17 lines · Bagas Sanjaya
Quoted 15 lines · Miriam Rubio

Add a test to control breakages in bisect visualize command.

---
  t/t6030-bisect-porcelain.sh | 7 +++++++
  1 file changed, 7 insertions(+)
t/t6030-bisect-porcelain.sh +0 -0
index f41453cc97..99b7517400 100755
@@ -976,4 +976,11 @@ test_expect_success 'bisect run fails with exit code smaller than 0' '
      test_must_fail git bisect run ./test_script.sh > my_bisect_log.txt
  '
 
Quoted 13 lines

t/t6030-bisect-porcelain.sh test_expect_success 'bisect run fails with exit code smaller than 0' ' · line 986

Quoted 9 lines
t/t6030-bisect-porcelain.sh +7 -0
+test_expect_success 'bisect visualize with a filename with dash and space' '
+     echo "My test line" >> -hello\ 2 &&
+     git add -- -hello\ 2 &&
+     git commit --quiet -m "Add test line" -- -hello\ 2 &&
+     git bisect visualize -p -- -hello\ 2 > my_bisect_log.txt
+'
+
  test_done

Seems like you're testing with filename with dash and space. Does git bisect visualize have any problems handling such filenames?

It was a suggestion of a reviewer in the previous version to detect possible breakages:
https://lore.kernel.org/git/[email protected]/

Thanks for reviewing,
Miriam

Quoted 2 lines

--
An old man doll... just what I always wanted! - Clara

author
#4 lore raw

Re: [PATCH v4 1/6] t6030-bisect-porcelain: add tests to control bisect run exit cases

In reply to Christian Couder

Hi,

Quoted 12 lines · Christian Couder
Quoted 10 lines · Bagas Sanjaya
Quoted 6 lines · Miriam Rubio
t/t6030-bisect-porcelain.sh +6 -0
+test_expect_success 'bisect run fails with exit code equals or greater than 128' '
+     write_script test_script.sh <<-\EOF &&
+     exit 128 >/dev/null
+     EOF
+     test_must_fail git bisect run ./test_script.sh > my_bisect_log.txt
+'
Quoted 3 lines
Quoted 3 lines

This only checks for exit code equals to 128. You should also check for exit code greater than 128, for example 255.

Noted.
Thank you for reviewing, Bagas.

Quoted 15 lines
Quoted 11 lines

t/t6030-bisect-porcelain.sh test_expect_success 'bisect handles annotated tags' ' · line 977

Quoted 7 lines
t/t6030-bisect-porcelain.sh +7 -0
+
+test_expect_success 'bisect run fails with exit code smaller than 0' '
+     write_script test_script.sh <<-\EOF &&
+     exit -1 >/dev/null
+     EOF
+     test_must_fail git bisect run ./test_script.sh > my_bisect_log.txt
+'

This test looks OK, using -1 as representative of negative exit code. However, wording of test name can also be 'bisect run fails with negative exit code'.

Actually I am not sure that it makes sense to test an exit code smaller than 0, as POSIX exit codes are between 0 and 255 (included).

Quoted 8 lines

For example:

$ bash -c 'exit -1'; echo $?
255
$ dash -c 'exit -1'; echo $?
dash: 1: exit: Illegal number: -1
2

Ok, I will remove this test. No problem.
Thanks, Christian.

author
#11 lore raw

Re: [PATCH v4 4/6] bisect--helper: reimplement `bisect_visualize()`shell function in C

In reply to Johannes Schindelin

Hi Johannes,

Quoted 31 lines · Johannes Schindelin

Hi Miriam,

this looks good!

Just one suggestion (but I won't insist on it):

Quoted 9 lines · Miriam Rubio
builtin/bisect--helper.c +5 -0
@@ -1036,6 +1037,44 @@ static enum bisect_error bisect_skip(struct bisect_terms *terms, const char **ar
      return res;
 }
 
+static int bisect_visualize(struct bisect_terms *terms, const char **argv, int argc)
+{
+     struct strvec args = STRVEC_INIT;
+     int flags = RUN_COMMAND_NO_STDIN, res = 0;
+     struct strbuf sb = STRBUF_INIT;

builtin/bisect--helper.c static enum bisect_error bisect_skip(struct bisect_terms *terms, const char **ar · line 1054

Quoted 10 lines
builtin/bisect--helper.c +10 -0
+
+     if (bisect_next_check(terms, NULL) != 0)
+             return BISECT_FAILED;
+
+     if (!argc) {
+             if ((getenv("DISPLAY") || getenv("SESSIONNAME") || getenv("MSYSTEM") ||
+                  getenv("SECURITYSESSIONID")) && exists_in_PATH("gitk"))
+                     strvec_push(&args, "gitk");
+             else {
+                     strvec_pushl(&args, "log", NULL);

This could be written more concisely as strvec_push(&args, "log").

Quoted 7 lines

builtin/bisect--helper.c static enum bisect_error bisect_skip(struct bisect_terms *terms, const char **ar · line 1059

Quoted 5 lines
builtin/bisect--helper.c +5 -0
+                     flags |= RUN_GIT_CMD;
+             }
+     } else {
+             if (argv[0][0] == '-') {
+                     strvec_pushl(&args, "log", NULL);

Same here.

Sure, I will change it in both cases.
Thank you for reviewing,
Miriam.

Quoted 104 lines

Otherwise, it looks good to me!

Thank you,
Dscho

Quoted 98 lines
builtin/bisect--helper.c +26 -1
+                     flags |= RUN_GIT_CMD;
+             } else if (strcmp(argv[0], "tig") && !starts_with(argv[0], "git"))
+                     flags |= RUN_GIT_CMD;
+
+             strvec_pushv(&args, argv);
+     }
+
+     strvec_pushl(&args, "--bisect", "--", NULL);
+
+     strbuf_read_file(&sb, git_path_bisect_names(), 0);
+     sq_dequote_to_strvec(sb.buf, &args);
+     strbuf_release(&sb);
+
+     res = run_command_v_opt(args.v, flags);
+     strvec_clear(&args);
+     return res;
+}
+
 int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 {
      enum {
@@ -1048,7 +1087,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
              BISECT_STATE,
              BISECT_LOG,
              BISECT_REPLAY,
-             BISECT_SKIP
+             BISECT_SKIP,
+             BISECT_VISUALIZE,
      } cmdmode = 0;
      int res = 0, nolog = 0;
      struct option options[] = {
@@ -1070,6 +1110,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
                       N_("replay the bisection process from the given file"), BISECT_REPLAY),
              OPT_CMDMODE(0, "bisect-skip", &cmdmode,
                       N_("skip some commits for checkout"), BISECT_SKIP),
+             OPT_CMDMODE(0, "bisect-visualize", &cmdmode,
+                      N_("visualize the bisection"), BISECT_VISUALIZE),
              OPT_BOOL(0, "no-log", &nolog,
                       N_("no log for BISECT_WRITE")),
              OPT_END()
@@ -1131,6 +1173,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
              get_terms(&terms);
              res = bisect_skip(&terms, argv, argc);
              break;
+     case BISECT_VISUALIZE:
+             get_terms(&terms);
+             res = bisect_visualize(&terms, argv, argc);
+             break;
      default:
              BUG("unknown subcommand %d", cmdmode);
      }
git-bisect.sh +1 -25
index 6a7afaea8d..95f7f3fb8c 100755
@@ -39,29 +39,6 @@ _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
 TERM_BAD=bad
 TERM_GOOD=good
 
-bisect_visualize() {
-     git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit
-
-     if test $# = 0
-     then
-             if test -n "${DISPLAY+set}${SESSIONNAME+set}${MSYSTEM+set}${SECURITYSESSIONID+set}" &&
-                     type gitk >/dev/null 2>&1
-             then
-                     set gitk
-             else
-                     set git log
-             fi
-     else
-             case "$1" in
-             git*|tig) ;;
-             -*)     set git log "$@" ;;
-             *)      set git "$@" ;;
-             esac
-     fi
-
-     eval '"$@"' --bisect -- $(cat "$GIT_DIR/BISECT_NAMES")
-}
-
 bisect_run () {
      git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit
 
@@ -152,7 +129,7 @@ case "$#" in
              # Not sure we want "next" at the UI level anymore.
              git bisect--helper --bisect-next "$@" || exit ;;
      visualize|view)
-             bisect_visualize "$@" ;;
+             git bisect--helper --bisect-visualize "$@" || exit;;
      reset)
              git bisect--helper --bisect-reset "$@" ;;
      replay)
--

2.29.2

author
#14 lore raw

Re: [PATCH v4 5/6] bisect--helper: reimplement `bisect_run` shell function in C

In reply to Johannes Schindelin

Hi Johannes,

Quoted 45 lines · Johannes Schindelin

Hi Miriam,

Quoted 40 lines · Miriam Rubio

From: Tanushree Tumane <[email protected]>

Reimplement the bisect_run() shell function in C and also add --bisect-run subcommand to git bisect--helper to call it from git-bisect.sh.

---
 builtin/bisect--helper.c | 75 ++++++++++++++++++++++++++++++++++++++++
 git-bisect.sh            | 62 +--------------------------------
 2 files changed, 76 insertions(+), 61 deletions(-)
builtin/bisect--helper.c +11 -0
index 4258429c1c..852e0a30fb 100644
@@ -31,6 +31,7 @@ static const char * const git_bisect_helper_usage[] = {
      N_("git bisect--helper --bisect-replay <filename>"),
      N_("git bisect--helper --bisect-skip [(<rev>|<range>)...]"),
      N_("git bisect--helper --bisect-visualize"),
+     N_("git bisect--helper --bisect-run <cmd>..."),
      NULL
 };
 
@@ -1075,6 +1076,71 @@ static int bisect_visualize(struct bisect_terms *terms, const char **argv, int a
      return res;
 }
 
+static int bisect_run(struct bisect_terms *terms, const char **argv, int argc)
+{
+     int res = BISECT_OK;
+     struct strbuf command = STRBUF_INIT;
+     struct strvec args = STRVEC_INIT;
+     struct strvec run_args = STRVEC_INIT;
+     int exit = 0;
+
+     if (bisect_next_check(terms, NULL))
+             return BISECT_FAILED;
Quoted 9 lines

builtin/bisect--helper.c static int bisect_visualize(struct bisect_terms *terms, const char **argv, int a · line 1093

Quoted 5 lines
builtin/bisect--helper.c +5 -0
+
+     if (argc)
+             sq_quote_argv(&command, argv);
+     else
+             return BISECT_FAILED;

Do we want to say something helpful here, e.g. _("bisect run failed: no command provided.")?

Ok, noted

Quoted 29 lines
Quoted 15 lines
builtin/bisect--helper.c +15 -0
+
+     strvec_push(&run_args, command.buf);
+
+     while (1) {
+             strvec_clear(&args);
+             exit = 1;
+
+             printf(_("running %s"), command.buf);
+             res = run_command_v_opt(run_args.v, RUN_USING_SHELL);
+
+             if (res < 0 || 128 <= res) {
+                     error(_("bisect run failed: exit code %d from"
+                             " '%s' is < 0 or >= 128"), res, command.buf);
+                     strbuf_release(&command);
+                     return res;

builtin/bisect--helper.c static int bisect_visualize(struct bisect_terms *terms, const char **argv, int a · line 1118

Quoted 10 lines
builtin/bisect--helper.c +10 -0
+             }
+
+             if (res == 125)
+                     strvec_push(&args, "skip");
+             else if (res > 0)
+                     strvec_push(&args, terms->term_bad);
+             else
+                     strvec_push(&args, terms->term_good);
+
+             res = bisect_state(terms, args.v, args.nr);

Since args.nr will always be 1, it would probably be better to use something like this:

Quoted 11 lines
                const char *new_state;

                [...]
                if (res == 125)
                        new_state = "skip";
                else
                        new_state = res > 0 ?
                                terms->term_bad : terms->term_good;

                res = bisect_state(terms, &new_state, 1);

Yes, indeed. I will change it.

Quoted 2 lines

Also: I think at this stage, an equivalent to cat "$GIT_DIR/BISECT_RUN" is missing.

In the previous patch series (v3), I implemented the equivalent to the cat command but I understood
reviewers wanted to print the output to the user, so I reverted my changes for this version.
https://lore.kernel.org/git/[email protected]/

Quoted 16 lines
Quoted 5 lines
builtin/bisect--helper.c +4 -0
+
+             if (res == BISECT_ONLY_SKIPPED_LEFT)
+                     error(_("bisect run cannot continue any more"));
+             else if (res == BISECT_INTERNAL_SUCCESS_MERGE_BASE) {

builtin/bisect--helper.c static int bisect_visualize(struct bisect_terms *terms, const char **argv, int a · line 1132

Quoted 10 lines
builtin/bisect--helper.c +10 -0
+                     printf(_("bisect run success"));
+                     res = BISECT_OK;
+             } else if (res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND) {
+                     printf(_("bisect found first bad commit"));
+                     res = BISECT_OK;
+             } else if (res) {
+                     error(_("bisect run failed:'git bisect--helper --bisect-state"
+                     " %s' exited with error code %d"), args.v[0], res);
+             } else {
+                     exit = 0;
Quoted 5 lines

Since the only purpose of exit seems to be that the loop should continue if exit is set to 0, and it is only set here, how about doing away with the variable altogether and writing continue; instead of exit = 0;? Then the conditional block below does not need to be conditional.

Noted.

Quoted 2 lines

Other than that: well done!

Thank you for reviewing!,
Miriam

Quoted 131 lines

Ciao,
Dscho

Quoted 128 lines
builtin/bisect--helper.c +20 -0
+             }
+
+             if (exit) {
+                     strbuf_release(&command);
+                     strvec_clear(&args);
+                     strvec_clear(&run_args);
+                     return res;
+             }
+     }
+}
+
 int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 {
      enum {
@@ -1089,6 +1155,7 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
              BISECT_REPLAY,
              BISECT_SKIP,
              BISECT_VISUALIZE,
+             BISECT_RUN,
      } cmdmode = 0;
      int res = 0, nolog = 0;
      struct option options[] = {
@@ -1112,6 +1179,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
                       N_("skip some commits for checkout"), BISECT_SKIP),
              OPT_CMDMODE(0, "bisect-visualize", &cmdmode,
                       N_("visualize the bisection"), BISECT_VISUALIZE),
+             OPT_CMDMODE(0, "bisect-run", &cmdmode,
+                      N_("use <cmd>... to automatically bisect."), BISECT_RUN),
              OPT_BOOL(0, "no-log", &nolog,
                       N_("no log for BISECT_WRITE")),
              OPT_END()
@@ -1177,6 +1246,12 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
              get_terms(&terms);
              res = bisect_visualize(&terms, argv, argc);
              break;
+     case BISECT_RUN:
+             if (!argc)
+                     return error(_("bisect run failed: no command provided."));
+             get_terms(&terms);
+             res = bisect_run(&terms, argv, argc);
+             break;
      default:
              BUG("unknown subcommand %d", cmdmode);
      }
git-bisect.sh +1 -62
index 95f7f3fb8c..e83d011e17 100755
@@ -39,66 +39,6 @@ _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
 TERM_BAD=bad
 TERM_GOOD=good
 
-bisect_run () {
-     git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit
-
-     test -n "$*" || die "$(gettext "bisect run failed: no command provided.")"
-
-     while true
-     do
-             command="$@"
-             eval_gettextln "running \$command"
-             "$@"
-             res=$?
-
-             # Check for really bad run error.
-             if [ $res -lt 0 -o $res -ge 128 ]
-             then
-                     eval_gettextln "bisect run failed:
-exit code \$res from '\$command' is < 0 or >= 128" >&2
-                     exit $res
-             fi
-
-             # Find current state depending on run success or failure.
-             # A special exit code of 125 means cannot test.
-             if [ $res -eq 125 ]
-             then
-                     state='skip'
-             elif [ $res -gt 0 ]
-             then
-                     state="$TERM_BAD"
-             else
-                     state="$TERM_GOOD"
-             fi
-
-             git bisect--helper --bisect-state $state >"$GIT_DIR/BISECT_RUN"
-             res=$?
-
-             cat "$GIT_DIR/BISECT_RUN"
-
-             if sane_grep "first $TERM_BAD commit could be any of" "$GIT_DIR/BISECT_RUN" \
-                     >/dev/null
-             then
-                     gettextln "bisect run cannot continue any more" >&2
-                     exit $res
-             fi
-
-             if [ $res -ne 0 ]
-             then
-                     eval_gettextln "bisect run failed:
-'bisect-state \$state' exited with error code \$res" >&2
-                     exit $res
-             fi
-
-             if sane_grep "is the first $TERM_BAD commit" "$GIT_DIR/BISECT_RUN" >/dev/null
-             then
-                     gettextln "bisect run success"
-                     exit 0;
-             fi
-
-     done
-}
-
 get_terms () {
      if test -s "$GIT_DIR/BISECT_TERMS"
      then
@@ -137,7 +77,7 @@ case "$#" in
      log)
              git bisect--helper --bisect-log || exit ;;
      run)
-             bisect_run "$@" ;;
+             git bisect--helper --bisect-run "$@" || exit;;
      terms)
              git bisect--helper --bisect-terms "$@" || exit;;
      *)
--

2.29.2

#15 lore raw

Re: [PATCH v4 5/6] bisect--helper: reimplement `bisect_run` shell function in C

Hi Miriam,

Quoted 35 lines · Miriam R.
Quoted 19 lines · Johannes Schindelin

builtin/bisect--helper.c static int bisect_visualize(struct bisect_terms *terms, const char **argv, int a · line 1118

Quoted 12 lines · Miriam Rubio

From: Tanushree Tumane <[email protected]>

[...]

builtin/bisect--helper.c +9 -0
+
+             if (res == 125)
+                     strvec_push(&args, "skip");
+             else if (res > 0)
+                     strvec_push(&args, terms->term_bad);
+             else
+                     strvec_push(&args, terms->term_good);
+
+             res = bisect_state(terms, args.v, args.nr);

Since args.nr will always be 1, it would probably be better to use something like this:

Quoted 11 lines
                const char *new_state;

                [...]
                if (res == 125)
                        new_state = "skip";
                else
                        new_state = res > 0 ?
                                terms->term_bad : terms->term_good;

                res = bisect_state(terms, &new_state, 1);

Yes, indeed. I will change it.

Quoted 7 lines
Quoted 2 lines

Also: I think at this stage, an equivalent to cat "$GIT_DIR/BISECT_RUN" is missing.

In the previous patch series (v3), I implemented the equivalent to the cat command but I understood reviewers wanted to print the output to the user, so I reverted my changes for this version.
https://lore.kernel.org/git/[email protected]/

I am a bit confused: doesn't bisect_state() write to the BISECT_RUN file? If so, I think we do need to show the contents by opening the file and piping it to stdout.

FWIW I read
https://lore.kernel.org/git/CAP8UFD3X24F3qgefHpi00PM-KUk+vcqxwy2Dbngbyj7ciavCVQ@mail.gmail.com/
to mean the same thing, although I have to admit that I am not 100% certain.

Just to make sure: with this patch, at the end of a git bisect run, the user is shown the commit message of the first bad commit?

Ciao,
Dscho

#16 lore raw

Re: [PATCH v4 5/6] bisect--helper: reimplement `bisect_run` shell function in C

Quoted 15 lines · Johannes Schindelin

Hi Miriam,

Quoted 9 lines · Miriam R.

El mar, 17 ago 2021 a las 13:42, Johannes Schindelin

Quoted 2 lines

Also: I think at this stage, an equivalent to cat "$GIT_DIR/BISECT_RUN" is missing.

In the previous patch series (v3), I implemented the equivalent to the cat command but I understood reviewers wanted to print the output to the user, so I reverted my changes for this version.
https://lore.kernel.org/git/[email protected]/

Quoted 8 lines

I am a bit confused: doesn't bisect_state() write to the BISECT_RUN file? If so, I think we do need to show the contents by opening the file and piping it to stdout.

FWIW I read
https://lore.kernel.org/git/CAP8UFD3X24F3qgefHpi00PM-KUk+vcqxwy2Dbngbyj7ciavCVQ@mail.gmail.com/
to mean the same thing, although I have to admit that I am not 100% certain.

I agree that, after bisect_state() has written into the BISECT_RUN file, we should indeed be opening it and piping it to stdout. That's what I meant in the above message.

author
#17 lore raw

Re: [PATCH v4 5/6] bisect--helper: reimplement `bisect_run` shell function in C

Hi,

Quoted 22 lines · Christian Couder
Quoted 19 lines · Johannes Schindelin

Hi Miriam,

Quoted 9 lines · Miriam R.

El mar, 17 ago 2021 a las 13:42, Johannes Schindelin

Quoted 2 lines

Also: I think at this stage, an equivalent to cat "$GIT_DIR/BISECT_RUN" is missing.

In the previous patch series (v3), I implemented the equivalent to the cat command but I understood reviewers wanted to print the output to the user, so I reverted my changes for this version.
https://lore.kernel.org/git/[email protected]/

I am a bit confused: doesn't bisect_state() write to the BISECT_RUN file? If so, I think we do need to show the contents by opening the file and piping it to stdout.

Quoted 8 lines
Quoted 4 lines

FWIW I read
https://lore.kernel.org/git/CAP8UFD3X24F3qgefHpi00PM-KUk+vcqxwy2Dbngbyj7ciavCVQ@mail.gmail.com/
to mean the same thing, although I have to admit that I am not 100% certain.

I agree that, after bisect_state() has written into the BISECT_RUN file, we should indeed be opening it and piping it to stdout. That's what I meant in the above message.

Sorry for the confusion, I was understanding that reviewers wanted a different approach, one thing or the other, not both.
I will do both then.
Thank you for the clarification!
Best,
Miriam.