Skip to contentSkip to conversation

Finish converting git bisect to C part 2 subset 1

PATCH Superseded Miriam Rubio 3 participants 10 messages Sep 23, 2020

v8 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v8 cover Finish converting git bisect to C part 2 subset 1

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/[email protected]) and continued by me.

This patch series version is composed by the first six patches of the previous version due to a bug discovered during the test stage, that affects the rest of the patches and that needs to be fixed before sending them.

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

I would like to thank Johannes Schindelin for reviewing this patch series and to Christian Couder for his advice and help.

General changes
---------------

* Rebased on 385c171a01, (Fifteenth batch, 2020-09-18) to update command help messages.

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

[4/6] bisect--helper: reimplement bisect_autostart shell function in C

* Amend commit message.
* Change to return -1 when we cannot autostart.
* Add || exit to bisect-autostart call in shell script.

---

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

* Amend commit message.
* Amend new modes order in cmdmode enum.
* Add || exit to bisect-auto-next call in shell script.

---

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 (2):
bisect--helper: reimplement bisect_autostart shell function in C
bisect--helper: reimplement bisect_next and bisect_auto_next shell
functions in C

 bisect.c                 |  13 +-
 builtin/bisect--helper.c | 269 +++++++++++++++++++++++++++++++++++++--
 git-bisect.sh            |  70 +---------
 3 files changed, 274 insertions(+), 78 deletions(-)
Signature · 1 line
2.25.0
#1 lore raw

PATCH v8 1/6 bisect--helper: BUG() in cmd_*() on invalid subcommand

In cmd_bisect__helper() function, if an invalid or no subcommand is passed there is a BUG.

BUG() out instead of returning an error.

---
 builtin/bisect--helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
builtin/bisect--helper.c +1 -1
index 7dcc1b5188..23a9f00035 100644
@@ -720,7 +720,7 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 		res = bisect_start(&terms, argv, argc);
 		break;
 	default:
-		return error("BUG: unknown subcommand '%d'", cmdmode);
+		BUG("unknown subcommand %d", cmdmode);
 	}
 	free_terms(&terms);
 
Signature · 1 line
2.25.0
#2 lore raw

PATCH v8 2/6 bisect--helper: use '-res' in 'cmd_bisect__helper' return

In reply to Miriam Rubio

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.

---
 builtin/bisect--helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
builtin/bisect--helper.c +1 -1
index 23a9f00035..8037888ae2 100644
@@ -731,5 +731,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;
 }
Signature · 1 line
2.25.0
#4 lore raw

PATCH v8 4/6 bisect--helper: reimplement `bisect_autostart` shell function in C

In reply to Miriam Rubio

From: Pranit Bauva <[email protected]>

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

Change behavior of shell script that returned success when user aborted the bisection.

---
 builtin/bisect--helper.c | 44 +++++++++++++++++++++++++++++++++++++++-
 git-bisect.sh            | 25 ++---------------------
 2 files changed, 45 insertions(+), 24 deletions(-)
builtin/bisect--helper.c +43 -1
index 46d2abb36f..ab27a8dff6 100644
@@ -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-{new,bad}=<term> --term-{old,good}=<term>]"
 					    " [--no-checkout] [--first-parent] [<bad> [<good>...]] [--] [<paths>...]"),
+	N_("git bisect--helper --bisect-autostart"),
 	NULL
 };
 
@@ -653,6 +654,38 @@ static int bisect_start(struct bisect_terms *terms, const char **argv, int argc)
 	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 -1;
+
+	/*
+	 * 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, empty_strvec, 0);
+
+	return res;
+}
+
 int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 {
 	enum {
@@ -665,7 +698,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 res = 0, nolog = 0;
 	struct option options[] = {
@@ -689,6 +723,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-log", &nolog,
 			 N_("no log for BISECT_WRITE")),
 		OPT_END()
@@ -748,6 +784,12 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 		set_terms(&terms, "bad", "good");
 		res = bisect_start(&terms, 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", cmdmode);
 	}
git-bisect.sh +2 -23
index 2f60fefcfa..4e9b2d36cb 100755
@@ -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 || exit
 	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
Signature · 1 line
2.25.0
#3 lore raw

PATCH v8 3/6 bisect--helper: introduce new `write_in_file()` function

In reply to Miriam Rubio

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.

---
 builtin/bisect--helper.c | 43 +++++++++++++++++++++++++++++++++-------
 1 file changed, 36 insertions(+), 7 deletions(-)
builtin/bisect--helper.c +36 -7
index 8037888ae2..46d2abb36f 100644
@@ -74,6 +74,40 @@ 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) {
+		int saved_errno = errno;
+		fclose(fp);
+		errno = saved_errno;
+		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 +138,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 +146,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)
Signature · 1 line
2.25.0
#6 lore raw

PATCH v8 5/6 bisect: call 'clear_commit_marks_all()' in 'bisect_next_all()'

In reply to Miriam Rubio

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

---
 bisect.c | 2 ++
 1 file changed, 2 insertions(+)
bisect.c +2 -0
index d42a3a3767..c6aba2b9f2 100644
@@ -1082,6 +1082,8 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
 		  "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);
 }
Signature · 1 line
2.25.0
#7 lore raw

PATCH v8 6/6 bisect--helper: reimplement `bisect_next` and `bisect_auto_next` shell functions in C

In reply to Miriam Rubio

From: Pranit Bauva <[email protected]>

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.

Return an error when bisect_next() fails, that fix a bug on shell script version.

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.

---
 bisect.c                 |  11 ++-
 builtin/bisect--helper.c | 180 ++++++++++++++++++++++++++++++++++++++-
 git-bisect.sh            |  47 +---------
 3 files changed, 191 insertions(+), 47 deletions(-)
bisect.c +9 -2
index c6aba2b9f2..f5b1368128 100644
@@ -988,8 +988,11 @@ 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.
- * If no_checkout is non-zero, the bisection process does not
- * checkout the trial commit but instead simply updates BISECT_HEAD.
+ *
+ * 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.
  */
 enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
 {
@@ -999,6 +1002,10 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix)
 	enum bisect_error res = BISECT_OK;
 	struct object_id *bisect_rev;
 	char *steps_msg;
+	/*
+	 * If no_checkout is non-zero, the bisection process does not
+	 * checkout the trial commit but instead simply updates BISECT_HEAD.
+	 */
 	int no_checkout = ref_exists("BISECT_HEAD");
 	unsigned bisect_flags = 0;
 
builtin/bisect--helper.c +178 -2

244 lines not on this page · raw message

git-bisect.sh +4 -43

71 lines not on this page · raw message

Signature · 1 line
2.25.0
#8 lore raw
In reply to Miriam Rubio
Quoted 10 lines · 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/[email protected]) and continued by me.

This patch series version is composed by the first six patches of the previous version due to a bug discovered during the test stage, that affects the rest of the patches and that needs to be fixed before sending them.

Thanks for being careful.

Quoted 2 lines

* Rebased on 385c171a01, (Fifteenth batch, 2020-09-18) to update command help messages.

Personally I find this rebase unwarranted. What ef4d9f8a (bisect:
swap command-line options in documentation, 2020-08-28) on the 'master' did does not overlap with this series in any meaningful way. Since it is also trivial to adjust these patches back on e9b77c84 (Tenth batch, 2020-08-24) on which the previous round was based, I won't complain ;-)

Quoted 5 lines

[4/6] bisect--helper: reimplement bisect_autostart shell function in C

* Amend commit message.
* Change to return -1 when we cannot autostart.
* Add || exit to bisect-autostart call in shell script.

Sounds good.

Quoted 5 lines

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

* Amend commit message.
* Amend new modes order in cmdmode enum.

Meaning that the two new ones can just be at the end and there wasn't a reason for them to come before autostart? OK.

Quoted 1 line

* Add || exit to bisect-auto-next call in shell script.

OK.

Thanks.

#5 lore raw

Re: [PATCH v8 4/6] bisect--helper: reimplement `bisect_autostart` shell function in C

In reply to Miriam Rubio

Hi Miriam,

Quoted 125 lines · Miriam Rubio
builtin/bisect--helper.c +43 -1
index 46d2abb36f..ab27a8dff6 100644
@@ -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-{new,bad}=<term> --term-{old,good}=<term>]"
 					    " [--no-checkout] [--first-parent] [<bad> [<good>...]] [--] [<paths>...]"),
+	N_("git bisect--helper --bisect-autostart"),
 	NULL
 };
 
@@ -653,6 +654,38 @@ static int bisect_start(struct bisect_terms *terms, const char **argv, int argc)
 	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 -1;
+
+	/*
+	 * 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, empty_strvec, 0);
+
+	return res;
+}
+
 int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 {
 	enum {
@@ -665,7 +698,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 res = 0, nolog = 0;
 	struct option options[] = {
@@ -689,6 +723,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-log", &nolog,
 			 N_("no log for BISECT_WRITE")),
 		OPT_END()
@@ -748,6 +784,12 @@ int cmd_bisect__helper(int argc, const char **argv, const char *prefix)
 		set_terms(&terms, "bad", "good");
 		res = bisect_start(&terms, 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", cmdmode);
 	}
git-bisect.sh +1 -22
index 2f60fefcfa..4e9b2d36cb 100755
@@ -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 || exit
 	state=$1
 	git bisect--helper --check-and-set-terms $state $TERM_GOOD $TERM_BAD || exit
 	get_terms

git-bisect.sh bisect_auto_next() { · line 131

Quoted 5 lines
git-bisect.sh +1 -1
@@ -149,7 +128,7 @@ bisect_auto_next() {
 
 bisect_next() {
 	case "$#" in 0) ;; *) usage ;; esac
-	bisect_autostart
+	git bisect--helper --bisect-autostart

Just like above, we need an || exit here.

Other than that, this patch looks good to me.

Thanks,
Dscho

Quoted 7 lines
git-bisect.sh +0 -1
 	git bisect--helper --bisect-next-check $TERM_GOOD $TERM_BAD $TERM_GOOD|| exit
 
 	# Perform all bisection computation, display and checkout
--

2.25.0

#9 lore raw
In reply to Miriam Rubio

Hi Miriam,

Quoted 11 lines · Miriam Rubio

General changes
---------------

* Rebased on 385c171a01, (Fifteenth batch, 2020-09-18) to update command help messages.

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

Quoted 5 lines

[4/6] bisect--helper: reimplement bisect_autostart shell function in C

* Amend commit message.
* Change to return -1 when we cannot autostart.
* Add || exit to bisect-autostart call in shell script.

I think the bisect_next function needs the same treatment. Other than that, this iteration is ready to go from my side.

Thank you,
Dscho