All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: pclouds@gmail.com
Cc: git@vger.kernel.org, szeder.dev@gmail.com,
	Eric Sunshine <sunshine@sunshineco.com>,
	philipoakley@iee.org, Junio C Hamano <gitster@pobox.com>
Subject: [PATCH v2 3/6] generate-cmdlist.sh: keep all information in common-cmds.h
Date: Sun, 15 Apr 2018 18:42:35 +0200	[thread overview]
Message-ID: <20180415164238.9107-4-pclouds@gmail.com> (raw)
In-Reply-To: <20180415164238.9107-1-pclouds@gmail.com>

common-cmds.h is used to extract the list of common commands (by
group) and a one-line summary of each command. Some information is
dropped, for example command category or summary of other commands.
Update generate-cmdlist.sh to keep all the information. The extra info
will be used shortly.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 generate-cmdlist.sh | 61 +++++++++++++++++++++++++++++++++------------
 help.c              | 42 ++++++++++++++++++++++++++-----
 2 files changed, 81 insertions(+), 22 deletions(-)

diff --git a/generate-cmdlist.sh b/generate-cmdlist.sh
index eeea4b67ea..e0893e979a 100755
--- a/generate-cmdlist.sh
+++ b/generate-cmdlist.sh
@@ -1,17 +1,30 @@
 #!/bin/sh
 
+# Don't let locale affect this script.
+LC_ALL=C
+LANG=C
+export LC_ALL LANG
+
+command_list () {
+	sed '1,/^### command list/d;/^#/d' "$1"
+}
+
+category_list () {
+	command_list "$1" | awk '{print $2;}' | sort | uniq
+}
+
 echo "/* Automatically generated by generate-cmdlist.sh */
 struct cmdname_help {
-	char name[16];
+	char name[32];
 	char help[80];
-	unsigned char group;
+	unsigned int category;
+	unsigned int group;
 };
 
 static const char *common_cmd_groups[] = {"
 
 grps=grps$$.tmp
-match=match$$.tmp
-trap "rm -f '$grps' '$match'" 0 1 2 3 15
+trap "rm -f '$grps'" 0 1 2 3 15
 
 sed -n '
 	1,/^### common groups/b
@@ -23,28 +36,44 @@ sed -n '
 	' "$1"
 printf '};\n\n'
 
+echo "#define GROUP_NONE 0xff /* no common group */"
 n=0
-substnum=
 while read grp
 do
-	echo "^git-..*[ 	]$grp"
-	substnum="$substnum${substnum:+;}s/[ 	]$grp/$n/"
+	echo "#define GROUP_${grp:-NONE} $n"
 	n=$(($n+1))
-done <"$grps" >"$match"
+done <"$grps"
+echo
+
+echo '/*'
+printf 'static const char *cmd_categories[] = {\n'
+category_list "$1" |
+while read category; do
+	printf '\t\"'$category'\",\n'
+done
+printf '\tNULL\n};\n\n'
+echo '*/'
+
+n=0
+category_list "$1" |
+while read category; do
+	echo "#define CAT_$category $n"
+	n=$(($n+1))
+done
+echo
 
-printf 'static struct cmdname_help common_cmds[] = {\n'
-grep -f "$match" "$1" |
-sed 's/^git-//' |
+printf 'static struct cmdname_help command_list[] = {\n'
+command_list "$1" |
 sort |
-while read cmd tags
+while read cmd category tags
 do
-	tag=$(echo "$tags" | sed "$substnum; s/[^0-9]//g")
+	name=${cmd/git-}
 	sed -n '
-		/^NAME/,/git-'"$cmd"'/H
+		/^NAME/,/'"$cmd"'/H
 		${
 			x
-			s/.*git-'"$cmd"' - \(.*\)/	{"'"$cmd"'", N_("\1"), '$tag'},/
+			s/.*'"$cmd"' - \(.*\)/	{"'"$name"'", N_("\1"), CAT_'$category', GROUP_'${tags:-NONE}' },/
 			p
-		}' "Documentation/git-$cmd.txt"
+		}' "Documentation/$cmd.txt"
 done
 echo "};"
diff --git a/help.c b/help.c
index e155c39870..b5da7fa013 100644
--- a/help.c
+++ b/help.c
@@ -190,6 +190,27 @@ void list_commands(unsigned int colopts,
 	}
 }
 
+static void extract_common_cmds(struct cmdname_help **p_common_cmds,
+				int *p_nr)
+{
+	int i, nr = 0;
+	struct cmdname_help *common_cmds;
+
+	ALLOC_ARRAY(common_cmds, ARRAY_SIZE(command_list));
+
+	for (i = 0; i < ARRAY_SIZE(command_list); i++) {
+		const struct cmdname_help *cmd = command_list + i;
+
+		if (cmd->category != CAT_mainporcelain)
+			continue;
+
+		common_cmds[nr++] = *cmd;
+	}
+
+	*p_common_cmds = common_cmds;
+	*p_nr = nr;
+}
+
 static int cmd_group_cmp(const void *elem1, const void *elem2)
 {
 	const struct cmdname_help *e1 = elem1;
@@ -206,17 +227,21 @@ void list_common_cmds_help(void)
 {
 	int i, longest = 0;
 	int current_grp = -1;
+	int nr = 0;
+	struct cmdname_help *common_cmds;
+
+	extract_common_cmds(&common_cmds, &nr);
 
-	for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
+	for (i = 0; i < nr; i++) {
 		if (longest < strlen(common_cmds[i].name))
 			longest = strlen(common_cmds[i].name);
 	}
 
-	QSORT(common_cmds, ARRAY_SIZE(common_cmds), cmd_group_cmp);
+	QSORT(common_cmds, nr, cmd_group_cmp);
 
 	puts(_("These are common Git commands used in various situations:"));
 
-	for (i = 0; i < ARRAY_SIZE(common_cmds); i++) {
+	for (i = 0; i < nr; 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;
@@ -226,6 +251,7 @@ void list_common_cmds_help(void)
 		mput_char(' ', longest - strlen(common_cmds[i].name));
 		puts(_(common_cmds[i].help));
 	}
+	free(common_cmds);
 }
 
 void list_all_cmds(void)
@@ -301,8 +327,9 @@ static const char bad_interpreter_advice[] =
 
 const char *help_unknown_cmd(const char *cmd)
 {
-	int i, n, best_similarity = 0;
+	int i, n, best_similarity = 0, nr_common;
 	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));
@@ -317,6 +344,8 @@ const char *help_unknown_cmd(const char *cmd)
 	QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare);
 	uniq(&main_cmds);
 
+	extract_common_cmds(&common_cmds, &nr_common);
+
 	/* This abuses cmdname->len for levenshtein distance */
 	for (i = 0, n = 0; i < main_cmds.cnt; i++) {
 		int cmp = 0; /* avoid compiler stupidity */
@@ -331,10 +360,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 (n < nr_common &&
 		       (cmp = strcmp(common_cmds[n].name, candidate)) < 0)
 			n++;
-		if ((n < ARRAY_SIZE(common_cmds)) && !cmp) {
+		if ((n < nr_common) && !cmp) {
 			/* Yes, this is one of the common commands */
 			n++; /* use the entry from common_cmds[] */
 			if (starts_with(candidate, cmd)) {
@@ -347,6 +376,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);
 
-- 
2.17.0.367.g5dd2e386c3


  parent reply	other threads:[~2018-04-15 16:46 UTC|newest]

Thread overview: 137+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-26 16:55 [PATCH/RFC 0/5] Keep all info in command-list.txt in git binary Nguyễn Thái Ngọc Duy
2018-03-26 16:55 ` [PATCH/RFC 1/5] git.c: convert --list-builtins to --list-cmds=builtins Nguyễn Thái Ngọc Duy
2018-03-26 16:55 ` [PATCH/RFC 2/5] git.c: implement --list-cmds=all and use it in git-completion.bash Nguyễn Thái Ngọc Duy
2018-04-09  3:32   ` Eric Sunshine
2018-03-26 16:55 ` [PATCH/RFC 3/5] generate-cmdlist.sh: keep all information in common-cmds.h Nguyễn Thái Ngọc Duy
2018-04-09  4:59   ` Eric Sunshine
2018-04-09  5:12     ` Eric Sunshine
2018-04-09 16:16     ` Duy Nguyen
2018-04-15 16:04     ` Duy Nguyen
2018-03-26 16:55 ` [PATCH/RFC 4/5] git.c: implement --list-cmds=porcelain Nguyễn Thái Ngọc Duy
2018-03-26 16:55 ` [PATCH/RFC 5/5] help: add "-a --verbose" to list all commands with synopsis Nguyễn Thái Ngọc Duy
2018-04-09  5:08   ` Eric Sunshine
2018-04-09  9:47     ` Junio C Hamano
2018-04-09  9:55       ` Eric Sunshine
2018-04-09 15:15         ` Duy Nguyen
2018-04-09  5:17 ` [PATCH/RFC 0/5] Keep all info in command-list.txt in git binary Eric Sunshine
2018-04-11 22:06   ` Philip Oakley
2018-04-14 15:44     ` Duy Nguyen
2018-04-15 21:21       ` Philip Oakley
2018-04-17 16:24         ` Duy Nguyen
2018-04-17 16:48           ` Duy Nguyen
2018-04-17 22:47             ` Philip Oakley
2018-04-18  6:38               ` Philip Oakley
2018-04-18 15:33               ` Duy Nguyen
2018-04-19  7:47                 ` Philip Oakley
2018-04-15 16:42 ` [PATCH v2 0/6] " Nguyễn Thái Ngọc Duy
2018-04-15 16:42   ` [PATCH v2 1/6] git.c: convert --list-builtins to --list-cmds=builtins Nguyễn Thái Ngọc Duy
2018-04-15 16:42   ` [PATCH v2 2/6] git.c: implement --list-cmds=all and use it in git-completion.bash Nguyễn Thái Ngọc Duy
2018-04-16  2:30     ` Junio C Hamano
2018-04-15 16:42   ` Nguyễn Thái Ngọc Duy [this message]
2018-04-16  2:38     ` [PATCH v2 3/6] generate-cmdlist.sh: keep all information in common-cmds.h Junio C Hamano
2018-04-23  8:23       ` Øystein Walle
2018-04-23  9:59         ` SZEDER Gábor
2018-04-16  6:28     ` Junio C Hamano
2018-04-16 15:43       ` Duy Nguyen
2018-04-16 15:43     ` SZEDER Gábor
2018-04-16 16:25       ` Ramsay Jones
2018-04-16 22:42       ` Junio C Hamano
2018-04-15 16:42   ` [PATCH v2 4/6] git.c: implement --list-cmds=porcelain Nguyễn Thái Ngọc Duy
2018-04-15 16:42   ` [PATCH v2 5/6] help: add "-a --verbose" to list all commands with synopsis Nguyễn Thái Ngọc Duy
2018-04-15 16:42   ` [PATCH v2 6/6] help: use command-list.txt for the source of guides Nguyễn Thái Ngọc Duy
2018-04-21 16:54   ` [PATCH v3 0/6] Keep all info in command-list.txt in git binary Nguyễn Thái Ngọc Duy
2018-04-21 16:54     ` [PATCH v3 1/6] git.c: convert --list-*builtins to --list-cmds=* Nguyễn Thái Ngọc Duy
2018-04-21 16:54     ` [PATCH v3 2/6] git.c: implement --list-cmds=all and use it in git-completion.bash Nguyễn Thái Ngọc Duy
2018-04-23 11:20       ` SZEDER Gábor
2018-04-21 16:54     ` [PATCH v3 3/6] generate-cmdlist.sh: keep all information in common-cmds.h Nguyễn Thái Ngọc Duy
2018-04-21 16:54     ` [PATCH v3 4/6] git.c: implement --list-cmds=porcelain Nguyễn Thái Ngọc Duy
2018-04-23 13:32       ` SZEDER Gábor
2018-04-24 16:12         ` Duy Nguyen
2018-04-24 16:17           ` Duy Nguyen
2018-04-25 13:46             ` SZEDER Gábor
2018-04-25 14:44               ` Duy Nguyen
2018-04-25 13:06       ` SZEDER Gábor
2018-04-25 14:39         ` Duy Nguyen
2018-04-21 16:54     ` [PATCH v3 5/6] help: add "-a --verbose" to list all commands with synopsis Nguyễn Thái Ngọc Duy
2018-04-21 16:54     ` [PATCH v3 6/6] help: use command-list.txt for the source of guides Nguyễn Thái Ngọc Duy
2018-04-23  6:07       ` Eric Sunshine
2018-04-21 16:56     ` [PATCH v3 0/6] Keep all info in command-list.txt in git binary Duy Nguyen
2018-04-22 14:45       ` Ramsay Jones
2018-04-22 15:22         ` Duy Nguyen
2018-04-22 15:58           ` Ramsay Jones
2018-04-22 16:12             ` Duy Nguyen
2018-04-22 16:36               ` Ramsay Jones
2018-04-25 16:30     ` [PATCH v4/wip 00/12] " Nguyễn Thái Ngọc Duy
2018-04-25 16:30       ` [PATCH v4/wip 01/12] generate-cmds.sh: factor out synopsis extract code Nguyễn Thái Ngọc Duy
2018-04-25 17:59         ` Eric Sunshine
2018-04-25 18:13           ` SZEDER Gábor
2018-04-25 16:30       ` [PATCH v4/wip 02/12] generate-cmds.sh: export all commands to command-list.h Nguyễn Thái Ngọc Duy
2018-04-25 18:07         ` Eric Sunshine
2018-04-29 16:11           ` Duy Nguyen
2018-04-25 16:30       ` [PATCH v4/wip 03/12] help: use command-list.h for common command list Nguyễn Thái Ngọc Duy
2018-04-25 16:30       ` [PATCH v4/wip 04/12] Remove common-cmds.h Nguyễn Thái Ngọc Duy
2018-04-25 16:31       ` [PATCH v4/wip 05/12] git.c: convert --list-*builtins to --list-cmds=* Nguyễn Thái Ngọc Duy
2018-04-25 16:31       ` [PATCH v4/wip 06/12] git: accept multiple --list-cmds options Nguyễn Thái Ngọc Duy
2018-04-25 18:12         ` Eric Sunshine
2018-04-25 16:31       ` [PATCH v4/wip 07/12] completion: implement and use --list-cmds=all Nguyễn Thái Ngọc Duy
2018-04-25 16:31       ` [PATCH v4/wip 08/12] git: support --list-cmds=<category> Nguyễn Thái Ngọc Duy
2018-04-25 18:16         ` Eric Sunshine
2018-04-25 16:31       ` [PATCH v4/wip 09/12] help: add "-a --verbose" to list all commands with synopsis Nguyễn Thái Ngọc Duy
2018-04-25 16:31       ` [PATCH v4/wip 10/12] help: use command-list.txt for the source of guides Nguyễn Thái Ngọc Duy
2018-04-25 18:22         ` Eric Sunshine
2018-04-25 16:31       ` [PATCH v4/wip 11/12] command-list.txt: add new category "complete" Nguyễn Thái Ngọc Duy
2018-04-25 16:31       ` [PATCH v4/wip 12/12] completion: let git provide the completable command list Nguyễn Thái Ngọc Duy
2018-04-29 18:18     ` [PATCH v5 00/10] Keep all info in command-list.txt in git binary Nguyễn Thái Ngọc Duy
2018-04-29 18:18       ` [PATCH v5 01/10] generate-cmds.sh: factor out synopsis extract code Nguyễn Thái Ngọc Duy
2018-04-29 18:18       ` [PATCH v5 02/10] generate-cmds.sh: export all commands to command-list.h Nguyễn Thái Ngọc Duy
2018-04-29 18:18       ` [PATCH v5 03/10] help: use command-list.h for common command list Nguyễn Thái Ngọc Duy
2018-04-29 18:18       ` [PATCH v5 04/10] Remove common-cmds.h Nguyễn Thái Ngọc Duy
2018-04-29 18:18       ` [PATCH v5 05/10] git.c: convert --list-*builtins to --list-cmds=* Nguyễn Thái Ngọc Duy
2018-04-29 18:18       ` [PATCH v5 06/10] completion: implement and use --list-cmds=main,others Nguyễn Thái Ngọc Duy
2018-04-29 18:18       ` [PATCH v5 07/10] git: support --list-cmds=list-<category> Nguyễn Thái Ngọc Duy
2018-04-29 18:18       ` [PATCH v5 08/10] help: add "-a --verbose" to list all commands with synopsis Nguyễn Thái Ngọc Duy
2018-04-29 18:18       ` [PATCH v5 09/10] help: use command-list.txt for the source of guides Nguyễn Thái Ngọc Duy
2018-04-29 18:23         ` Duy Nguyen
2018-04-29 18:18       ` [PATCH v5 10/10] completion: let git provide the completable command list Nguyễn Thái Ngọc Duy
2018-04-30 15:53       ` [PATCH v5 00/10] Keep all info in command-list.txt in git binary Duy Nguyen
2018-05-07 17:52       ` [PATCH v6 00/13] " Nguyễn Thái Ngọc Duy
2018-05-07 17:52         ` [PATCH v6 01/13] generate-cmds.sh: factor out synopsis extract code Nguyễn Thái Ngọc Duy
2018-05-07 17:52         ` [PATCH v6 02/13] generate-cmds.sh: export all commands to command-list.h Nguyễn Thái Ngọc Duy
2018-05-08  3:47           ` Junio C Hamano
2018-05-07 17:52         ` [PATCH v6 03/13] help: use command-list.h for common command list Nguyễn Thái Ngọc Duy
2018-05-07 17:52         ` [PATCH v6 04/13] Remove common-cmds.h Nguyễn Thái Ngọc Duy
2018-05-07 17:52         ` [PATCH v6 05/13] git.c: convert --list-* to --list-cmds=* Nguyễn Thái Ngọc Duy
2018-05-08  3:50           ` Junio C Hamano
2018-05-07 17:52         ` [PATCH v6 06/13] git --list-cmds: collect command list in a string_list Nguyễn Thái Ngọc Duy
2018-05-07 17:52         ` [PATCH v6 07/13] completion: implement and use --list-cmds=main,others Nguyễn Thái Ngọc Duy
2018-05-07 17:52         ` [PATCH v6 08/13] git: support --list-cmds=list-<category> Nguyễn Thái Ngọc Duy
2018-05-08  4:00           ` Junio C Hamano
2018-05-07 17:52         ` [PATCH v6 09/13] help: add "-a --verbose" to list all commands with synopsis Nguyễn Thái Ngọc Duy
2018-05-07 17:52         ` [PATCH v6 10/13] help: use command-list.txt for the source of guides Nguyễn Thái Ngọc Duy
2018-05-08  4:04           ` Junio C Hamano
2018-05-07 17:52         ` [PATCH v6 11/13] command-list.txt: documentation and guide line Nguyễn Thái Ngọc Duy
2018-05-12 17:50           ` Philip Oakley
2018-05-07 17:52         ` [PATCH v6 12/13] completion: let git provide the completable command list Nguyễn Thái Ngọc Duy
2018-05-07 17:52         ` [PATCH v6 13/13] completion: allow to customize " Nguyễn Thái Ngọc Duy
2018-05-10  8:46         ` [PATCH v7 00/13] Keep all info in command-list.txt in git binary Nguyễn Thái Ngọc Duy
2018-05-10  8:46           ` [PATCH v7 01/13] generate-cmds.sh: factor out synopsis extract code Nguyễn Thái Ngọc Duy
2018-05-10  8:46           ` [PATCH v7 02/13] generate-cmds.sh: export all commands to command-list.h Nguyễn Thái Ngọc Duy
2018-05-10  8:46           ` [PATCH v7 03/13] help: use command-list.h for common command list Nguyễn Thái Ngọc Duy
2018-05-10  8:46           ` [PATCH v7 04/13] Remove common-cmds.h Nguyễn Thái Ngọc Duy
2018-05-10  8:46           ` [PATCH v7 05/13] git.c: convert --list-* to --list-cmds=* Nguyễn Thái Ngọc Duy
2018-05-10  8:46           ` [PATCH v7 06/13] git --list-cmds: collect command list in a string_list Nguyễn Thái Ngọc Duy
2018-05-10  8:46           ` [PATCH v7 07/13] completion: implement and use --list-cmds=main,others Nguyễn Thái Ngọc Duy
2018-05-11 14:06             ` SZEDER Gábor
2018-05-11 14:32               ` SZEDER Gábor
2018-05-11 16:33               ` Duy Nguyen
2018-05-10  8:46           ` [PATCH v7 08/13] git: support --list-cmds=list-<category> Nguyễn Thái Ngọc Duy
2018-05-10  8:46           ` [PATCH v7 09/13] help: add "-a --verbose" to list all commands with synopsis Nguyễn Thái Ngọc Duy
2018-05-10  8:46           ` [PATCH v7 10/13] help: use command-list.txt for the source of guides Nguyễn Thái Ngọc Duy
2018-05-10  8:46           ` [PATCH v7 11/13] command-list.txt: documentation and guide line Nguyễn Thái Ngọc Duy
2018-05-10  8:46           ` [PATCH v7 12/13] completion: let git provide the completable command list Nguyễn Thái Ngọc Duy
2018-05-11 15:05             ` SZEDER Gábor
2018-05-13  6:50               ` Duy Nguyen
2018-05-10  8:46           ` [PATCH v7 13/13] completion: allow to customize " Nguyễn Thái Ngọc Duy
2018-04-19 10:37 ` [PATCH/RFC 0/5] Keep all info in command-list.txt in git binary Simon Ruderich
2018-04-19 11:26   ` SZEDER Gábor
2018-04-20  7:05     ` Simon Ruderich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180415164238.9107-4-pclouds@gmail.com \
    --to=pclouds@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=philipoakley@iee.org \
    --cc=sunshine@sunshineco.com \
    --cc=szeder.dev@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.