git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Rubén Justo" <rjusto@gmail.com>
To: Git List <git@vger.kernel.org>
Cc: Phillip Wood <phillip.wood@dunelm.org.uk>, Jeff King <peff@peff.net>
Subject: [PATCH] add-patch: end "add -p" gracefully on EOF
Date: Thu, 18 Jul 2024 01:22:16 +0900	[thread overview]
Message-ID: <42a04aac-3f60-4259-9c9e-f46e9b6e33b3@gmail.com> (raw)

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

                 reply	other threads:[~2024-07-17 16:22 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=42a04aac-3f60-4259-9c9e-f46e9b6e33b3@gmail.com \
    --to=rjusto@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=peff@peff.net \
    --cc=phillip.wood@dunelm.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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 NNTP newsgroup(s).