git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 3/3] help: respect aliases

From
Jeff King <peff@peff.net>
Date
Feb 24, 2008, 22:17 UTC
Message-ID
<20080224221737.GD31309@coredump.intra.peff.net>
In-Reply-To
<cover.1203890846.git.peff@peff.net>

If we have an alias "foo" defined, then the help text for "foo" (via "git help foo" or "git foo --help") now shows the definition of the alias.

Before showing an alias definition, we make sure that there is no git command which would override the alias (so that even though you may have a "log" alias, even though it will not work, we don't want to it supersede "git help log").

Signed-off-by: Jeff King <peff@peff.net>
---
 help.c |   33 ++++++++++++++++++++++++++++++++-
 1 files changed, 32 insertions(+), 1 deletions(-)
diff --git a/help.c b/help.c
index 5feb849..e57a50e 100644
--- a/help.c
+++ b/help.c
@@ -210,7 +210,7 @@ static unsigned int list_commands_in_dir(struct cmdnames *cmds,
 	return longest;
 }
 
-static void list_commands(void)
+static unsigned int load_command_list(void)
 {
 	unsigned int longest = 0;
 	unsigned int len;
@@ -250,6 +250,14 @@ static void list_commands(void)
 	uniq(&other_cmds);
 	exclude_cmds(&other_cmds, &main_cmds);
 
+	return longest;
+}
+
+static void list_commands(void)
+{
+	unsigned int longest = load_command_list();
+	const char *exec_path = git_exec_path();
+
 	if (main_cmds.cnt) {
 		printf("available git commands in '%s'\n", exec_path);
 		printf("----------------------------");
@@ -284,6 +292,22 @@ void list_common_cmds_help(void)
 	}
 }
 
+static int is_in_cmdlist(struct cmdnames *c, const char *s)
+{
+	int i;
+	for (i = 0; i < c->cnt; i++)
+		if (!strcmp(s, c->names[i]->name))
+			return 1;
+	return 0;
+}
+
+static int is_git_command(const char *s)
+{
+	load_command_list();
+	return is_in_cmdlist(&main_cmds, s) ||
+		is_in_cmdlist(&other_cmds, s);
+}
+
 static const char *cmd_to_page(const char *git_cmd)
 {
 	if (!git_cmd)
@@ -372,6 +396,7 @@ int cmd_version(int argc, const char **argv, const char *prefix)
 int cmd_help(int argc, const char **argv, const char *prefix)
 {
 	int nongit;
+	const char *alias;
 
 	setup_git_directory_gently(&nongit);
 	git_config(git_help_config);
@@ -391,6 +416,12 @@ int cmd_help(int argc, const char **argv, const char *prefix)
 		return 0;
 	}
 
+	alias = alias_lookup(argv[0]);
+	if (alias && !is_git_command(argv[0])) {
+		printf("`git %s' is aliased to `%s'\n", argv[0], alias);
+		return 0;
+	}
+
 	switch (help_format) {
 	case HELP_FORMAT_MAN:
 		show_man_page(argv[0]);
-- 
1.5.4.3.305.g073a4
Previous: Jeff KingNext: Jay Soffian
Message 6 of 12 in “help: use parseopt”
  1. 1/3 help: use parseoptJeff King, Feb 24, 2008
  2. Christian CouderFeb 25, 2008
  3. Jeff KingFeb 25, 2008
  4. Junio C HamanoFeb 25, 2008
  5. 2/3 make alias lookup a public, procedural functionJeff King, Feb 24, 2008
  6. 3/3 help: respect aliasesJeff King, Feb 24, 2008
  7. Jay SoffianFeb 25, 2008
  8. Johannes SchindelinFeb 26, 2008
  9. Jay SoffianFeb 26, 2008
  10. Johannes SchindelinFeb 26, 2008
  11. Junio C HamanoFeb 26, 2008
  12. Jay SoffianFeb 26, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.