summaryrefslogtreecommitdiff
path: root/git.c
diff options
context:
space:
mode:
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>2018-05-20 18:39:58 (GMT)
committerJunio C Hamano <gitster@pobox.com>2018-05-21 04:23:14 (GMT)
commite5d7a61953c236fbc468bc1bb01383766d2cb55b (patch)
tree441d42216f06365007c97399104a25a56c6faf38 /git.c
parent0089521cacd99db8018b7a31e205dad0bf0738c7 (diff)
downloadgit-e5d7a61953c236fbc468bc1bb01383766d2cb55b.zip
git-e5d7a61953c236fbc468bc1bb01383766d2cb55b.tar.gz
git-e5d7a61953c236fbc468bc1bb01383766d2cb55b.tar.bz2
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>
Diffstat (limited to 'git.c')
-rw-r--r--git.c22
1 files changed, 17 insertions, 5 deletions
diff --git a/git.c b/git.c
index cd85355..376a59b 100644
--- a/git.c
+++ b/git.c
@@ -36,7 +36,7 @@ const char git_more_info_string[] =
static int use_pager = -1;
-static void list_builtins(unsigned int exclude_option, char sep);
+static void list_builtins(struct string_list *list, unsigned int exclude_option);
static int match_token(const char *spec, int len, const char *token)
{
@@ -47,18 +47,24 @@ static int match_token(const char *spec, int len, const char *token)
static int list_cmds(const char *spec)
{
+ struct string_list list = STRING_LIST_INIT_DUP;
+ int i;
+
while (*spec) {
const char *sep = strchrnul(spec, ',');
int len = sep - spec;
if (match_token(spec, len, "builtins"))
- list_builtins(0, '\n');
+ list_builtins(&list, 0);
else
die(_("unsupported command listing type '%s'"), spec);
spec += len;
if (*spec == ',')
spec++;
}
+ for (i = 0; i < list.nr; i++)
+ puts(list.items[i].string);
+ string_list_clear(&list, 0);
return 0;
}
@@ -249,7 +255,13 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
(*argc)--;
} else if (skip_prefix(cmd, "--list-cmds=", &cmd)) {
if (!strcmp(cmd, "parseopt")) {
- list_builtins(NO_PARSEOPT, ' ');
+ struct string_list list = STRING_LIST_INIT_DUP;
+ int i;
+
+ list_builtins(&list, NO_PARSEOPT);
+ for (i = 0; i < list.nr; i++)
+ printf("%s ", list.items[i].string);
+ string_list_clear(&list, 0);
exit(0);
} else {
exit(list_cmds(cmd));
@@ -533,14 +545,14 @@ int is_builtin(const char *s)
return !!get_builtin(s);
}
-static void list_builtins(unsigned int exclude_option, char sep)
+static void list_builtins(struct string_list *out, unsigned int exclude_option)
{
int i;
for (i = 0; i < ARRAY_SIZE(commands); i++) {
if (exclude_option &&
(commands[i].option & exclude_option))
continue;
- printf("%s%c", commands[i].cmd, sep);
+ string_list_append(out, commands[i].cmd);
}
}