add-patch: modify patch_update_file() signature
The function `patch_update_file()` takes the add_p_state struct pointer and the current `struct file_diff` pointer and returns an int. When using the `--no-auto-advance` flag, we want to be able to request the next or previous file from the caller. Modify the function signature to instead take the index of the current `file_diff` and the `add_p_state` struct pointer so that we can compute the `file_diff` from the index while also having access to the file index. This will help us request the next or previous file from the caller. Signed-off-by: Abraham Samuel Adekunle <abrahamadekunle50@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Abraham Samuel Adekunle committed
Feb 14, 2026 at 12:04 UTC
57088095e90a940d232e36594c6fa761f283d35d
1 file changed
+19
-13
add-patch.c
+19
-13
@@ -1441,20 +1441,21 @@ static bool get_first_undecided(const struct file_diff *file_diff, size_t *idx)
1441
return false;
1442
}
1443
1444
-static int patch_update_file(struct add_p_state *s,
1445
- struct file_diff *file_diff)
1444
+static size_t patch_update_file(struct add_p_state *s, size_t idx)
1445
{
1446
size_t hunk_index = 0;
1447
ssize_t i, undecided_previous, undecided_next, rendered_hunk_index = -1;
1448
struct hunk *hunk;
1449
char ch;
1450
struct child_process cp = CHILD_PROCESS_INIT;
1452
- int colored = !!s->colored.len, quit = 0, use_pager = 0;
1451
+ int colored = !!s->colored.len, use_pager = 0;
1452
enum prompt_mode_type prompt_mode_type;
1453
+ struct file_diff *file_diff = s->file_diff + idx;
1454
+ size_t patch_update_resp = idx;
1455
1456
/* Empty added files have no hunks */
1457
if (!file_diff->hunk_nr && !file_diff->added)
1457
- return 0;
1458
+ return patch_update_resp + 1;
1459
1460
strbuf_reset(&s->buf);
1461
render_diff_header(s, file_diff, colored, &s->buf);
@@ -1498,9 +1499,10 @@ static int patch_update_file(struct add_p_state *s,
1499
1500
/* Everything decided? */
1501
if (undecided_previous < 0 && undecided_next < 0 &&
1501
- hunk->use != UNDECIDED_HUNK)
1502
- break;
1503
-
1502
+ hunk->use != UNDECIDED_HUNK) {
1503
+ patch_update_resp++;
1504
+ break;
1505
+ }
1506
strbuf_reset(&s->buf);
1507
if (file_diff->hunk_nr) {
1508
if (rendered_hunk_index != hunk_index) {
@@ -1570,7 +1572,7 @@ static int patch_update_file(struct add_p_state *s,
1572
fputs(s->s.reset_color_interactive, stdout);
1573
fflush(stdout);
1574
if (read_single_character(s) == EOF) {
1573
- quit = 1;
1575
+ patch_update_resp = s->file_diff_nr;
1576
break;
1577
}
1578
@@ -1616,7 +1618,7 @@ soft_increment:
1618
hunk->use = SKIP_HUNK;
1619
}
1620
} else if (ch == 'q') {
1619
- quit = 1;
1621
+ patch_update_resp = s->file_diff_nr;
1622
break;
1623
} else if (s->answer.buf[0] == 'K') {
1624
if (permitted & ALLOW_GOTO_PREVIOUS_HUNK)
@@ -1803,7 +1805,7 @@ soft_increment:
1805
}
1806
1807
putchar('\n');
1806
- return quit;
1808
+ return patch_update_resp;
1809
}
1810
1811
int run_add_p(struct repository *r, enum add_p_mode mode,
@@ -1852,11 +1854,15 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
1854
return -1;
1855
}
1856
1855
- for (i = 0; i < s.file_diff_nr; i++)
1856
- if (s.file_diff[i].binary && !s.file_diff[i].hunk_nr)
1857
+ for (i = 0; i < s.file_diff_nr;) {
1858
+ if (s.file_diff[i].binary && !s.file_diff[i].hunk_nr) {
1859
binary_count++;
1858
- else if (patch_update_file(&s, s.file_diff + i))
1860
+ i++;
1861
+ continue;
1862
+ }
1863
+ if ((i = patch_update_file(&s, i)) == s.file_diff_nr)
1864
break;
1865
+ }
1866
1867
if (s.file_diff_nr == 0)
1868
err(&s, _("No changes."));