3 #include "parse-options.h"
10 const struct cache_entry **entries;
13 #define MODULE_LIST_INIT { NULL, 0, 0 }
15 static int module_list_compute(int argc, const char **argv,
17 struct pathspec *pathspec,
18 struct module_list *list)
21 char *max_prefix, *ps_matched = NULL;
23 parse_pathspec(pathspec, 0,
24 PATHSPEC_PREFER_FULL |
25 PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP,
28 /* Find common prefix for all pathspec's */
29 max_prefix = common_prefix(pathspec);
30 max_prefix_len = max_prefix ? strlen(max_prefix) : 0;
33 ps_matched = xcalloc(pathspec->nr, 1);
36 die(_("index file corrupt"));
38 for (i = 0; i < active_nr; i++) {
39 const struct cache_entry *ce = active_cache[i];
41 if (!S_ISGITLINK(ce->ce_mode) ||
42 !match_pathspec(pathspec, ce->name, ce_namelen(ce),
43 max_prefix_len, ps_matched, 1))
46 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
47 list->entries[list->nr++] = ce;
48 while (i + 1 < active_nr &&
49 !strcmp(ce->name, active_cache[i + 1]->name))
51 * Skip entries with the same name in different stages
52 * to make sure an entry is returned only once.
58 if (ps_matched && report_path_error(ps_matched, pathspec, prefix))
66 static int module_list(int argc, const char **argv, const char *prefix)
69 struct pathspec pathspec;
70 struct module_list list = MODULE_LIST_INIT;
72 struct option module_list_options[] = {
73 OPT_STRING(0, "prefix", &prefix,
75 N_("alternative anchor for relative paths")),
79 const char *const git_submodule_helper_usage[] = {
80 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
84 argc = parse_options(argc, argv, prefix, module_list_options,
85 git_submodule_helper_usage, 0);
87 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) {
88 printf("#unmatched\n");
92 for (i = 0; i < list.nr; i++) {
93 const struct cache_entry *ce = list.entries[i];
96 printf("%06o %s U\t", ce->ce_mode, sha1_to_hex(null_sha1));
98 printf("%06o %s %d\t", ce->ce_mode, sha1_to_hex(ce->sha1), ce_stage(ce));
100 utf8_fprintf(stdout, "%s\n", ce->name);
108 int (*fn)(int, const char **, const char *);
111 static struct cmd_struct commands[] = {
112 {"list", module_list},
115 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
119 die(_("fatal: submodule--helper subcommand must be "
120 "called with a subcommand"));
122 for (i = 0; i < ARRAY_SIZE(commands); i++)
123 if (!strcmp(argv[1], commands[i].cmd))
124 return commands[i].fn(argc - 1, argv + 1, prefix);
126 die(_("fatal: '%s' is not a valid submodule--helper "
127 "subcommand"), argv[1]);