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>
Johannes Schindelin committed
Dec 13, 2019 at 08:07 UTC
7584dd3c66012e327f9cd7589d4f95ab2373cd47
1 file changed
+33
-11
add-patch.c
+33
-11
@@ -34,6 +34,18 @@ struct add_p_state {
34
size_t hunk_nr, hunk_alloc;
35
};
36
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
+
49
static void setup_child_process(struct add_p_state *s,
50
struct child_process *cp, ...)
51
{
@@ -368,17 +380,27 @@ soft_increment:
380
if (hunk->use == UNDECIDED_HUNK)
381
hunk->use = SKIP_HUNK;
382
}
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
404
color_fprintf(stdout, s->s.help_color,
405
_(help_patch_text));
406
}