Skip to content

Commit 7c75f74

Browse files
committed
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 <[email protected]>
1 parent bd92652 commit 7c75f74

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

add-patch.c

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ struct add_p_state {
3434
size_t hunk_nr, hunk_alloc;
3535
};
3636

37+
static void err(struct add_p_state *s, const char *fmt, ...)
38+
{
39+
va_list args;
40+
41+
va_start(args, fmt);
42+
fputs(s->s.error_color, stderr);
43+
vfprintf(stderr, fmt, args);
44+
fputs(s->s.reset_color, stderr);
45+
fputc('\n', stderr);
46+
}
47+
3748
static void setup_child_process(struct child_process *cp,
3849
struct add_p_state *s, ...)
3950
{
@@ -364,17 +375,27 @@ static int patch_update_file(struct add_p_state *s)
364375
if (hunk->use == UNDECIDED_HUNK)
365376
hunk->use = SKIP_HUNK;
366377
}
367-
} else if (hunk_index && s->answer.buf[0] == 'K')
368-
hunk_index--;
369-
else if (hunk_index + 1 < s->hunk_nr &&
370-
s->answer.buf[0] == 'J')
371-
hunk_index++;
372-
else if (undecided_previous >= 0 &&
373-
s->answer.buf[0] == 'k')
374-
hunk_index = undecided_previous;
375-
else if (undecided_next >= 0 && s->answer.buf[0] == 'j')
376-
hunk_index = undecided_next;
377-
else
378+
} else if (s->answer.buf[0] == 'K') {
379+
if (hunk_index)
380+
hunk_index--;
381+
else
382+
err(s, _("No previous hunk"));
383+
} else if (s->answer.buf[0] == 'J') {
384+
if (hunk_index + 1 < s->hunk_nr)
385+
hunk_index++;
386+
else
387+
err(s, _("No next hunk"));
388+
} else if (s->answer.buf[0] == 'k') {
389+
if (undecided_previous >= 0)
390+
hunk_index = undecided_previous;
391+
else
392+
err(s, _("No previous hunk"));
393+
} else if (s->answer.buf[0] == 'j') {
394+
if (undecided_next >= 0)
395+
hunk_index = undecided_next;
396+
else
397+
err(s, _("No next hunk"));
398+
} else
378399
color_fprintf(stdout, s->s.help_color,
379400
_(help_patch_text));
380401
}

0 commit comments

Comments
 (0)