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

[PATCH v5 03/10] help: use command-list.h for common command list

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Apr 29, 2018, 18:18 UTC
Message-ID
<20180429181844.21325-4-pclouds@gmail.com>
In-Reply-To
<20180429181844.21325-1-pclouds@gmail.com>

The previous commit added code generation for all_cmd_desc[] which includes almost everything we need to generate common command list. Convert help code to use that array instead and drop common_cmds[] array.

The description of each common command group is removed from command-list.txt. This keeps this file format simpler. common-cmds.h will not be generated correctly after this change due to the command-list.txt format change. But it does not matter and common-cmds.h will be removed.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Makefile            |   4 +-
 command-list.txt    |  10 ---
 generate-cmdlist.sh |   4 +-
 help.c              | 145 +++++++++++++++++++++++++++++++++-----------
 t/t0012-help.sh     |   9 +++
 5 files changed, 122 insertions(+), 50 deletions(-)
diff --git a/Makefile b/Makefile
index bb29470f88..ab67150e68 100644
--- a/Makefile
+++ b/Makefile
@@ -1916,9 +1916,9 @@ git$X: git.o GIT-LDFLAGS $(BUILTIN_OBJS) $(GITLIBS)
 	$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) \
 		$(filter %.o,$^) $(LIBS)
 
-help.sp help.s help.o: common-cmds.h
+help.sp help.s help.o: common-cmds.h command-list.h
 
-builtin/help.sp builtin/help.s builtin/help.o: common-cmds.h GIT-PREFIX
+builtin/help.sp builtin/help.s builtin/help.o: common-cmds.h command-list.h GIT-PREFIX
 builtin/help.sp builtin/help.s builtin/help.o: EXTRA_CPPFLAGS = \
 	'-DGIT_HTML_PATH="$(htmldir_relative_SQ)"' \
 	'-DGIT_MAN_PATH="$(mandir_relative_SQ)"' \
diff --git a/command-list.txt b/command-list.txt
index 786536aba0..3bd23201a6 100644
--- a/command-list.txt
+++ b/command-list.txt
@@ -1,13 +1,3 @@
-# common commands are grouped by themes
-# these groups are output by 'git help' in the order declared here.
-# map each common command in the command list to one of these groups.
-### common groups (do not change this line)
-init         start a working area (see also: git help tutorial)
-worktree     work on the current change (see also: git help everyday)
-info         examine the history and state (see also: git help revisions)
-history      grow, mark and tweak your common history
-remote       collaborate (see also: git help workflows)
-
 ### command list (do not change this line, also do not change alignment)
 # command name                          category [category] [category]
 git-add                                 mainporcelain           worktree
diff --git a/generate-cmdlist.sh b/generate-cmdlist.sh
index c9fd524760..93de8e8f59 100755
--- a/generate-cmdlist.sh
+++ b/generate-cmdlist.sh
@@ -6,7 +6,7 @@ die () {
 }
 
 command_list () {
-	sed '1,/^### command list/d;/^#/d' "$1"
+	grep -v '^#' "$1"
 }
 
 get_categories() {
@@ -65,7 +65,7 @@ echo "/* Automatically generated by generate-cmdlist.sh */
 struct cmdname_help {
 	const char *name;
 	const char *help;
-	uint32_t group;
+	uint32_t category;
 };
 "
 if [ -z "$2" ]
diff --git a/help.c b/help.c
index a4feef2ffe..bf2738e9ef 100644
--- a/help.c
+++ b/help.c
@@ -5,13 +5,114 @@
 #include "run-command.h"
 #include "levenshtein.h"
 #include "help.h"
-#include "common-cmds.h"
+#include "command-list.h"
 #include "string-list.h"
 #include "column.h"
 #include "version.h"
 #include "refs.h"
 #include "parse-options.h"
 
+struct category_description {
+	uint32_t category;
+	const char *desc;
+};
+static uint32_t common_mask =
+	CAT_init | CAT_worktree | CAT_info |
+	CAT_history | CAT_remote;
+static struct category_description common_categories[] = {
+	{ CAT_init, N_("start a working area (see also: git help tutorial)") },
+	{ CAT_worktree, N_("work on the current change (see also: git help everyday)") },
+	{ CAT_info, N_("examine the history and state (see also: git help revisions)") },
+	{ CAT_history, N_("grow, mark and tweak your common history") },
+	{ CAT_remote, N_("collaborate (see also: git help workflows)") },
+	{ 0, NULL }
+};
+
+static const char *drop_prefix(const char *name)
+{
+	const char *new_name;
+
+	if (skip_prefix(name, "git-", &new_name))
+		return new_name;
+	return name;
+
+}
+
+static void extract_cmds(struct cmdname_help **p_cmds, uint32_t mask)
+{
+	int i, nr = 0;
+	struct cmdname_help *cmds;
+
+	if (ARRAY_SIZE(command_list) == 0)
+		BUG("empty command_list[] is a sign of broken generate-cmdlist.sh");
+
+	ALLOC_ARRAY(cmds, ARRAY_SIZE(command_list) + 1);
+
+	for (i = 0; i < ARRAY_SIZE(command_list); i++) {
+		const struct cmdname_help *cmd = command_list + i;
+
+		if (!(cmd->category & mask))
+			continue;
+
+		cmds[nr] = *cmd;
+		cmds[nr].name = drop_prefix(cmd->name);
+
+		nr++;
+	}
+	cmds[nr].name = NULL;
+	*p_cmds = cmds;
+}
+
+static void print_command_list(const struct cmdname_help *cmds,
+			       uint32_t mask, int longest)
+{
+	int i;
+
+	for (i = 0; cmds[i].name; i++) {
+		if (cmds[i].category & mask) {
+			printf("   %s   ", cmds[i].name);
+			mput_char(' ', longest - strlen(cmds[i].name));
+			puts(_(cmds[i].help));
+		}
+	}
+}
+
+static int cmd_name_cmp(const void *elem1, const void *elem2)
+{
+	const struct cmdname_help *e1 = elem1;
+	const struct cmdname_help *e2 = elem2;
+
+	return strcmp(e1->name, e2->name);
+}
+
+static void print_cmd_by_category(const struct category_description *catdesc)
+{
+	struct cmdname_help *cmds;
+	int longest = 0;
+	int i, nr = 0;
+	uint32_t mask = 0;
+
+	for (i = 0; catdesc[i].desc; i++)
+		mask |= catdesc[i].category;
+
+	extract_cmds(&cmds, mask);
+
+	for (i = 0; cmds[i].name; i++, nr++) {
+		if (longest < strlen(cmds[i].name))
+			longest = strlen(cmds[i].name);
+	}
+	QSORT(cmds, nr, cmd_name_cmp);
+
+	for (i = 0; catdesc[i].desc; i++) {
+		uint32_t mask = catdesc[i].category;
+		const char *desc = catdesc[i].desc;
+
+		printf("\n%s\n", _(desc));
+		print_command_list(cmds, mask, longest);
+	}
+	free(cmds);
+}
+
 void add_cmdname(struct cmdnames *cmds, const char *name, int len)
 {
 	struct cmdname *ent;
@@ -190,42 +291,10 @@ void list_commands(unsigned int colopts,
 	}
 }
 
-static int cmd_group_cmp(const void *elem1, const void *elem2)
-{
-	const struct cmdname_help *e1 = elem1;
-	const struct cmdname_help *e2 = elem2;
-
-	if (e1->group < e2->group)
-		return -1;
-	if (e1->group > e2->group)
-		return 1;
-	return strcmp(e1->name, e2->name);
-}
-
 void list_common_cmds_help(void)
 {
-	int i, longest = 0;
-	int current_grp = -1;
-
-	for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
-		if (longest < strlen(common_cmds[i].name))
-			longest = strlen(common_cmds[i].name);
-	}
-
-	QSORT(common_cmds, ARRAY_SIZE(common_cmds), cmd_group_cmp);
-
 	puts(_("These are common Git commands used in various situations:"));
-
-	for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
-		if (common_cmds[i].group != current_grp) {
-			printf("\n%s\n", _(common_cmd_groups[common_cmds[i].group]));
-			current_grp = common_cmds[i].group;
-		}
-
-		printf("   %s   ", common_cmds[i].name);
-		mput_char(' ', longest - strlen(common_cmds[i].name));
-		puts(_(common_cmds[i].help));
-	}
+	print_cmd_by_category(common_categories);
 }
 
 int is_in_cmdlist(struct cmdnames *c, const char *s)
@@ -285,6 +354,7 @@ const char *help_unknown_cmd(const char *cmd)
 {
 	int i, n, best_similarity = 0;
 	struct cmdnames main_cmds, other_cmds;
+	struct cmdname_help *common_cmds;
 
 	memset(&main_cmds, 0, sizeof(main_cmds));
 	memset(&other_cmds, 0, sizeof(other_cmds));
@@ -299,6 +369,8 @@ const char *help_unknown_cmd(const char *cmd)
 	QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare);
 	uniq(&main_cmds);
 
+	extract_cmds(&common_cmds, common_mask);
+
 	/* This abuses cmdname->len for levenshtein distance */
 	for (i = 0, n = 0; i < main_cmds.cnt; i++) {
 		int cmp = 0; /* avoid compiler stupidity */
@@ -313,10 +385,10 @@ const char *help_unknown_cmd(const char *cmd)
 			die(_(bad_interpreter_advice), cmd, cmd);
 
 		/* Does the candidate appear in common_cmds list? */
-		while (n < ARRAY_SIZE(common_cmds) &&
+		while (common_cmds[n].name &&
 		       (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
 			n++;
-		if ((n < ARRAY_SIZE(common_cmds)) && !cmp) {
+		if (common_cmds[n].name && !cmp) {
 			/* Yes, this is one of the common commands */
 			n++; /* use the entry from common_cmds[] */
 			if (starts_with(candidate, cmd)) {
@@ -329,6 +401,7 @@ const char *help_unknown_cmd(const char *cmd)
 		main_cmds.names[i]->len =
 			levenshtein(cmd, candidate, 0, 2, 1, 3) + 1;
 	}
+	FREE_AND_NULL(common_cmds);
 
 	QSORT(main_cmds.names, main_cmds.cnt, levenshtein_compare);
 
diff --git a/t/t0012-help.sh b/t/t0012-help.sh
index 487b92a5de..c096f33505 100755
--- a/t/t0012-help.sh
+++ b/t/t0012-help.sh
@@ -49,6 +49,15 @@ test_expect_success "--help does not work for guides" "
 	test_i18ncmp expect actual
 "
 
+test_expect_success 'git help' '
+	git help >help.output &&
+	test_i18ngrep "^   clone  " help.output &&
+	test_i18ngrep "^   add    " help.output &&
+	test_i18ngrep "^   log    " help.output &&
+	test_i18ngrep "^   commit " help.output &&
+	test_i18ngrep "^   fetch  " help.output
+'
+
 test_expect_success 'generate builtin list' '
 	git --list-builtins >builtins
 '
-- 
2.17.0.664.g8924eee37a
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 87 of 137 in “Keep all info in command-list.txt in git binary”
  1. 0/5 Keep all info in command-list.txt in git binaryNguyễn Thái Ngọc Duy, Mar 26, 2018
  2. 2/5 git.c: implement --list-cmds=all and use it in git-completion.bashNguyễn Thái Ngọc Duy, Mar 26, 2018
  3. Eric SunshineApr 9, 2018
  4. 1/5 git.c: convert --list-builtins to --list-cmds=builtinsNguyễn Thái Ngọc Duy, Mar 26, 2018
  5. 3/5 generate-cmdlist.sh: keep all information in common-cmds.hNguyễn Thái Ngọc Duy, Mar 26, 2018
  6. Eric SunshineApr 9, 2018
  7. Eric SunshineApr 9, 2018
  8. Duy NguyenApr 9, 2018
  9. Duy NguyenApr 15, 2018
  10. 5/5 help: add "-a --verbose" to list all commands with synopsisNguyễn Thái Ngọc Duy, Mar 26, 2018
  11. Eric SunshineApr 9, 2018
  12. Junio C HamanoApr 9, 2018
  13. Eric SunshineApr 9, 2018
  14. Duy NguyenApr 9, 2018
  15. 4/5 git.c: implement --list-cmds=porcelainNguyễn Thái Ngọc Duy, Mar 26, 2018
  16. Eric SunshineApr 9, 2018
  17. Philip OakleyApr 11, 2018
  18. Duy NguyenApr 14, 2018
  19. Philip OakleyApr 15, 2018
  20. Duy NguyenApr 17, 2018
  21. Duy NguyenApr 17, 2018
  22. Philip OakleyApr 17, 2018
  23. Philip OakleyApr 18, 2018
  24. Duy NguyenApr 18, 2018
  25. Philip OakleyApr 19, 2018
  26. 0/6 Keep all info in command-list.txt in git binaryNguyễn Thái Ngọc Duy, Apr 15, 2018
  27. 1/6 git.c: convert --list-builtins to --list-cmds=builtinsNguyễn Thái Ngọc Duy, Apr 15, 2018
  28. 4/6 git.c: implement --list-cmds=porcelainNguyễn Thái Ngọc Duy, Apr 15, 2018
  29. 3/6 generate-cmdlist.sh: keep all information in common-cmds.hNguyễn Thái Ngọc Duy, Apr 15, 2018
  30. Junio C HamanoApr 16, 2018
  31. Øystein WalleApr 23, 2018
  32. SZEDER GáborApr 23, 2018
  33. Junio C HamanoApr 16, 2018
  34. Duy NguyenApr 16, 2018
  35. SZEDER GáborApr 16, 2018
  36. Ramsay JonesApr 16, 2018
  37. Junio C HamanoApr 16, 2018
  38. 6/6 help: use command-list.txt for the source of guidesNguyễn Thái Ngọc Duy, Apr 15, 2018
  39. 2/6 git.c: implement --list-cmds=all and use it in git-completion.bashNguyễn Thái Ngọc Duy, Apr 15, 2018
  40. Junio C HamanoApr 16, 2018
  41. 5/6 help: add "-a --verbose" to list all commands with synopsisNguyễn Thái Ngọc Duy, Apr 15, 2018
  42. 0/6 Keep all info in command-list.txt in git binaryNguyễn Thái Ngọc Duy, Apr 21, 2018
  43. 1/6 git.c: convert --list-*builtins to --list-cmds=*Nguyễn Thái Ngọc Duy, Apr 21, 2018
  44. 2/6 git.c: implement --list-cmds=all and use it in git-completion.bashNguyễn Thái Ngọc Duy, Apr 21, 2018
  45. SZEDER GáborApr 23, 2018
  46. 3/6 generate-cmdlist.sh: keep all information in common-cmds.hNguyễn Thái Ngọc Duy, Apr 21, 2018
  47. 6/6 help: use command-list.txt for the source of guidesNguyễn Thái Ngọc Duy, Apr 21, 2018
  48. Eric SunshineApr 23, 2018
  49. 5/6 help: add "-a --verbose" to list all commands with synopsisNguyễn Thái Ngọc Duy, Apr 21, 2018
  50. 4/6 git.c: implement --list-cmds=porcelainNguyễn Thái Ngọc Duy, Apr 21, 2018
  51. SZEDER GáborApr 23, 2018
  52. Duy NguyenApr 24, 2018
  53. Duy NguyenApr 24, 2018
  54. SZEDER GáborApr 25, 2018
  55. Duy NguyenApr 25, 2018
  56. SZEDER GáborApr 25, 2018
  57. Duy NguyenApr 25, 2018
  58. Duy NguyenApr 21, 2018
  59. Ramsay JonesApr 22, 2018
  60. Duy NguyenApr 22, 2018
  61. Ramsay JonesApr 22, 2018
  62. Duy NguyenApr 22, 2018
  63. Ramsay JonesApr 22, 2018
  64. 00/12 Keep all info in command-list.txt in git binaryNguyễn Thái Ngọc Duy, Apr 25, 2018
  65. 01/12 generate-cmds.sh: factor out synopsis extract codeNguyễn Thái Ngọc Duy, Apr 25, 2018
  66. Eric SunshineApr 25, 2018
  67. SZEDER GáborApr 25, 2018
  68. 02/12 generate-cmds.sh: export all commands to command-list.hNguyễn Thái Ngọc Duy, Apr 25, 2018
  69. Eric SunshineApr 25, 2018
  70. Duy NguyenApr 29, 2018
  71. 03/12 help: use command-list.h for common command listNguyễn Thái Ngọc Duy, Apr 25, 2018
  72. 08/12 git: support --list-cmds=<category>Nguyễn Thái Ngọc Duy, Apr 25, 2018
  73. Eric SunshineApr 25, 2018
  74. 07/12 completion: implement and use --list-cmds=allNguyễn Thái Ngọc Duy, Apr 25, 2018
  75. 06/12 git: accept multiple --list-cmds optionsNguyễn Thái Ngọc Duy, Apr 25, 2018
  76. Eric SunshineApr 25, 2018
  77. 11/12 command-list.txt: add new category "complete"Nguyễn Thái Ngọc Duy, Apr 25, 2018
  78. 09/12 help: add "-a --verbose" to list all commands with synopsisNguyễn Thái Ngọc Duy, Apr 25, 2018
  79. 10/12 help: use command-list.txt for the source of guidesNguyễn Thái Ngọc Duy, Apr 25, 2018
  80. Eric SunshineApr 25, 2018
  81. 05/12 git.c: convert --list-*builtins to --list-cmds=*Nguyễn Thái Ngọc Duy, Apr 25, 2018
  82. 04/12 Remove common-cmds.hNguyễn Thái Ngọc Duy, Apr 25, 2018
  83. 12/12 completion: let git provide the completable command listNguyễn Thái Ngọc Duy, Apr 25, 2018
  84. 00/10 Keep all info in command-list.txt in git binaryNguyễn Thái Ngọc Duy, Apr 29, 2018
  85. 01/10 generate-cmds.sh: factor out synopsis extract codeNguyễn Thái Ngọc Duy, Apr 29, 2018
  86. 02/10 generate-cmds.sh: export all commands to command-list.hNguyễn Thái Ngọc Duy, Apr 29, 2018
  87. 03/10 help: use command-list.h for common command listNguyễn Thái Ngọc Duy, Apr 29, 2018
  88. 06/10 completion: implement and use --list-cmds=main,othersNguyễn Thái Ngọc Duy, Apr 29, 2018
  89. 05/10 git.c: convert --list-*builtins to --list-cmds=*Nguyễn Thái Ngọc Duy, Apr 29, 2018
  90. 10/10 completion: let git provide the completable command listNguyễn Thái Ngọc Duy, Apr 29, 2018
  91. 04/10 Remove common-cmds.hNguyễn Thái Ngọc Duy, Apr 29, 2018
  92. 07/10 git: support --list-cmds=list-<category>Nguyễn Thái Ngọc Duy, Apr 29, 2018
  93. 08/10 help: add "-a --verbose" to list all commands with synopsisNguyễn Thái Ngọc Duy, Apr 29, 2018
  94. 09/10 help: use command-list.txt for the source of guidesNguyễn Thái Ngọc Duy, Apr 29, 2018
  95. Duy NguyenApr 29, 2018
  96. Duy NguyenApr 30, 2018
  97. 00/13 Keep all info in command-list.txt in git binaryNguyễn Thái Ngọc Duy, May 7, 2018
  98. 01/13 generate-cmds.sh: factor out synopsis extract codeNguyễn Thái Ngọc Duy, May 7, 2018
  99. 02/13 generate-cmds.sh: export all commands to command-list.hNguyễn Thái Ngọc Duy, May 7, 2018
  100. Junio C HamanoMay 8, 2018
  101. 04/13 Remove common-cmds.hNguyễn Thái Ngọc Duy, May 7, 2018
  102. 06/13 git --list-cmds: collect command list in a string_listNguyễn Thái Ngọc Duy, May 7, 2018
  103. 07/13 completion: implement and use --list-cmds=main,othersNguyễn Thái Ngọc Duy, May 7, 2018
  104. 05/13 git.c: convert --list-* to --list-cmds=*Nguyễn Thái Ngọc Duy, May 7, 2018
  105. Junio C HamanoMay 8, 2018
  106. 09/13 help: add "-a --verbose" to list all commands with synopsisNguyễn Thái Ngọc Duy, May 7, 2018
  107. 12/13 completion: let git provide the completable command listNguyễn Thái Ngọc Duy, May 7, 2018
  108. 13/13 completion: allow to customize the completable command listNguyễn Thái Ngọc Duy, May 7, 2018
  109. 10/13 help: use command-list.txt for the source of guidesNguyễn Thái Ngọc Duy, May 7, 2018
  110. Junio C HamanoMay 8, 2018
  111. 08/13 git: support --list-cmds=list-<category>Nguyễn Thái Ngọc Duy, May 7, 2018
  112. Junio C HamanoMay 8, 2018
  113. 11/13 command-list.txt: documentation and guide lineNguyễn Thái Ngọc Duy, May 7, 2018
  114. Philip OakleyMay 12, 2018
  115. 03/13 help: use command-list.h for common command listNguyễn Thái Ngọc Duy, May 7, 2018
  116. 00/13 Keep all info in command-list.txt in git binaryNguyễn Thái Ngọc Duy, May 10, 2018
  117. 01/13 generate-cmds.sh: factor out synopsis extract codeNguyễn Thái Ngọc Duy, May 10, 2018
  118. 03/13 help: use command-list.h for common command listNguyễn Thái Ngọc Duy, May 10, 2018
  119. 02/13 generate-cmds.sh: export all commands to command-list.hNguyễn Thái Ngọc Duy, May 10, 2018
  120. 04/13 Remove common-cmds.hNguyễn Thái Ngọc Duy, May 10, 2018
  121. 11/13 command-list.txt: documentation and guide lineNguyễn Thái Ngọc Duy, May 10, 2018
  122. 10/13 help: use command-list.txt for the source of guidesNguyễn Thái Ngọc Duy, May 10, 2018
  123. 12/13 completion: let git provide the completable command listNguyễn Thái Ngọc Duy, May 10, 2018
  124. SZEDER GáborMay 11, 2018
  125. Duy NguyenMay 13, 2018
  126. 13/13 completion: allow to customize the completable command listNguyễn Thái Ngọc Duy, May 10, 2018
  127. 05/13 git.c: convert --list-* to --list-cmds=*Nguyễn Thái Ngọc Duy, May 10, 2018
  128. 06/13 git --list-cmds: collect command list in a string_listNguyễn Thái Ngọc Duy, May 10, 2018
  129. 07/13 completion: implement and use --list-cmds=main,othersNguyễn Thái Ngọc Duy, May 10, 2018
  130. SZEDER GáborMay 11, 2018
  131. SZEDER GáborMay 11, 2018
  132. Duy NguyenMay 11, 2018
  133. 08/13 git: support --list-cmds=list-<category>Nguyễn Thái Ngọc Duy, May 10, 2018
  134. 09/13 help: add "-a --verbose" to list all commands with synopsisNguyễn Thái Ngọc Duy, May 10, 2018
  135. Simon RuderichApr 19, 2018
  136. SZEDER GáborApr 19, 2018
  137. Simon RuderichApr 20, 2018

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.