diff options
author | Junio C Hamano <gitster@pobox.com> | 2014-01-03 12:28:09 -0800 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2014-01-03 12:28:09 -0800 |
commit | 76c478a51f250f58289b710c0c2c9f592431c2b2 (patch) | |
tree | dbe668f4574bb6d57a7335fde3cce8f9687b24fc | |
parent | 44484662d83de2ae98d04738ec43d4dea1f859a8 (diff) | |
download | git-jc/is-git-command.tar.gz |
-rw-r--r-- | builtin/help.c | 6 | ||||
-rw-r--r-- | help.c | 69 | ||||
-rw-r--r-- | help.h | 1 |
3 files changed, 70 insertions, 6 deletions
diff --git a/builtin/help.c b/builtin/help.c index cc17e670ce..a31a538cbd 100644 --- a/builtin/help.c +++ b/builtin/help.c @@ -286,12 +286,6 @@ static int git_help_config(const char *var, const char *value, void *cb) static struct cmdnames main_cmds, other_cmds; -static int is_git_command(const char *s) -{ - return is_in_cmdlist(&main_cmds, s) || - is_in_cmdlist(&other_cmds, s); -} - static const char *prepend(const char *prefix, const char *cmd) { size_t pre_len = strlen(prefix); @@ -166,6 +166,75 @@ static void list_commands_in_dir(struct cmdnames *cmds, strbuf_release(&buf); } +static int command_exists_in_dir(const char *path, const char *name) +{ + int ret; + struct strbuf buf = STRBUF_INIT; + + strbuf_addf(&buf, "%s/%s", path, name); + ret = is_executable(buf.buf); + strbuf_release(&buf); + return ret; +} + +static void add_name_to_path(struct strbuf *buf, + const char *path, + int pathlen, const char *name) +{ + if (pathlen < 0) + pathlen = strlen(path); + + if (pathlen) + strbuf_addf(&buf, "%.*s/%s", pathlen, path, name); + else + strbuf_addstr(&buf, name); + +#if defined(GIT_WINDOWS_NATIVE) + strbuf_addstr(&buf, ".exe"); +#endif +} + +int is_git_command(const char *name) +{ + struct strbuf buf = STRBUF_INIT; + const char *env_path, *exec_path; + + if (is_builtin_command(name)) + return 1; + + exec_path = git_exec_path(); + if (exec_path) { + add_name_to_path(&buf, exec_path, -1, name); + if (is_executable(buf.buf)) + goto found; + strbuf_reset(&buf); + } + + env_path = getenv("PATH"); + if (env_path) { + const char *current = env_path; + + while (1) { + const char *next = strchr(current, PATH_SEP); + + add_name_to_path(&buf, current, next - current, name); + if (is_executable(buf.buf)) + goto found; + next++; + if (!*next) + break; + current = next; + } + } + + strbuf_release(&buf); + return 0; + +found: + strbuf_release(&buf); + return 1; +} + void load_command_list(const char *prefix, struct cmdnames *main_cmds, struct cmdnames *other_cmds) @@ -24,6 +24,7 @@ extern void load_command_list(const char *prefix, extern void add_cmdname(struct cmdnames *cmds, const char *name, int len); /* Here we require that excludes is a sorted list. */ extern void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes); +extern int is_git_command(const char *name); extern int is_in_cmdlist(struct cmdnames *cmds, const char *name); extern void list_commands(unsigned int colopts, struct cmdnames *main_cmds, struct cmdnames *other_cmds); |