Re: [PATCH v4/wip 08/12] git: support --list-cmds=

2018-04-25 Thread Eric Sunshine
On Wed, Apr 25, 2018 at 12:31 PM, Nguyễn Thái Ngọc Duy
 wrote:
> This allows us to select any group of commands by a category defined
> in command-list.txt. This is an internal/hidden option so we don't
> have to be picky about the category name or worried about exposing too
> much.
>
> This will be used later by git-completion.bash to retrieve certain
> command groups.
>
> Signed-off-by: Nguyễn Thái Ngọc Duy 
> ---
> diff --git a/help.c b/help.c
> @@ -305,6 +305,25 @@ void list_all_cmds(void)
> +void list_cmds_by_category(const char *cat)
> +{
> +   int i;
> +   int cat_id = 0;

Should 'cat_id' be unsigned...

> +
> +   for (i = 0; category_names[i]; i++) {
> +   if (!strcmp(cat, category_names[i])) {
> +   cat_id = 1 << i;

...since you're shifting it here?

> +   break;
> +   }
> +   }
> +   if (!cat_id)
> +   die("unsupported command listing type '%s'", cat);
> +
> +   for (i = 0; command_list[i].name; i++)
> +   if (command_list[i].category & cat_id)
> +   puts(command_list[i].name);
> +}


[PATCH v4/wip 08/12] git: support --list-cmds=

2018-04-25 Thread Nguyễn Thái Ngọc Duy
This allows us to select any group of commands by a category defined
in command-list.txt. This is an internal/hidden option so we don't
have to be picky about the category name or worried about exposing too
much.

This will be used later by git-completion.bash to retrieve certain
command groups.

Signed-off-by: Nguyễn Thái Ngọc Duy 
---
 generate-cmdlist.sh | 17 +
 git.c   |  2 +-
 help.c  | 19 +++
 help.h  |  1 +
 4 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/generate-cmdlist.sh b/generate-cmdlist.sh
index 57f9800123..c14c0a7adc 100755
--- a/generate-cmdlist.sh
+++ b/generate-cmdlist.sh
@@ -47,6 +47,21 @@ define_categories() {
test "$bit" -gt 32 && die "Urgh.. too many categories?"
 }
 
+define_category_names() {
+   echo
+   echo "/* Category names */"
+   echo "static const char *category_names[] = {"
+   bit=0
+   category_list "$1" |
+   while read cat
+   do
+   echo "  \"$cat\", /* (1UL << $bit) */"
+   bit=$(($bit+1))
+   done
+   echo "  NULL"
+   echo "};"
+}
+
 print_command_list() {
echo "static struct cmdname_help command_list[] = {"
 
@@ -72,4 +87,6 @@ struct cmdname_help {
 "
 define_categories "$1"
 echo
+define_category_names "$1"
+echo
 print_command_list "$1"
diff --git a/git.c b/git.c
index fa6e542d06..6c071d44f5 100644
--- a/git.c
+++ b/git.c
@@ -232,7 +232,7 @@ static int handle_options(const char ***argv, int *argc, 
int *envchanged)
else if (!strcmp(cmd, "all"))
list_all_cmds();
else
-   die("unsupported command listing type '%s'", 
cmd);
+   list_cmds_by_category(cmd);
commands_listed++;
} else {
fprintf(stderr, _("unknown option: %s\n"), cmd);
diff --git a/help.c b/help.c
index 217864999e..affa8e4343 100644
--- a/help.c
+++ b/help.c
@@ -305,6 +305,25 @@ void list_all_cmds(void)
clean_cmdnames(_cmds);
 }
 
+void list_cmds_by_category(const char *cat)
+{
+   int i;
+   int cat_id = 0;
+
+   for (i = 0; category_names[i]; i++) {
+   if (!strcmp(cat, category_names[i])) {
+   cat_id = 1 << i;
+   break;
+   }
+   }
+   if (!cat_id)
+   die("unsupported command listing type '%s'", cat);
+
+   for (i = 0; command_list[i].name; i++)
+   if (command_list[i].category & cat_id)
+   puts(command_list[i].name);
+}
+
 int is_in_cmdlist(struct cmdnames *c, const char *s)
 {
int i;
diff --git a/help.h b/help.h
index 0bf29f8dc5..3e30542927 100644
--- a/help.h
+++ b/help.h
@@ -18,6 +18,7 @@ static inline void mput_char(char c, unsigned int num)
 
 extern void list_common_cmds_help(void);
 extern void list_all_cmds(void);
+extern void list_cmds_by_category(const char *category);
 extern const char *help_unknown_cmd(const char *cmd);
 extern void load_command_list(const char *prefix,
  struct cmdnames *main_cmds,
-- 
2.17.0.519.gb89679a4aa