diff mbox series

add-patch: end "add -p" gracefully on EOF

Message ID 42a04aac-3f60-4259-9c9e-f46e9b6e33b3@gmail.com (mailing list archive)
State New, archived
Headers show
Series add-patch: end "add -p" gracefully on EOF | expand

Commit Message

Rubén Justo July 17, 2024, 4:22 p.m. UTC
If we receive an EOF during the loop in `patch_update_file()`, we break
the loop.  However, the loop in `run_add_p()` could brings us back to
`patch_update_file()`, to only receive EOF again.

This is a sample output:

   $ touch a b c
   $ git add -N a b c
   $ printf "%s\n" Z | git add -p
   diff --git a/a b/a
   new file mode 100644
   index 0000000..e69de29
   (1/1) Stage addition [y,n,q,a,d,?]? Unknown command 'Z' (use '?' for help)
   (1/1) Stage addition [y,n,q,a,d,?]?
   diff --git a/b b/b
   new file mode 100644
   index 0000000..e69de29
   (1/1) Stage addition [y,n,q,a,d,?]?
   diff --git a/c b/c
   new file mode 100644
   index 0000000..e69de29
   (1/1) Stage addition [y,n,q,a,d,?]?

When we see a "quit", this is the, much more expected, result:

   $ printf "%s\n" Z q | git add -p
   diff --git a/a b/a
   new file mode 100644
   index 0000000..e69de29
   (1/1) Stage addition [y,n,q,a,d,?]? Unknown command 'Z' (use '?' for help)
   (1/1) Stage addition [y,n,q,a,d,?]?

We can assume that EOF is a synonym for 'q'.  Let's do that.

We've had this behavior since before the port to C of "add -p", which
was ported faithfully.  Let's fix it today and live happily ever after.

Signed-off-by: Rubén Justo <rjusto@gmail.com>
---
 add-patch.c                |  4 +++-
 t/t3701-add-interactive.sh | 13 +++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/add-patch.c b/add-patch.c
index 6e176cd21a..d8c5496a9b 100644
--- a/add-patch.c
+++ b/add-patch.c
@@ -1508,8 +1508,10 @@  static int patch_update_file(struct add_p_state *s,
 		if (*s->s.reset_color)
 			fputs(s->s.reset_color, stdout);
 		fflush(stdout);
-		if (read_single_character(s) == EOF)
+		if (read_single_character(s) == EOF) {
+			quit = 1;
 			break;
+		}
 
 		if (!s->answer.len)
 			continue;
diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
index 5d78868ac1..6bf0c0b6ca 100755
--- a/t/t3701-add-interactive.sh
+++ b/t/t3701-add-interactive.sh
@@ -56,6 +56,19 @@  test_expect_success 'unknown command' '
 	test_cmp expect actual
 '
 
+test_expect_success 'end gracefully on EOF' '
+	test_when_finished "git reset --hard; rm -f a b" &&
+	touch a b &&
+	git add -N a b &&
+	git diff a >expect &&
+	cat >>expect <<-EOF &&
+	(1/1) Stage addition [y,n,q,a,d,?]? Unknown command ${SQ}R${SQ} (use ${SQ}?${SQ} for help)
+	(1/1) Stage addition [y,n,q,a,d,?]?$SP
+	EOF
+	test_write_lines R | git add -p >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'setup (initial)' '
 	echo content >file &&
 	git add file &&