Skip to content

Commit 7458cd5

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 7458cd5

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

add-patch.c

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ 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+
va_end(args);
47+
}
48+
3749
static void setup_child_process(struct child_process *cp,
3850
struct add_p_state *s, ...)
3951
{
@@ -364,17 +376,27 @@ static int patch_update_file(struct add_p_state *s)
364376
if (hunk->use == UNDECIDED_HUNK)
365377
hunk->use = SKIP_HUNK;
366378
}
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
379+
} else if (s->answer.buf[0] == 'K') {
380+
if (hunk_index)
381+
hunk_index--;
382+
else
383+
err(s, _("No previous hunk"));
384+
} else if (s->answer.buf[0] == 'J') {
385+
if (hunk_index + 1 < s->hunk_nr)
386+
hunk_index++;
387+
else
388+
err(s, _("No next hunk"));
389+
} else if (s->answer.buf[0] == 'k') {
390+
if (undecided_previous >= 0)
391+
hunk_index = undecided_previous;
392+
else
393+
err(s, _("No previous hunk"));
394+
} else if (s->answer.buf[0] == 'j') {
395+
if (undecided_next >= 0)
396+
hunk_index = undecided_next;
397+
else
398+
err(s, _("No next hunk"));
399+
} else
378400
color_fprintf(stdout, s->s.help_color,
379401
_(help_patch_text));
380402
}

0 commit comments

Comments
 (0)