Skip to content

Commit 9cb6073

Browse files
dschoGit for Windows Build Agent
authored andcommitted
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 ceb4a7d commit 9cb6073

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
@@ -962,8 +962,10 @@ N_("y - stage this hunk\n"
962962
"n - do not stage this hunk\n"
963963
"q - quit; do not stage this hunk or any of the remaining ones\n"
964964
"a - stage this and all the remaining hunks\n"
965-
"d - do not stage this hunk nor any of the remaining hunks\n"
966-
"j - leave this hunk undecided, see next undecided hunk\n"
965+
"d - do not stage this hunk nor any of the remaining hunks\n");
966+
967+
static const char help_patch_remainder[] =
968+
N_("j - leave this hunk undecided, see next undecided hunk\n"
967969
"J - leave this hunk undecided, see next hunk\n"
968970
"k - leave this hunk undecided, see previous undecided hunk\n"
969971
"K - leave this hunk undecided, see previous hunk\n"
@@ -1196,9 +1198,31 @@ static int patch_update_file(struct add_p_state *s,
11961198
hunk->use = USE_HUNK;
11971199
goto soft_increment;
11981200
}
1199-
} else
1200-
color_fprintf(stdout, s->s.help_color,
1201+
} else {
1202+
const char *p = _(help_patch_remainder), *eol = p;
1203+
1204+
color_fprintf(stdout, s->s.help_color, "%s",
12011205
_(help_patch_text));
1206+
1207+
/*
1208+
* Show only those lines of the remainder that are
1209+
* actually applicable with the current hunk.
1210+
*/
1211+
for (; *p; p = eol + (*eol == '\n')) {
1212+
eol = strchrnul(p, '\n');
1213+
1214+
/*
1215+
* `s->buf` still contains the part of the
1216+
* commands shown in the prompt that are not
1217+
* always available.
1218+
*/
1219+
if (*p != '?' && !strchr(s->buf.buf, *p))
1220+
continue;
1221+
1222+
color_fprintf_ln(stdout, s->s.help_color,
1223+
"%.*s", (int)(eol - p), p);
1224+
}
1225+
}
12021226
}
12031227

12041228
/* Any hunk to be used? */

0 commit comments

Comments
 (0)