@@ -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;
@@ -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 &&
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(-)