Skip to content

Commit 80dbfac

Browse files
committed
Merge branch 'rj/add-p-typo-reaction'
When the user responds to a prompt given by "git add -p" with an unsupported command, list of available commands were given, which was too much if the user knew what they wanted to type but merely made a typo. Now the user gets a much shorter error message. * rj/add-p-typo-reaction: add-patch: response to unknown command add-patch: do not show UI messages on stderr
2 parents 34f34d6 + 26998ed commit 80dbfac

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

add-patch.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,9 @@ static void err(struct add_p_state *s, const char *fmt, ...)
293293
va_list args;
294294

295295
va_start(args, fmt);
296-
fputs(s->s.error_color, stderr);
297-
vfprintf(stderr, fmt, args);
298-
fputs(s->s.reset_color, stderr);
299-
fputc('\n', stderr);
296+
fputs(s->s.error_color, stdout);
297+
vprintf(fmt, args);
298+
puts(s->s.reset_color);
300299
va_end(args);
301300
}
302301

@@ -1326,7 +1325,7 @@ static int apply_for_checkout(struct add_p_state *s, struct strbuf *diff,
13261325
err(s, _("Nothing was applied.\n"));
13271326
} else
13281327
/* As a last resort, show the diff to the user */
1329-
fwrite(diff->buf, diff->len, 1, stderr);
1328+
fwrite(diff->buf, diff->len, 1, stdout);
13301329

13311330
return 0;
13321331
}
@@ -1668,7 +1667,7 @@ static int patch_update_file(struct add_p_state *s,
16681667
}
16691668
} else if (s->answer.buf[0] == 'p') {
16701669
rendered_hunk_index = -1;
1671-
} else {
1670+
} else if (s->answer.buf[0] == '?') {
16721671
const char *p = _(help_patch_remainder), *eol = p;
16731672

16741673
color_fprintf(stdout, s->s.help_color, "%s",
@@ -1692,6 +1691,9 @@ static int patch_update_file(struct add_p_state *s,
16921691
color_fprintf_ln(stdout, s->s.help_color,
16931692
"%.*s", (int)(eol - p), p);
16941693
}
1694+
} else {
1695+
err(s, _("Unknown command '%s' (use '?' for help)"),
1696+
s->answer.buf);
16951697
}
16961698
}
16971699

@@ -1778,9 +1780,9 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
17781780
break;
17791781

17801782
if (s.file_diff_nr == 0)
1781-
fprintf(stderr, _("No changes.\n"));
1783+
err(&s, _("No changes."));
17821784
else if (binary_count == s.file_diff_nr)
1783-
fprintf(stderr, _("Only binary files changed.\n"));
1785+
err(&s, _("Only binary files changed."));
17841786

17851787
add_p_state_clear(&s);
17861788
return 0;

t/t3701-add-interactive.sh

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ TEST_PASSES_SANITIZE_LEAK=true
88
. ./test-lib.sh
99
. "$TEST_DIRECTORY"/lib-terminal.sh
1010

11+
SP=" "
12+
1113
diff_cmp () {
1214
for x
1315
do
@@ -45,17 +47,30 @@ test_expect_success 'warn about add.interactive.useBuiltin' '
4547
cat >expect <<-\EOF &&
4648
warning: the add.interactive.useBuiltin setting has been removed!
4749
See its entry in '\''git help config'\'' for details.
48-
No changes.
4950
EOF
51+
echo "No changes." >expect.out &&
5052
5153
for v in = =true =false
5254
do
5355
git -c "add.interactive.useBuiltin$v" add -p >out 2>actual &&
54-
test_must_be_empty out &&
56+
test_cmp expect.out out &&
5557
test_cmp expect actual || return 1
5658
done
5759
'
5860

61+
test_expect_success 'unknown command' '
62+
test_when_finished "git reset --hard; rm -f command" &&
63+
echo W >command &&
64+
git add -N command &&
65+
git diff command >expect &&
66+
cat >>expect <<-EOF &&
67+
(1/1) Stage addition [y,n,q,a,d,e,p,?]? Unknown command ${SQ}W${SQ} (use ${SQ}?${SQ} for help)
68+
(1/1) Stage addition [y,n,q,a,d,e,p,?]?$SP
69+
EOF
70+
git add -p -- command <command >actual 2>&1 &&
71+
test_cmp expect actual
72+
'
73+
5974
test_expect_success 'setup (initial)' '
6075
echo content >file &&
6176
git add file &&
@@ -232,7 +247,6 @@ test_expect_success 'setup file' '
232247
'
233248

234249
test_expect_success 'setup patch' '
235-
SP=" " &&
236250
NULL="" &&
237251
cat >patch <<-EOF
238252
@@ -1,4 +1,4 @@
@@ -335,13 +349,13 @@ test_expect_success 'different prompts for mode change/deleted' '
335349

336350
test_expect_success 'correct message when there is nothing to do' '
337351
git reset --hard &&
338-
git add -p 2>err &&
339-
test_grep "No changes" err &&
352+
git add -p >out &&
353+
test_grep "No changes" out &&
340354
printf "\\0123" >binary &&
341355
git add binary &&
342356
printf "\\0abc" >binary &&
343-
git add -p 2>err &&
344-
test_grep "Only binary files changed" err
357+
git add -p >out &&
358+
test_grep "Only binary files changed" out
345359
'
346360

347361
test_expect_success 'setup again' '

0 commit comments

Comments
 (0)