Skip to content

Commit 1bd8c11

Browse files
committed
built-in add -p: only show the applicable parts of the help text
When displaying the only hunk in a file's diff, the prompt already excludes the commands to navigate to the previous/next hunk. Let's also let the `?` command show only the help lines corresponding to the commands that are displayed in the prompt. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent fdae5e5 commit 1bd8c11

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

add-patch.c

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -955,8 +955,10 @@ N_("y - stage this hunk\n"
955955
"n - do not stage this hunk\n"
956956
"q - quit; do not stage this hunk or any of the remaining ones\n"
957957
"a - stage this and all the remaining hunks\n"
958-
"d - do not stage this hunk nor any of the remaining hunks\n"
959-
"j - leave this hunk undecided, see next undecided hunk\n"
958+
"d - do not stage this hunk nor any of the remaining hunks\n");
959+
960+
static const char help_patch_remainder[] =
961+
N_("j - leave this hunk undecided, see next undecided hunk\n"
960962
"J - leave this hunk undecided, see next hunk\n"
961963
"k - leave this hunk undecided, see previous undecided hunk\n"
962964
"K - leave this hunk undecided, see previous hunk\n"
@@ -1189,9 +1191,31 @@ static int patch_update_file(struct add_p_state *s,
11891191
hunk->use = USE_HUNK;
11901192
goto soft_increment;
11911193
}
1192-
} else
1193-
color_fprintf(stdout, s->s.help_color,
1194+
} else {
1195+
const char *p = _(help_patch_remainder), *eol = p;
1196+
1197+
color_fprintf(stdout, s->s.help_color, "%s",
11941198
_(help_patch_text));
1199+
1200+
/*
1201+
* Show only those lines of the remainder that are
1202+
* actually applicable with the current hunk.
1203+
*/
1204+
for (; *p; p = eol + (*eol == '\n')) {
1205+
eol = strchrnul(p, '\n');
1206+
1207+
/*
1208+
* `s->buf` still contains the part of the
1209+
* commands shown in the prompt that are not
1210+
* always available.
1211+
*/
1212+
if (*p != '?' && !strchr(s->buf.buf, *p))
1213+
continue;
1214+
1215+
color_fprintf_ln(stdout, s->s.help_color,
1216+
"%.*s", (int)(eol - p), p);
1217+
}
1218+
}
11951219
}
11961220

11971221
/* Any hunk to be used? */

0 commit comments

Comments
 (0)