git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] add-patch: end "add -p" gracefully on EOF

From
Rubén Justo <rjusto@gmail.com>
Date
Jul 17, 2024, 16:22 UTC
Message-ID
<42a04aac-3f60-4259-9c9e-f46e9b6e33b3@gmail.com>

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 --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 &&
-- 
2.45.1
Message 1 of 1 in “add-patch: end "add -p" gracefully on EOF”
  1. add-patch: end "add -p" gracefully on EOFRubén Justo, Jul 17, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.