Skip to content

Commit 171c168

Browse files
rscharfegitster
authored andcommitted
add-patch: let options y, n, j, and e roll over to next undecided
The options y, n, and e mark the current hunk as decided. If there's another undecided hunk towards the bottom of the hunk array they go there. If there isn't, but there is another undecided hunk towards the top then they go to the very first hunk, no matter if it has already been decided on. The option j does basically the same move. Technically it is not allowed if there's no undecided hunk towards the bottom, but the variable "permitted" is never reset, so this permission is retained from the very first hunk. That may a bug, but this behavior is at least consistent with y, n, and e and arguably more useful than refusing to move. Improve the roll-over behavior of these four options by moving to the first undecided hunk instead of hunk 1, consistent with what they do when not rolling over. Also adjust the error message for j, as it will only be shown if there's no other undecided hunk in either direction. Reported-by: Windl, Ulrich <u.windl@ukr.de> Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent c309b65 commit 171c168

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

Documentation/git-add.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ patch::
342342
d - do not stage this hunk or any of the later hunks in the file
343343
g - select a hunk to go to
344344
/ - search for a hunk matching the given regex
345-
j - go to the next undecided hunk
345+
j - go to the next undecided hunk, roll over at the bottom
346346
J - go to the next hunk, roll over at the bottom
347347
k - go to the previous undecided hunk
348348
K - go to the previous hunk

add-patch.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,7 +1397,7 @@ static size_t display_hunks(struct add_p_state *s,
13971397
}
13981398

13991399
static const char help_patch_remainder[] =
1400-
N_("j - go to the next undecided hunk\n"
1400+
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"
14021402
"k - go to the previous undecided hunk\n"
14031403
"K - go to the previous hunk\n"
@@ -1408,6 +1408,11 @@ N_("j - go to the next undecided hunk\n"
14081408
"p - print the current hunk, 'P' to use the pager\n"
14091409
"? - print help\n");
14101410

1411+
static size_t inc_mod(size_t a, size_t m)
1412+
{
1413+
return a < m - 1 ? a + 1 : 0;
1414+
}
1415+
14111416
static int patch_update_file(struct add_p_state *s,
14121417
struct file_diff *file_diff)
14131418
{
@@ -1451,7 +1456,9 @@ static int patch_update_file(struct add_p_state *s,
14511456
break;
14521457
}
14531458

1454-
for (i = hunk_index + 1; i < file_diff->hunk_nr; i++)
1459+
for (i = inc_mod(hunk_index, file_diff->hunk_nr);
1460+
i != hunk_index;
1461+
i = inc_mod(i, file_diff->hunk_nr))
14551462
if (file_diff->hunk[i].use == UNDECIDED_HUNK) {
14561463
undecided_next = i;
14571464
break;
@@ -1594,7 +1601,7 @@ static int patch_update_file(struct add_p_state *s,
15941601
if (permitted & ALLOW_GOTO_NEXT_UNDECIDED_HUNK)
15951602
hunk_index = undecided_next;
15961603
else
1597-
err(s, _("No next hunk"));
1604+
err(s, _("No other undecided hunk"));
15981605
} else if (s->answer.buf[0] == 'g') {
15991606
char *pend;
16001607
unsigned long response;

t/t3701-add-interactive.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,4 +1364,26 @@ test_expect_success 'option J rolls over' '
13641364
test_cmp expect actual
13651365
'
13661366

1367+
test_expect_success 'options y, n, j, e roll over to next undecided (1)' '
1368+
test_write_lines a b c d e f g h i j k l m n o p q >file &&
1369+
git add file &&
1370+
test_write_lines X b c d e f g h X j k l m n o p X >file &&
1371+
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 &&
1374+
sed -n -e "s-/.*--" -e "s/^(//p" <out >actual &&
1375+
test_cmp expect actual
1376+
'
1377+
1378+
test_expect_success 'options y, n, j, e roll over to next undecided (2)' '
1379+
test_write_lines a b c d e f g h i j k l m n o p q >file &&
1380+
git add file &&
1381+
test_write_lines X b c d e f g h X j k l m n o p X >file &&
1382+
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 &&
1385+
sed -n -e "s-/.*--" -e "s/^(//p" <out >actual &&
1386+
test_cmp expect actual
1387+
'
1388+
13671389
test_done

0 commit comments

Comments
 (0)