Skip to contentSkip to conversation

Fix git help -a with long alias names

PATCH In review Johannes Schindelin via GitGitGadget 2 participants 4 messages Dec 11, 2018 – Dec 12, 2018

lore
Share & tools
lore
#0 lore raw

cover Fix git help -a with long alias names

The code added in 26c7d0678324 (help -a: improve and make --verbose default, 2018-09-29) that intends to print out aliases in addition to commands failed to adjust for the length of the aliases. As a consequence, if there was any alias whose name is longer than 18 characters, git help -a tried to print an insanely large number of spaces, one at a time, causing what appeared to be a "hang".

Let's fix this, and while at it fix a style issue that I saw on the way as well.

Original report at https://github.com/git-for-windows/git/issues/1975

Johannes Schindelin (2):
help.h: fix coding style
help -a: handle aliases with long names gracefully

 help.c | 10 +++++++++-
 help.h |  2 +-
 2 files changed, 10 insertions(+), 2 deletions(-)

base-commit: 5d826e972970a784bd7a7bdf587512510097b8c7
Published-As: https://github.com/gitgitgadget/git/releases/tags/pr-97%2Fdscho%2Ffix-help-a-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-97/dscho/fix-help-a-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/97

Signature · 1 line
gitgitgadget
#1 lore raw

PATCH 1/2 help.h: fix coding style

From: Johannes Schindelin <[email protected]>

We want a space after the while keyword.

---
 help.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
help.h +1 -1
index 9eab6a3f89..a141e209ae 100644
@@ -15,7 +15,7 @@ struct cmdnames {
 
 static inline void mput_char(char c, unsigned int num)
 {
-	while(num--)
+	while (num--)
 		putchar(c);
 }
 
Signature · 1 line
gitgitgadget
#2 lore raw

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

In reply to Johannes Schindelin via GitGitGadget

From: Johannes Schindelin <[email protected]>

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.

---
 help.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
help.c +9 -1
index 4745b32299..ff05fd22df 100644
@@ -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);
Signature · 1 line
gitgitgadget
#3 lore raw

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

"Johannes Schindelin via GitGitGadget" <[email protected]>

Quoted 10 lines · writes:

From: Johannes Schindelin <[email protected]>

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.