Skip to content

Commit d3f616a

Browse files
committed
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 <[email protected]>
1 parent 80dbfac commit d3f616a

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

add-patch.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,7 @@ static int prompt_yesno(struct add_p_state *s, const char *prompt)
12271227
fflush(stdout);
12281228
if (read_single_character(s) == EOF)
12291229
return -1;
1230+
/* do not limit to 1-byte input to allow 'no' etc. */
12301231
switch (tolower(s->answer.buf[0])) {
12311232
case 'n': return 0;
12321233
case 'y': return 1;
@@ -1510,6 +1511,12 @@ static int patch_update_file(struct add_p_state *s,
15101511
if (!s->answer.len)
15111512
continue;
15121513
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+
}
15131520
if (ch == 'y') {
15141521
hunk->use = USE_HUNK;
15151522
soft_increment:

t/t3701-add-interactive.sh

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,14 @@ test_expect_success 'revert works (commit)' '
160160
grep "unchanged *+3/-0 file" output
161161
'
162162

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+
163171
test_expect_success 'setup expected' '
164172
cat >expected <<-\EOF
165173
EOF
@@ -526,7 +534,7 @@ test_expect_success 'split hunk setup' '
526534
test_write_lines 10 15 20 21 22 23 24 30 40 50 60 >test
527535
'
528536

529-
test_expect_success 'goto hunk' '
537+
test_expect_success 'goto hunk 1 with "g 1"' '
530538
test_when_finished "git reset" &&
531539
tr _ " " >expect <<-EOF &&
532540
(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' '
542550
test_cmp expect actual.trimmed
543551
'
544552

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' '
546567
test_when_finished "git reset" &&
547568
tr _ " " >expect <<-EOF &&
548569
(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' '
556577
test_cmp expect actual.trimmed
557578
'
558579

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+
559593
test_expect_success 'split hunk "add -p (edit)"' '
560594
# Split, say Edit and do nothing. Then:
561595
#

0 commit comments

Comments
 (0)