Skip to content

Commit fbc2722

Browse files
devdekunlegitster
authored andcommitted
add-patch: allow interfile navigation when selecting hunks
After deciding on all hunks in a file, the interactive session advances automatically to the next file if there is another, or the process ends. Now using the `--no-auto-advance` flag with `--patch`, the process does not advance automatically. A user can choose to go to the next file by pressing '>' or the previous file by pressing '<', before or after deciding on all hunks in the current file. After all hunks have been decided in a file, the user can still rework with the file by applying the options available in the permit set for that hunk, and after all the decisions, the user presses 'q' to submit. After all hunks have been decided, the user can press '?' which will show the hunk selection summary in the help patch remainder text including the total hunks, number of hunks marked for use and number of hunks marked for skip. This feature is enabled by passing the `--no-auto-advance` flag to `--patch` option of the subcommands add, stash, reset, and checkout. Signed-off-by: Abraham Samuel Adekunle <abrahamadekunle50@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 192d538 commit fbc2722

File tree

2 files changed

+161
-5
lines changed

2 files changed

+161
-5
lines changed

add-patch.c

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,7 +1418,10 @@ N_("j - go to the next undecided hunk, roll over at the bottom\n"
14181418
"e - manually edit the current hunk\n"
14191419
"p - print the current hunk\n"
14201420
"P - print the current hunk using the pager\n"
1421-
"? - print help\n");
1421+
"> - go to the next file, roll over at the bottom\n"
1422+
"< - go to the previous file, roll over at the top\n"
1423+
"? - print help\n"
1424+
"HUNKS SUMMARY - Hunks: %d, USE: %d, SKIP: %d\n");
14221425

14231426
static void apply_patch(struct add_p_state *s, struct file_diff *file_diff)
14241427
{
@@ -1483,6 +1486,7 @@ static ssize_t patch_update_file(struct add_p_state *s, size_t idx)
14831486
char ch;
14841487
int colored = !!s->colored.len, use_pager = 0;
14851488
enum prompt_mode_type prompt_mode_type;
1489+
int all_decided = 0;
14861490
struct file_diff *file_diff = s->file_diff + idx;
14871491
ssize_t patch_update_resp = (ssize_t)idx;
14881492

@@ -1501,7 +1505,9 @@ static ssize_t patch_update_file(struct add_p_state *s, size_t idx)
15011505
ALLOW_GOTO_NEXT_UNDECIDED_HUNK = 1 << 3,
15021506
ALLOW_SEARCH_AND_GOTO = 1 << 4,
15031507
ALLOW_SPLIT = 1 << 5,
1504-
ALLOW_EDIT = 1 << 6
1508+
ALLOW_EDIT = 1 << 6,
1509+
ALLOW_GOTO_PREVIOUS_FILE = 1 << 7,
1510+
ALLOW_GOTO_NEXT_FILE = 1 << 8
15051511
} permitted = 0;
15061512

15071513
if (hunk_index >= file_diff->hunk_nr)
@@ -1533,8 +1539,12 @@ static ssize_t patch_update_file(struct add_p_state *s, size_t idx)
15331539
/* Everything decided? */
15341540
if (undecided_previous < 0 && undecided_next < 0 &&
15351541
hunk->use != UNDECIDED_HUNK) {
1536-
patch_update_resp++;
1537-
break;
1542+
if (!s->s.auto_advance)
1543+
all_decided = 1;
1544+
else {
1545+
patch_update_resp++;
1546+
break;
1547+
}
15381548
}
15391549
strbuf_reset(&s->buf);
15401550
if (file_diff->hunk_nr) {
@@ -1583,6 +1593,14 @@ static ssize_t patch_update_file(struct add_p_state *s, size_t idx)
15831593
permitted |= ALLOW_EDIT;
15841594
strbuf_addstr(&s->buf, ",e");
15851595
}
1596+
if (!s->s.auto_advance && s->file_diff_nr > 1) {
1597+
permitted |= ALLOW_GOTO_NEXT_FILE;
1598+
strbuf_addstr(&s->buf, ",>");
1599+
}
1600+
if (!s->s.auto_advance && s->file_diff_nr > 1) {
1601+
permitted |= ALLOW_GOTO_PREVIOUS_FILE;
1602+
strbuf_addstr(&s->buf, ",<");
1603+
}
15861604
strbuf_addstr(&s->buf, ",p,P");
15871605
}
15881606
if (file_diff->deleted)
@@ -1653,6 +1671,28 @@ static ssize_t patch_update_file(struct add_p_state *s, size_t idx)
16531671
} else if (ch == 'q') {
16541672
patch_update_resp = -1;
16551673
break;
1674+
} else if (!s->s.auto_advance && s->answer.buf[0] == '>') {
1675+
if (permitted & ALLOW_GOTO_NEXT_FILE) {
1676+
if (patch_update_resp == s->file_diff_nr - 1)
1677+
patch_update_resp = 0;
1678+
else
1679+
patch_update_resp++;
1680+
break;
1681+
} else {
1682+
err(s, _("No next file"));
1683+
continue;
1684+
}
1685+
} else if (!s->s.auto_advance && s->answer.buf[0] == '<') {
1686+
if (permitted & ALLOW_GOTO_PREVIOUS_FILE) {
1687+
if (patch_update_resp == 0)
1688+
patch_update_resp = s->file_diff_nr - 1;
1689+
else
1690+
patch_update_resp--;
1691+
break;
1692+
} else {
1693+
err(s, _("No previous file"));
1694+
continue;
1695+
}
16561696
} else if (s->answer.buf[0] == 'K') {
16571697
if (permitted & ALLOW_GOTO_PREVIOUS_HUNK)
16581698
hunk_index = dec_mod(hunk_index,
@@ -1798,6 +1838,18 @@ static ssize_t patch_update_file(struct add_p_state *s, size_t idx)
17981838
* commands shown in the prompt that are not
17991839
* always available.
18001840
*/
1841+
if (all_decided && !strncmp(p, "HUNKS SUMMARY", 13)) {
1842+
int total = file_diff->hunk_nr, used = 0, skipped = 0;
1843+
1844+
for (i = 0; i < file_diff->hunk_nr; i++) {
1845+
if (file_diff->hunk[i].use == USE_HUNK)
1846+
used += 1;
1847+
if (file_diff->hunk[i].use == SKIP_HUNK)
1848+
skipped += 1;
1849+
}
1850+
color_fprintf_ln(stdout, s->s.help_color, _(p),
1851+
total, used, skipped);
1852+
}
18011853
if (*p != '?' && !strchr(s->buf.buf, *p))
18021854
continue;
18031855

@@ -1810,7 +1862,8 @@ static ssize_t patch_update_file(struct add_p_state *s, size_t idx)
18101862
}
18111863
}
18121864

1813-
apply_patch(s, file_diff);
1865+
if (s->s.auto_advance)
1866+
apply_patch(s, file_diff);
18141867

18151868
putchar('\n');
18161869
return patch_update_resp;
@@ -1874,6 +1927,9 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
18741927
break;
18751928
i = (size_t)patch_update_resp;
18761929
}
1930+
if (!s.s.auto_advance)
1931+
for (i = 0; i < s.file_diff_nr; i++)
1932+
apply_patch(&s, s.file_diff + i);
18771933

18781934
if (s.file_diff_nr == 0)
18791935
err(&s, _("No changes."));

t/t3701-add-interactive.sh

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,5 +1441,105 @@ test_expect_success 'EOF quits' '
14411441
test_grep file out &&
14421442
test_grep ! file2 out
14431443
'
1444+
for cmd in add checkout reset "stash save" "stash push"
1445+
do
1446+
test_expect_success "$cmd rejects invalid --no-auto-advance options" '
1447+
test_must_fail git $cmd --no-auto-advance 2>actual &&
1448+
test_grep -E "requires .*--(interactive|patch)" actual
1449+
'
1450+
done
1451+
1452+
test_expect_success 'manual advance (">") moves to next file with --no-auto-advance' '
1453+
git reset --hard &&
1454+
echo line1 >first-file &&
1455+
echo line2 >second-file &&
1456+
git add -A &&
1457+
git commit -m initial >/dev/null 2>&1 &&
1458+
echo change_first >>first-file &&
1459+
echo change_second >>second-file &&
1460+
1461+
printf ">\nq\n" | git add -p --no-auto-advance >output.test 2>&1 &&
1462+
test_grep -E "(a|b)/second-file" output.test
1463+
'
1464+
1465+
test_expect_success 'select n on a hunk, go to another file, come back and change to y stages' '
1466+
git reset --hard &&
1467+
echo one >f1 &&
1468+
echo one >f2 &&
1469+
git add -A &&
1470+
git commit -m initial >/dev/null 2>&1 &&
1471+
echo change1 >>f1 &&
1472+
echo change2 >>f2 &&
1473+
1474+
printf "n\n>\n<\ny\nq\n" | git add -p --no-auto-advance >output.staged 2>&1 &&
1475+
git diff --cached --name-only >staged &&
1476+
test_grep -E "(a/f1)" output.staged
1477+
'
1478+
1479+
test_expect_success 'select y on a hunk, go to another file, come back and change to n does not stage' '
1480+
git reset --hard &&
1481+
echo one >f1 &&
1482+
echo one >f2 &&
1483+
git add -A &&
1484+
git commit -m initial >/dev/null 2>&1 &&
1485+
echo change1 >>f1 &&
1486+
echo change2 >>f2 &&
1487+
1488+
printf "y\n>\n<\nn\nq\n" | git add -p --no-auto-advance >output.unstaged 2>&1 &&
1489+
git diff --cached --name-only >staged &&
1490+
test_must_be_empty staged
1491+
'
1492+
1493+
test_expect_success 'deciding all hunks in a file does not auto advance' '
1494+
git reset --hard &&
1495+
echo line >stay &&
1496+
echo line >other &&
1497+
git add -A &&
1498+
git commit -m initial >/dev/null 2>&1 &&
1499+
echo change >>stay &&
1500+
echo change >>other &&
1501+
test_write_lines y | git add -p --no-auto-advance >raw-output 2>&1 &&
1502+
test_grep "(1/1) Stage this hunk (was: y)" raw-output &&
1503+
test_grep ! "diff --git a/stay b/stay" raw-output
1504+
'
1505+
test_expect_success 'HUNKS SUMMARY does not show in help text when there are undecided hunks' '
1506+
git reset --hard &&
1507+
test_write_lines 1 2 3 4 5 6 7 8 9 >f &&
1508+
git add f &&
1509+
git commit -m initial >/dev/null 2>&1 &&
1510+
test_write_lines 1 X 3 4 Y 6 7 Z 9 >f &&
1511+
test_write_lines s y n | git add -p --no-auto-advance >raw-nostat 2>&1 &&
1512+
test_grep ! "HUNKS SUMMARY - Hunks: " raw-nostat
1513+
'
1514+
1515+
test_expect_success 'help text shows HUNK SUMMARY when all hunks have been decided' '
1516+
git reset --hard &&
1517+
test_write_lines 1 2 3 4 5 6 7 8 9 >f2 &&
1518+
git add f2 &&
1519+
git commit -m initial >/dev/null 2>&1 &&
1520+
test_write_lines 1 X 3 4 Y 6 7 Z 9 >f2 &&
1521+
printf "s\ny\nn\ny\n?\n" | git add -p --no-auto-advance >raw-stat 2>&1 &&
1522+
test_grep "HUNKS SUMMARY - Hunks: 3, USE: 2, SKIP: 1" raw-stat
1523+
'
1524+
1525+
test_expect_success 'selective staging across multiple files with --no-advance' '
1526+
git reset --hard &&
1527+
test_write_lines 1 2 3 4 5 6 7 8 9 >a.file &&
1528+
test_write_lines 1 2 3 4 5 6 7 8 9 >b.file &&
1529+
test_write_lines 1 2 3 4 5 6 7 8 9 >c.file &&
1530+
git add -A &&
1531+
git commit -m initial >/dev/null 2>&1 &&
1532+
test_write_lines 1 A2 3 4 A5 6 7 8 9 >a.file &&
1533+
test_write_lines 1 2 B3 4 5 6 7 B8 9 >b.file &&
1534+
test_write_lines C1 2 3 4 5 C6 7 8 9 >c.file &&
1535+
printf "s\ny\nn\n>\ns\nn\ny\n>\ns\ny\ny\nq\n" | git add -p --no-auto-advance >output.index 2>&1 &&
1536+
git diff --cached >staged.diff &&
1537+
test_grep "+A2" staged.diff &&
1538+
test_grep ! "+A5" staged.diff &&
1539+
test_grep "+B8" staged.diff &&
1540+
test_grep ! "+B3" staged.diff &&
1541+
test_grep "+C1" staged.diff &&
1542+
test_grep "+C6" staged.diff
1543+
'
14441544

14451545
test_done

0 commit comments

Comments
 (0)