Re: [RFC/PATCH 4/6] Add is_git_command_or_alias() for checking availability of a given git command
- From
Johannes Schindelin <johannes.schindelin@gmx.de>
- Date
- Aug 11, 2009, 12:21 UTC
- Message-ID
- <alpine.DEB.1.00.0908111358060.4638@intel-tinevez-2-302>
- In-Reply-To
- <1249985426-13726-5-git-send-email-johan@herland.net>
Hi,
On Tue, 11 Aug 2009, Johan Herland wrote:
Show 28 quoted lines
> 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);
> +}Sorting a list is an O(n log n) operation, searching through a list linearly is O(n). You throw away main_cmds (without freeing) after that, so I think it is not a good trade-off.
Ciao, Dscho