add-patch: enforce only one-letter response to prompts

In a "git add -p" session, especially when we are not using the single-key mode, we may see 'qa' as a response to a prompt (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? and then just do the 'q' thing (i.e. quit the session), ignoring everything other than the first byte. If 'q' and 'a' are next to each other on the user's keyboard, there is a plausible chance that we see 'qa' when the user who wanted to say 'a' fat-fingered and we ended up doing the 'q' thing instead. As we didn't think of a good reason during the review discussion why we want to accept excess letters only to ignore them, it appears to be a safe change to simply reject input that is longer than just one byte. The two exceptions are the 'g' command that takes a hunk number, and the '/' command that takes a regular expression. They have to be accompanied by their operands (this makes me wonder how users who set the interactive.singlekey configuration feed these operands---it turns out that we notice there is no operand and give them another chance to type the operand separately, without using single key input this time), so we accept a string that is more than one byte long. Keep the "use only the first byte, downcased" behaviour when we ask yes/no question, though. Neither on Qwerty or on Dvorak, 'y' and 'n' are not close to each other. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed May 22, 2024 at 14:45 UTC d3f616a4e56f359d84a9d439aa03dca1fe9ac280
2 files changed +43 -2
add-patch.c
+7
@@ -1227,6 +1227,7 @@ static int prompt_yesno(struct add_p_state *s, const char *prompt)
1227 fflush(stdout);
1228 if (read_single_character(s) == EOF)
1229 return -1;
1230 + /* do not limit to 1-byte input to allow 'no' etc. */
1231 switch (tolower(s->answer.buf[0])) {
1232 case 'n': return 0;
1233 case 'y': return 1;
@@ -1510,6 +1511,12 @@ static int patch_update_file(struct add_p_state *s,
1511 if (!s->answer.len)
1512 continue;
1513 ch = tolower(s->answer.buf[0]);
1514 +
1515 + /* 'g' takes a hunk number and '/' takes a regexp */
1516 + if (s->answer.len != 1 && (ch != 'g' && ch != '/')) {
1517 + err(s, _("Only one letter is expected, got '%s'"), s->answer.buf);
1518 + continue;
1519 + }
1520 if (ch == 'y') {
1521 hunk->use = USE_HUNK;
1522 soft_increment:
t/t3701-add-interactive.sh
+36 -2
@@ -160,6 +160,14 @@ test_expect_success 'revert works (commit)' '
160 grep "unchanged *+3/-0 file" output
161 '
162
163 +test_expect_success 'reject multi-key input' '
164 + saved=$(git hash-object -w file) &&
165 + test_when_finished "git cat-file blob $saved >file" &&
166 + echo an extra line >>file &&
167 + test_write_lines aa | git add -p >actual &&
168 + test_grep "is expected, got ${SQ}aa${SQ}" actual
169 +'
170 +
171 test_expect_success 'setup expected' '
172 cat >expected <<-\EOF
173 EOF
@@ -526,7 +534,7 @@ test_expect_success 'split hunk setup' '
534 test_write_lines 10 15 20 21 22 23 24 30 40 50 60 >test
535 '
536
529 -test_expect_success 'goto hunk' '
537 +test_expect_success 'goto hunk 1 with "g 1"' '
538 test_when_finished "git reset" &&
539 tr _ " " >expect <<-EOF &&
540 (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? + 1: -1,2 +1,3 +15
@@ -542,7 +550,20 @@ test_expect_success 'goto hunk' '
550 test_cmp expect actual.trimmed
551 '
552
545 -test_expect_success 'navigate to hunk via regex' '
553 +test_expect_success 'goto hunk 1 with "g1"' '
554 + test_when_finished "git reset" &&
555 + tr _ " " >expect <<-EOF &&
556 + _10
557 + +15
558 + _20
559 + (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_
560 + EOF
561 + test_write_lines s y g1 | git add -p >actual &&
562 + tail -n 4 <actual >actual.trimmed &&
563 + test_cmp expect actual.trimmed
564 +'
565 +
566 +test_expect_success 'navigate to hunk via regex /pattern' '
567 test_when_finished "git reset" &&
568 tr _ " " >expect <<-EOF &&
569 (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? @@ -1,2 +1,3 @@
@@ -556,6 +577,19 @@ test_expect_success 'navigate to hunk via regex' '
577 test_cmp expect actual.trimmed
578 '
579
580 +test_expect_success 'navigate to hunk via regex / pattern' '
581 + test_when_finished "git reset" &&
582 + tr _ " " >expect <<-EOF &&
583 + _10
584 + +15
585 + _20
586 + (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_
587 + EOF
588 + test_write_lines s y / 1,2 | git add -p >actual &&
589 + tail -n 4 <actual >actual.trimmed &&
590 + test_cmp expect actual.trimmed
591 +'
592 +
593 test_expect_success 'split hunk "add -p (edit)"' '
594 # Split, say Edit and do nothing. Then:
595 #