git --list-cmds: collect command list in a string_list
Instead of printing the command directly one by one, keep them in a list and print at the end. This allows more modification before we print out (e.g. sorting, removing duplicates or even excluding some items). 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
e5d7a61953c236fbc468bc1bb01383766d2cb55b
1 file changed
+17
-5
git.c
+17
-5
@@ -36,7 +36,7 @@ const char git_more_info_string[] =
36
37
static int use_pager = -1;
38
39
-static void list_builtins(unsigned int exclude_option, char sep);
39
+static void list_builtins(struct string_list *list, unsigned int exclude_option);
40
41
static int match_token(const char *spec, int len, const char *token)
42
{
@@ -47,18 +47,24 @@ static int match_token(const char *spec, int len, const char *token)
47
48
static int list_cmds(const char *spec)
49
{
50
+ struct string_list list = STRING_LIST_INIT_DUP;
51
+ int i;
52
+
53
while (*spec) {
54
const char *sep = strchrnul(spec, ',');
55
int len = sep - spec;
56
57
if (match_token(spec, len, "builtins"))
55
- list_builtins(0, '\n');
58
+ list_builtins(&list, 0);
59
else
60
die(_("unsupported command listing type '%s'"), spec);
61
spec += len;
62
if (*spec == ',')
63
spec++;
64
}
65
+ for (i = 0; i < list.nr; i++)
66
+ puts(list.items[i].string);
67
+ string_list_clear(&list, 0);
68
return 0;
69
}
70
@@ -249,7 +255,13 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
255
(*argc)--;
256
} else if (skip_prefix(cmd, "--list-cmds=", &cmd)) {
257
if (!strcmp(cmd, "parseopt")) {
252
- list_builtins(NO_PARSEOPT, ' ');
258
+ struct string_list list = STRING_LIST_INIT_DUP;
259
+ int i;
260
+
261
+ list_builtins(&list, NO_PARSEOPT);
262
+ for (i = 0; i < list.nr; i++)
263
+ printf("%s ", list.items[i].string);
264
+ string_list_clear(&list, 0);
265
exit(0);
266
} else {
267
exit(list_cmds(cmd));
@@ -533,14 +545,14 @@ int is_builtin(const char *s)
545
return !!get_builtin(s);
546
}
547
536
-static void list_builtins(unsigned int exclude_option, char sep)
548
+static void list_builtins(struct string_list *out, unsigned int exclude_option)
549
{
550
int i;
551
for (i = 0; i < ARRAY_SIZE(commands); i++) {
552
if (exclude_option &&
553
(commands[i].option & exclude_option))
554
continue;
543
- printf("%s%c", commands[i].cmd, sep);
555
+ string_list_append(out, commands[i].cmd);
556
}
557
}
558