built-in add -p: offer a helpful error message when hunk navigation failed

... just like the Perl version currently does...

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Johannes Schindelin
2019-12-13 08:07:53 +00:00
committed by Junio C Hamano
parent 12c24cf850
commit 7584dd3c66

View File

@ -34,6 +34,18 @@ struct add_p_state {
size_t hunk_nr, hunk_alloc; size_t hunk_nr, hunk_alloc;
}; };
static void err(struct add_p_state *s, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
fputs(s->s.error_color, stderr);
vfprintf(stderr, fmt, args);
fputs(s->s.reset_color, stderr);
fputc('\n', stderr);
va_end(args);
}
static void setup_child_process(struct add_p_state *s, static void setup_child_process(struct add_p_state *s,
struct child_process *cp, ...) struct child_process *cp, ...)
{ {
@ -368,17 +380,27 @@ soft_increment:
if (hunk->use == UNDECIDED_HUNK) if (hunk->use == UNDECIDED_HUNK)
hunk->use = SKIP_HUNK; hunk->use = SKIP_HUNK;
} }
} else if (hunk_index && s->answer.buf[0] == 'K') } else if (s->answer.buf[0] == 'K') {
hunk_index--; if (hunk_index)
else if (hunk_index + 1 < s->hunk_nr && hunk_index--;
s->answer.buf[0] == 'J') else
hunk_index++; err(s, _("No previous hunk"));
else if (undecided_previous >= 0 && } else if (s->answer.buf[0] == 'J') {
s->answer.buf[0] == 'k') if (hunk_index + 1 < s->hunk_nr)
hunk_index = undecided_previous; hunk_index++;
else if (undecided_next >= 0 && s->answer.buf[0] == 'j') else
hunk_index = undecided_next; err(s, _("No next hunk"));
else } else if (s->answer.buf[0] == 'k') {
if (undecided_previous >= 0)
hunk_index = undecided_previous;
else
err(s, _("No previous hunk"));
} else if (s->answer.buf[0] == 'j') {
if (undecided_next >= 0)
hunk_index = undecided_next;
else
err(s, _("No next hunk"));
} else
color_fprintf(stdout, s->s.help_color, color_fprintf(stdout, s->s.help_color,
_(help_patch_text)); _(help_patch_text));
} }