3 #include "parse-options.h"
9 #include "submodule-config.h"
10 #include "string-list.h"
11 #include "run-command.h"
14 const struct cache_entry **entries;
17 #define MODULE_LIST_INIT { NULL, 0, 0 }
19 static int module_list_compute(int argc, const char **argv,
21 struct pathspec *pathspec,
22 struct module_list *list)
25 char *max_prefix, *ps_matched = NULL;
27 parse_pathspec(pathspec, 0,
28 PATHSPEC_PREFER_FULL |
29 PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP,
32 /* Find common prefix for all pathspec's */
33 max_prefix = common_prefix(pathspec);
34 max_prefix_len = max_prefix ? strlen(max_prefix) : 0;
37 ps_matched = xcalloc(pathspec->nr, 1);
40 die(_("index file corrupt"));
42 for (i = 0; i < active_nr; i++) {
43 const struct cache_entry *ce = active_cache[i];
45 if (!S_ISGITLINK(ce->ce_mode) ||
46 !match_pathspec(pathspec, ce->name, ce_namelen(ce),
47 max_prefix_len, ps_matched, 1))
50 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
51 list->entries[list->nr++] = ce;
52 while (i + 1 < active_nr &&
53 !strcmp(ce->name, active_cache[i + 1]->name))
55 * Skip entries with the same name in different stages
56 * to make sure an entry is returned only once.
62 if (ps_matched && report_path_error(ps_matched, pathspec, prefix))
70 static int module_list(int argc, const char **argv, const char *prefix)
73 struct pathspec pathspec;
74 struct module_list list = MODULE_LIST_INIT;
76 struct option module_list_options[] = {
77 OPT_STRING(0, "prefix", &prefix,
79 N_("alternative anchor for relative paths")),
83 const char *const git_submodule_helper_usage[] = {
84 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
88 argc = parse_options(argc, argv, prefix, module_list_options,
89 git_submodule_helper_usage, 0);
91 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0) {
92 printf("#unmatched\n");
96 for (i = 0; i < list.nr; i++) {
97 const struct cache_entry *ce = list.entries[i];
100 printf("%06o %s U\t", ce->ce_mode, sha1_to_hex(null_sha1));
102 printf("%06o %s %d\t", ce->ce_mode, sha1_to_hex(ce->sha1), ce_stage(ce));
104 utf8_fprintf(stdout, "%s\n", ce->name);
109 static int module_name(int argc, const char **argv, const char *prefix)
111 const struct submodule *sub;
114 usage(_("git submodule--helper name <path>"));
117 sub = submodule_from_path(null_sha1, argv[1]);
120 die(_("no submodule mapping found in .gitmodules for path '%s'"),
123 printf("%s\n", sub->name);
128 static int clone_submodule(const char *path, const char *gitdir, const char *url,
129 const char *depth, const char *reference, int quiet)
131 struct child_process cp;
132 child_process_init(&cp);
134 argv_array_push(&cp.args, "clone");
135 argv_array_push(&cp.args, "--no-checkout");
137 argv_array_push(&cp.args, "--quiet");
139 argv_array_pushl(&cp.args, "--depth", depth, NULL);
140 if (reference && *reference)
141 argv_array_pushl(&cp.args, "--reference", reference, NULL);
142 if (gitdir && *gitdir)
143 argv_array_pushl(&cp.args, "--separate-git-dir", gitdir, NULL);
145 argv_array_push(&cp.args, url);
146 argv_array_push(&cp.args, path);
149 prepare_submodule_repo_env(&cp.env_array);
152 return run_command(&cp);
155 static int module_clone(int argc, const char **argv, const char *prefix)
157 const char *path = NULL, *name = NULL, *url = NULL;
158 const char *reference = NULL, *depth = NULL;
160 FILE *submodule_dot_git;
161 char *sm_gitdir, *cwd, *p;
162 struct strbuf rel_path = STRBUF_INIT;
163 struct strbuf sb = STRBUF_INIT;
165 struct option module_clone_options[] = {
166 OPT_STRING(0, "prefix", &prefix,
168 N_("alternative anchor for relative paths")),
169 OPT_STRING(0, "path", &path,
171 N_("where the new submodule will be cloned to")),
172 OPT_STRING(0, "name", &name,
174 N_("name of the new submodule")),
175 OPT_STRING(0, "url", &url,
177 N_("url where to clone the submodule from")),
178 OPT_STRING(0, "reference", &reference,
180 N_("reference repository")),
181 OPT_STRING(0, "depth", &depth,
183 N_("depth for shallow clones")),
184 OPT__QUIET(&quiet, "Suppress output for cloning a submodule"),
188 const char *const git_submodule_helper_usage[] = {
189 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
190 "[--reference <repository>] [--name <name>] [--depth <depth>] "
191 "--url <url> --path <path>"),
195 argc = parse_options(argc, argv, prefix, module_clone_options,
196 git_submodule_helper_usage, 0);
198 if (argc || !url || !path)
199 usage_with_options(git_submodule_helper_usage,
200 module_clone_options);
202 strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
203 sm_gitdir = strbuf_detach(&sb, NULL);
205 if (!file_exists(sm_gitdir)) {
206 if (safe_create_leading_directories_const(sm_gitdir) < 0)
207 die(_("could not create directory '%s'"), sm_gitdir);
208 if (clone_submodule(path, sm_gitdir, url, depth, reference, quiet))
209 die(_("clone of '%s' into submodule path '%s' failed"),
212 if (safe_create_leading_directories_const(path) < 0)
213 die(_("could not create directory '%s'"), path);
214 strbuf_addf(&sb, "%s/index", sm_gitdir);
215 unlink_or_warn(sb.buf);
219 /* Write a .git file in the submodule to redirect to the superproject. */
220 if (safe_create_leading_directories_const(path) < 0)
221 die(_("could not create directory '%s'"), path);
224 strbuf_addf(&sb, "%s/.git", path);
226 strbuf_addstr(&sb, ".git");
228 if (safe_create_leading_directories_const(sb.buf) < 0)
229 die(_("could not create leading directories of '%s'"), sb.buf);
230 submodule_dot_git = fopen(sb.buf, "w");
231 if (!submodule_dot_git)
232 die_errno(_("cannot open file '%s'"), sb.buf);
234 fprintf(submodule_dot_git, "gitdir: %s\n",
235 relative_path(sm_gitdir, path, &rel_path));
236 if (fclose(submodule_dot_git))
237 die(_("could not close file %s"), sb.buf);
239 strbuf_reset(&rel_path);
242 /* Redirect the worktree of the submodule in the superproject's config */
243 if (!is_absolute_path(sm_gitdir)) {
244 strbuf_addf(&sb, "%s/%s", cwd, sm_gitdir);
246 sm_gitdir = strbuf_detach(&sb, NULL);
249 strbuf_addf(&sb, "%s/%s", cwd, path);
250 p = git_pathdup_submodule(path, "config");
252 die(_("could not get submodule directory for '%s'"), path);
253 git_config_set_in_file(p, "core.worktree",
254 relative_path(sb.buf, sm_gitdir, &rel_path));
256 strbuf_release(&rel_path);
263 static int module_sanitize_config(int argc, const char **argv, const char *prefix)
265 struct strbuf sanitized_config = STRBUF_INIT;
268 usage(_("git submodule--helper sanitize-config"));
270 git_config_from_parameters(sanitize_submodule_config, &sanitized_config);
271 if (sanitized_config.len)
272 printf("%s\n", sanitized_config.buf);
274 strbuf_release(&sanitized_config);
281 int (*fn)(int, const char **, const char *);
284 static struct cmd_struct commands[] = {
285 {"list", module_list},
286 {"name", module_name},
287 {"clone", module_clone},
288 {"sanitize-config", module_sanitize_config},
291 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
295 die(_("fatal: submodule--helper subcommand must be "
296 "called with a subcommand"));
298 for (i = 0; i < ARRAY_SIZE(commands); i++)
299 if (!strcmp(argv[1], commands[i].cmd))
300 return commands[i].fn(argc - 1, argv + 1, prefix);
302 die(_("fatal: '%s' is not a valid submodule--helper "
303 "subcommand"), argv[1]);