Skip tests that fail due to incomplete implementations, missing tools...
[git/mingw/j6t.git] / builtin / submodule--helper.c
blob67dba1ced44887d5b2930fa042549b7744bb7431
1 #include "builtin.h"
2 #include "cache.h"
3 #include "parse-options.h"
4 #include "quote.h"
5 #include "pathspec.h"
6 #include "dir.h"
7 #include "utf8.h"
8 #include "submodule.h"
9 #include "submodule-config.h"
10 #include "string-list.h"
11 #include "run-command.h"
13 struct module_list {
14 const struct cache_entry **entries;
15 int alloc, nr;
17 #define MODULE_LIST_INIT { NULL, 0, 0 }
19 static int module_list_compute(int argc, const char **argv,
20 const char *prefix,
21 struct pathspec *pathspec,
22 struct module_list *list)
24 int i, result = 0;
25 char *max_prefix, *ps_matched = NULL;
26 int max_prefix_len;
27 parse_pathspec(pathspec, 0,
28 PATHSPEC_PREFER_FULL |
29 PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP,
30 prefix, argv);
32 /* Find common prefix for all pathspec's */
33 max_prefix = common_prefix(pathspec);
34 max_prefix_len = max_prefix ? strlen(max_prefix) : 0;
36 if (pathspec->nr)
37 ps_matched = xcalloc(pathspec->nr, 1);
39 if (read_cache() < 0)
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))
48 continue;
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.
58 i++;
60 free(max_prefix);
62 if (ps_matched && report_path_error(ps_matched, pathspec, prefix))
63 result = -1;
65 free(ps_matched);
67 return result;
70 static int module_list(int argc, const char **argv, const char *prefix)
72 int i;
73 struct pathspec pathspec;
74 struct module_list list = MODULE_LIST_INIT;
76 struct option module_list_options[] = {
77 OPT_STRING(0, "prefix", &prefix,
78 N_("path"),
79 N_("alternative anchor for relative paths")),
80 OPT_END()
83 const char *const git_submodule_helper_usage[] = {
84 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
85 NULL
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");
93 return 1;
96 for (i = 0; i < list.nr; i++) {
97 const struct cache_entry *ce = list.entries[i];
99 if (ce_stage(ce))
100 printf("%06o %s U\t", ce->ce_mode, sha1_to_hex(null_sha1));
101 else
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);
106 return 0;
109 static int module_name(int argc, const char **argv, const char *prefix)
111 const struct submodule *sub;
113 if (argc != 2)
114 usage(_("git submodule--helper name <path>"));
116 gitmodules_config();
117 sub = submodule_from_path(null_sha1, argv[1]);
119 if (!sub)
120 die(_("no submodule mapping found in .gitmodules for path '%s'"),
121 argv[1]);
123 printf("%s\n", sub->name);
125 return 0;
127 static int clone_submodule(const char *path, const char *gitdir, const char *url,
128 const char *depth, const char *reference, int quiet)
130 struct child_process cp;
131 child_process_init(&cp);
133 argv_array_push(&cp.args, "clone");
134 argv_array_push(&cp.args, "--no-checkout");
135 if (quiet)
136 argv_array_push(&cp.args, "--quiet");
137 if (depth && *depth)
138 argv_array_pushl(&cp.args, "--depth", depth, NULL);
139 if (reference && *reference)
140 argv_array_pushl(&cp.args, "--reference", reference, NULL);
141 if (gitdir && *gitdir)
142 argv_array_pushl(&cp.args, "--separate-git-dir", gitdir, NULL);
144 argv_array_push(&cp.args, url);
145 argv_array_push(&cp.args, path);
147 cp.git_cmd = 1;
148 cp.env = local_repo_env;
149 cp.no_stdin = 1;
151 return run_command(&cp);
154 static int module_clone(int argc, const char **argv, const char *prefix)
156 const char *path = NULL, *name = NULL, *url = NULL;
157 const char *reference = NULL, *depth = NULL;
158 int quiet = 0;
159 FILE *submodule_dot_git;
160 char *sm_gitdir, *cwd, *p;
161 struct strbuf rel_path = STRBUF_INIT;
162 struct strbuf sb = STRBUF_INIT;
164 struct option module_clone_options[] = {
165 OPT_STRING(0, "prefix", &prefix,
166 N_("path"),
167 N_("alternative anchor for relative paths")),
168 OPT_STRING(0, "path", &path,
169 N_("path"),
170 N_("where the new submodule will be cloned to")),
171 OPT_STRING(0, "name", &name,
172 N_("string"),
173 N_("name of the new submodule")),
174 OPT_STRING(0, "url", &url,
175 N_("string"),
176 N_("url where to clone the submodule from")),
177 OPT_STRING(0, "reference", &reference,
178 N_("string"),
179 N_("reference repository")),
180 OPT_STRING(0, "depth", &depth,
181 N_("string"),
182 N_("depth for shallow clones")),
183 OPT__QUIET(&quiet, "Suppress output for cloning a submodule"),
184 OPT_END()
187 const char *const git_submodule_helper_usage[] = {
188 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
189 "[--reference <repository>] [--name <name>] [--url <url>]"
190 "[--depth <depth>] [--] [<path>...]"),
191 NULL
194 argc = parse_options(argc, argv, prefix, module_clone_options,
195 git_submodule_helper_usage, 0);
197 strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
198 sm_gitdir = strbuf_detach(&sb, NULL);
200 if (!file_exists(sm_gitdir)) {
201 if (safe_create_leading_directories_const(sm_gitdir) < 0)
202 die(_("could not create directory '%s'"), sm_gitdir);
203 if (clone_submodule(path, sm_gitdir, url, depth, reference, quiet))
204 die(_("clone of '%s' into submodule path '%s' failed"),
205 url, path);
206 } else {
207 if (safe_create_leading_directories_const(path) < 0)
208 die(_("could not create directory '%s'"), path);
209 strbuf_addf(&sb, "%s/index", sm_gitdir);
210 unlink_or_warn(sb.buf);
211 strbuf_reset(&sb);
214 /* Write a .git file in the submodule to redirect to the superproject. */
215 if (safe_create_leading_directories_const(path) < 0)
216 die(_("could not create directory '%s'"), path);
218 if (path && *path)
219 strbuf_addf(&sb, "%s/.git", path);
220 else
221 strbuf_addstr(&sb, ".git");
223 if (safe_create_leading_directories_const(sb.buf) < 0)
224 die(_("could not create leading directories of '%s'"), sb.buf);
225 submodule_dot_git = fopen(sb.buf, "w");
226 if (!submodule_dot_git)
227 die_errno(_("cannot open file '%s'"), sb.buf);
229 fprintf(submodule_dot_git, "gitdir: %s\n",
230 relative_path(sm_gitdir, path, &rel_path));
231 if (fclose(submodule_dot_git))
232 die(_("could not close file %s"), sb.buf);
233 strbuf_reset(&sb);
234 strbuf_reset(&rel_path);
236 cwd = xgetcwd();
237 /* Redirect the worktree of the submodule in the superproject's config */
238 if (!is_absolute_path(sm_gitdir)) {
239 strbuf_addf(&sb, "%s/%s", cwd, sm_gitdir);
240 free(sm_gitdir);
241 sm_gitdir = strbuf_detach(&sb, NULL);
244 strbuf_addf(&sb, "%s/%s", cwd, path);
245 p = git_pathdup_submodule(path, "config");
246 if (!p)
247 die(_("could not get submodule directory for '%s'"), path);
248 git_config_set_in_file(p, "core.worktree",
249 relative_path(sb.buf, sm_gitdir, &rel_path));
250 strbuf_release(&sb);
251 strbuf_release(&rel_path);
252 free(sm_gitdir);
253 free(cwd);
254 free(p);
255 return 0;
258 static int git_submodule_config(const char *var, const char *value, void *cb)
260 return parse_submodule_config_option(var, value);
263 struct submodule_update_clone {
264 int count;
265 int quiet;
266 int print_unmatched;
267 char *reference;
268 char *depth;
269 char *update;
270 const char *recursive_prefix;
271 const char *prefix;
272 struct module_list list;
273 struct string_list projectlines;
274 struct pathspec pathspec;
276 #define SUBMODULE_UPDATE_CLONE_INIT {0, 0, 0, NULL, NULL, NULL, NULL, NULL, MODULE_LIST_INIT, STRING_LIST_INIT_DUP}
278 static void fill_clone_command(struct child_process *cp, int quiet,
279 const char *prefix, const char *path,
280 const char *name, const char *url,
281 const char *reference, const char *depth)
283 cp->git_cmd = 1;
284 cp->no_stdin = 1;
285 cp->stdout_to_stderr = 1;
286 cp->err = -1;
287 argv_array_push(&cp->args, "submodule--helper");
288 argv_array_push(&cp->args, "clone");
289 if (quiet)
290 argv_array_push(&cp->args, "--quiet");
292 if (prefix) {
293 argv_array_push(&cp->args, "--prefix");
294 argv_array_push(&cp->args, prefix);
296 argv_array_push(&cp->args, "--path");
297 argv_array_push(&cp->args, path);
299 argv_array_push(&cp->args, "--name");
300 argv_array_push(&cp->args, name);
302 argv_array_push(&cp->args, "--url");
303 argv_array_push(&cp->args, url);
304 if (reference)
305 argv_array_push(&cp->args, reference);
306 if (depth)
307 argv_array_push(&cp->args, depth);
310 static int update_clone_get_next_task(void **pp_task_cb,
311 struct child_process *cp,
312 struct strbuf *err,
313 void *pp_cb)
315 struct submodule_update_clone *pp = pp_cb;
317 for (; pp->count < pp->list.nr; pp->count++) {
318 const struct submodule *sub = NULL;
319 const char *displaypath = NULL;
320 const struct cache_entry *ce = pp->list.entries[pp->count];
321 struct strbuf sb = STRBUF_INIT;
322 const char *update_module = NULL;
323 char *url = NULL;
324 int just_cloned = 0;
326 if (ce_stage(ce)) {
327 if (pp->recursive_prefix)
328 strbuf_addf(err, "Skipping unmerged submodule %s/%s\n",
329 pp->recursive_prefix, ce->name);
330 else
331 strbuf_addf(err, "Skipping unmerged submodule %s\n",
332 ce->name);
333 continue;
336 sub = submodule_from_path(null_sha1, ce->name);
337 if (!sub) {
338 strbuf_addf(err, "BUG: internal error managing submodules. "
339 "The cache could not locate '%s'", ce->name);
340 pp->print_unmatched = 1;
341 return 0;
344 if (pp->recursive_prefix)
345 displaypath = relative_path(pp->recursive_prefix, ce->name, &sb);
346 else
347 displaypath = ce->name;
349 if (pp->update)
350 update_module = pp->update;
351 if (!update_module)
352 update_module = sub->update;
353 if (!update_module)
354 update_module = "checkout";
355 if (!strcmp(update_module, "none")) {
356 strbuf_addf(err, "Skipping submodule '%s'\n", displaypath);
357 continue;
361 * Looking up the url in .git/config.
362 * We cannot fall back to .gitmodules as we only want to process
363 * configured submodules. This renders the submodule lookup API
364 * useless, as it cannot lookup without fallback.
366 strbuf_reset(&sb);
367 strbuf_addf(&sb, "submodule.%s.url", sub->name);
368 git_config_get_string(sb.buf, &url);
369 if (!url) {
371 * Only mention uninitialized submodules when its
372 * path have been specified
374 if (pp->pathspec.nr)
375 strbuf_addf(err, _("Submodule path '%s' not initialized\n"
376 "Maybe you want to use 'update --init'?"), displaypath);
377 continue;
380 strbuf_reset(&sb);
381 strbuf_addf(&sb, "%s/.git", ce->name);
382 just_cloned = !file_exists(sb.buf);
384 strbuf_reset(&sb);
385 strbuf_addf(&sb, "%06o %s %d %d\t%s\n", ce->ce_mode,
386 sha1_to_hex(ce->sha1), ce_stage(ce),
387 just_cloned, ce->name);
388 string_list_append(&pp->projectlines, sb.buf);
390 if (just_cloned) {
391 fill_clone_command(cp, pp->quiet, pp->prefix, ce->name,
392 sub->name, url, pp->reference, pp->depth);
393 pp->count++;
394 free(url);
395 return 1;
396 } else
397 free(url);
399 return 0;
402 static int update_clone_start_failure(struct child_process *cp,
403 struct strbuf *err,
404 void *pp_cb,
405 void *pp_task_cb)
407 struct submodule_update_clone *pp = pp_cb;
409 strbuf_addf(err, "error when starting a child process");
410 pp->print_unmatched = 1;
412 return 1;
415 static int update_clone_task_finished(int result,
416 struct child_process *cp,
417 struct strbuf *err,
418 void *pp_cb,
419 void *pp_task_cb)
421 struct submodule_update_clone *pp = pp_cb;
423 if (!result) {
424 return 0;
425 } else {
426 strbuf_addf(err, "error in one child process");
427 pp->print_unmatched = 1;
428 return 1;
432 static int update_clone(int argc, const char **argv, const char *prefix)
434 int max_jobs = -1;
435 struct string_list_item *item;
436 struct submodule_update_clone pp = SUBMODULE_UPDATE_CLONE_INIT;
438 struct option module_list_options[] = {
439 OPT_STRING(0, "prefix", &prefix,
440 N_("path"),
441 N_("path into the working tree")),
442 OPT_STRING(0, "recursive_prefix", &pp.recursive_prefix,
443 N_("path"),
444 N_("path into the working tree, across nested "
445 "submodule boundaries")),
446 OPT_STRING(0, "update", &pp.update,
447 N_("string"),
448 N_("update command for submodules")),
449 OPT_STRING(0, "reference", &pp.reference, "<repository>",
450 N_("Use the local reference repository "
451 "instead of a full clone")),
452 OPT_STRING(0, "depth", &pp.depth, "<depth>",
453 N_("Create a shallow clone truncated to the "
454 "specified number of revisions")),
455 OPT_INTEGER('j', "jobs", &max_jobs,
456 N_("parallel jobs")),
457 OPT__QUIET(&pp.quiet, N_("do't print cloning progress")),
458 OPT_END()
461 const char *const git_submodule_helper_usage[] = {
462 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
463 NULL
465 pp.prefix = prefix;
467 argc = parse_options(argc, argv, prefix, module_list_options,
468 git_submodule_helper_usage, 0);
470 if (module_list_compute(argc, argv, prefix, &pp.pathspec, &pp.list) < 0) {
471 printf("#unmatched\n");
472 return 1;
475 gitmodules_config();
476 /* Overlay the parsed .gitmodules file with .git/config */
477 git_config(git_submodule_config, NULL);
479 if (max_jobs < 0)
480 max_jobs = config_parallel_submodules();
481 if (max_jobs < 0)
482 max_jobs = 1;
484 run_processes_parallel(max_jobs,
485 update_clone_get_next_task,
486 update_clone_start_failure,
487 update_clone_task_finished,
488 &pp);
490 if (pp.print_unmatched) {
491 printf("#unmatched\n");
492 return 1;
495 for_each_string_list_item(item, &pp.projectlines) {
496 utf8_fprintf(stdout, "%s", item->string);
498 return 0;
501 struct cmd_struct {
502 const char *cmd;
503 int (*fn)(int, const char **, const char *);
506 static struct cmd_struct commands[] = {
507 {"list", module_list},
508 {"name", module_name},
509 {"clone", module_clone},
510 {"update-clone", update_clone}
513 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
515 int i;
516 if (argc < 2)
517 die(_("fatal: submodule--helper subcommand must be "
518 "called with a subcommand"));
520 for (i = 0; i < ARRAY_SIZE(commands); i++)
521 if (!strcmp(argv[1], commands[i].cmd))
522 return commands[i].fn(argc - 1, argv + 1, prefix);
524 die(_("fatal: '%s' is not a valid submodule--helper "
525 "subcommand"), argv[1]);