Skip to content

Commit 2dce956

Browse files
chriscoolgitster
authored andcommitted
help: check early if we have a command, if not try a documentation topic
Before this patch, something like "git help tutorial" did not work, people had to use "git help gittutorial" which is not very intuitive. This patch uses the "is_git_command" function to test early if the argument passed to "git help" is a git command, and if this is not the case then we prefix the argument with "git" instead of "git-". This way, things like "git help tutorial" or "git help glossary" will work fine. The little downside of this patch is that the "is_git_command" is a little bit slow. Signed-off-by: Christian Couder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d0658ec commit 2dce956

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

help.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -527,20 +527,26 @@ static int is_git_command(const char *s)
527527
is_in_cmdlist(&other_cmds, s);
528528
}
529529

530+
static const char *prepend(const char *prefix, const char *cmd)
531+
{
532+
size_t pre_len = strlen(prefix);
533+
size_t cmd_len = strlen(cmd);
534+
char *p = xmalloc(pre_len + cmd_len + 1);
535+
memcpy(p, prefix, pre_len);
536+
strcpy(p + pre_len, cmd);
537+
return p;
538+
}
539+
530540
static const char *cmd_to_page(const char *git_cmd)
531541
{
532542
if (!git_cmd)
533543
return "git";
534544
else if (!prefixcmp(git_cmd, "git"))
535545
return git_cmd;
536-
else {
537-
int page_len = strlen(git_cmd) + 4;
538-
char *p = xmalloc(page_len + 1);
539-
strcpy(p, "git-");
540-
strcpy(p + 4, git_cmd);
541-
p[page_len] = 0;
542-
return p;
543-
}
546+
else if (is_git_command(git_cmd))
547+
return prepend("git-", git_cmd);
548+
else
549+
return prepend("git", git_cmd);
544550
}
545551

546552
static void setup_man_path(void)

0 commit comments

Comments
 (0)