[RFC/PATCH 4/6] Add is_git_command_or_alias() for checking availability of a given git command

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The transport-helper mechanism requires us to gracefully handle the case
where a git command is not available for some reason.

This patch introduces a simple function for querying the availability of a
git command, without attempting to execute said command.

The new function is very similar to the static is_git_command() function in
builtin-help.c, except that this function also accepts a matching alias.

Signed-off-by: Johan Herland <johan@xxxxxxxxxxx>
---
 help.c |   21 +++++++++++++++++++++
 help.h |    2 ++
 2 files changed, 23 insertions(+), 0 deletions(-)

diff --git a/help.c b/help.c
index 994561d..a616277 100644
--- a/help.c
+++ b/help.c
@@ -296,6 +296,27 @@ static void add_cmd_list(struct cmdnames *cmds, struct cmdnames *old)
 	old->names = NULL;
 }
 
+int is_git_command_or_alias(const char *cmd)
+{
+	struct cmdnames main_cmds, other_cmds;
+
+	memset(&main_cmds, 0, sizeof(main_cmds));
+	memset(&other_cmds, 0, sizeof(other_cmds));
+	memset(&aliases, 0, sizeof(aliases));
+
+	git_config(git_unknown_cmd_config, NULL);
+
+	load_command_list("git-", &main_cmds, &other_cmds);
+
+	add_cmd_list(&main_cmds, &aliases);
+	add_cmd_list(&main_cmds, &other_cmds);
+	qsort(main_cmds.names, main_cmds.cnt,
+	      sizeof(main_cmds.names), cmdname_compare);
+	uniq(&main_cmds);
+
+	return is_in_cmdlist(&main_cmds, cmd);
+}
+
 const char *help_unknown_cmd(const char *cmd)
 {
 	int i, n, best_similarity = 0;
diff --git a/help.h b/help.h
index 56bc154..6c43452 100644
--- a/help.h
+++ b/help.h
@@ -26,4 +26,6 @@ int is_in_cmdlist(struct cmdnames *c, const char *s);
 void list_commands(const char *title, struct cmdnames *main_cmds,
 		   struct cmdnames *other_cmds);
 
+int is_git_command_or_alias(const char *cmd);
+
 #endif /* HELP_H */
-- 
1.6.4.rc3.138.ga6b98.dirty

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]