Skip to content

Commit 7584dd3

Browse files
dschogitster
authored andcommitted
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]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 12c24cf commit 7584dd3

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 add_p_state *s,
3850
struct child_process *cp, ...)
3951
{
@@ -368,17 +380,27 @@ static int patch_update_file(struct add_p_state *s)
368380
if (hunk->use == UNDECIDED_HUNK)
369381
hunk->use = SKIP_HUNK;
370382
}
371-
} else if (hunk_index && s->answer.buf[0] == 'K')
372-
hunk_index--;
373-
else if (hunk_index + 1 < s->hunk_nr &&
374-
s->answer.buf[0] == 'J')
375-
hunk_index++;
376-
else if (undecided_previous >= 0 &&
377-
s->answer.buf[0] == 'k')
378-
hunk_index = undecided_previous;
379-
else if (undecided_next >= 0 && s->answer.buf[0] == 'j')
380-
hunk_index = undecided_next;
381-
else
383+
} else if (s->answer.buf[0] == 'K') {
384+
if (hunk_index)
385+
hunk_index--;
386+
else
387+
err(s, _("No previous hunk"));
388+
} else if (s->answer.buf[0] == 'J') {
389+
if (hunk_index + 1 < s->hunk_nr)
390+
hunk_index++;
391+
else
392+
err(s, _("No next hunk"));
393+
} else if (s->answer.buf[0] == 'k') {
394+
if (undecided_previous >= 0)
395+
hunk_index = undecided_previous;
396+
else
397+
err(s, _("No previous hunk"));
398+
} else if (s->answer.buf[0] == 'j') {
399+
if (undecided_next >= 0)
400+
hunk_index = undecided_next;
401+
else
402+
err(s, _("No next hunk"));
403+
} else
382404
color_fprintf(stdout, s->s.help_color,
383405
_(help_patch_text));
384406
}

0 commit comments

Comments
 (0)