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

[PATCH 05/14] git.c: convert --list-* to --list-cmds=*

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
May 19, 2018, 04:27 UTC
Message-ID
<20180519042752.8666-6-pclouds@gmail.com>
In-Reply-To
<20180519042752.8666-1-pclouds@gmail.com>

Even if these are hidden options, let's make them a bit more generic since we're introducing more listing types shortly. The code is structured to allow combining multiple listing types together because we will soon add more types the 'builtins'.

'parseopt' remains separate because it has separate (SPC) to match git-completion.bash needs and will not combine with others.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 contrib/completion/git-completion.bash |  2 +-
 git.c                                  | 37 +++++++++++++++++++++-----
 t/t0012-help.sh                        |  2 +-
 3 files changed, 33 insertions(+), 8 deletions(-)
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index a757073945..3556838759 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -3049,7 +3049,7 @@ __git_complete_common () {
 __git_cmds_with_parseopt_helper=
 __git_support_parseopt_helper () {
 	test -n "$__git_cmds_with_parseopt_helper" ||
-		__git_cmds_with_parseopt_helper="$(__git --list-parseopt-builtins)"
+		__git_cmds_with_parseopt_helper="$(__git --list-cmds=parseopt)"
 
 	case " $__git_cmds_with_parseopt_helper " in
 	*" $1 "*)
diff --git a/git.c b/git.c
index 3a89893712..cd85355d81 100644
--- a/git.c
+++ b/git.c
@@ -38,6 +38,30 @@ static int use_pager = -1;
 
 static void list_builtins(unsigned int exclude_option, char sep);
 
+static int match_token(const char *spec, int len, const char *token)
+{
+	int token_len = strlen(token);
+
+	return len == token_len && !strncmp(spec, token, token_len);
+}
+
+static int list_cmds(const char *spec)
+{
+	while (*spec) {
+		const char *sep = strchrnul(spec, ',');
+		int len = sep - spec;
+
+		if (match_token(spec, len, "builtins"))
+			list_builtins(0, '\n');
+		else
+			die(_("unsupported command listing type '%s'"), spec);
+		spec += len;
+		if (*spec == ',')
+			spec++;
+	}
+	return 0;
+}
+
 static void commit_pager_choice(void) {
 	switch (use_pager) {
 	case 0:
@@ -223,12 +247,13 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
 			}
 			(*argv)++;
 			(*argc)--;
-		} else if (!strcmp(cmd, "--list-builtins")) {
-			list_builtins(0, '\n');
-			exit(0);
-		} else if (!strcmp(cmd, "--list-parseopt-builtins")) {
-			list_builtins(NO_PARSEOPT, ' ');
-			exit(0);
+		} else if (skip_prefix(cmd, "--list-cmds=", &cmd)) {
+			if (!strcmp(cmd, "parseopt")) {
+				list_builtins(NO_PARSEOPT, ' ');
+				exit(0);
+			} else {
+				exit(list_cmds(cmd));
+			}
 		} else {
 			fprintf(stderr, _("unknown option: %s\n"), cmd);
 			usage(git_usage_string);
diff --git a/t/t0012-help.sh b/t/t0012-help.sh
index c096f33505..3c91a9024a 100755
--- a/t/t0012-help.sh
+++ b/t/t0012-help.sh
@@ -59,7 +59,7 @@ test_expect_success 'git help' '
 '
 
 test_expect_success 'generate builtin list' '
-	git --list-builtins >builtins
+	git --list-cmds=builtins >builtins
 '
 
 while read builtin
-- 
2.17.0.705.g3525833791
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 8 of 40 in “nd/command-list updates”
  1. 00/14 nd/command-list updatesNguyễn Thái Ngọc Duy, May 19, 2018
  2. 01/14 generate-cmds.sh: factor out synopsis extract codeNguyễn Thái Ngọc Duy, May 19, 2018
  3. 02/14 generate-cmds.sh: export all commands to command-list.hNguyễn Thái Ngọc Duy, May 19, 2018
  4. 06/14 git --list-cmds: collect command list in a string_listNguyễn Thái Ngọc Duy, May 19, 2018
  5. 07/14 completion: implement and use --list-cmds=main,othersNguyễn Thái Ngọc Duy, May 19, 2018
  6. 09/14 help: add "-a --verbose" to list all commands with synopsisNguyễn Thái Ngọc Duy, May 19, 2018
  7. 08/14 git: support --list-cmds=list-<category>Nguyễn Thái Ngọc Duy, May 19, 2018
  8. 05/14 git.c: convert --list-* to --list-cmds=*Nguyễn Thái Ngọc Duy, May 19, 2018
  9. 04/14 Remove common-cmds.hNguyễn Thái Ngọc Duy, May 19, 2018
  10. 14/14 completion: allow to customize the completable command listNguyễn Thái Ngọc Duy, May 19, 2018
  11. SZEDER GáborMay 20, 2018
  12. Duy NguyenMay 20, 2018
  13. 13/14 completion: reduce completable command listNguyễn Thái Ngọc Duy, May 19, 2018
  14. SZEDER GáborMay 20, 2018
  15. Junio C HamanoMay 21, 2018
  16. 10/14 help: use command-list.txt for the source of guidesNguyễn Thái Ngọc Duy, May 19, 2018
  17. 03/14 help: use command-list.h for common command listNguyễn Thái Ngọc Duy, May 19, 2018
  18. 11/14 command-list.txt: documentation and guide lineNguyễn Thái Ngọc Duy, May 19, 2018
  19. 12/14 completion: let git provide the completable command listNguyễn Thái Ngọc Duy, May 19, 2018
  20. SZEDER GáborMay 20, 2018
  21. Duy NguyenMay 20, 2018
  22. 00/17 nd/command-list updatesNguyễn Thái Ngọc Duy, May 20, 2018
  23. 01/17 generate-cmds.sh: factor out synopsis extract codeNguyễn Thái Ngọc Duy, May 20, 2018
  24. 02/17 generate-cmds.sh: export all commands to command-list.hNguyễn Thái Ngọc Duy, May 20, 2018
  25. 03/17 help: use command-list.h for common command listNguyễn Thái Ngọc Duy, May 20, 2018
  26. 07/17 completion: implement and use --list-cmds=main,othersNguyễn Thái Ngọc Duy, May 20, 2018
  27. 06/17 git --list-cmds: collect command list in a string_listNguyễn Thái Ngọc Duy, May 20, 2018
  28. 08/17 git: support --list-cmds=list-<category>Nguyễn Thái Ngọc Duy, May 20, 2018
  29. 13/17 completion: reduce completable command listNguyễn Thái Ngọc Duy, May 20, 2018
  30. 14/17 Move declaration for alias.c to alias.hNguyễn Thái Ngọc Duy, May 20, 2018
  31. 15/17 completion: add and use --list-cmds=nohelpersNguyễn Thái Ngọc Duy, May 20, 2018
  32. 12/17 completion: let git provide the completable command listNguyễn Thái Ngọc Duy, May 20, 2018
  33. 11/17 command-list.txt: documentation and guide lineNguyễn Thái Ngọc Duy, May 20, 2018
  34. 10/17 help: use command-list.txt for the source of guidesNguyễn Thái Ngọc Duy, May 20, 2018
  35. Ævar Arnfjörð BjarmasonNov 20, 2018
  36. 17/17 completion: allow to customize the completable command listNguyễn Thái Ngọc Duy, May 20, 2018
  37. 16/17 completion: add and use --list-cmds=aliasNguyễn Thái Ngọc Duy, May 20, 2018
  38. 05/17 git.c: convert --list-* to --list-cmds=*Nguyễn Thái Ngọc Duy, May 20, 2018
  39. 04/17 Remove common-cmds.hNguyễn Thái Ngọc Duy, May 20, 2018
  40. 09/17 help: add "-a --verbose" to list all commands with synopsisNguyễn Thái Ngọc Duy, May 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.