Skip to content

Commit d019b80

Browse files
committed
Merge branch 'jc/add-patch-enforce-single-letter-input'
"git add -p" learned to complain when an answer with more than one letter is given to a prompt that expects a single letter answer. * jc/add-patch-enforce-single-letter-input: add-patch: enforce only one-letter response to prompts
2 parents 99d3cbe + d3f616a commit d019b80

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)