Skip to content

Commit 26998ed

Browse files
rjustogitster
authored andcommitted
add-patch: response to unknown command
When the user gives an unknown command to the "add -p" prompt, the list of accepted commands with their explanation is given. This is the same output they get when they say '?'. However, the unknown command may be due to a user input error rather than the user not knowing the valid command. To reduce the likelihood of user confusion and error repetition, instead of displaying the list of accepted commands, display a short error message with the unknown command received, as feedback to the user. Include a reminder about the current command '?' in the new message, to guide the user if they want help. Signed-off-by: Rubén Justo <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9d225b0 commit 26998ed

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

add-patch.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1666,7 +1666,7 @@ static int patch_update_file(struct add_p_state *s,
16661666
}
16671667
} else if (s->answer.buf[0] == 'p') {
16681668
rendered_hunk_index = -1;
1669-
} else {
1669+
} else if (s->answer.buf[0] == '?') {
16701670
const char *p = _(help_patch_remainder), *eol = p;
16711671

16721672
color_fprintf(stdout, s->s.help_color, "%s",
@@ -1690,6 +1690,9 @@ static int patch_update_file(struct add_p_state *s,
16901690
color_fprintf_ln(stdout, s->s.help_color,
16911691
"%.*s", (int)(eol - p), p);
16921692
}
1693+
} else {
1694+
err(s, _("Unknown command '%s' (use '?' for help)"),
1695+
s->answer.buf);
16931696
}
16941697
}
16951698

t/t3701-add-interactive.sh

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
77
. ./test-lib.sh
88
. "$TEST_DIRECTORY"/lib-terminal.sh
99

10+
SP=" "
11+
1012
diff_cmp () {
1113
for x
1214
do
@@ -55,6 +57,19 @@ test_expect_success 'warn about add.interactive.useBuiltin' '
5557
done
5658
'
5759

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

233248
test_expect_success 'setup patch' '
234-
SP=" " &&
235249
NULL="" &&
236250
cat >patch <<-EOF
237251
@@ -1,4 +1,4 @@

0 commit comments

Comments
 (0)