Skip to content

Commit ec69170

Browse files
devdekunlegitster
authored andcommitted
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>
1 parent 3a160f8 commit ec69170

1 file changed

Lines changed: 22 additions & 13 deletions

File tree

add-patch.c

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,20 +1441,21 @@ static bool get_first_undecided(const struct file_diff *file_diff, size_t *idx)
14411441
return false;
14421442
}
14431443

1444-
static int patch_update_file(struct add_p_state *s,
1445-
struct file_diff *file_diff)
1444+
static ssize_t patch_update_file(struct add_p_state *s, size_t idx)
14461445
{
14471446
size_t hunk_index = 0;
14481447
ssize_t i, undecided_previous, undecided_next, rendered_hunk_index = -1;
14491448
struct hunk *hunk;
14501449
char ch;
14511450
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;
14531452
enum prompt_mode_type prompt_mode_type;
1453+
struct file_diff *file_diff = s->file_diff + idx;
1454+
ssize_t patch_update_resp = (ssize_t)idx;
14541455

14551456
/* Empty added files have no hunks */
14561457
if (!file_diff->hunk_nr && !file_diff->added)
1457-
return 0;
1458+
return patch_update_resp + 1;
14581459

14591460
strbuf_reset(&s->buf);
14601461
render_diff_header(s, file_diff, colored, &s->buf);
@@ -1498,9 +1499,10 @@ static int patch_update_file(struct add_p_state *s,
14981499

14991500
/* Everything decided? */
15001501
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+
}
15041506
strbuf_reset(&s->buf);
15051507
if (file_diff->hunk_nr) {
15061508
if (rendered_hunk_index != hunk_index) {
@@ -1570,7 +1572,7 @@ static int patch_update_file(struct add_p_state *s,
15701572
fputs(s->s.reset_color_interactive, stdout);
15711573
fflush(stdout);
15721574
if (read_single_character(s) == EOF) {
1573-
quit = 1;
1575+
patch_update_resp = -1;
15741576
break;
15751577
}
15761578

@@ -1616,7 +1618,7 @@ static int patch_update_file(struct add_p_state *s,
16161618
hunk->use = SKIP_HUNK;
16171619
}
16181620
} else if (ch == 'q') {
1619-
quit = 1;
1621+
patch_update_resp = -1;
16201622
break;
16211623
} else if (s->answer.buf[0] == 'K') {
16221624
if (permitted & ALLOW_GOTO_PREVIOUS_HUNK)
@@ -1803,7 +1805,7 @@ static int patch_update_file(struct add_p_state *s,
18031805
}
18041806

18051807
putchar('\n');
1806-
return quit;
1808+
return patch_update_resp;
18071809
}
18081810

18091811
int run_add_p(struct repository *r, enum add_p_mode mode,
@@ -1814,6 +1816,7 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
18141816
{ r }, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT, STRBUF_INIT
18151817
};
18161818
size_t i, binary_count = 0;
1819+
ssize_t patch_update_resp;
18171820

18181821
init_add_i_state(&s.s, r, o);
18191822

@@ -1852,11 +1855,17 @@ int run_add_p(struct repository *r, enum add_p_mode mode,
18521855
return -1;
18531856
}
18541857

1855-
for (i = 0; i < s.file_diff_nr; i++)
1856-
if (s.file_diff[i].binary && !s.file_diff[i].hunk_nr)
1858+
for (i = 0; i < s.file_diff_nr;) {
1859+
if (s.file_diff[i].binary && !s.file_diff[i].hunk_nr) {
18571860
binary_count++;
1858-
else if (patch_update_file(&s, s.file_diff + i))
1861+
i++;
1862+
continue;
1863+
}
1864+
patch_update_resp = patch_update_file(&s, i);
1865+
if (patch_update_resp < 0)
18591866
break;
1867+
i = (size_t)patch_update_resp;
1868+
}
18601869

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

0 commit comments

Comments
 (0)