Skip to contentSkip to conversation

Finish converting git bisect to C part 4

PATCH Superseded Miriam Rubio 3 participants 17 messages Sep 2, 2021 – Sep 9, 2021

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v6 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-v6.

I would like to thank Johannes Schindelin for reviewing this patch series.

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

[5/6] bisect--helper: reimplement bisect_run shell function in C
* Format line.
* Use copy_fd() in print_file_to_stdout().
---

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    | 152 ++++++++++++++++++++++++++++++++++--
 git-bisect.sh               |  87 +--------------------
 run-command.c               |   2 +-
 run-command.h               |  12 +++
 t/t6030-bisect-porcelain.sh |  18 +++++
 5 files changed, 177 insertions(+), 94 deletions(-)
Signature · 1 line
2.29.2
#1 lore raw

PATCH v6 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 | 11 +++++++++++
 1 file changed, 11 insertions(+)
t/t6030-bisect-porcelain.sh +11 -0
index a1baf4e451..e61b8143fd 100755
@@ -962,4 +962,15 @@ 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 &&
+	write_script test_script.sh <<-\EOF &&
+	exit 255 >/dev/null
+	EOF
+	test_must_fail git bisect run ./test_script.sh >> my_bisect_log.txt
+'
+
 test_done
Signature · 1 line
2.29.2
#2 lore raw

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

Quoted 16 lines · Miriam Rubio

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 | 11 +++++++++++
 1 file changed, 11 insertions(+)
t/t6030-bisect-porcelain.sh +0 -0
index a1baf4e451..e61b8143fd 100755
@@ -962,4 +962,15 @@ test_expect_success 'bisect handles annotated tags' '
 	grep "$bad is the first bad commit" output
 '
 

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

Quoted 10 lines
t/t6030-bisect-porcelain.sh +10 -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 &&
+	write_script test_script.sh <<-\EOF &&
+	exit 255 >/dev/null
+	EOF
+	test_must_fail git bisect run ./test_script.sh >> my_bisect_log.txt
+'

Two and a half glitches.

* It is not obvious why you need to redirect output from "exit" to /dev/null; drop them or explain the reason in the proposed log message, perhaps.

* The contents of my_bisect_log.txt is never inspected. If it does not matter how the command fails, not inspecting is perfectly OK, but then perhaps not capturing it is the right thing to do? We do not even want to redirect the output to /dev/null, as the output from the commands run in these test pieces will not be shown unless the test scripts are run under an option for debugging purposes.

* Style: no space after ">" or ">>" before my_bisect_log.txt

Thanks.

#3 lore raw

PATCH v6 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 e61b8143fd..f13eeac9ce 100755
@@ -973,4 +973,11 @@ test_expect_success 'bisect run fails with exit code equals or greater than 128'
 	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
#4 lore raw

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

Quoted 12 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 e61b8143fd..f13eeac9ce 100755

t/t6030-bisect-porcelain.sh test_expect_success 'bisect run fails with exit code equals or greater than 128' · line 977

Quoted 5 lines
t/t6030-bisect-porcelain.sh +2 -0
@@ -973,4 +973,11 @@ test_expect_success 'bisect run fails with exit code equals or greater than 128'
 	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 &&

The same style guide for redirection applies here.

Also, it makes sense to quote such an unusual filename for human readers, i.e.

	echo "My test line" >"./-hello 2" &&

t/t6030-bisect-porcelain.sh test_expect_success 'bisect run fails with exit code equals or greater than 128' · line 979

Quoted 2 lines
t/t6030-bisect-porcelain.sh +2 -0
+	git add -- -hello\ 2 &&
+	git commit --quiet -m "Add test line" -- -hello\ 2 &&

Likewise.

Especially since this is not a test for "git add" or "git commit", instead of writing "-hello 2", "./-hello 2" may help human readers better.

t/t6030-bisect-porcelain.sh test_expect_success 'bisect run fails with exit code equals or greater than 128' · line 980

Quoted 1 line
t/t6030-bisect-porcelain.sh +1 -0
+	git bisect visualize -p -- -hello\ 2 > my_bisect_log.txt

This one, if it is meant to test the pathspec parsing of the command being tested (i.e. "git bisect"), is probably better to be left without "./" prefix, i.e. "-hello 2".

The same comment applies to the redirection into my_bisect_log.txt file. It is better not to redirect this at all.

This is the first use of "git bisect visualize" in our tests. How are we making sure that we won't open gitk and leave it hanging and doing silly things like that?

    ... goes and looks ...

Ah, OK. "git bisect --help" makes it clear that giving an option like "-p" tells us to run "git log", so we are OK.

THanks.

Quoted 3 lines
t/t6030-bisect-porcelain.sh +2 -0
+'
+
 test_done
#5 lore raw

PATCH v6 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
#6 lore raw

Re: [PATCH v6 3/6] run-command: make `exists_in_PATH()` non-static

Quoted 4 lines · 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.

"Remove" and "declare", as if we are giving an order to somebody else to make these changes.

Quoted 30 lines

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 +0 -0
index af1296769f..54d74b706f 100644

run-command.h void child_process_clear(struct child_process *); · line 186

Quoted 5 lines
run-command.h +2 -0
@@ -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

The first sentence does not make sense to me. Isn't this for checking if a command exists in one of the directories on $PATH?

Check if the command exists on $PATH.

may make more sense, especially since "search" may hint that the caller may be able to learn where it exists, which is not the case.

run-command.h void child_process_clear(struct child_process *); · line 191

Quoted 5 lines
run-command.h +5 -0
+ * 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.

Consistently use "command" instead of "file" and rename the parameter in the prototype below from "file" to "command".

Alternatively, you can rewrite the first paragraph above to make sure that it is clear to the readers that "command" it refers to is actually the "file" parameter the function takes. A rewrite of the first sentence I just rewrote above may become

Check if an executable "file" exists on $PATH.

which does not look too bad, but "executing the file so it can ..." and "to run a file using..." smell a bit strange, and that is why I suggested to consistently use "command" instead.

run-command.h void child_process_clear(struct child_process *); · line 195

Quoted 4 lines
run-command.h +4 -0
+ *
+ * Returns 1 if it is found in $PATH or 0 if the command could not be found.
+ */
+int exists_in_PATH(const char *file);

Thanks.

#7 lore raw

PATCH v6 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..1e118a966a 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_push(&args, "log");
+			flags |= RUN_GIT_CMD;
+		}
+	} else {
+		if (argv[0][0] == '-') {
+			strvec_push(&args, "log");
+			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
#8 lore raw

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

Quoted 1 line · Miriam Rubio

From: Pranit Bauva <[email protected]>

Need a SP before "shell" on the title line.

Quoted 3 lines

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.

Nice.

Quoted 7 lines
builtin/bisect--helper.c +7 -0
+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)

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

Quoted 10 lines
builtin/bisect--helper.c +10 -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_push(&args, "log");
+			flags |= RUN_GIT_CMD;
+		}

Let's have {} on the if() side, even though it only has one statement and does not require one, because the else side needs one.

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

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

OK, any -option makes it "git log -option ..." invocation.

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

Quoted 2 lines
builtin/bisect--helper.c +2 -0
+		} else if (strcmp(argv[0], "tig") && !starts_with(argv[0], "git"))
+			flags |= RUN_GIT_CMD;

OK, when the first token is "tig", or it begins with "git", the scripted version just leaves the command line intact. Everything else is taken as a subcommand to git. And this conditional is a faithful translation of that logic.

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

Quoted 12 lines
builtin/bisect--helper.c +12 -0
+		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;

OK.

The code is quite easy to follow, thanks to many helpers that have been invented for this exact purpose, like sq_dequote_to_strvec().

Quoted 16 lines
git-bisect.sh +0 -3
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() {
...
-}
-
 bisect_run () {
 	git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD fail || exit

git-bisect.sh case "$#" in · line 132

Quoted 5 lines
git-bisect.sh +1 -1
@@ -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;;

Nice.

Thanks.

#9 lore raw

PATCH v6 5/6 bisect--helper: reimplement `bisect_run` shell

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 | 97 ++++++++++++++++++++++++++++++++++++++++
 git-bisect.sh            | 62 +------------------------
 2 files changed, 98 insertions(+), 61 deletions(-)
builtin/bisect--helper.c +97 -0
index 1e118a966a..8e9ed9c318 100644
@@ -18,6 +18,7 @@ static GIT_PATH_FUNC(git_path_bisect_log, "BISECT_LOG")
 static GIT_PATH_FUNC(git_path_head_name, "head-name")
 static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES")
 static GIT_PATH_FUNC(git_path_bisect_first_parent, "BISECT_FIRST_PARENT")
+static GIT_PATH_FUNC(git_path_bisect_run, "BISECT_RUN")
 
 static const char * const git_bisect_helper_usage[] = {
 	N_("git bisect--helper --bisect-reset [<commit>]"),
@@ -31,6 +32,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
 };
 
@@ -144,6 +146,19 @@ static int append_to_file(const char *path, const char *format, ...)
 	return res;
 }
 
+static int print_file_to_stdout(const char *path)
+{
+	int fd = open(path, O_RDONLY);
+	int ret = 0;
+
+	if (fd < 0)
+		return error_errno(_("cannot open file '%s' for reading"), path);
+	if (copy_fd(fd, 1) < 0)
+		ret = error_errno(_("failed to read '%s'"), path);
+	close(fd);
+	return ret;
+}
+
 static int check_term_format(const char *term, const char *orig_term)
 {
 	int res;
@@ -1075,6 +1090,79 @@ 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;
+	const char *new_state;
+	int temporary_stdout_fd, saved_stdout;
+
+	if (bisect_next_check(terms, NULL))
+		return BISECT_FAILED;
+
+	if (argc)
+		sq_quote_argv(&command, argv);
+	else {
+		error(_("bisect run failed: no command provided."));
+		return BISECT_FAILED;
+	}
+
+	strvec_push(&run_args, command.buf);
+
+	while (1) {
+		strvec_clear(&args);
+
+		printf(_("running %s\n"), 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)
+			new_state = "skip";
+		else
+			new_state = res > 0 ? terms->term_bad : terms->term_good;
+
+		temporary_stdout_fd = open(git_path_bisect_run(), O_CREAT | O_WRONLY | O_TRUNC, 0666);
+		saved_stdout = dup(1);
+		dup2(temporary_stdout_fd, 1);
+
+		res = bisect_state(terms, &new_state, 1);
+
+		dup2(saved_stdout, 1);
+		close(saved_stdout);
+		close(temporary_stdout_fd);
+
+		print_file_to_stdout(git_path_bisect_run());
+
+		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 {
+			continue;
+		}
+
+		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 +1177,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 +1201,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 +1268,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
#10 lore raw

Re: [PATCH v6 5/6] bisect--helper: reimplement `bisect_run` shell

Quoted 85 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 | 97 ++++++++++++++++++++++++++++++++++++++++
 git-bisect.sh            | 62 +------------------------
 2 files changed, 98 insertions(+), 61 deletions(-)
builtin/bisect--helper.c +42 -0
index 1e118a966a..8e9ed9c318 100644
@@ -18,6 +18,7 @@ static GIT_PATH_FUNC(git_path_bisect_log, "BISECT_LOG")
 static GIT_PATH_FUNC(git_path_head_name, "head-name")
 static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES")
 static GIT_PATH_FUNC(git_path_bisect_first_parent, "BISECT_FIRST_PARENT")
+static GIT_PATH_FUNC(git_path_bisect_run, "BISECT_RUN")
 
 static const char * const git_bisect_helper_usage[] = {
 	N_("git bisect--helper --bisect-reset [<commit>]"),
@@ -31,6 +32,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
 };
 
@@ -144,6 +146,19 @@ static int append_to_file(const char *path, const char *format, ...)
 	return res;
 }
 
+static int print_file_to_stdout(const char *path)
+{
+	int fd = open(path, O_RDONLY);
+	int ret = 0;
+
+	if (fd < 0)
+		return error_errno(_("cannot open file '%s' for reading"), path);
+	if (copy_fd(fd, 1) < 0)
+		ret = error_errno(_("failed to read '%s'"), path);
+	close(fd);
+	return ret;
+}
+
 static int check_term_format(const char *term, const char *orig_term)
 {
 	int res;
@@ -1075,6 +1090,79 @@ 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;
+	const char *new_state;
+	int temporary_stdout_fd, saved_stdout;
+
+	if (bisect_next_check(terms, NULL))
+		return BISECT_FAILED;
+
+	if (argc)
+		sq_quote_argv(&command, argv);
+	else {
+		error(_("bisect run failed: no command provided."));
+		return BISECT_FAILED;
+	}
+	strvec_push(&run_args, command.buf);
+
+	while (1) {
+		strvec_clear(&args);
+
+		printf(_("running %s\n"), command.buf);
+		res = run_command_v_opt(run_args.v, RUN_USING_SHELL);
+
+		if (res < 0 || 128 <= res) {

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

Quoted 10 lines
builtin/bisect--helper.c +10 -0
+			error(_("bisect run failed: exit code %d from"
+				" '%s' is < 0 or >= 128"), res, command.buf);
+			strbuf_release(&command);
+			return res;
+		}
+
+		if (res == 125)
+			new_state = "skip";
+		else
+			new_state = res > 0 ? terms->term_bad : terms->term_good;

It is easier to follow the code if you spelled out this part as

		else if (!res)
			new_state = terms->term_good;
		else
			new_state = terms->term_bad;

because that would consistently handle the three cases. Of course you _could_ do

		new_state = (res == 125)
			  ? "skip"
			  : (res > 0)
			  ? terms->term_bad
			  : terms->term_good;

instead, but that would be harder to read.

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

Quoted 1 line
builtin/bisect--helper.c +1 -0
+		temporary_stdout_fd = open(git_path_bisect_run(), O_CREAT | O_WRONLY | O_TRUNC, 0666);

Can this open fail, and if it fails, what do we want to do?

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

Quoted 8 lines
builtin/bisect--helper.c +8 -0
+		saved_stdout = dup(1);
+		dup2(temporary_stdout_fd, 1);
+
+		res = bisect_state(terms, &new_state, 1);
+
+		dup2(saved_stdout, 1);
+		close(saved_stdout);
+		close(temporary_stdout_fd);

Hmph, now you lost me. Whose output are we working around here with the redirection?

	... goes and looks ...

Ahh, OK. bisect_next_all() to bisect_checkout() all assume that they only need to write to the standard output, so we need to do this dance (unless we are willing to update the bisect.c functions to accept FILE * as parameter, that is).

However, they use not just write(2) but stdio to do their output, no? Don't we need to fflush(stdout) around the redirection dance, one to empty the output that was associated with the real standard output stream before asking bisect_state() to write to fd #1 via stdio, and one more time to flush out what bisect_state() wrote to the stdio after the call returns before closing the fd we opened to the BISECT_RUN file?

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

Quoted 1 line
builtin/bisect--helper.c +1 -0
+		print_file_to_stdout(git_path_bisect_run());

OK. So this corresponds to the "write bisect-state to ./git/BISECT_RUN and then cat it" in the scripted version.

Quoted 11 lines
builtin/bisect--helper.c +11 -0
+		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);

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

Quoted 10 lines
builtin/bisect--helper.c +10 -0
+		} else {
+			continue;
+		}
+
+		strbuf_release(&command);
+		strvec_clear(&args);
+		strvec_clear(&run_args);
+		return res;
+	}
+}

OK, the "res to diag" and clearing the resources at the end of the function looks good to me.

Thanks.

#11 lore raw

Re: [PATCH v6 5/6] bisect--helper: reimplement `bisect_run` shell

Hi Junio & Miriam,

Quoted 55 lines · Junio C Hamano

Miriam Rubio <[email protected]> writes:

[...]

Quoted 31 lines
(quoted diff) +27 -0
@@ -1075,6 +1090,79 @@ 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;
+	const char *new_state;
+	int temporary_stdout_fd, saved_stdout;
+
+	if (bisect_next_check(terms, NULL))
+		return BISECT_FAILED;
+
+	if (argc)
+		sq_quote_argv(&command, argv);
+	else {
+		error(_("bisect run failed: no command provided."));
+		return BISECT_FAILED;
+	}
+	strvec_push(&run_args, command.buf);
+
+	while (1) {
+		strvec_clear(&args);
+
+		printf(_("running %s\n"), command.buf);
+		res = run_command_v_opt(run_args.v, RUN_USING_SHELL);
+
+		if (res < 0 || 128 <= res) {

Review static int bisect_visualize(struct bisect_terms *terms, const char **argv, int a · line 1129

Quoted 10 lines
(quoted diff) +10 -0
+			error(_("bisect run failed: exit code %d from"
+				" '%s' is < 0 or >= 128"), res, command.buf);
+			strbuf_release(&command);
+			return res;
+		}
+
+		if (res == 125)
+			new_state = "skip";
+		else
+			new_state = res > 0 ? terms->term_bad : terms->term_good;

It is easier to follow the code if you spelled out this part as

		else if (!res)
			new_state = terms->term_good;
		else
			new_state = terms->term_bad;

because that would consistently handle the three cases. Of course you _could_ do

Quoted 7 lines
		new_state = (res == 125)
			  ? "skip"
			  : (res > 0)
			  ? terms->term_bad
			  : terms->term_good;

instead, but that would be harder to read.

FWIW I agree with this, after seeing the resulting code.

Quoted 23 lines

Review static int bisect_visualize(struct bisect_terms *terms, const char **argv, int a · line 1130

Quoted 1 line
(quoted diff) +1 -0
+		temporary_stdout_fd = open(git_path_bisect_run(), O_CREAT | O_WRONLY | O_TRUNC, 0666);

Can this open fail, and if it fails, what do we want to do?

Review static int bisect_visualize(struct bisect_terms *terms, const char **argv, int a · line 1138

Quoted 8 lines
(quoted diff) +8 -0
+		saved_stdout = dup(1);
+		dup2(temporary_stdout_fd, 1);
+
+		res = bisect_state(terms, &new_state, 1);
+
+		dup2(saved_stdout, 1);
+		close(saved_stdout);
+		close(temporary_stdout_fd);

Hmph, now you lost me. Whose output are we working around here with the redirection?

	... goes and looks ...

Ahh, OK. bisect_next_all() to bisect_checkout() all assume that they only need to write to the standard output, so we need to do this dance (unless we are willing to update the bisect.c functions to accept FILE * as parameter, that is).

Quoted 7 lines

However, they use not just write(2) but stdio to do their output, no? Don't we need to fflush(stdout) around the redirection dance, one to empty the output that was associated with the real standard output stream before asking bisect_state() to write to fd #1 via stdio, and one more time to flush out what bisect_state() wrote to the stdio after the call returns before closing the fd we opened to the BISECT_RUN file?

Yes, we would have to fflush(stdout).

However, I still don't like that we play such a dup2() game. I gave it a quick try to avoid it (see the diff below, which corresponds to the commit I pushed up as git-bisect-work-part4-v7 to
https://github.com/dscho/git), which still could benefit from a bit of polishing (maybe we should rethink the object model and extend/rename bisect_terms to bisect_state and accumulate more fields, such as out_fd.

Obviously this will need to be cleaned up, and while I would _love_ to see this make it into your next iteration, ultimately it is up to you, Miriam, to decide whether you want to build on my diff (quite possibly making the entire object model of the bisect part of Git's code more elegant and more maintainable), and up to you, Junio, to decide whether you would be willing to accept the patch series without this refactoring.

-- snipsnap --

bisect.c +37 -27
index af2863d044b..405bf60b4b6 100644
@@ -683,20 +683,21 @@ static void bisect_common(struct rev_info *revs)
 }
 
 static enum bisect_error error_if_skipped_commits(struct commit_list *tried,
-				    const struct object_id *bad)
+						  const struct object_id *bad,
+						  FILE *out)
 {
 	if (!tried)
 		return BISECT_OK;
 
-	printf("There are only 'skip'ped commits left to test.\n"
-	       "The first %s commit could be any of:\n", term_bad);
+	fprintf(out, "There are only 'skip'ped commits left to test.\n"
+		"The first %s commit could be any of:\n", term_bad);
 
 	for ( ; tried; tried = tried->next)
-		printf("%s\n", oid_to_hex(&tried->item->object.oid));
+		fprintf(out, "%s\n", oid_to_hex(&tried->item->object.oid));
 
 	if (bad)
-		printf("%s\n", oid_to_hex(bad));
-	printf(_("We cannot bisect more!\n"));
+		fprintf(out, "%s\n", oid_to_hex(bad));
+	fprintf(out, _("We cannot bisect more!\n"));
 
 	return BISECT_ONLY_SKIPPED_LEFT;
 }
@@ -725,10 +726,12 @@ static int is_expected_rev(const struct object_id *oid)
 	return res;
 }
 
-static enum bisect_error bisect_checkout(const struct object_id *bisect_rev, int no_checkout)
+static enum bisect_error bisect_checkout(const struct object_id *bisect_rev,
+					 int no_checkout, FILE *out)
 {
 	char bisect_rev_hex[GIT_MAX_HEXSZ + 1];
 	enum bisect_error res = BISECT_OK;
+	struct child_process cp = CHILD_PROCESS_INIT;
 
 	oid_to_hex_r(bisect_rev_hex, bisect_rev);
 	update_ref(NULL, "BISECT_EXPECTED_REV", bisect_rev, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
@@ -749,7 +752,10 @@ static enum bisect_error bisect_checkout(const struct object_id *bisect_rev, int
 	}
 
 	argv_show_branch[1] = bisect_rev_hex;
-	res = run_command_v_opt(argv_show_branch, RUN_GIT_CMD);
+	cp.argv = argv_show_branch;
+	cp.git_cmd = 1;
+	cp.out = dup(fileno(out));
+	res = run_command(&cp);
 	/*
 	 * Errors in `run_command()` itself, signaled by res < 0,
 	 * and errors in the child process, signaled by res > 0
@@ -841,7 +847,8 @@ static void handle_skipped_merge_base(const struct object_id *mb)
  * for early success, this will be converted back to 0 in
  * check_good_are_ancestors_of_bad().
  */
-static enum bisect_error check_merge_bases(int rev_nr, struct commit **rev, int no_checkout)
+static enum bisect_error check_merge_bases(int rev_nr, struct commit **rev,
+					   int no_checkout, FILE *out)
 {
 	enum bisect_error res = BISECT_OK;
 	struct commit_list *result;
@@ -858,8 +865,8 @@ static enum bisect_error check_merge_bases(int rev_nr, struct commit **rev, int
 		} else if (0 <= oid_array_lookup(&skipped_revs, mb)) {
 			handle_skipped_merge_base(mb);
 		} else {
-			printf(_("Bisecting: a merge base must be tested\n"));
-			res = bisect_checkout(mb, no_checkout);
+			fprintf(out, _("Bisecting: a merge base must be tested\n"));
+			res = bisect_checkout(mb, no_checkout, out);
 			if (!res)
 				/* indicate early success */
 				res = BISECT_INTERNAL_SUCCESS_MERGE_BASE;
@@ -898,8 +905,9 @@ static int check_ancestors(struct repository *r, int rev_nr,
  */
 
 static enum bisect_error check_good_are_ancestors_of_bad(struct repository *r,
-					    const char *prefix,
-					    int no_checkout)
+							 const char *prefix,
+							 int no_checkout,
+							 FILE *out)
 {
 	char *filename;
 	struct stat st;
@@ -924,7 +932,7 @@ static enum bisect_error check_good_are_ancestors_of_bad(struct repository *r,
 
 	rev = get_bad_and_good_commits(r, &rev_nr);
 	if (check_ancestors(r, rev_nr, rev, prefix))
-		res = check_merge_bases(rev_nr, rev, no_checkout);
+		res = check_merge_bases(rev_nr, rev, no_checkout, out);
 	free(rev);
 
 	if (!res) {
@@ -953,7 +961,7 @@ static enum bisect_error check_good_are_ancestors_of_bad(struct repository *r,
  */
 static void show_diff_tree(struct repository *r,
 			   const char *prefix,
-			   struct commit *commit)
+			   struct commit *commit, FILE *out)
 {
 	const char *argv[] = {
 		"diff-tree", "--pretty", "--stat", "--summary", "--cc", NULL
@@ -964,6 +972,7 @@ static void show_diff_tree(struct repository *r,
 	repo_init_revisions(r, &opt, prefix);
 
 	setup_revisions(ARRAY_SIZE(argv) - 1, argv, &opt, NULL);
+	opt.diffopt.file = out;
 	log_tree_commit(&opt, commit);
 }
 
@@ -1007,7 +1016,8 @@ void read_bisect_terms(const char **read_bad, const char **read_good)
  * the end of bisect_helper::cmd_bisect__helper() helps bypassing
  * all the code related to finding a commit to test.
  */
-enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
+enum bisect_error bisect_next_all(struct repository *r, const char *prefix,
+				  FILE *out)
 {
 	struct rev_info revs;
 	struct commit_list *tried;
@@ -1032,7 +1042,7 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
 	if (skipped_revs.nr)
 		bisect_flags |= FIND_BISECTION_ALL;
 
-	res = check_good_are_ancestors_of_bad(r, prefix, no_checkout);
+	res = check_good_are_ancestors_of_bad(r, prefix, no_checkout, out);
 	if (res)
 		return res;
 
@@ -1051,10 +1061,10 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
 		 * We should return error here only if the "bad"
 		 * commit is also a "skip" commit.
 		 */
-		res = error_if_skipped_commits(tried, NULL);
+		res = error_if_skipped_commits(tried, NULL, out);
 		if (res < 0)
 			return res;
-		printf(_("%s was both %s and %s\n"),
+		fprintf(out, _("%s was both %s and %s\n"),
 		       oid_to_hex(current_bad_oid),
 		       term_good,
 		       term_bad);
@@ -1072,13 +1082,13 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
 	bisect_rev = &revs.commits->item->object.oid;
 
 	if (oideq(bisect_rev, current_bad_oid)) {
-		res = error_if_skipped_commits(tried, current_bad_oid);
+		res = error_if_skipped_commits(tried, current_bad_oid, out);
 		if (res)
 			return res;
-		printf("%s is the first %s commit\n", oid_to_hex(bisect_rev),
-			term_bad);
+		fprintf(out, "%s is the first %s commit\n",
+			oid_to_hex(bisect_rev), term_bad);
 
-		show_diff_tree(r, prefix, revs.commits->item);
+		show_diff_tree(r, prefix, revs.commits->item, out);
 		/*
 		 * This means the bisection process succeeded.
 		 * Using BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND (-10)
@@ -1098,14 +1108,14 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
 	 * TRANSLATORS: the last %s will be replaced with "(roughly %d
 	 * steps)" translation.
 	 */
-	printf(Q_("Bisecting: %d revision left to test after this %s\n",
-		  "Bisecting: %d revisions left to test after this %s\n",
-		  nr), nr, steps_msg);
+	fprintf(out, Q_("Bisecting: %d revision left to test after this %s\n",
+			"Bisecting: %d revisions left to test after this %s\n",
+			nr), nr, steps_msg);
 	free(steps_msg);
 	/* Clean up objects used, as they will be reused. */
 	repo_clear_commit_marks(r, ALL_REV_FLAGS);
 
-	return bisect_checkout(bisect_rev, no_checkout);
+	return bisect_checkout(bisect_rev, no_checkout, out);
 }
 
 static inline int log2i(int n)
bisect.h +2 -1
index ec24ac2d7ee..72bfd7b0053 100644
@@ -61,7 +61,8 @@ enum bisect_error {
 	BISECT_INTERNAL_SUCCESS_MERGE_BASE = -11
 };
 
-enum bisect_error bisect_next_all(struct repository *r, const char *prefix);
+enum bisect_error bisect_next_all(struct repository *r, const char *prefix,
+				  FILE *out);
 
 int estimate_bisect_steps(int all);
 
builtin/bisect--helper.c +19 -22

124 lines not on this page · raw message

author
#12 lore raw

Re: [PATCH v6 5/6] bisect--helper: reimplement `bisect_run` shell

Hi Johannes,

Quoted 111 lines · Johannes Schindelin

Hi Junio & Miriam,

Quoted 55 lines · Junio C Hamano

Miriam Rubio <[email protected]> writes:

[...]

Quoted 31 lines
(quoted diff) +27 -0
@@ -1075,6 +1090,79 @@ 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;
+   const char *new_state;
+   int temporary_stdout_fd, saved_stdout;
+
+   if (bisect_next_check(terms, NULL))
+           return BISECT_FAILED;
+
+   if (argc)
+           sq_quote_argv(&command, argv);
+   else {
+           error(_("bisect run failed: no command provided."));
+           return BISECT_FAILED;
+   }
+   strvec_push(&run_args, command.buf);
+
+   while (1) {
+           strvec_clear(&args);
+
+           printf(_("running %s\n"), command.buf);
+           res = run_command_v_opt(run_args.v, RUN_USING_SHELL);
+
+           if (res < 0 || 128 <= res) {

Review static int bisect_visualize(struct bisect_terms *terms, const char **argv, int a · line 1129

Quoted 10 lines
(quoted diff) +10 -0
+                   error(_("bisect run failed: exit code %d from"
+                           " '%s' is < 0 or >= 128"), res, command.buf);
+                   strbuf_release(&command);
+                   return res;
+           }
+
+           if (res == 125)
+                   new_state = "skip";
+           else
+                   new_state = res > 0 ? terms->term_bad : terms->term_good;

It is easier to follow the code if you spelled out this part as

              else if (!res)
                      new_state = terms->term_good;
              else
                      new_state = terms->term_bad;

because that would consistently handle the three cases. Of course you _could_ do

Quoted 7 lines
              new_state = (res == 125)
                        ? "skip"
                        : (res > 0)
                        ? terms->term_bad
                        : terms->term_good;

instead, but that would be harder to read.

FWIW I agree with this, after seeing the resulting code.

Quoted 23 lines

Review static int bisect_visualize(struct bisect_terms *terms, const char **argv, int a · line 1130

Quoted 1 line
(quoted diff) +1 -0
+           temporary_stdout_fd = open(git_path_bisect_run(), O_CREAT | O_WRONLY | O_TRUNC, 0666);

Can this open fail, and if it fails, what do we want to do?

Review static int bisect_visualize(struct bisect_terms *terms, const char **argv, int a · line 1138

Quoted 8 lines
(quoted diff) +8 -0
+           saved_stdout = dup(1);
+           dup2(temporary_stdout_fd, 1);
+
+           res = bisect_state(terms, &new_state, 1);
+
+           dup2(saved_stdout, 1);
+           close(saved_stdout);
+           close(temporary_stdout_fd);

Hmph, now you lost me. Whose output are we working around here with the redirection?

      ... goes and looks ...

Ahh, OK. bisect_next_all() to bisect_checkout() all assume that they only need to write to the standard output, so we need to do this dance (unless we are willing to update the bisect.c functions to accept FILE * as parameter, that is).

Quoted 7 lines

However, they use not just write(2) but stdio to do their output, no? Don't we need to fflush(stdout) around the redirection dance, one to empty the output that was associated with the real standard output stream before asking bisect_state() to write to fd #1 via stdio, and one more time to flush out what bisect_state() wrote to the stdio after the call returns before closing the fd we opened to the BISECT_RUN file?

Yes, we would have to fflush(stdout).

However, I still don't like that we play such a dup2() game. I gave it a quick try to avoid it (see the diff below, which corresponds to the commit I pushed up as git-bisect-work-part4-v7 to
https://github.com/dscho/git), which still could benefit from a bit of polishing (maybe we should rethink the object model and extend/rename bisect_terms to bisect_state and accumulate more fields, such as out_fd.

Quoted 7 lines

Obviously this will need to be cleaned up, and while I would _love_ to see this make it into your next iteration, ultimately it is up to you, Miriam, to decide whether you want to build on my diff (quite possibly making the entire object model of the bisect part of Git's code more elegant and more maintainable), and up to you, Junio, to decide whether you would be willing to accept the patch series without this refactoring.

I also don’t love this dup2() game but I implemented it as a possible solution to recreate the cat command as it is
in the shell script, without changing behavior or parameters in other functions. Also thank you for your solution, I agree that it is more elegant and maintainable.

If Junio accepts the patch series with my dup2() solution, I can implement your suggestion as an improvement after finishing the porting of git bisect to C. Because after this patch series, there will be only one last patch series left and I believe rethinking the object model and extend/rename bisect_terms to bisect_state and accumulate more fields, such as out_fd should be better separated of the porting project.

Best,
Miriam.

Quoted 341 lines

-- snipsnap --

bisect.c +37 -27
index af2863d044b..405bf60b4b6 100644
@@ -683,20 +683,21 @@ static void bisect_common(struct rev_info *revs)
 }
 
 static enum bisect_error error_if_skipped_commits(struct commit_list *tried,
-                                   const struct object_id *bad)
+                                                 const struct object_id *bad,
+                                                 FILE *out)
 {
        if (!tried)
                return BISECT_OK;
 
-       printf("There are only 'skip'ped commits left to test.\n"
-              "The first %s commit could be any of:\n", term_bad);
+       fprintf(out, "There are only 'skip'ped commits left to test.\n"
+               "The first %s commit could be any of:\n", term_bad);
 
        for ( ; tried; tried = tried->next)
-               printf("%s\n", oid_to_hex(&tried->item->object.oid));
+               fprintf(out, "%s\n", oid_to_hex(&tried->item->object.oid));
 
        if (bad)
-               printf("%s\n", oid_to_hex(bad));
-       printf(_("We cannot bisect more!\n"));
+               fprintf(out, "%s\n", oid_to_hex(bad));
+       fprintf(out, _("We cannot bisect more!\n"));
 
        return BISECT_ONLY_SKIPPED_LEFT;
 }
@@ -725,10 +726,12 @@ static int is_expected_rev(const struct object_id *oid)
        return res;
 }
 
-static enum bisect_error bisect_checkout(const struct object_id *bisect_rev, int no_checkout)
+static enum bisect_error bisect_checkout(const struct object_id *bisect_rev,
+                                        int no_checkout, FILE *out)
 {
        char bisect_rev_hex[GIT_MAX_HEXSZ + 1];
        enum bisect_error res = BISECT_OK;
+       struct child_process cp = CHILD_PROCESS_INIT;
 
        oid_to_hex_r(bisect_rev_hex, bisect_rev);
        update_ref(NULL, "BISECT_EXPECTED_REV", bisect_rev, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
@@ -749,7 +752,10 @@ static enum bisect_error bisect_checkout(const struct object_id *bisect_rev, int
        }
 
        argv_show_branch[1] = bisect_rev_hex;
-       res = run_command_v_opt(argv_show_branch, RUN_GIT_CMD);
+       cp.argv = argv_show_branch;
+       cp.git_cmd = 1;
+       cp.out = dup(fileno(out));
+       res = run_command(&cp);
        /*
         * Errors in `run_command()` itself, signaled by res < 0,
         * and errors in the child process, signaled by res > 0
@@ -841,7 +847,8 @@ static void handle_skipped_merge_base(const struct object_id *mb)
  * for early success, this will be converted back to 0 in
  * check_good_are_ancestors_of_bad().
  */
-static enum bisect_error check_merge_bases(int rev_nr, struct commit **rev, int no_checkout)
+static enum bisect_error check_merge_bases(int rev_nr, struct commit **rev,
+                                          int no_checkout, FILE *out)
 {
        enum bisect_error res = BISECT_OK;
        struct commit_list *result;
@@ -858,8 +865,8 @@ static enum bisect_error check_merge_bases(int rev_nr, struct commit **rev, int
                } else if (0 <= oid_array_lookup(&skipped_revs, mb)) {
                        handle_skipped_merge_base(mb);
                } else {
-                       printf(_("Bisecting: a merge base must be tested\n"));
-                       res = bisect_checkout(mb, no_checkout);
+                       fprintf(out, _("Bisecting: a merge base must be tested\n"));
+                       res = bisect_checkout(mb, no_checkout, out);
                        if (!res)
                                /* indicate early success */
                                res = BISECT_INTERNAL_SUCCESS_MERGE_BASE;
@@ -898,8 +905,9 @@ static int check_ancestors(struct repository *r, int rev_nr,
  */
 
 static enum bisect_error check_good_are_ancestors_of_bad(struct repository *r,
-                                           const char *prefix,
-                                           int no_checkout)
+                                                        const char *prefix,
+                                                        int no_checkout,
+                                                        FILE *out)
 {
        char *filename;
        struct stat st;
@@ -924,7 +932,7 @@ static enum bisect_error check_good_are_ancestors_of_bad(struct repository *r,
 
        rev = get_bad_and_good_commits(r, &rev_nr);
        if (check_ancestors(r, rev_nr, rev, prefix))
-               res = check_merge_bases(rev_nr, rev, no_checkout);
+               res = check_merge_bases(rev_nr, rev, no_checkout, out);
        free(rev);
 
        if (!res) {
@@ -953,7 +961,7 @@ static enum bisect_error check_good_are_ancestors_of_bad(struct repository *r,
  */
 static void show_diff_tree(struct repository *r,
                           const char *prefix,
-                          struct commit *commit)
+                          struct commit *commit, FILE *out)
 {
        const char *argv[] = {
                "diff-tree", "--pretty", "--stat", "--summary", "--cc", NULL
@@ -964,6 +972,7 @@ static void show_diff_tree(struct repository *r,
        repo_init_revisions(r, &opt, prefix);
 
        setup_revisions(ARRAY_SIZE(argv) - 1, argv, &opt, NULL);
+       opt.diffopt.file = out;
        log_tree_commit(&opt, commit);
 }
 
@@ -1007,7 +1016,8 @@ void read_bisect_terms(const char **read_bad, const char **read_good)
  * the end of bisect_helper::cmd_bisect__helper() helps bypassing
  * all the code related to finding a commit to test.
  */
-enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
+enum bisect_error bisect_next_all(struct repository *r, const char *prefix,
+                                 FILE *out)
 {
        struct rev_info revs;
        struct commit_list *tried;
@@ -1032,7 +1042,7 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
        if (skipped_revs.nr)
                bisect_flags |= FIND_BISECTION_ALL;
 
-       res = check_good_are_ancestors_of_bad(r, prefix, no_checkout);
+       res = check_good_are_ancestors_of_bad(r, prefix, no_checkout, out);
        if (res)
                return res;
 
@@ -1051,10 +1061,10 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
                 * We should return error here only if the "bad"
                 * commit is also a "skip" commit.
                 */
-               res = error_if_skipped_commits(tried, NULL);
+               res = error_if_skipped_commits(tried, NULL, out);
                if (res < 0)
                        return res;
-               printf(_("%s was both %s and %s\n"),
+               fprintf(out, _("%s was both %s and %s\n"),
                       oid_to_hex(current_bad_oid),
                       term_good,
                       term_bad);
@@ -1072,13 +1082,13 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
        bisect_rev = &revs.commits->item->object.oid;
 
        if (oideq(bisect_rev, current_bad_oid)) {
-               res = error_if_skipped_commits(tried, current_bad_oid);
+               res = error_if_skipped_commits(tried, current_bad_oid, out);
                if (res)
                        return res;
-               printf("%s is the first %s commit\n", oid_to_hex(bisect_rev),
-                       term_bad);
+               fprintf(out, "%s is the first %s commit\n",
+                       oid_to_hex(bisect_rev), term_bad);
 
-               show_diff_tree(r, prefix, revs.commits->item);
+               show_diff_tree(r, prefix, revs.commits->item, out);
                /*
                 * This means the bisection process succeeded.
                 * Using BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND (-10)
@@ -1098,14 +1108,14 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
         * TRANSLATORS: the last %s will be replaced with "(roughly %d
         * steps)" translation.
         */
-       printf(Q_("Bisecting: %d revision left to test after this %s\n",
-                 "Bisecting: %d revisions left to test after this %s\n",
-                 nr), nr, steps_msg);
+       fprintf(out, Q_("Bisecting: %d revision left to test after this %s\n",
+                       "Bisecting: %d revisions left to test after this %s\n",
+                       nr), nr, steps_msg);
        free(steps_msg);
        /* Clean up objects used, as they will be reused. */
        repo_clear_commit_marks(r, ALL_REV_FLAGS);
 
-       return bisect_checkout(bisect_rev, no_checkout);
+       return bisect_checkout(bisect_rev, no_checkout, out);
 }
 
 static inline int log2i(int n)
bisect.h +2 -1
index ec24ac2d7ee..72bfd7b0053 100644
@@ -61,7 +61,8 @@ enum bisect_error {
        BISECT_INTERNAL_SUCCESS_MERGE_BASE = -11
 };
 
-enum bisect_error bisect_next_all(struct repository *r, const char *prefix);
+enum bisect_error bisect_next_all(struct repository *r, const char *prefix,
+                                 FILE *out);
 
 int estimate_bisect_steps(int all);
 
builtin/bisect--helper.c +19 -22
index 1c96580bd49..29969763d35 100644
@@ -581,7 +581,8 @@ static int bisect_successful(struct bisect_terms *terms)
        return res;
 }
 
-static enum bisect_error bisect_next(struct bisect_terms *terms, const char *prefix)
+static enum bisect_error bisect_next(struct bisect_terms *terms,
+                                    const char *prefix, FILE *out)
 {
        enum bisect_error res;
 
@@ -592,7 +593,7 @@ static enum bisect_error bisect_next(struct bisect_terms *terms, const char *pre
                return BISECT_FAILED;
 
        /* Perform all bisection computation */
-       res = bisect_next_all(the_repository, prefix);
+       res = bisect_next_all(the_repository, prefix, out);
 
        if (res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND) {
                res = bisect_successful(terms);
@@ -604,12 +605,13 @@ static enum bisect_error bisect_next(struct bisect_terms *terms, const char *pre
        return res;
 }
 
-static enum bisect_error bisect_auto_next(struct bisect_terms *terms, const char *prefix)
+static enum bisect_error bisect_auto_next(struct bisect_terms *terms,
+                                         const char *prefix, FILE *out)
 {
        if (bisect_next_check(terms, NULL))
                return BISECT_OK;
 
-       return bisect_next(terms, prefix);
+       return bisect_next(terms, prefix, out);
 }
 
 static enum bisect_error bisect_start(struct bisect_terms *terms, const char **argv, int argc)
@@ -808,7 +810,7 @@ static enum bisect_error bisect_start(struct bisect_terms *terms, const char **a
        if (res)
                return res;
 
-       res = bisect_auto_next(terms, NULL);
+       res = bisect_auto_next(terms, NULL, stdout);
        if (!is_bisect_success(res))
                bisect_clean_state();
        return res;
@@ -847,7 +849,7 @@ static int bisect_autostart(struct bisect_terms *terms)
 }
 
 static enum bisect_error bisect_state(struct bisect_terms *terms, const char **argv,
-                                     int argc)
+                                     int argc, FILE *out)
 {
        const char *state;
        int i, verify_expected = 1;
@@ -924,7 +926,7 @@ static enum bisect_error bisect_state(struct bisect_terms *terms, const char **a
        }
 
        oid_array_clear(&revs);
-       return bisect_auto_next(terms, NULL);
+       return bisect_auto_next(terms, NULL, out);
 }
 
 static enum bisect_error bisect_log(void)
@@ -1013,7 +1015,7 @@ static enum bisect_error bisect_replay(struct bisect_terms *terms, const char *f
        if (res)
                return BISECT_FAILED;
 
-       return bisect_auto_next(terms, NULL);
+       return bisect_auto_next(terms, NULL, stdout);
 }
 
 static enum bisect_error bisect_skip(struct bisect_terms *terms, const char **argv, int argc)
@@ -1045,7 +1047,7 @@ static enum bisect_error bisect_skip(struct bisect_terms *terms, const char **ar
                        strvec_push(&argv_state, argv[i]);
                }
        }
-       res = bisect_state(terms, argv_state.v, argv_state.nr);
+       res = bisect_state(terms, argv_state.v, argv_state.nr, stdout);
 
        strvec_clear(&argv_state);
        return res;
@@ -1096,7 +1098,6 @@ static int bisect_run(struct bisect_terms *terms, const char **argv, int argc)
        struct strvec args = STRVEC_INIT;
        struct strvec run_args = STRVEC_INIT;
        const char *new_state;
-       int temporary_stdout_fd, saved_stdout;
 
        if (bisect_next_check(terms, NULL))
                return BISECT_FAILED;
@@ -1111,6 +1112,8 @@ static int bisect_run(struct bisect_terms *terms, const char **argv, int argc)
        strvec_push(&run_args, command.buf);
 
        while (1) {
+               FILE *f;
+
                strvec_clear(&args);
 
                printf(_("running %s\n"), command.buf);
@@ -1130,19 +1133,13 @@ static int bisect_run(struct bisect_terms *terms, const char **argv, int argc)
                else
                        new_state = terms->term_bad;
 
-               temporary_stdout_fd = open(git_path_bisect_run(), O_CREAT | O_WRONLY | O_TRUNC, 0666);
+               f = fopen_for_writing(git_path_bisect_run());
 
-               if (temporary_stdout_fd < 0)
+               if (!f)
                        return error_errno(_("cannot open file '%s' for writing"), git_path_bisect_run());
 
-               saved_stdout = dup(1);
-               dup2(temporary_stdout_fd, 1);
-
-               res = bisect_state(terms, &new_state, 1);
-
-               dup2(saved_stdout, 1);
-               close(saved_stdout);
-               close(temporary_stdout_fd);
+               res = bisect_state(terms, &new_state, 1, f);
+               fclose(f);
 
                print_file_to_stdout(git_path_bisect_run());
 
@@ -1240,12 +1237,12 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
                if (argc)
                        return error(_("--bisect-next requires 0 arguments"));
                get_terms(&terms);
-               res = bisect_next(&terms, prefix);
+               res = bisect_next(&terms, prefix, stdout);
                break;
        case BISECT_STATE:
                set_terms(&terms, "bad", "good");
                get_terms(&terms);
-               res = bisect_state(&terms, argv, argc);
+               res = bisect_state(&terms, argv, argc, stdout);
                break;
        case BISECT_LOG:
                if (argc)
#13 lore raw

Re: [PATCH v6 5/6] bisect--helper: reimplement `bisect_run` shell

Quoted 8 lines · Miriam R.
Quoted 8 lines

However, I still don't like that we play such a dup2() game. I gave it a quick try to avoid it (see the diff below, which corresponds to the commit I pushed up as git-bisect-work-part4-v7 to
https://github.com/dscho/git), which still could benefit from a bit of polishing (maybe we should rethink the object model and extend/rename bisect_terms to bisect_state and accumulate more fields, such as out_fd.

Quoted 6 lines
Quoted 6 lines

Obviously this will need to be cleaned up, and while I would _love_ to see this make it into your next iteration, ultimately it is up to you, Miriam, to decide whether you want to build on my diff (quite possibly making the entire object model of the bisect part of Git's code more elegant and more maintainable), and up to you, Junio, to decide whether you would be willing to accept the patch series without this refactoring.

If the code paths involved are shallow and narrow enough that not too many existing callers need to start passing FILE *stdout down (from the looks of your illustration patch, it does not seem to be too bad), I do not mind a series that is a bit longer than the current 6-patch series that has a preliminary enhancement step that allows callers to pass their own "FILE *" for output destination before the main part of the topic.

Thanks.

#14 lore raw

Re: [PATCH v6 5/6] bisect--helper: reimplement `bisect_run` shell

Hi Junio,

Quoted 17 lines · Junio C Hamano
Quoted 8 lines · Miriam R.
Quoted 8 lines

However, I still don't like that we play such a dup2() game. I gave it a quick try to avoid it (see the diff below, which corresponds to the commit I pushed up as git-bisect-work-part4-v7 to
https://github.com/dscho/git), which still could benefit from a bit of polishing (maybe we should rethink the object model and extend/rename bisect_terms to bisect_state and accumulate more fields, such as out_fd.

Quoted 6 lines
Quoted 6 lines

Obviously this will need to be cleaned up, and while I would _love_ to see this make it into your next iteration, ultimately it is up to you, Miriam, to decide whether you want to build on my diff (quite possibly making the entire object model of the bisect part of Git's code more elegant and more maintainable), and up to you, Junio, to decide whether you would be willing to accept the patch series without this refactoring.

Quoted 7 lines

If the code paths involved are shallow and narrow enough that not too many existing callers need to start passing FILE *stdout down (from the looks of your illustration patch, it does not seem to be too bad), I do not mind a series that is a bit longer than the current 6-patch series that has a preliminary enhancement step that allows callers to pass their own "FILE *" for output destination before the main part of the topic.

My impression, from the diff that I sent, is that this is too deep and wide, and indeed needs a follow-up patch series as indicated by Miriam. My preference would be (as I hinted at) to accumulate relevant data (such as the terms and, yes, the FILE *) into a struct bisect_state and pass that around. Sort of a light-weight object-oriented design, similar to how we do things in builtin/am.c with struct am_state.

Thanks,
Dscho

#15 lore raw

PATCH v6 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 8e9ed9c318..10c4cd24a1 100644
@@ -22,7 +22,6 @@ static GIT_PATH_FUNC(git_path_bisect_run, "BISECT_RUN")
 
 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>...]"),
@@ -1222,12 +1221,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
#16 lore raw

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

Quoted 4 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.

Yay. Nice.