Skip to content

Commit 1967b60

Browse files
rscharfegitster
authored andcommitted
add-patch: let options k and K roll over like j and J
Options j and J roll over at the bottom and go to the first undecided hunk and hunk 1, respectively. Let options k and K do the same when they reach the top of the hunk array, so let them go to the last undecided hunk and the last hunk, respectively, for consistency. Also use the same direction-neutral error messages. Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 171c168 commit 1967b60

File tree

3 files changed

+37
-29
lines changed

3 files changed

+37
-29
lines changed

Documentation/git-add.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,8 @@ patch::
344344
/ - search for a hunk matching the given regex
345345
j - go to the next undecided hunk, roll over at the bottom
346346
J - go to the next hunk, roll over at the bottom
347-
k - go to the previous undecided hunk
348-
K - go to the previous hunk
347+
k - go to the previous undecided hunk, roll over at the top
348+
K - go to the previous hunk, roll over at the top
349349
s - split the current hunk into smaller hunks
350350
e - manually edit the current hunk
351351
p - print the current hunk

add-patch.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,15 +1399,20 @@ static size_t display_hunks(struct add_p_state *s,
13991399
static const char help_patch_remainder[] =
14001400
N_("j - go to the next undecided hunk, roll over at the bottom\n"
14011401
"J - go to the next hunk, roll over at the bottom\n"
1402-
"k - go to the previous undecided hunk\n"
1403-
"K - go to the previous hunk\n"
1402+
"k - go to the previous undecided hunk, roll over at the top\n"
1403+
"K - go to the previous hunk, roll over at the top\n"
14041404
"g - select a hunk to go to\n"
14051405
"/ - search for a hunk matching the given regex\n"
14061406
"s - split the current hunk into smaller hunks\n"
14071407
"e - manually edit the current hunk\n"
14081408
"p - print the current hunk, 'P' to use the pager\n"
14091409
"? - print help\n");
14101410

1411+
static size_t dec_mod(size_t a, size_t m)
1412+
{
1413+
return a > 0 ? a - 1 : m - 1;
1414+
}
1415+
14111416
static size_t inc_mod(size_t a, size_t m)
14121417
{
14131418
return a < m - 1 ? a + 1 : 0;
@@ -1450,7 +1455,9 @@ static int patch_update_file(struct add_p_state *s,
14501455
undecided_next = -1;
14511456

14521457
if (file_diff->hunk_nr) {
1453-
for (i = hunk_index - 1; i >= 0; i--)
1458+
for (i = dec_mod(hunk_index, file_diff->hunk_nr);
1459+
i != hunk_index;
1460+
i = dec_mod(i, file_diff->hunk_nr))
14541461
if (file_diff->hunk[i].use == UNDECIDED_HUNK) {
14551462
undecided_previous = i;
14561463
break;
@@ -1492,7 +1499,7 @@ static int patch_update_file(struct add_p_state *s,
14921499
permitted |= ALLOW_GOTO_PREVIOUS_UNDECIDED_HUNK;
14931500
strbuf_addstr(&s->buf, ",k");
14941501
}
1495-
if (hunk_index) {
1502+
if (file_diff->hunk_nr > 1) {
14961503
permitted |= ALLOW_GOTO_PREVIOUS_HUNK;
14971504
strbuf_addstr(&s->buf, ",K");
14981505
}
@@ -1584,9 +1591,10 @@ static int patch_update_file(struct add_p_state *s,
15841591
}
15851592
} else if (s->answer.buf[0] == 'K') {
15861593
if (permitted & ALLOW_GOTO_PREVIOUS_HUNK)
1587-
hunk_index--;
1594+
hunk_index = dec_mod(hunk_index,
1595+
file_diff->hunk_nr);
15881596
else
1589-
err(s, _("No previous hunk"));
1597+
err(s, _("No other hunk"));
15901598
} else if (s->answer.buf[0] == 'J') {
15911599
if (permitted & ALLOW_GOTO_NEXT_HUNK)
15921600
hunk_index++;
@@ -1596,7 +1604,7 @@ static int patch_update_file(struct add_p_state *s,
15961604
if (permitted & ALLOW_GOTO_PREVIOUS_UNDECIDED_HUNK)
15971605
hunk_index = undecided_previous;
15981606
else
1599-
err(s, _("No previous hunk"));
1607+
err(s, _("No other undecided hunk"));
16001608
} else if (s->answer.buf[0] == 'j') {
16011609
if (permitted & ALLOW_GOTO_NEXT_UNDECIDED_HUNK)
16021610
hunk_index = undecided_next;

t/t3701-add-interactive.sh

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ test_expect_success 'different prompts for mode change/deleted' '
333333
sed -n "s/^\(([0-9/]*) Stage .*?\).*/\1/p" actual >actual.filtered &&
334334
cat >expect <<-\EOF &&
335335
(1/1) Stage deletion [y,n,q,a,d,p,?]?
336-
(1/2) Stage mode change [y,n,q,a,d,j,J,g,/,p,?]?
336+
(1/2) Stage mode change [y,n,q,a,d,k,K,j,J,g,/,p,?]?
337337
(2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,e,p,?]?
338338
EOF
339339
test_cmp expect actual.filtered
@@ -527,7 +527,7 @@ test_expect_success 'goto hunk 1 with "g 1"' '
527527
_10
528528
+15
529529
_20
530-
(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_
530+
(1/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,p,?]?_
531531
EOF
532532
test_write_lines s y g 1 | git add -p >actual &&
533533
tail -n 7 <actual >actual.trimmed &&
@@ -540,7 +540,7 @@ test_expect_success 'goto hunk 1 with "g1"' '
540540
_10
541541
+15
542542
_20
543-
(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_
543+
(1/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,p,?]?_
544544
EOF
545545
test_write_lines s y g1 | git add -p >actual &&
546546
tail -n 4 <actual >actual.trimmed &&
@@ -554,7 +554,7 @@ test_expect_success 'navigate to hunk via regex /pattern' '
554554
_10
555555
+15
556556
_20
557-
(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_
557+
(1/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,p,?]?_
558558
EOF
559559
test_write_lines s y /1,2 | git add -p >actual &&
560560
tail -n 5 <actual >actual.trimmed &&
@@ -567,7 +567,7 @@ test_expect_success 'navigate to hunk via regex / pattern' '
567567
_10
568568
+15
569569
_20
570-
(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_
570+
(1/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,p,?]?_
571571
EOF
572572
test_write_lines s y / 1,2 | git add -p >actual &&
573573
tail -n 4 <actual >actual.trimmed &&
@@ -579,11 +579,11 @@ test_expect_success 'print again the hunk' '
579579
tr _ " " >expect <<-EOF &&
580580
+15
581581
20
582-
(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? @@ -1,2 +1,3 @@
582+
(1/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,p,?]? @@ -1,2 +1,3 @@
583583
10
584584
+15
585585
20
586-
(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_
586+
(1/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,p,?]?_
587587
EOF
588588
test_write_lines s y g 1 p | git add -p >actual &&
589589
tail -n 7 <actual >actual.trimmed &&
@@ -595,11 +595,11 @@ test_expect_success TTY 'print again the hunk (PAGER)' '
595595
cat >expect <<-EOF &&
596596
<GREEN>+<RESET><GREEN>15<RESET>
597597
20<RESET>
598-
<BOLD;BLUE>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? <RESET>PAGER <CYAN>@@ -1,2 +1,3 @@<RESET>
598+
<BOLD;BLUE>(1/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,p,?]? <RESET>PAGER <CYAN>@@ -1,2 +1,3 @@<RESET>
599599
PAGER 10<RESET>
600600
PAGER <GREEN>+<RESET><GREEN>15<RESET>
601601
PAGER 20<RESET>
602-
<BOLD;BLUE>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? <RESET>
602+
<BOLD;BLUE>(1/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,p,?]? <RESET>
603603
EOF
604604
test_write_lines s y g 1 P |
605605
(
@@ -802,15 +802,15 @@ test_expect_success 'colors can be overridden' '
802802
<BOLD>-old<RESET>
803803
<BLUE>+<RESET><BLUE>new<RESET>
804804
<CYAN> more-context<RESET>
805-
<YELLOW>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? <RESET><MAGENTA>@@ -3 +3,2 @@<RESET>
805+
<YELLOW>(1/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,p,?]? <RESET><MAGENTA>@@ -3 +3,2 @@<RESET>
806806
<CYAN> more-context<RESET>
807807
<BLUE>+<RESET><BLUE>another-one<RESET>
808808
<YELLOW>(2/2) Stage this hunk [y,n,q,a,d,K,J,g,/,e,p,?]? <RESET><MAGENTA>@@ -1,3 +1,3 @@<RESET>
809809
<CYAN> context<RESET>
810810
<BOLD>-old<RESET>
811811
<BLUE>+new<RESET>
812812
<CYAN> more-context<RESET>
813-
<YELLOW>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]? <RESET>
813+
<YELLOW>(1/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,p,?]? <RESET>
814814
EOF
815815
test_cmp expect actual
816816
'
@@ -1354,34 +1354,34 @@ do
13541354
'
13551355
done
13561356

1357-
test_expect_success 'option J rolls over' '
1357+
test_expect_success 'options J, K roll over' '
13581358
test_write_lines a b c d e f g h i >file &&
13591359
git add file &&
13601360
test_write_lines X b c d e f g h X >file &&
1361-
test_write_lines J J q | git add -p >out &&
1362-
test_write_lines 1 2 1 >expect &&
1361+
test_write_lines J J K q | git add -p >out &&
1362+
test_write_lines 1 2 1 2 >expect &&
13631363
sed -n -e "s-/.*--" -e "s/^(//p" <out >actual &&
13641364
test_cmp expect actual
13651365
'
13661366

1367-
test_expect_success 'options y, n, j, e roll over to next undecided (1)' '
1367+
test_expect_success 'options y, n, j, k, e roll over to next undecided (1)' '
13681368
test_write_lines a b c d e f g h i j k l m n o p q >file &&
13691369
git add file &&
13701370
test_write_lines X b c d e f g h X j k l m n o p X >file &&
13711371
test_set_editor : &&
1372-
test_write_lines g3 y g3 n g3 j g3 e q | git add -p >out &&
1373-
test_write_lines 1 3 1 3 1 3 1 3 1 >expect &&
1372+
test_write_lines g3 y g3 n g3 j g3 e k q | git add -p >out &&
1373+
test_write_lines 1 3 1 3 1 3 1 3 1 2 >expect &&
13741374
sed -n -e "s-/.*--" -e "s/^(//p" <out >actual &&
13751375
test_cmp expect actual
13761376
'
13771377

1378-
test_expect_success 'options y, n, j, e roll over to next undecided (2)' '
1378+
test_expect_success 'options y, n, j, k, e roll over to next undecided (2)' '
13791379
test_write_lines a b c d e f g h i j k l m n o p q >file &&
13801380
git add file &&
13811381
test_write_lines X b c d e f g h X j k l m n o p X >file &&
13821382
test_set_editor : &&
1383-
test_write_lines y g3 y g3 n g3 j g3 e q | git add -p >out &&
1384-
test_write_lines 1 2 3 2 3 2 3 2 3 2 >expect &&
1383+
test_write_lines y g3 y g3 n g3 j g3 e g1 k q | git add -p >out &&
1384+
test_write_lines 1 2 3 2 3 2 3 2 3 2 1 2 >expect &&
13851385
sed -n -e "s-/.*--" -e "s/^(//p" <out >actual &&
13861386
test_cmp expect actual
13871387
'

0 commit comments

Comments
 (0)