git.c: convert --list-* to --list-cmds=*
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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
May 20, 2018 at 20:39 UTC
0089521cacd99db8018b7a31e205dad0bf0738c7
4 files changed
+39
-8
Documentation/git.txt
+6
@@ -163,6 +163,12 @@ foo.bar= ...`) sets `foo.bar` to the empty string which `git config
163
Do not perform optional operations that require locks. This is
164
equivalent to setting the `GIT_OPTIONAL_LOCKS` to `0`.
165
166
+--list-cmds=group[,group...]::
167
+ List commands by group. This is an internal/experimental
168
+ option and may change or be removed in the future. Supported
169
+ groups are: builtins, parseopt (builtin commands that use
170
+ parse-options).
171
+
172
GIT COMMANDS
173
------------
174
contrib/completion/git-completion.bash
+1
-1
@@ -3049,7 +3049,7 @@ __git_complete_common () {
3049
__git_cmds_with_parseopt_helper=
3050
__git_support_parseopt_helper () {
3051
test -n "$__git_cmds_with_parseopt_helper" ||
3052
- __git_cmds_with_parseopt_helper="$(__git --list-parseopt-builtins)"
3052
+ __git_cmds_with_parseopt_helper="$(__git --list-cmds=parseopt)"
3053
3054
case " $__git_cmds_with_parseopt_helper " in
3055
*" $1 "*)
git.c
+31
-6
@@ -38,6 +38,30 @@ static int use_pager = -1;
38
39
static void list_builtins(unsigned int exclude_option, char sep);
40
41
+static int match_token(const char *spec, int len, const char *token)
42
+{
43
+ int token_len = strlen(token);
44
+
45
+ return len == token_len && !strncmp(spec, token, token_len);
46
+}
47
+
48
+static int list_cmds(const char *spec)
49
+{
50
+ while (*spec) {
51
+ const char *sep = strchrnul(spec, ',');
52
+ int len = sep - spec;
53
+
54
+ if (match_token(spec, len, "builtins"))
55
+ list_builtins(0, '\n');
56
+ else
57
+ die(_("unsupported command listing type '%s'"), spec);
58
+ spec += len;
59
+ if (*spec == ',')
60
+ spec++;
61
+ }
62
+ return 0;
63
+}
64
+
65
static void commit_pager_choice(void) {
66
switch (use_pager) {
67
case 0:
@@ -223,12 +247,13 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
247
}
248
(*argv)++;
249
(*argc)--;
226
- } else if (!strcmp(cmd, "--list-builtins")) {
227
- list_builtins(0, '\n');
228
- exit(0);
229
- } else if (!strcmp(cmd, "--list-parseopt-builtins")) {
230
- list_builtins(NO_PARSEOPT, ' ');
231
- exit(0);
250
+ } else if (skip_prefix(cmd, "--list-cmds=", &cmd)) {
251
+ if (!strcmp(cmd, "parseopt")) {
252
+ list_builtins(NO_PARSEOPT, ' ');
253
+ exit(0);
254
+ } else {
255
+ exit(list_cmds(cmd));
256
+ }
257
} else {
258
fprintf(stderr, _("unknown option: %s\n"), cmd);
259
usage(git_usage_string);
t/t0012-help.sh
+1
-1
@@ -59,7 +59,7 @@ test_expect_success 'git help' '
59
'
60
61
test_expect_success 'generate builtin list' '
62
- git --list-builtins >builtins
62
+ git --list-cmds=builtins >builtins
63
'
64
65
while read builtin