git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH v6 0/6] Finish converting git bisect to C part 4
@ 2021-09-02  9:04 Miriam Rubio
  2021-09-02  9:04 ` [PATCH v6 1/6] t6030-bisect-porcelain: add tests to control bisect run exit cases Miriam Rubio
                   ` (5 more replies)
  0 siblings, 6 replies; 17+ messages in thread
From: Miriam Rubio @ 2021-09-02  9:04 UTC (permalink / raw)
  To: git; +Cc: Miriam Rubio

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/pull.117.git.gitgitgadget@gmail.com) 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(-)

-- 
2.29.2


^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH v6 1/6] t6030-bisect-porcelain: add tests to control bisect run exit cases
  2021-09-02  9:04 [PATCH v6 0/6] Finish converting git bisect to C part 4 Miriam Rubio
@ 2021-09-02  9:04 ` Miriam Rubio
  2021-09-02 21:44   ` Junio C Hamano
  2021-09-02  9:04 ` [PATCH v6 2/6] t6030-bisect-porcelain: add test for bisect visualize Miriam Rubio
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 17+ messages in thread
From: Miriam Rubio @ 2021-09-02  9:04 UTC (permalink / raw)
  To: git; +Cc: Miriam Rubio

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

Signed-off-by: Miriam Rubio <mirucam@gmail.com>
---
 t/t6030-bisect-porcelain.sh | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/t/t6030-bisect-porcelain.sh b/t/t6030-bisect-porcelain.sh
index a1baf4e451..e61b8143fd 100755
--- a/t/t6030-bisect-porcelain.sh
+++ b/t/t6030-bisect-porcelain.sh
@@ -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
-- 
2.29.2


^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH v6 2/6] t6030-bisect-porcelain: add test for bisect visualize
  2021-09-02  9:04 [PATCH v6 0/6] Finish converting git bisect to C part 4 Miriam Rubio
  2021-09-02  9:04 ` [PATCH v6 1/6] t6030-bisect-porcelain: add tests to control bisect run exit cases Miriam Rubio
@ 2021-09-02  9:04 ` Miriam Rubio
  2021-09-02 22:05   ` Junio C Hamano
  2021-09-02  9:04 ` [PATCH v6 3/6] run-command: make `exists_in_PATH()` non-static Miriam Rubio
                   ` (3 subsequent siblings)
  5 siblings, 1 reply; 17+ messages in thread
From: Miriam Rubio @ 2021-09-02  9:04 UTC (permalink / raw)
  To: git; +Cc: Miriam Rubio

Add a test to control breakages in bisect visualize command.

Signed-off-by: Miriam Rubio <mirucam@gmail.com>
---
 t/t6030-bisect-porcelain.sh | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/t/t6030-bisect-porcelain.sh b/t/t6030-bisect-porcelain.sh
index e61b8143fd..f13eeac9ce 100755
--- a/t/t6030-bisect-porcelain.sh
+++ b/t/t6030-bisect-porcelain.sh
@@ -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
-- 
2.29.2


^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH v6 3/6] run-command: make `exists_in_PATH()` non-static
  2021-09-02  9:04 [PATCH v6 0/6] Finish converting git bisect to C part 4 Miriam Rubio
  2021-09-02  9:04 ` [PATCH v6 1/6] t6030-bisect-porcelain: add tests to control bisect run exit cases Miriam Rubio
  2021-09-02  9:04 ` [PATCH v6 2/6] t6030-bisect-porcelain: add test for bisect visualize Miriam Rubio
@ 2021-09-02  9:04 ` Miriam Rubio
  2021-09-02 22:19   ` Junio C Hamano
  2021-09-02  9:04 ` [PATCH v6 4/6] bisect--helper: reimplement `bisect_visualize()`shell function in C Miriam Rubio
                   ` (2 subsequent siblings)
  5 siblings, 1 reply; 17+ messages in thread
From: Miriam Rubio @ 2021-09-02  9:04 UTC (permalink / raw)
  To: git; +Cc: Pranit Bauva, Tanushree Tumane, Miriam Rubio

From: Pranit Bauva <pranit.bauva@gmail.com>

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 <chriscool@tuxfamily.org>
Mentored by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Signed-off-by: Tanushree Tumane <tanushreetumane@gmail.com>
Signed-off-by: Miriam Rubio <mirucam@gmail.com>
---
 run-command.c |  2 +-
 run-command.h | 12 ++++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/run-command.c b/run-command.c
index f72e72cce7..390f46819f 100644
--- a/run-command.c
+++ b/run-command.c
@@ -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;
diff --git a/run-command.h b/run-command.h
index af1296769f..54d74b706f 100644
--- a/run-command.h
+++ b/run-command.h
@@ -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).
-- 
2.29.2


^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH v6 4/6] bisect--helper: reimplement `bisect_visualize()`shell function in C
  2021-09-02  9:04 [PATCH v6 0/6] Finish converting git bisect to C part 4 Miriam Rubio
                   ` (2 preceding siblings ...)
  2021-09-02  9:04 ` [PATCH v6 3/6] run-command: make `exists_in_PATH()` non-static Miriam Rubio
@ 2021-09-02  9:04 ` Miriam Rubio
  2021-09-02 22:28   ` Junio C Hamano
  2021-09-02  9:04 ` [PATCH v6 5/6] bisect--helper: reimplement `bisect_run` shell Miriam Rubio
  2021-09-02  9:04 ` [PATCH v6 6/6] bisect--helper: retire `--bisect-next-check` subcommand Miriam Rubio
  5 siblings, 1 reply; 17+ messages in thread
From: Miriam Rubio @ 2021-09-02  9:04 UTC (permalink / raw)
  To: git
  Cc: Pranit Bauva, Christian Couder, Johannes Schindelin,
	Tanushree Tumane, Miriam Rubio

From: Pranit Bauva <pranit.bauva@gmail.com>

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.

Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Mentored-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Signed-off-by: Tanushree Tumane <tanushreetumane@gmail.com>
Signed-off-by: Miriam Rubio <mirucam@gmail.com>
---
 builtin/bisect--helper.c | 48 +++++++++++++++++++++++++++++++++++++++-
 git-bisect.sh            | 25 +--------------------
 2 files changed, 48 insertions(+), 25 deletions(-)

diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c
index f184eaeac6..1e118a966a 100644
--- a/builtin/bisect--helper.c
+++ b/builtin/bisect--helper.c
@@ -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);
 	}
diff --git a/git-bisect.sh b/git-bisect.sh
index 6a7afaea8d..95f7f3fb8c 100755
--- a/git-bisect.sh
+++ b/git-bisect.sh
@@ -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


^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH v6 5/6] bisect--helper: reimplement `bisect_run` shell
  2021-09-02  9:04 [PATCH v6 0/6] Finish converting git bisect to C part 4 Miriam Rubio
                   ` (3 preceding siblings ...)
  2021-09-02  9:04 ` [PATCH v6 4/6] bisect--helper: reimplement `bisect_visualize()`shell function in C Miriam Rubio
@ 2021-09-02  9:04 ` Miriam Rubio
  2021-09-02 23:43   ` Junio C Hamano
  2021-09-02  9:04 ` [PATCH v6 6/6] bisect--helper: retire `--bisect-next-check` subcommand Miriam Rubio
  5 siblings, 1 reply; 17+ messages in thread
From: Miriam Rubio @ 2021-09-02  9:04 UTC (permalink / raw)
  To: git; +Cc: Tanushree Tumane, Christian Couder, Miriam Rubio

From: Tanushree Tumane <tanushreetumane@gmail.com>

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.

Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Tanushree Tumane <tanushreetumane@gmail.com>
Signed-off-by: Miriam Rubio <mirucam@gmail.com>
---
 builtin/bisect--helper.c | 97 ++++++++++++++++++++++++++++++++++++++++
 git-bisect.sh            | 62 +------------------------
 2 files changed, 98 insertions(+), 61 deletions(-)

diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c
index 1e118a966a..8e9ed9c318 100644
--- a/builtin/bisect--helper.c
+++ b/builtin/bisect--helper.c
@@ -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);
 	}
diff --git a/git-bisect.sh b/git-bisect.sh
index 95f7f3fb8c..e83d011e17 100755
--- a/git-bisect.sh
+++ b/git-bisect.sh
@@ -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


^ permalink raw reply related	[flat|nested] 17+ messages in thread

* [PATCH v6 6/6] bisect--helper: retire `--bisect-next-check` subcommand
  2021-09-02  9:04 [PATCH v6 0/6] Finish converting git bisect to C part 4 Miriam Rubio
                   ` (4 preceding siblings ...)
  2021-09-02  9:04 ` [PATCH v6 5/6] bisect--helper: reimplement `bisect_run` shell Miriam Rubio
@ 2021-09-02  9:04 ` Miriam Rubio
  2021-09-02 23:43   ` Junio C Hamano
  5 siblings, 1 reply; 17+ messages in thread
From: Miriam Rubio @ 2021-09-02  9:04 UTC (permalink / raw)
  To: git; +Cc: 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 <chriscool@tuxfamily.org>
Signed-off-by: Miriam Rubio <mirucam@gmail.com>
---
 builtin/bisect--helper.c | 7 -------
 1 file changed, 7 deletions(-)

diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c
index 8e9ed9c318..10c4cd24a1 100644
--- a/builtin/bisect--helper.c
+++ b/builtin/bisect--helper.c
@@ -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"));
-- 
2.29.2


^ permalink raw reply related	[flat|nested] 17+ messages in thread

* Re: [PATCH v6 1/6] t6030-bisect-porcelain: add tests to control bisect run exit cases
  2021-09-02  9:04 ` [PATCH v6 1/6] t6030-bisect-porcelain: add tests to control bisect run exit cases Miriam Rubio
@ 2021-09-02 21:44   ` Junio C Hamano
  0 siblings, 0 replies; 17+ messages in thread
From: Junio C Hamano @ 2021-09-02 21:44 UTC (permalink / raw)
  To: Miriam Rubio; +Cc: git

Miriam Rubio <mirucam@gmail.com> writes:

> There is a gap on bisect run test coverage related with error exits.
> Add two tests to control these error cases.
>
> Signed-off-by: Miriam Rubio <mirucam@gmail.com>
> ---
>  t/t6030-bisect-porcelain.sh | 11 +++++++++++
>  1 file changed, 11 insertions(+)
>
> diff --git a/t/t6030-bisect-porcelain.sh b/t/t6030-bisect-porcelain.sh
> index a1baf4e451..e61b8143fd 100755
> --- a/t/t6030-bisect-porcelain.sh
> +++ b/t/t6030-bisect-porcelain.sh
> @@ -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
> +'

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.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v6 2/6] t6030-bisect-porcelain: add test for bisect visualize
  2021-09-02  9:04 ` [PATCH v6 2/6] t6030-bisect-porcelain: add test for bisect visualize Miriam Rubio
@ 2021-09-02 22:05   ` Junio C Hamano
  0 siblings, 0 replies; 17+ messages in thread
From: Junio C Hamano @ 2021-09-02 22:05 UTC (permalink / raw)
  To: Miriam Rubio; +Cc: git

Miriam Rubio <mirucam@gmail.com> writes:

> Add a test to control breakages in bisect visualize command.
>
> Signed-off-by: Miriam Rubio <mirucam@gmail.com>
> ---
>  t/t6030-bisect-porcelain.sh | 7 +++++++
>  1 file changed, 7 insertions(+)
>
> diff --git a/t/t6030-bisect-porcelain.sh b/t/t6030-bisect-porcelain.sh
> index e61b8143fd..f13eeac9ce 100755
> --- a/t/t6030-bisect-porcelain.sh
> +++ b/t/t6030-bisect-porcelain.sh
> @@ -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" &&

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

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




> +'
> +
>  test_done

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v6 3/6] run-command: make `exists_in_PATH()` non-static
  2021-09-02  9:04 ` [PATCH v6 3/6] run-command: make `exists_in_PATH()` non-static Miriam Rubio
@ 2021-09-02 22:19   ` Junio C Hamano
  0 siblings, 0 replies; 17+ messages in thread
From: Junio C Hamano @ 2021-09-02 22:19 UTC (permalink / raw)
  To: Miriam Rubio; +Cc: git, Pranit Bauva, Tanushree Tumane

Miriam Rubio <mirucam@gmail.com> writes:

> From: Pranit Bauva <pranit.bauva@gmail.com>
>
> 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.

> The function will be used in bisect_visualize() in a later
> commit.
>
> Mentored by: Christian Couder <chriscool@tuxfamily.org>
> Mentored by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
> Signed-off-by: Tanushree Tumane <tanushreetumane@gmail.com>
> Signed-off-by: Miriam Rubio <mirucam@gmail.com>
> ---
>  run-command.c |  2 +-
>  run-command.h | 12 ++++++++++++
>  2 files changed, 13 insertions(+), 1 deletion(-)
>
> diff --git a/run-command.c b/run-command.c
> index f72e72cce7..390f46819f 100644
> --- a/run-command.c
> +++ b/run-command.c
> @@ -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;
> diff --git a/run-command.h b/run-command.h
> index af1296769f..54d74b706f 100644
> --- a/run-command.h
> +++ b/run-command.h
> @@ -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.

> + * 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.

> + *
> + * 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.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v6 4/6] bisect--helper: reimplement `bisect_visualize()`shell function in C
  2021-09-02  9:04 ` [PATCH v6 4/6] bisect--helper: reimplement `bisect_visualize()`shell function in C Miriam Rubio
@ 2021-09-02 22:28   ` Junio C Hamano
  0 siblings, 0 replies; 17+ messages in thread
From: Junio C Hamano @ 2021-09-02 22:28 UTC (permalink / raw)
  To: Miriam Rubio
  Cc: git, Pranit Bauva, Christian Couder, Johannes Schindelin,
	Tanushree Tumane

Miriam Rubio <mirucam@gmail.com> writes:

> From: Pranit Bauva <pranit.bauva@gmail.com>

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

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

> +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;
> +		}

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.

> +	} else {
> +		if (argv[0][0] == '-') {
> +			strvec_push(&args, "log");
> +			flags |= RUN_GIT_CMD;

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

> +		} 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.

> +		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().

> diff --git a/git-bisect.sh b/git-bisect.sh
> index 6a7afaea8d..95f7f3fb8c 100755
> --- a/git-bisect.sh
> +++ b/git-bisect.sh
> @@ -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
>  
> @@ -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.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v6 5/6] bisect--helper: reimplement `bisect_run` shell
  2021-09-02  9:04 ` [PATCH v6 5/6] bisect--helper: reimplement `bisect_run` shell Miriam Rubio
@ 2021-09-02 23:43   ` Junio C Hamano
  2021-09-06  7:33     ` Johannes Schindelin
  0 siblings, 1 reply; 17+ messages in thread
From: Junio C Hamano @ 2021-09-02 23:43 UTC (permalink / raw)
  To: Miriam Rubio; +Cc: git, Tanushree Tumane, Christian Couder

Miriam Rubio <mirucam@gmail.com> writes:

> From: Tanushree Tumane <tanushreetumane@gmail.com>
>
> 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.
>
> Mentored-by: Christian Couder <chriscool@tuxfamily.org>
> Signed-off-by: Tanushree Tumane <tanushreetumane@gmail.com>
> Signed-off-by: Miriam Rubio <mirucam@gmail.com>
> ---
>  builtin/bisect--helper.c | 97 ++++++++++++++++++++++++++++++++++++++++
>  git-bisect.sh            | 62 +------------------------
>  2 files changed, 98 insertions(+), 61 deletions(-)
>
> diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c
> index 1e118a966a..8e9ed9c318 100644
> --- a/builtin/bisect--helper.c
> +++ b/builtin/bisect--helper.c
> @@ -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;

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.


> +		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?

> +		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?

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

> +		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;
> +	}
> +}

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

Thanks.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v6 6/6] bisect--helper: retire `--bisect-next-check` subcommand
  2021-09-02  9:04 ` [PATCH v6 6/6] bisect--helper: retire `--bisect-next-check` subcommand Miriam Rubio
@ 2021-09-02 23:43   ` Junio C Hamano
  0 siblings, 0 replies; 17+ messages in thread
From: Junio C Hamano @ 2021-09-02 23:43 UTC (permalink / raw)
  To: Miriam Rubio; +Cc: git

Miriam Rubio <mirucam@gmail.com> writes:

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

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v6 5/6] bisect--helper: reimplement `bisect_run` shell
  2021-09-02 23:43   ` Junio C Hamano
@ 2021-09-06  7:33     ` Johannes Schindelin
  2021-09-06  8:34       ` Miriam R.
  0 siblings, 1 reply; 17+ messages in thread
From: Johannes Schindelin @ 2021-09-06  7:33 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Miriam Rubio, git, Tanushree Tumane, Christian Couder

Hi Junio & Miriam,

On Thu, 2 Sep 2021, Junio C Hamano wrote:

> Miriam Rubio <mirucam@gmail.com> writes:
>
> [...]
> > @@ -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;
>
> 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.

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

> > +		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?
>
> > +		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?

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 --
diff --git a/bisect.c b/bisect.c
index af2863d044b..405bf60b4b6 100644
--- a/bisect.c
+++ b/bisect.c
@@ -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)
diff --git a/bisect.h b/bisect.h
index ec24ac2d7ee..72bfd7b0053 100644
--- a/bisect.h
+++ b/bisect.h
@@ -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);

diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c
index 1c96580bd49..29969763d35 100644
--- a/builtin/bisect--helper.c
+++ b/builtin/bisect--helper.c
@@ -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)

^ permalink raw reply related	[flat|nested] 17+ messages in thread

* Re: [PATCH v6 5/6] bisect--helper: reimplement `bisect_run` shell
  2021-09-06  7:33     ` Johannes Schindelin
@ 2021-09-06  8:34       ` Miriam R.
  2021-09-07 18:32         ` Junio C Hamano
  0 siblings, 1 reply; 17+ messages in thread
From: Miriam R. @ 2021-09-06  8:34 UTC (permalink / raw)
  To: Johannes Schindelin
  Cc: Junio C Hamano, git, Tanushree Tumane, Christian Couder

Hi Johannes,

El lun, 6 sept 2021 a las 9:33, Johannes Schindelin
(<Johannes.Schindelin@gmx.de>) escribió:
>
> Hi Junio & Miriam,
>
> On Thu, 2 Sep 2021, Junio C Hamano wrote:
>
> > Miriam Rubio <mirucam@gmail.com> writes:
> >
> > [...]
> > > @@ -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;
> >
> > 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.
>
> FWIW I agree with this, after seeing the resulting code.
>
> > > +           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?
> >
> > > +           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?
>
> 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.
>
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.

> -- snipsnap --
> diff --git a/bisect.c b/bisect.c
> index af2863d044b..405bf60b4b6 100644
> --- a/bisect.c
> +++ b/bisect.c
> @@ -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)
> diff --git a/bisect.h b/bisect.h
> index ec24ac2d7ee..72bfd7b0053 100644
> --- a/bisect.h
> +++ b/bisect.h
> @@ -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);
>
> diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c
> index 1c96580bd49..29969763d35 100644
> --- a/builtin/bisect--helper.c
> +++ b/builtin/bisect--helper.c
> @@ -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)

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v6 5/6] bisect--helper: reimplement `bisect_run` shell
  2021-09-06  8:34       ` Miriam R.
@ 2021-09-07 18:32         ` Junio C Hamano
  2021-09-09  7:51           ` Johannes Schindelin
  0 siblings, 1 reply; 17+ messages in thread
From: Junio C Hamano @ 2021-09-07 18:32 UTC (permalink / raw)
  To: Miriam R.; +Cc: Johannes Schindelin, git, Tanushree Tumane, Christian Couder

"Miriam R." <mirucam@gmail.com> writes:

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

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.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH v6 5/6] bisect--helper: reimplement `bisect_run` shell
  2021-09-07 18:32         ` Junio C Hamano
@ 2021-09-09  7:51           ` Johannes Schindelin
  0 siblings, 0 replies; 17+ messages in thread
From: Johannes Schindelin @ 2021-09-09  7:51 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Miriam R., git, Tanushree Tumane, Christian Couder

Hi Junio,

On Tue, 7 Sep 2021, Junio C Hamano wrote:

> "Miriam R." <mirucam@gmail.com> writes:
>
> >> 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.
>
> 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


^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2021-09-09  7:52 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-02  9:04 [PATCH v6 0/6] Finish converting git bisect to C part 4 Miriam Rubio
2021-09-02  9:04 ` [PATCH v6 1/6] t6030-bisect-porcelain: add tests to control bisect run exit cases Miriam Rubio
2021-09-02 21:44   ` Junio C Hamano
2021-09-02  9:04 ` [PATCH v6 2/6] t6030-bisect-porcelain: add test for bisect visualize Miriam Rubio
2021-09-02 22:05   ` Junio C Hamano
2021-09-02  9:04 ` [PATCH v6 3/6] run-command: make `exists_in_PATH()` non-static Miriam Rubio
2021-09-02 22:19   ` Junio C Hamano
2021-09-02  9:04 ` [PATCH v6 4/6] bisect--helper: reimplement `bisect_visualize()`shell function in C Miriam Rubio
2021-09-02 22:28   ` Junio C Hamano
2021-09-02  9:04 ` [PATCH v6 5/6] bisect--helper: reimplement `bisect_run` shell Miriam Rubio
2021-09-02 23:43   ` Junio C Hamano
2021-09-06  7:33     ` Johannes Schindelin
2021-09-06  8:34       ` Miriam R.
2021-09-07 18:32         ` Junio C Hamano
2021-09-09  7:51           ` Johannes Schindelin
2021-09-02  9:04 ` [PATCH v6 6/6] bisect--helper: retire `--bisect-next-check` subcommand Miriam Rubio
2021-09-02 23:43   ` Junio C Hamano

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).