Skip to content

Commit e5d7a61

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0089521 commit e5d7a61

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

git.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const char git_more_info_string[] =
3636

3737
static int use_pager = -1;
3838

39-
static void list_builtins(unsigned int exclude_option, char sep);
39+
static void list_builtins(struct string_list *list, unsigned int exclude_option);
4040

4141
static int match_token(const char *spec, int len, const char *token)
4242
{
@@ -47,18 +47,24 @@ static int match_token(const char *spec, int len, const char *token)
4747

4848
static int list_cmds(const char *spec)
4949
{
50+
struct string_list list = STRING_LIST_INIT_DUP;
51+
int i;
52+
5053
while (*spec) {
5154
const char *sep = strchrnul(spec, ',');
5255
int len = sep - spec;
5356

5457
if (match_token(spec, len, "builtins"))
55-
list_builtins(0, '\n');
58+
list_builtins(&list, 0);
5659
else
5760
die(_("unsupported command listing type '%s'"), spec);
5861
spec += len;
5962
if (*spec == ',')
6063
spec++;
6164
}
65+
for (i = 0; i < list.nr; i++)
66+
puts(list.items[i].string);
67+
string_list_clear(&list, 0);
6268
return 0;
6369
}
6470

@@ -249,7 +255,13 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
249255
(*argc)--;
250256
} else if (skip_prefix(cmd, "--list-cmds=", &cmd)) {
251257
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);
253265
exit(0);
254266
} else {
255267
exit(list_cmds(cmd));
@@ -533,14 +545,14 @@ int is_builtin(const char *s)
533545
return !!get_builtin(s);
534546
}
535547

536-
static void list_builtins(unsigned int exclude_option, char sep)
548+
static void list_builtins(struct string_list *out, unsigned int exclude_option)
537549
{
538550
int i;
539551
for (i = 0; i < ARRAY_SIZE(commands); i++) {
540552
if (exclude_option &&
541553
(commands[i].option & exclude_option))
542554
continue;
543-
printf("%s%c", commands[i].cmd, sep);
555+
string_list_append(out, commands[i].cmd);
544556
}
545557
}
546558

0 commit comments

Comments
 (0)