git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH v5 00/13] Finish converting git bisect to C part 2
@ 2020-07-17 10:53 Miriam Rubio
  2020-07-17 10:53 ` [PATCH v5 01/13] bisect--helper: BUG() in cmd_*() on invalid subcommand Miriam Rubio
                   ` (12 more replies)
  0 siblings, 13 replies; 24+ messages in thread
From: Miriam Rubio @ 2020-07-17 10:53 UTC (permalink / raw)
  To: git; +Cc: Miriam Rubio

These patches correspond to a second 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.

These patch series emails were generated from:
https://gitlab.com/mirucam/git/commits/git-bisect-work-part2-v5.

I would like to thank Chris Torek and Junio Hamano for reviewing
and Christian Couder for his help.


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

[3/12] bisect--helper: introduce new `write_in_file()` function

* Fix typo in commit message.
* Change BUG() message.
* Control 'mode' parameter in write mode only.
* Return vprintf error when result is < 0;
* Simplify return in write_terms().

--

[6/12] bisect--helper: reimplement `bisect_next` and `bisect_auto_next` shell
functions in C

*  Add 'mode' parameter control in append mode.

--

[10/12] bisect--helper: reimplement `bisect_state` & `bisect_head` shell
functions in C

* Remove mark_for_removal() function.

--


Miriam Rubio (4):
  bisect--helper: BUG() in cmd_*() on invalid subcommand
  bisect--helper: use '-res' in 'cmd_bisect__helper' return
  bisect--helper: introduce new `write_in_file()` function
  bisect: call 'clear_commit_marks_all()' in 'bisect_next_all()'

Pranit Bauva (9):
  bisect--helper: reimplement `bisect_autostart` shell function in C
  bisect--helper: reimplement `bisect_next` and `bisect_auto_next` shell
    functions in C
  bisect--helper: finish porting `bisect_start()` to C
  bisect--helper: retire `--bisect-clean-state` subcommand
  bisect--helper: retire `--next-all` subcommand
  bisect--helper: reimplement `bisect_state` & `bisect_head` shell
    functions in C
  bisect--helper: retire `--check-expected-revs` subcommand
  bisect--helper: retire `--write-terms` subcommand
  bisect--helper: retire `--bisect-autostart` subcommand

 bisect.c                 |   8 +
 builtin/bisect--helper.c | 443 ++++++++++++++++++++++++++++++++-------
 git-bisect.sh            | 145 +------------
 3 files changed, 378 insertions(+), 218 deletions(-)

-- 
2.25.0


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

* [PATCH v5 01/13] bisect--helper: BUG() in cmd_*() on invalid subcommand
  2020-07-17 10:53 [PATCH v5 00/13] Finish converting git bisect to C part 2 Miriam Rubio
@ 2020-07-17 10:53 ` Miriam Rubio
  2020-07-25 17:27   ` Shourya Shukla
  2020-07-17 10:53 ` [PATCH v5 02/13] bisect--helper: use '-res' in 'cmd_bisect__helper' return Miriam Rubio
                   ` (11 subsequent siblings)
  12 siblings, 1 reply; 24+ messages in thread
From: Miriam Rubio @ 2020-07-17 10:53 UTC (permalink / raw)
  To: git; +Cc: Miriam Rubio, Christian Couder, Johannes Schindelin

In cmd_bisect__helper() function, there would be a bug
if an invalid or no subcommand was passed.return error()
should be replaced by BUG() in that case.

Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Mentored-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Signed-off-by: Miriam Rubio <mirucam@gmail.com>
---
 builtin/bisect--helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c
index ec4996282e..c452d3f6b7 100644
--- a/builtin/bisect--helper.c
+++ b/builtin/bisect--helper.c
@@ -716,7 +716,7 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 		res = bisect_start(&terms, no_checkout, argv, argc);
 		break;
 	default:
-		return error("BUG: unknown subcommand '%d'", cmdmode);
+		BUG("unknown subcommand %d", (int)cmdmode);
 	}
 	free_terms(&terms);
 
-- 
2.25.0


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

* [PATCH v5 02/13] bisect--helper: use '-res' in 'cmd_bisect__helper' return
  2020-07-17 10:53 [PATCH v5 00/13] Finish converting git bisect to C part 2 Miriam Rubio
  2020-07-17 10:53 ` [PATCH v5 01/13] bisect--helper: BUG() in cmd_*() on invalid subcommand Miriam Rubio
@ 2020-07-17 10:53 ` Miriam Rubio
  2020-07-25 17:31   ` Shourya Shukla
  2020-07-17 10:53 ` [PATCH v5 03/13] bisect--helper: introduce new `write_in_file()` function Miriam Rubio
                   ` (10 subsequent siblings)
  12 siblings, 1 reply; 24+ messages in thread
From: Miriam Rubio @ 2020-07-17 10:53 UTC (permalink / raw)
  To: git; +Cc: Miriam Rubio, Christian Couder, Junio C Hamano

Following 'enum bisect_error' vocabulary, return variable 'res' is
always non-positive.
Let's use '-res' instead of 'abs(res)' to make the code clearer.

Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Miriam Rubio <mirucam@gmail.com>
Helped-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/bisect--helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c
index c452d3f6b7..0466b07a43 100644
--- a/builtin/bisect--helper.c
+++ b/builtin/bisect--helper.c
@@ -727,5 +727,5 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 	if (res == BISECT_INTERNAL_SUCCESS_MERGE_BASE)
 		res = BISECT_OK;
 
-	return abs(res);
+	return -res;
 }
-- 
2.25.0


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

* [PATCH v5 03/13] bisect--helper: introduce new `write_in_file()` function
  2020-07-17 10:53 [PATCH v5 00/13] Finish converting git bisect to C part 2 Miriam Rubio
  2020-07-17 10:53 ` [PATCH v5 01/13] bisect--helper: BUG() in cmd_*() on invalid subcommand Miriam Rubio
  2020-07-17 10:53 ` [PATCH v5 02/13] bisect--helper: use '-res' in 'cmd_bisect__helper' return Miriam Rubio
@ 2020-07-17 10:53 ` Miriam Rubio
  2020-07-17 16:33   ` Junio C Hamano
  2020-07-17 10:53 ` [PATCH v5 04/13] bisect--helper: reimplement `bisect_autostart` shell function in C Miriam Rubio
                   ` (9 subsequent siblings)
  12 siblings, 1 reply; 24+ messages in thread
From: Miriam Rubio @ 2020-07-17 10:53 UTC (permalink / raw)
  To: git; +Cc: Miriam Rubio, Christian Couder, Johannes Schindelin

Let's refactor code adding a new `write_in_file()` function
that opens a file for writing a message and closes it and a
wrapper for writing mode.

This helper will be used in later steps and makes the code
simpler and easier to understand.

Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Mentored-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Signed-off-by: Miriam Rubio <mirucam@gmail.com>
---
 builtin/bisect--helper.c | 41 +++++++++++++++++++++++++++++++++-------
 1 file changed, 34 insertions(+), 7 deletions(-)

diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c
index 0466b07a43..d620d84d92 100644
--- a/builtin/bisect--helper.c
+++ b/builtin/bisect--helper.c
@@ -74,6 +74,38 @@ static int one_of(const char *term, ...)
 	return res;
 }
 
+static int write_in_file(const char *path, const char *mode, const char *format, va_list args)
+{
+	FILE *fp = NULL;
+	int res = 0;
+
+	if (strcmp(mode, "w"))
+		BUG("write-in-file does not support '%s' mode", mode);
+	fp = fopen(path, mode);
+	if (!fp)
+		return error_errno(_("cannot open file '%s' in mode '%s'"), path, mode);
+	res = vfprintf(fp, format, args);
+
+	if (res < 0) {
+		fclose(fp);
+		return error_errno(_("could not write to file '%s'"), path);
+	}
+
+	return fclose(fp);
+}
+
+static int write_to_file(const char *path, const char *format, ...)
+{
+	int res;
+	va_list args;
+
+	va_start(args, format);
+	res = write_in_file(path, "w", format, args);
+	va_end(args);
+
+	return res;
+}
+
 static int check_term_format(const char *term, const char *orig_term)
 {
 	int res;
@@ -104,7 +136,6 @@ static int check_term_format(const char *term, const char *orig_term)
 
 static int write_terms(const char *bad, const char *good)
 {
-	FILE *fp = NULL;
 	int res;
 
 	if (!strcmp(bad, good))
@@ -113,13 +144,9 @@ static int write_terms(const char *bad, const char *good)
 	if (check_term_format(bad, "bad") || check_term_format(good, "good"))
 		return -1;
 
-	fp = fopen(git_path_bisect_terms(), "w");
-	if (!fp)
-		return error_errno(_("could not open the file BISECT_TERMS"));
+	res = write_to_file(git_path_bisect_terms(), "%s\n%s\n", bad, good);
 
-	res = fprintf(fp, "%s\n%s\n", bad, good);
-	res |= fclose(fp);
-	return (res < 0) ? -1 : 0;
+	return res;
 }
 
 static int is_expected_rev(const char *expected_hex)
-- 
2.25.0


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

* [PATCH v5 04/13] bisect--helper: reimplement `bisect_autostart` shell function in C
  2020-07-17 10:53 [PATCH v5 00/13] Finish converting git bisect to C part 2 Miriam Rubio
                   ` (2 preceding siblings ...)
  2020-07-17 10:53 ` [PATCH v5 03/13] bisect--helper: introduce new `write_in_file()` function Miriam Rubio
@ 2020-07-17 10:53 ` Miriam Rubio
  2020-07-26  1:53   ` Đoàn Trần Công Danh
  2020-07-17 10:53 ` [PATCH v5 05/13] bisect: call 'clear_commit_marks_all()' in 'bisect_next_all()' Miriam Rubio
                   ` (8 subsequent siblings)
  12 siblings, 1 reply; 24+ messages in thread
From: Miriam Rubio @ 2020-07-17 10:53 UTC (permalink / raw)
  To: git
  Cc: Pranit Bauva, Lars Schneider, Christian Couder,
	Johannes Schindelin, Tanushree Tumane, Miriam Rubio

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

Reimplement the `bisect_autostart()` shell function in C and add the
C implementation from `bisect_next()` which was previously left
uncovered.

Add `--bisect-autostart` subcommand to be called from git-bisect.sh.
Using `--bisect-autostart` subcommand is a temporary measure to port
the shell function to C so as to use the existing test suite. As more
functions are ported, this subcommand will be retired and
bisect_autostart() will be called directly by `bisect_state()`.

Mentored-by: Lars Schneider <larsxschneider@gmail.com>
Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Mentored-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com>
Signed-off-by: Tanushree Tumane <tanushreetumane@gmail.com>
Signed-off-by: Miriam Rubio <mirucam@gmail.com>
---
 builtin/bisect--helper.c | 44 +++++++++++++++++++++++++++++++++++++++-
 git-bisect.sh            | 25 ++---------------------
 2 files changed, 45 insertions(+), 24 deletions(-)

diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c
index d620d84d92..7662ec0ce3 100644
--- a/builtin/bisect--helper.c
+++ b/builtin/bisect--helper.c
@@ -29,6 +29,7 @@ static const char * const git_bisect_helper_usage[] = {
 	N_("git bisect--helper --bisect-terms [--term-good | --term-old | --term-bad | --term-new]"),
 	N_("git bisect--helper --bisect-start [--term-{old,good}=<term> --term-{new,bad}=<term>]"
 					     "[--no-checkout] [<bad> [<good>...]] [--] [<paths>...]"),
+	N_("git bisect--helper --bisect-autostart"),
 	NULL
 };
 
@@ -645,6 +646,38 @@ static int bisect_start(struct bisect_terms *terms, int no_checkout,
 	return res;
 }
 
+static inline int file_is_not_empty(const char *path)
+{
+	return !is_empty_or_missing_file(path);
+}
+
+static int bisect_autostart(struct bisect_terms *terms)
+{
+	int res;
+	const char *yesno;
+
+	if (file_is_not_empty(git_path_bisect_start()))
+		return 0;
+
+	fprintf_ln(stderr, _("You need to start by \"git bisect "
+			  "start\"\n"));
+
+	if (!isatty(STDIN_FILENO))
+		return 0;
+
+	/*
+	 * TRANSLATORS: Make sure to include [Y] and [n] in your
+	 * translation. The program will only accept English input
+	 * at this point.
+	 */
+	yesno = git_prompt(_("Do you want me to do it for you "
+			     "[Y/n]? "), PROMPT_ECHO);
+	res = tolower(*yesno) == 'n' ?
+		-1 : bisect_start(terms, 0, NULL, 0);
+
+		return res;
+}
+
 int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 {
 	enum {
@@ -657,7 +690,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 		CHECK_AND_SET_TERMS,
 		BISECT_NEXT_CHECK,
 		BISECT_TERMS,
-		BISECT_START
+		BISECT_START,
+		BISECT_AUTOSTART,
 	} cmdmode = 0;
 	int no_checkout = 0, res = 0, nolog = 0;
 	struct option options[] = {
@@ -681,6 +715,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 			 N_("print out the bisect terms"), BISECT_TERMS),
 		OPT_CMDMODE(0, "bisect-start", &cmdmode,
 			 N_("start the bisect session"), BISECT_START),
+		OPT_CMDMODE(0, "bisect-autostart", &cmdmode,
+			 N_("start the bisection if it has not yet been started"), BISECT_AUTOSTART),
 		OPT_BOOL(0, "no-checkout", &no_checkout,
 			 N_("update BISECT_HEAD instead of checking out the current commit")),
 		OPT_BOOL(0, "no-log", &nolog,
@@ -742,6 +778,12 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 		set_terms(&terms, "bad", "good");
 		res = bisect_start(&terms, no_checkout, argv, argc);
 		break;
+	case BISECT_AUTOSTART:
+		if (argc)
+			return error(_("--bisect-autostart does not accept arguments"));
+		set_terms(&terms, "bad", "good");
+		res = bisect_autostart(&terms);
+		break;
 	default:
 		BUG("unknown subcommand %d", (int)cmdmode);
 	}
diff --git a/git-bisect.sh b/git-bisect.sh
index 08a6ed57dd..d7a6990c29 100755
--- a/git-bisect.sh
+++ b/git-bisect.sh
@@ -49,27 +49,6 @@ bisect_head()
 	fi
 }
 
-bisect_autostart() {
-	test -s "$GIT_DIR/BISECT_START" || {
-		gettextln "You need to start by \"git bisect start\"" >&2
-		if test -t 0
-		then
-			# TRANSLATORS: Make sure to include [Y] and [n] in your
-			# translation. The program will only accept English input
-			# at this point.
-			gettext "Do you want me to do it for you [Y/n]? " >&2
-			read yesno
-			case "$yesno" in
-			[Nn]*)
-				exit ;;
-			esac
-			bisect_start
-		else
-			exit 1
-		fi
-	}
-}
-
 bisect_start() {
 	git bisect--helper --bisect-start $@ || exit
 
@@ -108,7 +87,7 @@ bisect_skip() {
 }
 
 bisect_state() {
-	bisect_autostart
+	git bisect--helper --bisect-autostart
 	state=$1
 	git bisect--helper --check-and-set-terms $state $TERM_GOOD $TERM_BAD || exit
 	get_terms
@@ -149,7 +128,7 @@ bisect_auto_next() {
 
 bisect_next() {
 	case "$#" in 0) ;; *) usage ;; esac
-	bisect_autostart
+	git bisect--helper --bisect-autostart
 	git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD $TERM_GOOD|| exit
 
 	# Perform all bisection computation, display and checkout
-- 
2.25.0


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

* [PATCH v5 05/13] bisect: call 'clear_commit_marks_all()' in 'bisect_next_all()'
  2020-07-17 10:53 [PATCH v5 00/13] Finish converting git bisect to C part 2 Miriam Rubio
                   ` (3 preceding siblings ...)
  2020-07-17 10:53 ` [PATCH v5 04/13] bisect--helper: reimplement `bisect_autostart` shell function in C Miriam Rubio
@ 2020-07-17 10:53 ` Miriam Rubio
  2020-07-17 10:53 ` [PATCH v5 06/13] bisect--helper: reimplement `bisect_next` and `bisect_auto_next` shell functions in C Miriam Rubio
                   ` (7 subsequent siblings)
  12 siblings, 0 replies; 24+ messages in thread
From: Miriam Rubio @ 2020-07-17 10:53 UTC (permalink / raw)
  To: git; +Cc: Miriam Rubio, Christian Couder

As there can be other revision walks after bisect_next_all(),
let's add a call to a function to clear all the marks at the
end of bisect_next_all().

Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Miriam Rubio <mirucam@gmail.com>
---
 bisect.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/bisect.c b/bisect.c
index d5e830410f..db00850065 100644
--- a/bisect.c
+++ b/bisect.c
@@ -1064,6 +1064,8 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix, int
 		  "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. */
+	clear_commit_marks_all(ALL_REV_FLAGS);
 
 	return bisect_checkout(bisect_rev, no_checkout);
 }
-- 
2.25.0


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

* [PATCH v5 06/13] bisect--helper: reimplement `bisect_next` and `bisect_auto_next` shell functions in C
  2020-07-17 10:53 [PATCH v5 00/13] Finish converting git bisect to C part 2 Miriam Rubio
                   ` (4 preceding siblings ...)
  2020-07-17 10:53 ` [PATCH v5 05/13] bisect: call 'clear_commit_marks_all()' in 'bisect_next_all()' Miriam Rubio
@ 2020-07-17 10:53 ` Miriam Rubio
  2020-07-17 16:54   ` Junio C Hamano
  2020-07-17 10:54 ` [PATCH v5 07/13] bisect--helper: finish porting `bisect_start()` to C Miriam Rubio
                   ` (6 subsequent siblings)
  12 siblings, 1 reply; 24+ messages in thread
From: Miriam Rubio @ 2020-07-17 10:53 UTC (permalink / raw)
  To: git
  Cc: Pranit Bauva, Lars Schneider, Christian Couder,
	Johannes Schindelin, Tanushree Tumane, Miriam Rubio

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

Reimplement the `bisect_next()` and the `bisect_auto_next()` shell functions
in C and add the subcommands to `git bisect--helper` to call them from
git-bisect.sh .

bisect_auto_next() function returns an enum bisect_error type as whole
`git bisect` can exit with an error code when bisect_next() does.

Using `--bisect-next` and `--bisect-auto-next` subcommands is a
temporary measure to port shell function to C so as to use the existing
test suite. As more functions are ported, `--bisect-auto-next`
subcommand will be retired and will be called by some other methods.

Mentored-by: Lars Schneider <larsxschneider@gmail.com>
Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Mentored-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com>
Signed-off-by: Tanushree Tumane <tanushreetumane@gmail.com>
Signed-off-by: Miriam Rubio <mirucam@gmail.com>
---
 bisect.c                 |   6 ++
 builtin/bisect--helper.c | 183 ++++++++++++++++++++++++++++++++++++++-
 git-bisect.sh            |  47 +---------
 3 files changed, 190 insertions(+), 46 deletions(-)

diff --git a/bisect.c b/bisect.c
index db00850065..0864e2a21b 100644
--- a/bisect.c
+++ b/bisect.c
@@ -980,6 +980,12 @@ void read_bisect_terms(const char **read_bad, const char **read_good)
  * the bisection process finished successfully.
  * In this case the calling function or command should not turn a
  * BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND return code into an error or a non zero exit code.
+ *
+ * Checking BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND
+ * in bisect_helper::bisect_next() and only transforming it to 0 at
+ * the end of bisect_helper::cmd_bisect__helper() helps bypassing
+ * all the code related to finding a commit to test.
+ *
  * If no_checkout is non-zero, the bisection process does not
  * checkout the trial commit but instead simply updates BISECT_HEAD.
  */
diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c
index 7662ec0ce3..4223af144a 100644
--- a/builtin/bisect--helper.c
+++ b/builtin/bisect--helper.c
@@ -8,6 +8,7 @@
 #include "run-command.h"
 #include "prompt.h"
 #include "quote.h"
+#include "revision.h"
 
 static GIT_PATH_FUNC(git_path_bisect_terms, "BISECT_TERMS")
 static GIT_PATH_FUNC(git_path_bisect_expected_rev, "BISECT_EXPECTED_REV")
@@ -29,10 +30,17 @@ static const char * const git_bisect_helper_usage[] = {
 	N_("git bisect--helper --bisect-terms [--term-good | --term-old | --term-bad | --term-new]"),
 	N_("git bisect--helper --bisect-start [--term-{old,good}=<term> --term-{new,bad}=<term>]"
 					     "[--no-checkout] [<bad> [<good>...]] [--] [<paths>...]"),
+	N_("git bisect--helper --bisect-next"),
+	N_("git bisect--helper --bisect-auto-next"),
 	N_("git bisect--helper --bisect-autostart"),
 	NULL
 };
 
+struct add_bisect_ref_data {
+	struct rev_info *revs;
+	unsigned int object_flags;
+};
+
 struct bisect_terms {
 	char *term_good;
 	char *term_bad;
@@ -56,6 +64,8 @@ static void set_terms(struct bisect_terms *terms, const char *bad,
 static const char vocab_bad[] = "bad|new";
 static const char vocab_good[] = "good|old";
 
+static int bisect_autostart(struct bisect_terms *terms);
+
 /*
  * Check whether the string `term` belongs to the set of strings
  * included in the variable arguments.
@@ -80,7 +90,7 @@ static int write_in_file(const char *path, const char *mode, const char *format,
 	FILE *fp = NULL;
 	int res = 0;
 
-	if (strcmp(mode, "w"))
+	if (strcmp(mode, "w") && strcmp(mode, "a"))
 		BUG("write-in-file does not support '%s' mode", mode);
 	fp = fopen(path, mode);
 	if (!fp)
@@ -107,6 +117,18 @@ static int write_to_file(const char *path, const char *format, ...)
 	return res;
 }
 
+static int append_to_file(const char *path, const char *format, ...)
+{
+	int res;
+	va_list args;
+
+	va_start(args, format);
+	res = write_in_file(path, "a", format, args);
+	va_end(args);
+
+	return res;
+}
+
 static int check_term_format(const char *term, const char *orig_term)
 {
 	int res;
@@ -449,6 +471,143 @@ static int bisect_append_log_quoted(const char **argv)
 	return res;
 }
 
+static int add_bisect_ref(const char *refname, const struct object_id *oid,
+			  int flags, void *cb)
+{
+	struct add_bisect_ref_data *data = cb;
+
+	add_pending_oid(data->revs, refname, oid, data->object_flags);
+
+	return 0;
+}
+
+static int prepare_revs(struct bisect_terms *terms, struct rev_info *revs)
+{
+	int res = 0;
+	struct add_bisect_ref_data cb = { revs };
+	char *good = xstrfmt("%s-*", terms->term_good);
+
+	/*
+	 * We cannot use terms->term_bad directly in
+	 * for_each_glob_ref_in() and we have to append a '*' to it,
+	 * otherwise for_each_glob_ref_in() will append '/' and '*'.
+	 */
+	char *bad = xstrfmt("%s*", terms->term_bad);
+
+	/*
+	 * It is important to reset the flags used by revision walks
+	 * as the previous call to bisect_next_all() in turn
+	 * sets up a revision walk.
+	 */
+	reset_revision_walk();
+	init_revisions(revs, NULL);
+	setup_revisions(0, NULL, revs, NULL);
+	for_each_glob_ref_in(add_bisect_ref, bad, "refs/bisect/", &cb);
+	cb.object_flags = UNINTERESTING;
+	for_each_glob_ref_in(add_bisect_ref, good, "refs/bisect/", &cb);
+	if (prepare_revision_walk(revs))
+		res = error(_("revision walk setup failed\n"));
+
+	free(good);
+	free(bad);
+	return res;
+}
+
+static int bisect_skipped_commits(struct bisect_terms *terms)
+{
+	int res;
+	FILE *fp = NULL;
+	struct rev_info revs;
+	struct commit *commit;
+	struct pretty_print_context pp = {0};
+	struct strbuf commit_name = STRBUF_INIT;
+
+	res = prepare_revs(terms, &revs);
+	if (res)
+		return res;
+
+	fp = fopen(git_path_bisect_log(), "a");
+	if (!fp)
+		return error_errno(_("could not open '%s' for appending"),
+				  git_path_bisect_log());
+
+	if (fprintf(fp, "# only skipped commits left to test\n") < 0)
+		return error_errno(_("failed to write to '%s'"), git_path_bisect_log());
+
+	while ((commit = get_revision(&revs)) != NULL) {
+		strbuf_reset(&commit_name);
+		format_commit_message(commit, "%s",
+				      &commit_name, &pp);
+		fprintf(fp, "# possible first %s commit: [%s] %s\n",
+			terms->term_bad, oid_to_hex(&commit->object.oid),
+			commit_name.buf);
+	}
+
+	/*
+	 * Reset the flags used by revision walks in case
+	 * there is another revision walk after this one.
+	 */
+	reset_revision_walk();
+
+	strbuf_release(&commit_name);
+	fclose(fp);
+	return 0;
+}
+
+static int bisect_successful(struct bisect_terms *terms)
+{
+	struct object_id oid;
+	struct commit *commit;
+	struct pretty_print_context pp = {0};
+	struct strbuf commit_name = STRBUF_INIT;
+	char *bad_ref = xstrfmt("refs/bisect/%s",terms->term_bad);
+	int res;
+
+	read_ref(bad_ref, &oid);
+	commit = lookup_commit_reference_by_name(bad_ref);
+	format_commit_message(commit, "%s", &commit_name, &pp);
+
+	res = append_to_file(git_path_bisect_log(), "# first %s commit: [%s] %s\n",
+			    terms->term_bad, oid_to_hex(&commit->object.oid),
+			    commit_name.buf);
+
+	strbuf_release(&commit_name);
+	free(bad_ref);
+	return res;
+}
+
+static enum bisect_error bisect_next(struct bisect_terms *terms, const char *prefix)
+{
+	int no_checkout;
+	enum bisect_error res;
+
+	bisect_autostart(terms);
+	if (bisect_next_check(terms, terms->term_good))
+		return BISECT_FAILED;
+
+	no_checkout = file_exists(git_path_bisect_head());
+
+	/* Perform all bisection computation, display and checkout */
+	res = bisect_next_all(the_repository, prefix, no_checkout);
+
+	if (res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND) {
+		res = bisect_successful(terms);
+		return res ? res : BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND;
+	} else if (res == BISECT_ONLY_SKIPPED_LEFT) {
+		res = bisect_skipped_commits(terms);
+		return res ? res : BISECT_ONLY_SKIPPED_LEFT;
+	}
+	return res;
+}
+
+static enum bisect_error bisect_auto_next(struct bisect_terms *terms, const char *prefix)
+{
+	if (bisect_next_check(terms, NULL))
+		return BISECT_OK;
+
+	return bisect_next(terms, prefix);
+}
+
 static int bisect_start(struct bisect_terms *terms, int no_checkout,
 			const char **argv, int argc)
 {
@@ -691,7 +850,9 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 		BISECT_NEXT_CHECK,
 		BISECT_TERMS,
 		BISECT_START,
-		BISECT_AUTOSTART,
+		BISECT_NEXT,
+		BISECT_AUTO_NEXT,
+		BISECT_AUTOSTART
 	} cmdmode = 0;
 	int no_checkout = 0, res = 0, nolog = 0;
 	struct option options[] = {
@@ -715,6 +876,10 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 			 N_("print out the bisect terms"), BISECT_TERMS),
 		OPT_CMDMODE(0, "bisect-start", &cmdmode,
 			 N_("start the bisect session"), BISECT_START),
+		OPT_CMDMODE(0, "bisect-next", &cmdmode,
+			 N_("find the next bisection commit"), BISECT_NEXT),
+		OPT_CMDMODE(0, "bisect-auto-next", &cmdmode,
+			 N_("verify the next bisection state then checkout the next bisection commit"), BISECT_AUTO_NEXT),
 		OPT_CMDMODE(0, "bisect-autostart", &cmdmode,
 			 N_("start the bisection if it has not yet been started"), BISECT_AUTOSTART),
 		OPT_BOOL(0, "no-checkout", &no_checkout,
@@ -778,6 +943,18 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 		set_terms(&terms, "bad", "good");
 		res = bisect_start(&terms, no_checkout, argv, argc);
 		break;
+	case BISECT_NEXT:
+		if (argc)
+			return error(_("--bisect-next requires 0 arguments"));
+		get_terms(&terms);
+		res = bisect_next(&terms, prefix);
+		break;
+	case BISECT_AUTO_NEXT:
+		if (argc)
+			return error(_("--bisect-auto-next requires 0 arguments"));
+		get_terms(&terms);
+		res = bisect_auto_next(&terms, prefix);
+		break;
 	case BISECT_AUTOSTART:
 		if (argc)
 			return error(_("--bisect-autostart does not accept arguments"));
@@ -793,7 +970,7 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 	 * Handle early success
 	 * From check_merge_bases > check_good_are_ancestors_of_bad > bisect_next_all
 	 */
-	if (res == BISECT_INTERNAL_SUCCESS_MERGE_BASE)
+	if ((res == BISECT_INTERNAL_SUCCESS_MERGE_BASE) || (res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND))
 		res = BISECT_OK;
 
 	return -res;
diff --git a/git-bisect.sh b/git-bisect.sh
index d7a6990c29..809be25424 100755
--- a/git-bisect.sh
+++ b/git-bisect.sh
@@ -65,8 +65,7 @@ bisect_start() {
 	#
 	# Check if we can proceed to the next bisect state.
 	#
-	get_terms
-	bisect_auto_next
+	git bisect--helper --bisect-auto-next || exit
 
 	trap '-' 0
 }
@@ -119,45 +118,7 @@ bisect_state() {
 	*)
 		usage ;;
 	esac
-	bisect_auto_next
-}
-
-bisect_auto_next() {
-	git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD && bisect_next || :
-}
-
-bisect_next() {
-	case "$#" in 0) ;; *) usage ;; esac
-	git bisect--helper --bisect-autostart
-	git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD $TERM_GOOD|| exit
-
-	# Perform all bisection computation, display and checkout
-	git bisect--helper --next-all $(test -f "$GIT_DIR/BISECT_HEAD" && echo --no-checkout)
-	res=$?
-
-	# Check if we should exit because bisection is finished
-	if test $res -eq 10
-	then
-		bad_rev=$(git show-ref --hash --verify refs/bisect/$TERM_BAD)
-		bad_commit=$(git show-branch $bad_rev)
-		echo "# first $TERM_BAD commit: $bad_commit" >>"$GIT_DIR/BISECT_LOG"
-		exit 0
-	elif test $res -eq 2
-	then
-		echo "# only skipped commits left to test" >>"$GIT_DIR/BISECT_LOG"
-		good_revs=$(git for-each-ref --format="%(objectname)" "refs/bisect/$TERM_GOOD-*")
-		for skipped in $(git rev-list refs/bisect/$TERM_BAD --not $good_revs)
-		do
-			skipped_commit=$(git show-branch $skipped)
-			echo "# possible first $TERM_BAD commit: $skipped_commit" >>"$GIT_DIR/BISECT_LOG"
-		done
-		exit $res
-	fi
-
-	# Check for an error in the bisection process
-	test $res -ne 0 && exit $res
-
-	return 0
+	git bisect--helper --bisect-auto-next
 }
 
 bisect_visualize() {
@@ -213,7 +174,7 @@ bisect_replay () {
 		esac
 	done <"$file"
 	IFS="$oIFS"
-	bisect_auto_next
+	git bisect--helper --bisect-auto-next
 }
 
 bisect_run () {
@@ -310,7 +271,7 @@ case "$#" in
 		bisect_skip "$@" ;;
 	next)
 		# Not sure we want "next" at the UI level anymore.
-		bisect_next "$@" ;;
+		git bisect--helper --bisect-next "$@" || exit ;;
 	visualize|view)
 		bisect_visualize "$@" ;;
 	reset)
-- 
2.25.0


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

* [PATCH v5 07/13] bisect--helper: finish porting `bisect_start()` to C
  2020-07-17 10:53 [PATCH v5 00/13] Finish converting git bisect to C part 2 Miriam Rubio
                   ` (5 preceding siblings ...)
  2020-07-17 10:53 ` [PATCH v5 06/13] bisect--helper: reimplement `bisect_next` and `bisect_auto_next` shell functions in C Miriam Rubio
@ 2020-07-17 10:54 ` Miriam Rubio
  2020-07-17 10:54 ` [PATCH v5 08/13] bisect--helper: retire `--bisect-clean-state` subcommand Miriam Rubio
                   ` (5 subsequent siblings)
  12 siblings, 0 replies; 24+ messages in thread
From: Miriam Rubio @ 2020-07-17 10:54 UTC (permalink / raw)
  To: git
  Cc: Pranit Bauva, Christian Couder, Johannes Schindelin,
	Tanushree Tumane, Miriam Rubio

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

Add the subcommand to `git bisect--helper` and call it from
git-bisect.sh.

With the conversion of `bisect_auto_next()` from shell to C in a
previous commit, `bisect_start()` can now be fully ported to C.

So let's complete the `--bisect-start` subcommand of
`git bisect--helper` so that it fully implements `bisect_start()`,
and let's use this subcommand in `git-bisect.sh` instead of
`bisect_start()`.

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

diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c
index 4223af144a..2b7bad558a 100644
--- a/builtin/bisect--helper.c
+++ b/builtin/bisect--helper.c
@@ -85,6 +85,19 @@ static int one_of(const char *term, ...)
 	return res;
 }
 
+/*
+ * return code BISECT_INTERNAL_SUCCESS_MERGE_BASE
+ * and BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND are codes
+ * that indicate special success.
+ */
+
+static int is_bisect_success(enum bisect_error res)
+{
+	return !res ||
+		res == BISECT_INTERNAL_SUCCESS_1ST_BAD_FOUND ||
+		res == BISECT_INTERNAL_SUCCESS_MERGE_BASE;
+}
+
 static int write_in_file(const char *path, const char *mode, const char *format, va_list args)
 {
 	FILE *fp = NULL;
@@ -608,11 +621,12 @@ static enum bisect_error bisect_auto_next(struct bisect_terms *terms, const char
 	return bisect_next(terms, prefix);
 }
 
-static int bisect_start(struct bisect_terms *terms, int no_checkout,
+static enum bisect_error bisect_start(struct bisect_terms *terms, int no_checkout,
 			const char **argv, int argc)
 {
 	int i, has_double_dash = 0, must_write_terms = 0, bad_seen = 0;
-	int flags, pathspec_pos, res = 0;
+	int flags, pathspec_pos;
+	enum bisect_error res = BISECT_OK;
 	struct string_list revs = STRING_LIST_INIT_DUP;
 	struct string_list states = STRING_LIST_INIT_DUP;
 	struct strbuf start_head = STRBUF_INIT;
@@ -670,9 +684,12 @@ static int bisect_start(struct bisect_terms *terms, int no_checkout,
 			return error(_("unrecognized option: '%s'"), arg);
 		} else {
 			char *commit_id = xstrfmt("%s^{commit}", arg);
-			if (get_oid(commit_id, &oid) && has_double_dash)
-				die(_("'%s' does not appear to be a valid "
-				      "revision"), arg);
+			if (get_oid(commit_id, &oid) && has_double_dash) {
+				error(_("'%s' does not appear to be a valid "
+					"revision"), arg);
+				free(commit_id);
+				return BISECT_FAILED;
+			}
 
 			string_list_append(&revs, oid_to_hex(&oid));
 			free(commit_id);
@@ -750,14 +767,7 @@ static int bisect_start(struct bisect_terms *terms, int no_checkout,
 	 * Get rid of any old bisect state.
 	 */
 	if (bisect_clean_state())
-		return -1;
-
-	/*
-	 * In case of mistaken revs or checkout error, or signals received,
-	 * "bisect_auto_next" below may exit or misbehave.
-	 * We have to trap this to be able to clean up using
-	 * "bisect_clean_state".
-	 */
+		return BISECT_FAILED;
 
 	/*
 	 * Write new start state
@@ -771,7 +781,7 @@ static int bisect_start(struct bisect_terms *terms, int no_checkout,
 		}
 		if (update_ref(NULL, "BISECT_HEAD", &oid, NULL, 0,
 			       UPDATE_REFS_MSG_ON_ERR)) {
-			res = -1;
+			res = BISECT_FAILED;
 			goto finish;
 		}
 	}
@@ -783,25 +793,37 @@ static int bisect_start(struct bisect_terms *terms, int no_checkout,
 	for (i = 0; i < states.nr; i++)
 		if (bisect_write(states.items[i].string,
 				 revs.items[i].string, terms, 1)) {
-			res = -1;
+			res = BISECT_FAILED;
 			goto finish;
 		}
 
 	if (must_write_terms && write_terms(terms->term_bad,
 					    terms->term_good)) {
-		res = -1;
+		res = BISECT_FAILED;
 		goto finish;
 	}
 
 	res = bisect_append_log_quoted(argv);
 	if (res)
-		res = -1;
+		res = BISECT_FAILED;
 
 finish:
 	string_list_clear(&revs, 0);
 	string_list_clear(&states, 0);
 	strbuf_release(&start_head);
 	strbuf_release(&bisect_names);
+	if (res)
+		return res;
+
+	res = bisect_auto_next(terms, NULL);
+	/*
+	 * In case of mistaken revs or checkout error,
+	 * "bisect_auto_next" above may exit or misbehave.
+	 * We have to handle this to be able to clean up using
+	 * "bisect_clean_state".
+	 */
+	if (!is_bisect_success(res))
+		bisect_clean_state();
 	return res;
 }
 
diff --git a/git-bisect.sh b/git-bisect.sh
index 809be25424..cfb05c0511 100755
--- a/git-bisect.sh
+++ b/git-bisect.sh
@@ -49,27 +49,6 @@ bisect_head()
 	fi
 }
 
-bisect_start() {
-	git bisect--helper --bisect-start $@ || exit
-
-	#
-	# Change state.
-	# In case of mistaken revs or checkout error, or signals received,
-	# "bisect_auto_next" below may exit or misbehave.
-	# We have to trap this to be able to clean up using
-	# "bisect_clean_state".
-	#
-	trap 'git bisect--helper --bisect-clean-state' 0
-	trap 'exit 255' 1 2 3 15
-
-	#
-	# Check if we can proceed to the next bisect state.
-	#
-	git bisect--helper --bisect-auto-next || exit
-
-	trap '-' 0
-}
-
 bisect_skip() {
 	all=''
 	for arg in "$@"
@@ -163,8 +142,7 @@ bisect_replay () {
 		get_terms
 		case "$command" in
 		start)
-			cmd="bisect_start $rev $tail"
-			eval "$cmd" ;;
+			eval "git bisect--helper --bisect-start $rev $tail" ;;
 		"$TERM_GOOD"|"$TERM_BAD"|skip)
 			git bisect--helper --bisect-write "$command" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit;;
 		terms)
@@ -264,7 +242,7 @@ case "$#" in
 	help)
 		git bisect -h ;;
 	start)
-		bisect_start "$@" ;;
+		git bisect--helper --bisect-start "$@" ;;
 	bad|good|new|old|"$TERM_BAD"|"$TERM_GOOD")
 		bisect_state "$cmd" "$@" ;;
 	skip)
-- 
2.25.0


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

* [PATCH v5 08/13] bisect--helper: retire `--bisect-clean-state` subcommand
  2020-07-17 10:53 [PATCH v5 00/13] Finish converting git bisect to C part 2 Miriam Rubio
                   ` (6 preceding siblings ...)
  2020-07-17 10:54 ` [PATCH v5 07/13] bisect--helper: finish porting `bisect_start()` to C Miriam Rubio
@ 2020-07-17 10:54 ` Miriam Rubio
  2020-07-17 10:54 ` [PATCH v5 09/13] bisect--helper: retire `--next-all` subcommand Miriam Rubio
                   ` (4 subsequent siblings)
  12 siblings, 0 replies; 24+ messages in thread
From: Miriam Rubio @ 2020-07-17 10:54 UTC (permalink / raw)
  To: git
  Cc: Pranit Bauva, Lars Schneider, Christian Couder, Tanushree Tumane,
	Miriam Rubio

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

The `--bisect-clean-state` subcommand is no longer used from the
git-bisect.sh shell script. Instead the function
`bisect_clean_state()` is directly called from the C
implementation.

Mentored-by: Lars Schneider <larsxschneider@gmail.com>
Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com>
Signed-off-by: Tanushree Tumane <tanushreetumane@gmail.com>
Signed-off-by: Miriam Rubio <mirucam@gmail.com>
---
 builtin/bisect--helper.c | 8 --------
 1 file changed, 8 deletions(-)

diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c
index 2b7bad558a..a8742dc37d 100644
--- a/builtin/bisect--helper.c
+++ b/builtin/bisect--helper.c
@@ -22,7 +22,6 @@ static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES")
 static const char * const git_bisect_helper_usage[] = {
 	N_("git bisect--helper --next-all [--no-checkout]"),
 	N_("git bisect--helper --write-terms <bad_term> <good_term>"),
-	N_("git bisect--helper --bisect-clean-state"),
 	N_("git bisect--helper --bisect-reset [<commit>]"),
 	N_("git bisect--helper --bisect-write [--no-log] <state> <revision> <good_term> <bad_term>"),
 	N_("git bisect--helper --bisect-check-and-set-terms <command> <good_term> <bad_term>"),
@@ -864,7 +863,6 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 	enum {
 		NEXT_ALL = 1,
 		WRITE_TERMS,
-		BISECT_CLEAN_STATE,
 		CHECK_EXPECTED_REVS,
 		BISECT_RESET,
 		BISECT_WRITE,
@@ -882,8 +880,6 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 			 N_("perform 'git bisect next'"), NEXT_ALL),
 		OPT_CMDMODE(0, "write-terms", &cmdmode,
 			 N_("write the terms to .git/BISECT_TERMS"), WRITE_TERMS),
-		OPT_CMDMODE(0, "bisect-clean-state", &cmdmode,
-			 N_("cleanup the bisection state"), BISECT_CLEAN_STATE),
 		OPT_CMDMODE(0, "check-expected-revs", &cmdmode,
 			 N_("check for expected revs"), CHECK_EXPECTED_REVS),
 		OPT_CMDMODE(0, "bisect-reset", &cmdmode,
@@ -927,10 +923,6 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 		if (argc != 2)
 			return error(_("--write-terms requires two arguments"));
 		return write_terms(argv[0], argv[1]);
-	case BISECT_CLEAN_STATE:
-		if (argc != 0)
-			return error(_("--bisect-clean-state requires no arguments"));
-		return bisect_clean_state();
 	case CHECK_EXPECTED_REVS:
 		check_expected_revs(argv, argc);
 		return 0;
-- 
2.25.0


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

* [PATCH v5 09/13] bisect--helper: retire `--next-all` subcommand
  2020-07-17 10:53 [PATCH v5 00/13] Finish converting git bisect to C part 2 Miriam Rubio
                   ` (7 preceding siblings ...)
  2020-07-17 10:54 ` [PATCH v5 08/13] bisect--helper: retire `--bisect-clean-state` subcommand Miriam Rubio
@ 2020-07-17 10:54 ` Miriam Rubio
  2020-07-17 10:54 ` [PATCH v5 10/13] bisect--helper: reimplement `bisect_state` & `bisect_head` shell functions in C Miriam Rubio
                   ` (3 subsequent siblings)
  12 siblings, 0 replies; 24+ messages in thread
From: Miriam Rubio @ 2020-07-17 10:54 UTC (permalink / raw)
  To: git
  Cc: Pranit Bauva, Lars Schneider, Christian Couder, Tanushree Tumane,
	Miriam Rubio

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

The `--next-all` subcommand is no longer used from the git-bisect.sh
shell script. Instead the function `bisect_next_all()` is called from
the C implementation of `bisect_next()`.

Mentored-by: Lars Schneider <larsxschneider@gmail.com>
Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com>
Signed-off-by: Tanushree Tumane <tanushreetumane@gmail.com>
Signed-off-by: Miriam Rubio <mirucam@gmail.com>
---
 builtin/bisect--helper.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)

diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c
index a8742dc37d..fc6bf36e42 100644
--- a/builtin/bisect--helper.c
+++ b/builtin/bisect--helper.c
@@ -20,7 +20,6 @@ static GIT_PATH_FUNC(git_path_head_name, "head-name")
 static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES")
 
 static const char * const git_bisect_helper_usage[] = {
-	N_("git bisect--helper --next-all [--no-checkout]"),
 	N_("git bisect--helper --write-terms <bad_term> <good_term>"),
 	N_("git bisect--helper --bisect-reset [<commit>]"),
 	N_("git bisect--helper --bisect-write [--no-log] <state> <revision> <good_term> <bad_term>"),
@@ -861,8 +860,7 @@ static int bisect_autostart(struct bisect_terms *terms)
 int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 {
 	enum {
-		NEXT_ALL = 1,
-		WRITE_TERMS,
+		WRITE_TERMS = 1,
 		CHECK_EXPECTED_REVS,
 		BISECT_RESET,
 		BISECT_WRITE,
@@ -876,8 +874,6 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 	} cmdmode = 0;
 	int no_checkout = 0, res = 0, nolog = 0;
 	struct option options[] = {
-		OPT_CMDMODE(0, "next-all", &cmdmode,
-			 N_("perform 'git bisect next'"), NEXT_ALL),
 		OPT_CMDMODE(0, "write-terms", &cmdmode,
 			 N_("write the terms to .git/BISECT_TERMS"), WRITE_TERMS),
 		OPT_CMDMODE(0, "check-expected-revs", &cmdmode,
@@ -916,9 +912,6 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 		usage_with_options(git_bisect_helper_usage, options);
 
 	switch (cmdmode) {
-	case NEXT_ALL:
-		res = bisect_next_all(the_repository, prefix, no_checkout);
-		break;
 	case WRITE_TERMS:
 		if (argc != 2)
 			return error(_("--write-terms requires two arguments"));
-- 
2.25.0


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

* [PATCH v5 10/13] bisect--helper: reimplement `bisect_state` & `bisect_head` shell functions in C
  2020-07-17 10:53 [PATCH v5 00/13] Finish converting git bisect to C part 2 Miriam Rubio
                   ` (8 preceding siblings ...)
  2020-07-17 10:54 ` [PATCH v5 09/13] bisect--helper: retire `--next-all` subcommand Miriam Rubio
@ 2020-07-17 10:54 ` Miriam Rubio
  2020-07-17 10:54 ` [PATCH v5 11/13] bisect--helper: retire `--check-expected-revs` subcommand Miriam Rubio
                   ` (2 subsequent siblings)
  12 siblings, 0 replies; 24+ messages in thread
From: Miriam Rubio @ 2020-07-17 10:54 UTC (permalink / raw)
  To: git
  Cc: Pranit Bauva, Lars Schneider, Christian Couder,
	Johannes Schindelin, Tanushree Tumane, Miriam Rubio

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

Reimplement the `bisect_state()` shell functions in C and also add a
subcommand `--bisect-state` to `git-bisect--helper` to call them from
git-bisect.sh .

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

`bisect_head()` is only called from `bisect_state()`, thus it is not
required to introduce another subcommand.

Mentored-by: Lars Schneider <larsxschneider@gmail.com>
Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Mentored-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com>
Signed-off-by: Tanushree Tumane <tanushreetumane@gmail.com>
Signed-off-by: Miriam Rubio <mirucam@gmail.com>
---
 builtin/bisect--helper.c | 78 +++++++++++++++++++++++++++++++++++++++-
 git-bisect.sh            | 55 +++-------------------------
 2 files changed, 81 insertions(+), 52 deletions(-)

diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c
index fc6bf36e42..7369605e4c 100644
--- a/builtin/bisect--helper.c
+++ b/builtin/bisect--helper.c
@@ -31,6 +31,8 @@ static const char * const git_bisect_helper_usage[] = {
 	N_("git bisect--helper --bisect-next"),
 	N_("git bisect--helper --bisect-auto-next"),
 	N_("git bisect--helper --bisect-autostart"),
+	N_("git bisect--helper --bisect-state (bad|new) [<rev>]"),
+	N_("git bisect--helper --bisect-state (good|old) [<rev>...]"),
 	NULL
 };
 
@@ -857,6 +859,72 @@ static int bisect_autostart(struct bisect_terms *terms)
 		return res;
 }
 
+static enum bisect_error bisect_state(struct bisect_terms *terms, const char **argv,
+				      int argc)
+{
+	const char *state;
+	int i, verify_expected = 1;
+	struct object_id oid, expected;
+	struct strbuf buf = STRBUF_INIT;
+	struct oid_array revs = OID_ARRAY_INIT;
+
+	if (!argc)
+		return error(_("Please call `--bisect-state` with at least one argument"));
+
+	state = argv[0];
+	if (check_and_set_terms(terms, state) ||
+	    !one_of(state, terms->term_good, terms->term_bad, "skip", NULL))
+		return BISECT_FAILED;
+
+	argv++;
+	argc--;
+	if (argc > 1 && !strcmp(state, terms->term_bad))
+		return error(_("'git bisect %s' can take only one argument."), terms->term_bad);
+
+	if (argc == 0) {
+		enum get_oid_result res_head = get_oid("BISECT_HEAD", &oid);
+		if (res_head == MISSING_OBJECT)
+			res_head = get_oid("HEAD", &oid);
+		if (res_head) {
+			error(_("Bad bisect_head rev input"));
+			return BISECT_FAILED;
+		}
+		oid_array_append(&revs, &oid);
+	}
+
+	/*
+	 * All input revs must be checked before executing bisect_write()
+	 * to discard junk revs.
+	 */
+
+	for (; argc; argc--, argv++) {
+		if (get_oid(*argv, &oid)){
+			error(_("Bad rev input: %s"), *argv);
+			return BISECT_FAILED;
+		}
+		oid_array_append(&revs, &oid);
+	}
+
+	if (strbuf_read_file(&buf, git_path_bisect_expected_rev(), 0) < the_hash_algo->hexsz ||
+	    get_oid_hex(buf.buf, &expected) < 0)
+		verify_expected = 0; /* Ignore invalid file contents */
+	strbuf_release(&buf);
+
+	for (i = 0; i < revs.nr; i++) {
+		if (bisect_write(state, oid_to_hex(&revs.oid[i]), terms, 0))
+			return BISECT_FAILED;
+
+		if (verify_expected && !oideq(&revs.oid[i], &expected)) {
+			unlink_or_warn(git_path_bisect_ancestors_ok());
+			unlink_or_warn(git_path_bisect_expected_rev());
+			verify_expected = 0;
+		}
+	}
+
+	oid_array_clear(&revs);
+	return bisect_auto_next(terms, NULL);
+}
+
 int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 {
 	enum {
@@ -870,7 +938,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 		BISECT_START,
 		BISECT_NEXT,
 		BISECT_AUTO_NEXT,
-		BISECT_AUTOSTART
+		BISECT_AUTOSTART,
+		BISECT_STATE
 	} cmdmode = 0;
 	int no_checkout = 0, res = 0, nolog = 0;
 	struct option options[] = {
@@ -896,6 +965,8 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 			 N_("verify the next bisection state then checkout the next bisection commit"), BISECT_AUTO_NEXT),
 		OPT_CMDMODE(0, "bisect-autostart", &cmdmode,
 			 N_("start the bisection if it has not yet been started"), BISECT_AUTOSTART),
+		OPT_CMDMODE(0, "bisect-state", &cmdmode,
+			 N_("mark the state of ref (or refs)"), BISECT_STATE),
 		OPT_BOOL(0, "no-checkout", &no_checkout,
 			 N_("update BISECT_HEAD instead of checking out the current commit")),
 		OPT_BOOL(0, "no-log", &nolog,
@@ -968,6 +1039,11 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 		set_terms(&terms, "bad", "good");
 		res = bisect_autostart(&terms);
 		break;
+	case BISECT_STATE:
+		set_terms(&terms, "bad", "good");
+		get_terms(&terms);
+		res = bisect_state(&terms, argv, argc);
+		break;
 	default:
 		BUG("unknown subcommand %d", (int)cmdmode);
 	}
diff --git a/git-bisect.sh b/git-bisect.sh
index cfb05c0511..7a8f796251 100755
--- a/git-bisect.sh
+++ b/git-bisect.sh
@@ -39,16 +39,6 @@ _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
 TERM_BAD=bad
 TERM_GOOD=good
 
-bisect_head()
-{
-	if test -f "$GIT_DIR/BISECT_HEAD"
-	then
-		echo BISECT_HEAD
-	else
-		echo HEAD
-	fi
-}
-
 bisect_skip() {
 	all=''
 	for arg in "$@"
@@ -61,43 +51,7 @@ bisect_skip() {
 		esac
 		all="$all $revs"
 	done
-	eval bisect_state 'skip' $all
-}
-
-bisect_state() {
-	git bisect--helper --bisect-autostart
-	state=$1
-	git bisect--helper --check-and-set-terms $state $TERM_GOOD $TERM_BAD || exit
-	get_terms
-	case "$#,$state" in
-	0,*)
-		die "Please call 'bisect_state' with at least one argument." ;;
-	1,"$TERM_BAD"|1,"$TERM_GOOD"|1,skip)
-		bisected_head=$(bisect_head)
-		rev=$(git rev-parse --verify "$bisected_head") ||
-			die "$(eval_gettext "Bad rev input: \$bisected_head")"
-		git bisect--helper --bisect-write "$state" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit
-		git bisect--helper --check-expected-revs "$rev" ;;
-	2,"$TERM_BAD"|*,"$TERM_GOOD"|*,skip)
-		shift
-		hash_list=''
-		for rev in "$@"
-		do
-			sha=$(git rev-parse --verify "$rev^{commit}") ||
-				die "$(eval_gettext "Bad rev input: \$rev")"
-			hash_list="$hash_list $sha"
-		done
-		for rev in $hash_list
-		do
-			git bisect--helper --bisect-write "$state" "$rev" "$TERM_GOOD" "$TERM_BAD" || exit
-		done
-		git bisect--helper --check-expected-revs $hash_list ;;
-	*,"$TERM_BAD")
-		die "$(eval_gettext "'git bisect \$TERM_BAD' can take only one argument.")" ;;
-	*)
-		usage ;;
-	esac
-	git bisect--helper --bisect-auto-next
+	eval git bisect--helper --bisect-state 'skip' $all
 }
 
 bisect_visualize() {
@@ -187,8 +141,7 @@ exit code \$res from '\$command' is < 0 or >= 128" >&2
 			state="$TERM_GOOD"
 		fi
 
-		# We have to use a subshell because "bisect_state" can exit.
-		( bisect_state $state >"$GIT_DIR/BISECT_RUN" )
+		git bisect--helper --bisect-state $state >"$GIT_DIR/BISECT_RUN"
 		res=$?
 
 		cat "$GIT_DIR/BISECT_RUN"
@@ -203,7 +156,7 @@ exit code \$res from '\$command' is < 0 or >= 128" >&2
 		if [ $res -ne 0 ]
 		then
 			eval_gettextln "bisect run failed:
-'bisect_state \$state' exited with error code \$res" >&2
+'git bisect--helper --bisect-state \$state' exited with error code \$res" >&2
 			exit $res
 		fi
 
@@ -244,7 +197,7 @@ case "$#" in
 	start)
 		git bisect--helper --bisect-start "$@" ;;
 	bad|good|new|old|"$TERM_BAD"|"$TERM_GOOD")
-		bisect_state "$cmd" "$@" ;;
+		git bisect--helper --bisect-state "$cmd" "$@" ;;
 	skip)
 		bisect_skip "$@" ;;
 	next)
-- 
2.25.0


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

* [PATCH v5 11/13] bisect--helper: retire `--check-expected-revs` subcommand
  2020-07-17 10:53 [PATCH v5 00/13] Finish converting git bisect to C part 2 Miriam Rubio
                   ` (9 preceding siblings ...)
  2020-07-17 10:54 ` [PATCH v5 10/13] bisect--helper: reimplement `bisect_state` & `bisect_head` shell functions in C Miriam Rubio
@ 2020-07-17 10:54 ` Miriam Rubio
  2020-07-17 10:54 ` [PATCH v5 12/13] bisect--helper: retire `--write-terms` subcommand Miriam Rubio
  2020-07-17 10:54 ` [PATCH v5 13/13] bisect--helper: retire `--bisect-autostart` subcommand Miriam Rubio
  12 siblings, 0 replies; 24+ messages in thread
From: Miriam Rubio @ 2020-07-17 10:54 UTC (permalink / raw)
  To: git
  Cc: Pranit Bauva, Lars Schneider, Christian Couder,
	Johannes Schindelin, Tanushree Tumane, Miriam Rubio

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

The `--check-expected-revs` subcommand is no longer used from the
git-bisect.sh shell script. Functions `check_expected_revs` and
`is_expected_revs` are also deleted.

Mentored-by: Lars Schneider <larsxschneider@gmail.com>
Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Mentored-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com>
Signed-off-by: Tanushree Tumane <tanushreetumane@gmail.com>
Signed-off-by: Miriam Rubio <mirucam@gmail.com>
---
 builtin/bisect--helper.c | 30 ------------------------------
 1 file changed, 30 deletions(-)

diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c
index 7369605e4c..370d344bbd 100644
--- a/builtin/bisect--helper.c
+++ b/builtin/bisect--helper.c
@@ -185,30 +185,6 @@ static int write_terms(const char *bad, const char *good)
 	return res;
 }
 
-static int is_expected_rev(const char *expected_hex)
-{
-	struct strbuf actual_hex = STRBUF_INIT;
-	int res = 0;
-	if (strbuf_read_file(&actual_hex, git_path_bisect_expected_rev(), 0) >= 40) {
-		strbuf_trim(&actual_hex);
-		res = !strcmp(actual_hex.buf, expected_hex);
-	}
-	strbuf_release(&actual_hex);
-	return res;
-}
-
-static void check_expected_revs(const char **revs, int rev_nr)
-{
-	int i;
-
-	for (i = 0; i < rev_nr; i++) {
-		if (!is_expected_rev(revs[i])) {
-			unlink_or_warn(git_path_bisect_ancestors_ok());
-			unlink_or_warn(git_path_bisect_expected_rev());
-		}
-	}
-}
-
 static int bisect_reset(const char *commit)
 {
 	struct strbuf branch = STRBUF_INIT;
@@ -929,7 +905,6 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 {
 	enum {
 		WRITE_TERMS = 1,
-		CHECK_EXPECTED_REVS,
 		BISECT_RESET,
 		BISECT_WRITE,
 		CHECK_AND_SET_TERMS,
@@ -945,8 +920,6 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 	struct option options[] = {
 		OPT_CMDMODE(0, "write-terms", &cmdmode,
 			 N_("write the terms to .git/BISECT_TERMS"), WRITE_TERMS),
-		OPT_CMDMODE(0, "check-expected-revs", &cmdmode,
-			 N_("check for expected revs"), CHECK_EXPECTED_REVS),
 		OPT_CMDMODE(0, "bisect-reset", &cmdmode,
 			 N_("reset the bisection state"), BISECT_RESET),
 		OPT_CMDMODE(0, "bisect-write", &cmdmode,
@@ -987,9 +960,6 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 		if (argc != 2)
 			return error(_("--write-terms requires two arguments"));
 		return write_terms(argv[0], argv[1]);
-	case CHECK_EXPECTED_REVS:
-		check_expected_revs(argv, argc);
-		return 0;
 	case BISECT_RESET:
 		if (argc > 1)
 			return error(_("--bisect-reset requires either no argument or a commit"));
-- 
2.25.0


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

* [PATCH v5 12/13] bisect--helper: retire `--write-terms` subcommand
  2020-07-17 10:53 [PATCH v5 00/13] Finish converting git bisect to C part 2 Miriam Rubio
                   ` (10 preceding siblings ...)
  2020-07-17 10:54 ` [PATCH v5 11/13] bisect--helper: retire `--check-expected-revs` subcommand Miriam Rubio
@ 2020-07-17 10:54 ` Miriam Rubio
  2020-07-17 10:54 ` [PATCH v5 13/13] bisect--helper: retire `--bisect-autostart` subcommand Miriam Rubio
  12 siblings, 0 replies; 24+ messages in thread
From: Miriam Rubio @ 2020-07-17 10:54 UTC (permalink / raw)
  To: git
  Cc: Pranit Bauva, Lars Schneider, Christian Couder,
	Johannes Schindelin, Tanushree Tumane, Miriam Rubio

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

The `--write-terms` subcommand is no longer used from the
git-bisect.sh shell script. Instead the function `write_terms()`
is called from the C implementation of `set_terms()` and
`bisect_start()`.

Mentored-by: Lars Schneider <larsxschneider@gmail.com>
Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Mentored-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com>
Signed-off-by: Tanushree Tumane <tanushreetumane@gmail.com>
Signed-off-by: Miriam Rubio <mirucam@gmail.com>
---
 builtin/bisect--helper.c | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)

diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c
index 370d344bbd..6f42e968ad 100644
--- a/builtin/bisect--helper.c
+++ b/builtin/bisect--helper.c
@@ -20,7 +20,6 @@ static GIT_PATH_FUNC(git_path_head_name, "head-name")
 static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES")
 
 static const char * const git_bisect_helper_usage[] = {
-	N_("git bisect--helper --write-terms <bad_term> <good_term>"),
 	N_("git bisect--helper --bisect-reset [<commit>]"),
 	N_("git bisect--helper --bisect-write [--no-log] <state> <revision> <good_term> <bad_term>"),
 	N_("git bisect--helper --bisect-check-and-set-terms <command> <good_term> <bad_term>"),
@@ -904,8 +903,7 @@ static enum bisect_error bisect_state(struct bisect_terms *terms, const char **a
 int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 {
 	enum {
-		WRITE_TERMS = 1,
-		BISECT_RESET,
+		BISECT_RESET = 1,
 		BISECT_WRITE,
 		CHECK_AND_SET_TERMS,
 		BISECT_NEXT_CHECK,
@@ -918,8 +916,6 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 	} cmdmode = 0;
 	int no_checkout = 0, res = 0, nolog = 0;
 	struct option options[] = {
-		OPT_CMDMODE(0, "write-terms", &cmdmode,
-			 N_("write the terms to .git/BISECT_TERMS"), WRITE_TERMS),
 		OPT_CMDMODE(0, "bisect-reset", &cmdmode,
 			 N_("reset the bisection state"), BISECT_RESET),
 		OPT_CMDMODE(0, "bisect-write", &cmdmode,
@@ -956,10 +952,6 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 		usage_with_options(git_bisect_helper_usage, options);
 
 	switch (cmdmode) {
-	case WRITE_TERMS:
-		if (argc != 2)
-			return error(_("--write-terms requires two arguments"));
-		return write_terms(argv[0], argv[1]);
 	case BISECT_RESET:
 		if (argc > 1)
 			return error(_("--bisect-reset requires either no argument or a commit"));
-- 
2.25.0


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

* [PATCH v5 13/13] bisect--helper: retire `--bisect-autostart` subcommand
  2020-07-17 10:53 [PATCH v5 00/13] Finish converting git bisect to C part 2 Miriam Rubio
                   ` (11 preceding siblings ...)
  2020-07-17 10:54 ` [PATCH v5 12/13] bisect--helper: retire `--write-terms` subcommand Miriam Rubio
@ 2020-07-17 10:54 ` Miriam Rubio
  12 siblings, 0 replies; 24+ messages in thread
From: Miriam Rubio @ 2020-07-17 10:54 UTC (permalink / raw)
  To: git
  Cc: Pranit Bauva, Lars Schneider, Christian Couder,
	Johannes Schindelin, Tanushree Tumane, Miriam Rubio

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

The `--bisect-autostart` subcommand is no longer used from the
git-bisect.sh shell script. Instead the function
`bisect_autostart()` is directly called from the C implementation.

Mentored-by: Lars Schneider <larsxschneider@gmail.com>
Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Mentored-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Signed-off-by: Pranit Bauva <pranit.bauva@gmail.com>
Signed-off-by: Tanushree Tumane <tanushreetumane@gmail.com>
Signed-off-by: Miriam Rubio <mirucam@gmail.com>
---
 builtin/bisect--helper.c | 10 ----------
 1 file changed, 10 deletions(-)

diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c
index 6f42e968ad..20d359b2ca 100644
--- a/builtin/bisect--helper.c
+++ b/builtin/bisect--helper.c
@@ -29,7 +29,6 @@ static const char * const git_bisect_helper_usage[] = {
 					     "[--no-checkout] [<bad> [<good>...]] [--] [<paths>...]"),
 	N_("git bisect--helper --bisect-next"),
 	N_("git bisect--helper --bisect-auto-next"),
-	N_("git bisect--helper --bisect-autostart"),
 	N_("git bisect--helper --bisect-state (bad|new) [<rev>]"),
 	N_("git bisect--helper --bisect-state (good|old) [<rev>...]"),
 	NULL
@@ -911,7 +910,6 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 		BISECT_START,
 		BISECT_NEXT,
 		BISECT_AUTO_NEXT,
-		BISECT_AUTOSTART,
 		BISECT_STATE
 	} cmdmode = 0;
 	int no_checkout = 0, res = 0, nolog = 0;
@@ -932,8 +930,6 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 			 N_("find the next bisection commit"), BISECT_NEXT),
 		OPT_CMDMODE(0, "bisect-auto-next", &cmdmode,
 			 N_("verify the next bisection state then checkout the next bisection commit"), BISECT_AUTO_NEXT),
-		OPT_CMDMODE(0, "bisect-autostart", &cmdmode,
-			 N_("start the bisection if it has not yet been started"), BISECT_AUTOSTART),
 		OPT_CMDMODE(0, "bisect-state", &cmdmode,
 			 N_("mark the state of ref (or refs)"), BISECT_STATE),
 		OPT_BOOL(0, "no-checkout", &no_checkout,
@@ -995,12 +991,6 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 		get_terms(&terms);
 		res = bisect_auto_next(&terms, prefix);
 		break;
-	case BISECT_AUTOSTART:
-		if (argc)
-			return error(_("--bisect-autostart does not accept arguments"));
-		set_terms(&terms, "bad", "good");
-		res = bisect_autostart(&terms);
-		break;
 	case BISECT_STATE:
 		set_terms(&terms, "bad", "good");
 		get_terms(&terms);
-- 
2.25.0


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

* Re: [PATCH v5 03/13] bisect--helper: introduce new `write_in_file()` function
  2020-07-17 10:53 ` [PATCH v5 03/13] bisect--helper: introduce new `write_in_file()` function Miriam Rubio
@ 2020-07-17 16:33   ` Junio C Hamano
  0 siblings, 0 replies; 24+ messages in thread
From: Junio C Hamano @ 2020-07-17 16:33 UTC (permalink / raw)
  To: Miriam Rubio; +Cc: git, Christian Couder, Johannes Schindelin

Miriam Rubio <mirucam@gmail.com> writes:

> +	fp = fopen(path, mode);
> +	if (!fp)
> +		return error_errno(_("cannot open file '%s' in mode '%s'"), path, mode);
> +	res = vfprintf(fp, format, args);
> +
> +	if (res < 0) {
> +		fclose(fp);
> +		return error_errno(_("could not write to file '%s'"), path);

If the fclose(fp) failed, the errno from that failure (i.e. why fp
cannot be closed) would be reported by the error_errno(), which may
not be ideal.  Either use just error() (which may be suboptimal),
follow an often-used pattern to use save_errno (grep for the symbol
to learn from the existing code) and keep using error_errno(), or
check errors from fclose() too (which may be overkill).

	if (res < 0) {
		int saved_errno = errno;
#if OVERKILL
		if (fclose(fp))
                	error_errno(_("..."));
#else
		fclose(fp);
#endif
		errno = saved_errno;
		return error_errno(_("..."), ...);
	}

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

* Re: [PATCH v5 06/13] bisect--helper: reimplement `bisect_next` and `bisect_auto_next` shell functions in C
  2020-07-17 10:53 ` [PATCH v5 06/13] bisect--helper: reimplement `bisect_next` and `bisect_auto_next` shell functions in C Miriam Rubio
@ 2020-07-17 16:54   ` Junio C Hamano
  2020-07-25  0:19     ` Junio C Hamano
  0 siblings, 1 reply; 24+ messages in thread
From: Junio C Hamano @ 2020-07-17 16:54 UTC (permalink / raw)
  To: Miriam Rubio
  Cc: git, Pranit Bauva, Lars Schneider, Christian Couder,
	Johannes Schindelin, Tanushree Tumane

Miriam Rubio <mirucam@gmail.com> writes:

> From: Pranit Bauva <pranit.bauva@gmail.com>
>
> Reimplement the `bisect_next()` and the `bisect_auto_next()` shell functions
> in C and add the subcommands to `git bisect--helper` to call them from
> git-bisect.sh .
>
> bisect_auto_next() function returns an enum bisect_error type as whole
> `git bisect` can exit with an error code when bisect_next() does.
>
> Using `--bisect-next` and `--bisect-auto-next` subcommands is a
> temporary measure to port shell function to C so as to use the existing
> test suite. As more functions are ported, `--bisect-auto-next`
> subcommand will be retired and will be called by some other methods.
> ...
> +static enum bisect_error bisect_next(struct bisect_terms *terms, const char *prefix)
> +{
> +	int no_checkout;
> +	enum bisect_error res;
> +
> +	bisect_autostart(terms);
> +	if (bisect_next_check(terms, terms->term_good))
> +		return BISECT_FAILED;
> +
> +	no_checkout = file_exists(git_path_bisect_head());

The ref API is getting updated so that the 40-hex files whose names
are all capital and ends with _HEAD are being converted from "files
that happen to record 40-hex" to "refs that live at the toplevel
outside refs/ hierarchy".  Checking with

	no_checkout = ref_exists("BISECT_HEAD");

would work in both old and new worlds, hopefully.

Similarly, if the new code you are writing reads directly from
git_path_bisect_head(), write into it, or unlink it, these accesses
should be replaced with calls to refs API (e.g. you already do so in
your bisect_state() helper where you use get_oid() on the refname,
not read from the file).

Please learn to pay attention to your surroundings, or you risk
colliding into other topics in flight.

Thanks.

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

* Re: [PATCH v5 06/13] bisect--helper: reimplement `bisect_next` and `bisect_auto_next` shell functions in C
  2020-07-17 16:54   ` Junio C Hamano
@ 2020-07-25  0:19     ` Junio C Hamano
  0 siblings, 0 replies; 24+ messages in thread
From: Junio C Hamano @ 2020-07-25  0:19 UTC (permalink / raw)
  To: Miriam Rubio
  Cc: git, Pranit Bauva, Lars Schneider, Christian Couder,
	Johannes Schindelin, Tanushree Tumane

Junio C Hamano <gitster@pobox.com> writes:

> The ref API is getting updated so that the 40-hex files whose names
> are all capital and ends with _HEAD are being converted from "files
> that happen to record 40-hex" to "refs that live at the toplevel
> outside refs/ hierarchy".  Checking with
>
> 	no_checkout = ref_exists("BISECT_HEAD");
>
> would work in both old and new worlds, hopefully.
>
> Similarly, if the new code you are writing reads directly from
> git_path_bisect_head(), write into it, or unlink it, these accesses
> should be replaced with calls to refs API (e.g. you already do so in
> your bisect_state() helper where you use get_oid() on the refname,
> not read from the file).

In the meantime, I have applied the following fix-up on top of these
13 patches, but when the topic gets rerolled (as we are in the
feature freeze before the upcoming release, there is no need to
hurry till the end of the month, though), I'd rather want to see the
new use of file_exists(git_path_bisect_head()) disappear.  As it
stands as a 14-patch series, the series would instead end up with us
making a mistake to introduce new use of git_path_bisect_head() at
step 06/14 and then later realize the mistake and fix it at step
14/14, which would be a waste of time for future readers of the code.

Thanks.

-- >8 --
Subject: SQUASH??? do not add new users of git_path_bisect_head()

---
 builtin/bisect--helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c
index 20d359b2ca..eb3d30389b 100644
--- a/builtin/bisect--helper.c
+++ b/builtin/bisect--helper.c
@@ -572,7 +572,7 @@ static enum bisect_error bisect_next(struct bisect_terms *terms, const char *pre
 	if (bisect_next_check(terms, terms->term_good))
 		return BISECT_FAILED;
 
-	no_checkout = file_exists(git_path_bisect_head());
+	no_checkout = ref_exists("BISECT_HEAD");
 
 	/* Perform all bisection computation, display and checkout */
 	res = bisect_next_all(the_repository, prefix, no_checkout);
-- 
2.28.0-rc1


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

* Re: [PATCH v5 01/13] bisect--helper: BUG() in cmd_*() on invalid subcommand
  2020-07-17 10:53 ` [PATCH v5 01/13] bisect--helper: BUG() in cmd_*() on invalid subcommand Miriam Rubio
@ 2020-07-25 17:27   ` Shourya Shukla
  0 siblings, 0 replies; 24+ messages in thread
From: Shourya Shukla @ 2020-07-25 17:27 UTC (permalink / raw)
  To: mirucam; +Cc: Johannes.Schindelin, chriscool, git

Hello Miriam,

> In cmd_bisect__helper() function, there would be a bug
> if an invalid or no subcommand was passed.return error()
> should be replaced by BUG() in that case.

> Mentored-by: Christian Couder <chriscool@tuxfamily.org>
> Mentored-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
> Signed-off-by: Miriam Rubio <mirucam@gmail.com>
> ---

I think we can try for a better message. Maybe something like:

    One encounters a BUG when an invalid or no subcommand is passed to
    'cmd_bisect__helper()'. BUG() out instead of returning an error.

> -		return error("BUG: unknown subcommand '%d'", cmdmode);
> +		BUG("unknown subcommand %d", (int)cmdmode);

Do we need to typecast an integer to an integer here?

Regards,
Shourya Shukla


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

* Re: [PATCH v5 02/13] bisect--helper: use '-res' in 'cmd_bisect__helper' return
  2020-07-17 10:53 ` [PATCH v5 02/13] bisect--helper: use '-res' in 'cmd_bisect__helper' return Miriam Rubio
@ 2020-07-25 17:31   ` Shourya Shukla
  2020-07-25 23:03     ` Junio C Hamano
  2020-07-27  5:42     ` Pratyush Yadav
  0 siblings, 2 replies; 24+ messages in thread
From: Shourya Shukla @ 2020-07-25 17:31 UTC (permalink / raw)
  To: mirucam; +Cc: chriscool, git, gitster

> Following 'enum bisect_error' vocabulary, return variable 'res' is
> always non-positive.
> Let's use '-res' instead of 'abs(res)' to make the code clearer.

> Mentored-by: Christian Couder <chriscool@tuxfamily.org>
> Signed-off-by: Miriam Rubio <mirucam@gmail.com>
> Helped-by: Junio C Hamano <gitster@pobox.com>

Although there is no set rule for this, but I think that your sign-off
goes at the last generally. This is what I have seen on various patches.


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

* Re: [PATCH v5 02/13] bisect--helper: use '-res' in 'cmd_bisect__helper' return
  2020-07-25 17:31   ` Shourya Shukla
@ 2020-07-25 23:03     ` Junio C Hamano
  2020-07-27  5:42     ` Pratyush Yadav
  1 sibling, 0 replies; 24+ messages in thread
From: Junio C Hamano @ 2020-07-25 23:03 UTC (permalink / raw)
  To: Shourya Shukla; +Cc: mirucam, chriscool, git

Shourya Shukla <shouryashukla.oo@gmail.com> writes:

>> Following 'enum bisect_error' vocabulary, return variable 'res' is
>> always non-positive.
>> Let's use '-res' instead of 'abs(res)' to make the code clearer.
>
>> Mentored-by: Christian Couder <chriscool@tuxfamily.org>
>> Signed-off-by: Miriam Rubio <mirucam@gmail.com>
>> Helped-by: Junio C Hamano <gitster@pobox.com>
>
> Although there is no set rule for this, but I think that your sign-off
> goes at the last generally. This is what I have seen on various patches.

It is most natural to list these entries in chronological order.

Code was written while others helped, and the final seal on what
gets sent out is stamped by appending the author's sign-off at the
end.

Thanks.

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

* Re: [PATCH v5 04/13] bisect--helper: reimplement `bisect_autostart` shell function in C
  2020-07-17 10:53 ` [PATCH v5 04/13] bisect--helper: reimplement `bisect_autostart` shell function in C Miriam Rubio
@ 2020-07-26  1:53   ` Đoàn Trần Công Danh
  0 siblings, 0 replies; 24+ messages in thread
From: Đoàn Trần Công Danh @ 2020-07-26  1:53 UTC (permalink / raw)
  To: Miriam Rubio
  Cc: git, Pranit Bauva, Lars Schneider, Christian Couder,
	Johannes Schindelin, Tanushree Tumane

On 2020-07-17 12:53:57+0200, Miriam Rubio <mirucam@gmail.com> wrote:
> From: Pranit Bauva <pranit.bauva@gmail.com>
> 
> +static int bisect_autostart(struct bisect_terms *terms)
> +{
> +	int res;
> +	const char *yesno;
> +
> +	if (file_is_not_empty(git_path_bisect_start()))
> +		return 0;
> +
> +	fprintf_ln(stderr, _("You need to start by \"git bisect "
> +			  "start\"\n"));
> +
> +	if (!isatty(STDIN_FILENO))
> +		return 0;
> +
> +	/*
> +	 * TRANSLATORS: Make sure to include [Y] and [n] in your
> +	 * translation. The program will only accept English input
> +	 * at this point.
> +	 */
> +	yesno = git_prompt(_("Do you want me to do it for you "
> +			     "[Y/n]? "), PROMPT_ECHO);
> +	res = tolower(*yesno) == 'n' ?
> +		-1 : bisect_start(terms, 0, NULL, 0);

Applying 01-04 ontop of v2.28.0-rc2-3-g418cca9555 (current git/git.git's master).

This line crash my git-bisect, MVCE:

	./git-bisect--helper --bisect-autostart

Since it will pass NULL as argv to bisect_start, which expect an array
of "argc + 1" of "char*" (the last element is NULL).
And bisect_start attempts to log bisect entry for this call.

Quick fix:
----------------8<---------------
diff --git a/builtin/bisect--helper.c b/builtin/bisect--helper.c
index 7662ec0ce3..25d3d07721 100644
--- a/builtin/bisect--helper.c
+++ b/builtin/bisect--helper.c
@@ -673,7 +673,7 @@ static int bisect_autostart(struct bisect_terms *terms)
 	yesno = git_prompt(_("Do you want me to do it for you "
 			     "[Y/n]? "), PROMPT_ECHO);
 	res = tolower(*yesno) == 'n' ?
-		-1 : bisect_start(terms, 0, NULL, 0);
+		-1 : bisect_start(terms, 0, empty_argv, 0);
 
 		return res;
 }
-------------------->8------------------


> +
> +		return res;

This line is over-indented. IOW, move left 1 TAB.

-- 
Danh

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

* Re: [PATCH v5 02/13] bisect--helper: use '-res' in 'cmd_bisect__helper' return
  2020-07-25 17:31   ` Shourya Shukla
  2020-07-25 23:03     ` Junio C Hamano
@ 2020-07-27  5:42     ` Pratyush Yadav
  2020-07-27  6:46       ` Pratyush Yadav
  1 sibling, 1 reply; 24+ messages in thread
From: Pratyush Yadav @ 2020-07-27  5:42 UTC (permalink / raw)
  To: Shourya Shukla; +Cc: mirucam, chriscool, git, gitster

On 25/07/20 11:01PM, Shourya Shukla wrote:
> > Following 'enum bisect_error' vocabulary, return variable 'res' is
> > always non-positive.
> > Let's use '-res' instead of 'abs(res)' to make the code clearer.
> 
> > Mentored-by: Christian Couder <chriscool@tuxfamily.org>
> > Signed-off-by: Miriam Rubio <mirucam@gmail.com>
> > Helped-by: Junio C Hamano <gitster@pobox.com>
> 
> Although there is no set rule for this, but I think that your sign-off
> goes at the last generally. This is what I have seen on various patches.

The rule of thumb I follow is to have these in chronological order. So 
Christian first mentored, then the patch was published so Junio helped 
next and finally you sign off the patch before sending it out so that 
comes last.

-- 
Regards,
Pratyush Yadav

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

* Re: [PATCH v5 02/13] bisect--helper: use '-res' in 'cmd_bisect__helper' return
  2020-07-27  5:42     ` Pratyush Yadav
@ 2020-07-27  6:46       ` Pratyush Yadav
  2020-07-27 16:21         ` Junio C Hamano
  0 siblings, 1 reply; 24+ messages in thread
From: Pratyush Yadav @ 2020-07-27  6:46 UTC (permalink / raw)
  To: Shourya Shukla; +Cc: mirucam, chriscool, git, gitster

On 27/07/20 11:12AM, Pratyush Yadav wrote:
> On 25/07/20 11:01PM, Shourya Shukla wrote:
> > > Following 'enum bisect_error' vocabulary, return variable 'res' is
> > > always non-positive.
> > > Let's use '-res' instead of 'abs(res)' to make the code clearer.
> > 
> > > Mentored-by: Christian Couder <chriscool@tuxfamily.org>
> > > Signed-off-by: Miriam Rubio <mirucam@gmail.com>
> > > Helped-by: Junio C Hamano <gitster@pobox.com>
> > 
> > Although there is no set rule for this, but I think that your sign-off
> > goes at the last generally. This is what I have seen on various patches.
> 
> The rule of thumb I follow is to have these in chronological order. So 
> Christian first mentored, then the patch was published so Junio helped 
> next and finally you sign off the patch before sending it out so that 
> comes last.

My MUA didn't download Junio's reply earlier for some reason. Sorry for 
the redundant message.

-- 
Regards,
Pratyush Yadav

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

* Re: [PATCH v5 02/13] bisect--helper: use '-res' in 'cmd_bisect__helper' return
  2020-07-27  6:46       ` Pratyush Yadav
@ 2020-07-27 16:21         ` Junio C Hamano
  0 siblings, 0 replies; 24+ messages in thread
From: Junio C Hamano @ 2020-07-27 16:21 UTC (permalink / raw)
  To: Pratyush Yadav; +Cc: Shourya Shukla, mirucam, chriscool, git

Pratyush Yadav <me@yadavpratyush.com> writes:

> My MUA didn't download Junio's reply earlier for some reason. Sorry for 
> the redundant message.

Mails cross all the time---you do not have to apologize.


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

end of thread, other threads:[~2020-07-27 16:21 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-17 10:53 [PATCH v5 00/13] Finish converting git bisect to C part 2 Miriam Rubio
2020-07-17 10:53 ` [PATCH v5 01/13] bisect--helper: BUG() in cmd_*() on invalid subcommand Miriam Rubio
2020-07-25 17:27   ` Shourya Shukla
2020-07-17 10:53 ` [PATCH v5 02/13] bisect--helper: use '-res' in 'cmd_bisect__helper' return Miriam Rubio
2020-07-25 17:31   ` Shourya Shukla
2020-07-25 23:03     ` Junio C Hamano
2020-07-27  5:42     ` Pratyush Yadav
2020-07-27  6:46       ` Pratyush Yadav
2020-07-27 16:21         ` Junio C Hamano
2020-07-17 10:53 ` [PATCH v5 03/13] bisect--helper: introduce new `write_in_file()` function Miriam Rubio
2020-07-17 16:33   ` Junio C Hamano
2020-07-17 10:53 ` [PATCH v5 04/13] bisect--helper: reimplement `bisect_autostart` shell function in C Miriam Rubio
2020-07-26  1:53   ` Đoàn Trần Công Danh
2020-07-17 10:53 ` [PATCH v5 05/13] bisect: call 'clear_commit_marks_all()' in 'bisect_next_all()' Miriam Rubio
2020-07-17 10:53 ` [PATCH v5 06/13] bisect--helper: reimplement `bisect_next` and `bisect_auto_next` shell functions in C Miriam Rubio
2020-07-17 16:54   ` Junio C Hamano
2020-07-25  0:19     ` Junio C Hamano
2020-07-17 10:54 ` [PATCH v5 07/13] bisect--helper: finish porting `bisect_start()` to C Miriam Rubio
2020-07-17 10:54 ` [PATCH v5 08/13] bisect--helper: retire `--bisect-clean-state` subcommand Miriam Rubio
2020-07-17 10:54 ` [PATCH v5 09/13] bisect--helper: retire `--next-all` subcommand Miriam Rubio
2020-07-17 10:54 ` [PATCH v5 10/13] bisect--helper: reimplement `bisect_state` & `bisect_head` shell functions in C Miriam Rubio
2020-07-17 10:54 ` [PATCH v5 11/13] bisect--helper: retire `--check-expected-revs` subcommand Miriam Rubio
2020-07-17 10:54 ` [PATCH v5 12/13] bisect--helper: retire `--write-terms` subcommand Miriam Rubio
2020-07-17 10:54 ` [PATCH v5 13/13] bisect--helper: retire `--bisect-autostart` subcommand Miriam Rubio

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