diff mbox series

[2/2] help -a: handle aliases with long names gracefully

Message ID 850cd5d15abe5b8a4356efe86ba81bc032278dce.1544540287.git.gitgitgadget@gmail.com (mailing list archive)
State New, archived
Headers show
Series Fix git help -a with long alias names | expand

Commit Message

Linus Arver via GitGitGadget Dec. 11, 2018, 2:58 p.m. UTC
From: Johannes Schindelin <johannes.schindelin@gmx.de>

We take pains to determine the longest command beforehand, so that we
can align the category column after printing the command names.

However, then we re-use that value when printing the aliases. If any
alias name is longer than the longest command name, we consequently try
to add a negative number of spaces (but `mput_char()` does not expect
any negative values and simply decrements until the value is 0, i.e.
it tries to add close to 2**31 spaces).

Let's fix this by adjusting the `longest` variable before printing the
aliases.

This fixes https://github.com/git-for-windows/git/issues/1975.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 help.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

Comments

Junio C Hamano Dec. 12, 2018, 9:43 a.m. UTC | #1
"Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com>
writes:

> From: Johannes Schindelin <johannes.schindelin@gmx.de>
>
> We take pains to determine the longest command beforehand, so that we
> can align the category column after printing the command names.
>
> However, then we re-use that value when printing the aliases. If any
> alias name is longer than the longest command name, we consequently try
> to add a negative number of spaces (but `mput_char()` does not expect
> any negative values and simply decrements until the value is 0, i.e.
> it tries to add close to 2**31 spaces).

Thanks.  Let's take this and a few other topics that fix 2.20 brown
paper bag breakages and prepare for 2.20.1 soonish.
diff mbox series

Patch

diff --git a/help.c b/help.c
index 4745b32299..ff05fd22df 100644
--- a/help.c
+++ b/help.c
@@ -83,8 +83,9 @@  static void print_command_list(const struct cmdname_help *cmds,
 
 	for (i = 0; cmds[i].name; i++) {
 		if (cmds[i].category & mask) {
+			size_t len = strlen(cmds[i].name);
 			printf("   %s   ", cmds[i].name);
-			mput_char(' ', longest - strlen(cmds[i].name));
+			mput_char(' ', longest > len ? longest - len : 1);
 			puts(_(cmds[i].help));
 		}
 	}
@@ -526,6 +527,13 @@  void list_all_cmds_help(void)
 
 	git_config(get_alias, &alias_list);
 	string_list_sort(&alias_list);
+
+	for (i = 0; i < alias_list.nr; i++) {
+		size_t len = strlen(alias_list.items[i].string);
+		if (longest < len)
+			longest = len;
+	}
+
 	if (alias_list.nr) {
 		printf("\n%s\n", _("Command aliases"));
 		ALLOC_ARRAY(aliases, alias_list.nr + 1);