Skip to content

Commit 54d9d9b

Browse files
dschogitster
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]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ade246e commit 54d9d9b

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
@@ -1008,8 +1008,10 @@ N_("y - stage this hunk\n"
10081008
"n - do not stage this hunk\n"
10091009
"q - quit; do not stage this hunk or any of the remaining ones\n"
10101010
"a - stage this and all the remaining hunks\n"
1011-
"d - do not stage this hunk nor any of the remaining hunks\n"
1012-
"j - leave this hunk undecided, see next undecided hunk\n"
1011+
"d - do not stage this hunk nor any of the remaining hunks\n");
1012+
1013+
static const char help_patch_remainder[] =
1014+
N_("j - leave this hunk undecided, see next undecided hunk\n"
10131015
"J - leave this hunk undecided, see next hunk\n"
10141016
"k - leave this hunk undecided, see previous undecided hunk\n"
10151017
"K - leave this hunk undecided, see previous hunk\n"
@@ -1246,9 +1248,31 @@ static int patch_update_file(struct add_p_state *s,
12461248
hunk->use = USE_HUNK;
12471249
goto soft_increment;
12481250
}
1249-
} else
1250-
color_fprintf(stdout, s->s.help_color,
1251+
} else {
1252+
const char *p = _(help_patch_remainder), *eol = p;
1253+
1254+
color_fprintf(stdout, s->s.help_color, "%s",
12511255
_(help_patch_text));
1256+
1257+
/*
1258+
* Show only those lines of the remainder that are
1259+
* actually applicable with the current hunk.
1260+
*/
1261+
for (; *p; p = eol + (*eol == '\n')) {
1262+
eol = strchrnul(p, '\n');
1263+
1264+
/*
1265+
* `s->buf` still contains the part of the
1266+
* commands shown in the prompt that are not
1267+
* always available.
1268+
*/
1269+
if (*p != '?' && !strchr(s->buf.buf, *p))
1270+
continue;
1271+
1272+
color_fprintf_ln(stdout, s->s.help_color,
1273+
"%.*s", (int)(eol - p), p);
1274+
}
1275+
}
12521276
}
12531277

12541278
/* Any hunk to be used? */

0 commit comments

Comments
 (0)