]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/submodule--helper.c
Merge branch 'jk/clone-allow-bare-and-o-together'
[thirdparty/git.git] / builtin / submodule--helper.c
1 #define USE_THE_INDEX_COMPATIBILITY_MACROS
2 #include "builtin.h"
3 #include "repository.h"
4 #include "cache.h"
5 #include "config.h"
6 #include "parse-options.h"
7 #include "quote.h"
8 #include "pathspec.h"
9 #include "dir.h"
10 #include "submodule.h"
11 #include "submodule-config.h"
12 #include "string-list.h"
13 #include "run-command.h"
14 #include "remote.h"
15 #include "refs.h"
16 #include "refspec.h"
17 #include "connect.h"
18 #include "revision.h"
19 #include "diffcore.h"
20 #include "diff.h"
21 #include "object-store.h"
22 #include "advice.h"
23 #include "branch.h"
24 #include "list-objects-filter-options.h"
25
26 #define OPT_QUIET (1 << 0)
27 #define OPT_CACHED (1 << 1)
28 #define OPT_RECURSIVE (1 << 2)
29 #define OPT_FORCE (1 << 3)
30
31 typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
32 void *cb_data);
33
34 static int repo_get_default_remote(struct repository *repo, char **default_remote)
35 {
36 char *dest = NULL;
37 struct strbuf sb = STRBUF_INIT;
38 struct ref_store *store = get_main_ref_store(repo);
39 const char *refname = refs_resolve_ref_unsafe(store, "HEAD", 0, NULL,
40 NULL);
41
42 if (!refname)
43 return die_message(_("No such ref: %s"), "HEAD");
44
45 /* detached HEAD */
46 if (!strcmp(refname, "HEAD")) {
47 *default_remote = xstrdup("origin");
48 return 0;
49 }
50
51 if (!skip_prefix(refname, "refs/heads/", &refname))
52 return die_message(_("Expecting a full ref name, got %s"),
53 refname);
54
55 strbuf_addf(&sb, "branch.%s.remote", refname);
56 if (repo_config_get_string(repo, sb.buf, &dest))
57 *default_remote = xstrdup("origin");
58 else
59 *default_remote = dest;
60
61 strbuf_release(&sb);
62 return 0;
63 }
64
65 static int get_default_remote_submodule(const char *module_path, char **default_remote)
66 {
67 struct repository subrepo;
68 int ret;
69
70 if (repo_submodule_init(&subrepo, the_repository, module_path,
71 null_oid()) < 0)
72 return die_message(_("could not get a repository handle for submodule '%s'"),
73 module_path);
74 ret = repo_get_default_remote(&subrepo, default_remote);
75 repo_clear(&subrepo);
76
77 return ret;
78 }
79
80 static char *get_default_remote(void)
81 {
82 char *default_remote;
83 int code = repo_get_default_remote(the_repository, &default_remote);
84
85 if (code)
86 exit(code);
87
88 return default_remote;
89 }
90
91 static char *resolve_relative_url(const char *rel_url, const char *up_path, int quiet)
92 {
93 char *remoteurl, *resolved_url;
94 char *remote = get_default_remote();
95 struct strbuf remotesb = STRBUF_INIT;
96
97 strbuf_addf(&remotesb, "remote.%s.url", remote);
98 if (git_config_get_string(remotesb.buf, &remoteurl)) {
99 if (!quiet)
100 warning(_("could not look up configuration '%s'. "
101 "Assuming this repository is its own "
102 "authoritative upstream."),
103 remotesb.buf);
104 remoteurl = xgetcwd();
105 }
106 resolved_url = relative_url(remoteurl, rel_url, up_path);
107
108 free(remote);
109 free(remoteurl);
110 strbuf_release(&remotesb);
111
112 return resolved_url;
113 }
114
115 /* the result should be freed by the caller. */
116 static char *get_submodule_displaypath(const char *path, const char *prefix)
117 {
118 const char *super_prefix = get_super_prefix();
119
120 if (prefix && super_prefix) {
121 BUG("cannot have prefix '%s' and superprefix '%s'",
122 prefix, super_prefix);
123 } else if (prefix) {
124 struct strbuf sb = STRBUF_INIT;
125 char *displaypath = xstrdup(relative_path(path, prefix, &sb));
126 strbuf_release(&sb);
127 return displaypath;
128 } else if (super_prefix) {
129 return xstrfmt("%s%s", super_prefix, path);
130 } else {
131 return xstrdup(path);
132 }
133 }
134
135 static char *compute_rev_name(const char *sub_path, const char* object_id)
136 {
137 struct strbuf sb = STRBUF_INIT;
138 const char ***d;
139
140 static const char *describe_bare[] = { NULL };
141
142 static const char *describe_tags[] = { "--tags", NULL };
143
144 static const char *describe_contains[] = { "--contains", NULL };
145
146 static const char *describe_all_always[] = { "--all", "--always", NULL };
147
148 static const char **describe_argv[] = { describe_bare, describe_tags,
149 describe_contains,
150 describe_all_always, NULL };
151
152 for (d = describe_argv; *d; d++) {
153 struct child_process cp = CHILD_PROCESS_INIT;
154 prepare_submodule_repo_env(&cp.env);
155 cp.dir = sub_path;
156 cp.git_cmd = 1;
157 cp.no_stderr = 1;
158
159 strvec_push(&cp.args, "describe");
160 strvec_pushv(&cp.args, *d);
161 strvec_push(&cp.args, object_id);
162
163 if (!capture_command(&cp, &sb, 0)) {
164 strbuf_strip_suffix(&sb, "\n");
165 return strbuf_detach(&sb, NULL);
166 }
167 }
168
169 strbuf_release(&sb);
170 return NULL;
171 }
172
173 struct module_list {
174 const struct cache_entry **entries;
175 int alloc, nr;
176 };
177 #define MODULE_LIST_INIT { 0 }
178
179 static void module_list_release(struct module_list *ml)
180 {
181 free(ml->entries);
182 }
183
184 static int module_list_compute(int argc, const char **argv,
185 const char *prefix,
186 struct pathspec *pathspec,
187 struct module_list *list)
188 {
189 int i, result = 0;
190 char *ps_matched = NULL;
191
192 parse_pathspec(pathspec, 0,
193 PATHSPEC_PREFER_FULL,
194 prefix, argv);
195
196 if (pathspec->nr)
197 ps_matched = xcalloc(pathspec->nr, 1);
198
199 if (read_cache() < 0)
200 die(_("index file corrupt"));
201
202 for (i = 0; i < active_nr; i++) {
203 const struct cache_entry *ce = active_cache[i];
204
205 if (!match_pathspec(&the_index, pathspec, ce->name, ce_namelen(ce),
206 0, ps_matched, 1) ||
207 !S_ISGITLINK(ce->ce_mode))
208 continue;
209
210 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
211 list->entries[list->nr++] = ce;
212 while (i + 1 < active_nr &&
213 !strcmp(ce->name, active_cache[i + 1]->name))
214 /*
215 * Skip entries with the same name in different stages
216 * to make sure an entry is returned only once.
217 */
218 i++;
219 }
220
221 if (ps_matched && report_path_error(ps_matched, pathspec))
222 result = -1;
223
224 free(ps_matched);
225
226 return result;
227 }
228
229 static void module_list_active(struct module_list *list)
230 {
231 int i;
232 struct module_list active_modules = MODULE_LIST_INIT;
233
234 for (i = 0; i < list->nr; i++) {
235 const struct cache_entry *ce = list->entries[i];
236
237 if (!is_submodule_active(the_repository, ce->name))
238 continue;
239
240 ALLOC_GROW(active_modules.entries,
241 active_modules.nr + 1,
242 active_modules.alloc);
243 active_modules.entries[active_modules.nr++] = ce;
244 }
245
246 module_list_release(list);
247 *list = active_modules;
248 }
249
250 static char *get_up_path(const char *path)
251 {
252 int i;
253 struct strbuf sb = STRBUF_INIT;
254
255 for (i = count_slashes(path); i; i--)
256 strbuf_addstr(&sb, "../");
257
258 /*
259 * Check if 'path' ends with slash or not
260 * for having the same output for dir/sub_dir
261 * and dir/sub_dir/
262 */
263 if (!is_dir_sep(path[strlen(path) - 1]))
264 strbuf_addstr(&sb, "../");
265
266 return strbuf_detach(&sb, NULL);
267 }
268
269 static void for_each_listed_submodule(const struct module_list *list,
270 each_submodule_fn fn, void *cb_data)
271 {
272 int i;
273
274 for (i = 0; i < list->nr; i++)
275 fn(list->entries[i], cb_data);
276 }
277
278 struct foreach_cb {
279 int argc;
280 const char **argv;
281 const char *prefix;
282 int quiet;
283 int recursive;
284 };
285 #define FOREACH_CB_INIT { 0 }
286
287 static void runcommand_in_submodule_cb(const struct cache_entry *list_item,
288 void *cb_data)
289 {
290 struct foreach_cb *info = cb_data;
291 const char *path = list_item->name;
292 const struct object_id *ce_oid = &list_item->oid;
293 const struct submodule *sub;
294 struct child_process cp = CHILD_PROCESS_INIT;
295 char *displaypath;
296
297 displaypath = get_submodule_displaypath(path, info->prefix);
298
299 sub = submodule_from_path(the_repository, null_oid(), path);
300
301 if (!sub)
302 die(_("No url found for submodule path '%s' in .gitmodules"),
303 displaypath);
304
305 if (!is_submodule_populated_gently(path, NULL))
306 goto cleanup;
307
308 prepare_submodule_repo_env(&cp.env);
309
310 /*
311 * For the purpose of executing <command> in the submodule,
312 * separate shell is used for the purpose of running the
313 * child process.
314 */
315 cp.use_shell = 1;
316 cp.dir = path;
317
318 /*
319 * NEEDSWORK: the command currently has access to the variables $name,
320 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
321 * contains a single argument. This is done for maintaining a faithful
322 * translation from shell script.
323 */
324 if (info->argc == 1) {
325 char *toplevel = xgetcwd();
326 struct strbuf sb = STRBUF_INIT;
327
328 strvec_pushf(&cp.env, "name=%s", sub->name);
329 strvec_pushf(&cp.env, "sm_path=%s", path);
330 strvec_pushf(&cp.env, "displaypath=%s", displaypath);
331 strvec_pushf(&cp.env, "sha1=%s",
332 oid_to_hex(ce_oid));
333 strvec_pushf(&cp.env, "toplevel=%s", toplevel);
334
335 /*
336 * Since the path variable was accessible from the script
337 * before porting, it is also made available after porting.
338 * The environment variable "PATH" has a very special purpose
339 * on windows. And since environment variables are
340 * case-insensitive in windows, it interferes with the
341 * existing PATH variable. Hence, to avoid that, we expose
342 * path via the args strvec and not via env.
343 */
344 sq_quote_buf(&sb, path);
345 strvec_pushf(&cp.args, "path=%s; %s",
346 sb.buf, info->argv[0]);
347 strbuf_release(&sb);
348 free(toplevel);
349 } else {
350 strvec_pushv(&cp.args, info->argv);
351 }
352
353 if (!info->quiet)
354 printf(_("Entering '%s'\n"), displaypath);
355
356 if (info->argv[0] && run_command(&cp))
357 die(_("run_command returned non-zero status for %s\n."),
358 displaypath);
359
360 if (info->recursive) {
361 struct child_process cpr = CHILD_PROCESS_INIT;
362
363 cpr.git_cmd = 1;
364 cpr.dir = path;
365 prepare_submodule_repo_env(&cpr.env);
366
367 strvec_pushl(&cpr.args, "--super-prefix", NULL);
368 strvec_pushf(&cpr.args, "%s/", displaypath);
369 strvec_pushl(&cpr.args, "submodule--helper", "foreach", "--recursive",
370 NULL);
371
372 if (info->quiet)
373 strvec_push(&cpr.args, "--quiet");
374
375 strvec_push(&cpr.args, "--");
376 strvec_pushv(&cpr.args, info->argv);
377
378 if (run_command(&cpr))
379 die(_("run_command returned non-zero status while "
380 "recursing in the nested submodules of %s\n."),
381 displaypath);
382 }
383
384 cleanup:
385 free(displaypath);
386 }
387
388 static int module_foreach(int argc, const char **argv, const char *prefix)
389 {
390 struct foreach_cb info = FOREACH_CB_INIT;
391 struct pathspec pathspec = { 0 };
392 struct module_list list = MODULE_LIST_INIT;
393 struct option module_foreach_options[] = {
394 OPT__QUIET(&info.quiet, N_("suppress output of entering each submodule command")),
395 OPT_BOOL(0, "recursive", &info.recursive,
396 N_("recurse into nested submodules")),
397 OPT_END()
398 };
399 const char *const git_submodule_helper_usage[] = {
400 N_("git submodule foreach [--quiet] [--recursive] [--] <command>"),
401 NULL
402 };
403 int ret = 1;
404
405 argc = parse_options(argc, argv, prefix, module_foreach_options,
406 git_submodule_helper_usage, 0);
407
408 if (module_list_compute(0, NULL, prefix, &pathspec, &list) < 0)
409 goto cleanup;
410
411 info.argc = argc;
412 info.argv = argv;
413 info.prefix = prefix;
414
415 for_each_listed_submodule(&list, runcommand_in_submodule_cb, &info);
416
417 ret = 0;
418 cleanup:
419 module_list_release(&list);
420 clear_pathspec(&pathspec);
421 return ret;
422 }
423
424 static int starts_with_dot_slash(const char *const path)
425 {
426 return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_SLASH |
427 PATH_MATCH_XPLATFORM);
428 }
429
430 static int starts_with_dot_dot_slash(const char *const path)
431 {
432 return path_match_flags(path, PATH_MATCH_STARTS_WITH_DOT_DOT_SLASH |
433 PATH_MATCH_XPLATFORM);
434 }
435
436 struct init_cb {
437 const char *prefix;
438 unsigned int flags;
439 };
440 #define INIT_CB_INIT { 0 }
441
442 static void init_submodule(const char *path, const char *prefix,
443 unsigned int flags)
444 {
445 const struct submodule *sub;
446 struct strbuf sb = STRBUF_INIT;
447 const char *upd;
448 char *url = NULL, *displaypath;
449
450 displaypath = get_submodule_displaypath(path, prefix);
451
452 sub = submodule_from_path(the_repository, null_oid(), path);
453
454 if (!sub)
455 die(_("No url found for submodule path '%s' in .gitmodules"),
456 displaypath);
457
458 /*
459 * NEEDSWORK: In a multi-working-tree world, this needs to be
460 * set in the per-worktree config.
461 *
462 * Set active flag for the submodule being initialized
463 */
464 if (!is_submodule_active(the_repository, path)) {
465 strbuf_addf(&sb, "submodule.%s.active", sub->name);
466 git_config_set_gently(sb.buf, "true");
467 strbuf_reset(&sb);
468 }
469
470 /*
471 * Copy url setting when it is not set yet.
472 * To look up the url in .git/config, we must not fall back to
473 * .gitmodules, so look it up directly.
474 */
475 strbuf_addf(&sb, "submodule.%s.url", sub->name);
476 if (git_config_get_string(sb.buf, &url)) {
477 if (!sub->url)
478 die(_("No url found for submodule path '%s' in .gitmodules"),
479 displaypath);
480
481 url = xstrdup(sub->url);
482
483 /* Possibly a url relative to parent */
484 if (starts_with_dot_dot_slash(url) ||
485 starts_with_dot_slash(url)) {
486 char *oldurl = url;
487
488 url = resolve_relative_url(oldurl, NULL, 0);
489 free(oldurl);
490 }
491
492 if (git_config_set_gently(sb.buf, url))
493 die(_("Failed to register url for submodule path '%s'"),
494 displaypath);
495 if (!(flags & OPT_QUIET))
496 fprintf(stderr,
497 _("Submodule '%s' (%s) registered for path '%s'\n"),
498 sub->name, url, displaypath);
499 }
500 strbuf_reset(&sb);
501
502 /* Copy "update" setting when it is not set yet */
503 strbuf_addf(&sb, "submodule.%s.update", sub->name);
504 if (git_config_get_string_tmp(sb.buf, &upd) &&
505 sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
506 if (sub->update_strategy.type == SM_UPDATE_COMMAND) {
507 fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"),
508 sub->name);
509 upd = "none";
510 } else {
511 upd = submodule_update_type_to_string(sub->update_strategy.type);
512 }
513
514 if (git_config_set_gently(sb.buf, upd))
515 die(_("Failed to register update mode for submodule path '%s'"), displaypath);
516 }
517 strbuf_release(&sb);
518 free(displaypath);
519 free(url);
520 }
521
522 static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data)
523 {
524 struct init_cb *info = cb_data;
525
526 init_submodule(list_item->name, info->prefix, info->flags);
527 }
528
529 static int module_init(int argc, const char **argv, const char *prefix)
530 {
531 struct init_cb info = INIT_CB_INIT;
532 struct pathspec pathspec = { 0 };
533 struct module_list list = MODULE_LIST_INIT;
534 int quiet = 0;
535 struct option module_init_options[] = {
536 OPT__QUIET(&quiet, N_("suppress output for initializing a submodule")),
537 OPT_END()
538 };
539 const char *const git_submodule_helper_usage[] = {
540 N_("git submodule init [<options>] [<path>]"),
541 NULL
542 };
543 int ret = 1;
544
545 argc = parse_options(argc, argv, prefix, module_init_options,
546 git_submodule_helper_usage, 0);
547
548 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
549 goto cleanup;
550
551 /*
552 * If there are no path args and submodule.active is set then,
553 * by default, only initialize 'active' modules.
554 */
555 if (!argc && git_config_get_value_multi("submodule.active"))
556 module_list_active(&list);
557
558 info.prefix = prefix;
559 if (quiet)
560 info.flags |= OPT_QUIET;
561
562 for_each_listed_submodule(&list, init_submodule_cb, &info);
563
564 ret = 0;
565 cleanup:
566 module_list_release(&list);
567 clear_pathspec(&pathspec);
568 return ret;
569 }
570
571 struct status_cb {
572 const char *prefix;
573 unsigned int flags;
574 };
575 #define STATUS_CB_INIT { 0 }
576
577 static void print_status(unsigned int flags, char state, const char *path,
578 const struct object_id *oid, const char *displaypath)
579 {
580 if (flags & OPT_QUIET)
581 return;
582
583 printf("%c%s %s", state, oid_to_hex(oid), displaypath);
584
585 if (state == ' ' || state == '+') {
586 char *name = compute_rev_name(path, oid_to_hex(oid));
587
588 if (name)
589 printf(" (%s)", name);
590 free(name);
591 }
592
593 printf("\n");
594 }
595
596 static int handle_submodule_head_ref(const char *refname UNUSED,
597 const struct object_id *oid,
598 int flags UNUSED,
599 void *cb_data)
600 {
601 struct object_id *output = cb_data;
602
603 if (oid)
604 oidcpy(output, oid);
605
606 return 0;
607 }
608
609 static void status_submodule(const char *path, const struct object_id *ce_oid,
610 unsigned int ce_flags, const char *prefix,
611 unsigned int flags)
612 {
613 char *displaypath;
614 struct strvec diff_files_args = STRVEC_INIT;
615 struct rev_info rev = REV_INFO_INIT;
616 int diff_files_result;
617 struct strbuf buf = STRBUF_INIT;
618 const char *git_dir;
619
620 if (!submodule_from_path(the_repository, null_oid(), path))
621 die(_("no submodule mapping found in .gitmodules for path '%s'"),
622 path);
623
624 displaypath = get_submodule_displaypath(path, prefix);
625
626 if ((CE_STAGEMASK & ce_flags) >> CE_STAGESHIFT) {
627 print_status(flags, 'U', path, null_oid(), displaypath);
628 goto cleanup;
629 }
630
631 strbuf_addf(&buf, "%s/.git", path);
632 git_dir = read_gitfile(buf.buf);
633 if (!git_dir)
634 git_dir = buf.buf;
635
636 if (!is_submodule_active(the_repository, path) ||
637 !is_git_directory(git_dir)) {
638 print_status(flags, '-', path, ce_oid, displaypath);
639 strbuf_release(&buf);
640 goto cleanup;
641 }
642 strbuf_release(&buf);
643
644 strvec_pushl(&diff_files_args, "diff-files",
645 "--ignore-submodules=dirty", "--quiet", "--",
646 path, NULL);
647
648 git_config(git_diff_basic_config, NULL);
649
650 repo_init_revisions(the_repository, &rev, NULL);
651 rev.abbrev = 0;
652 diff_files_args.nr = setup_revisions(diff_files_args.nr,
653 diff_files_args.v,
654 &rev, NULL);
655 diff_files_result = run_diff_files(&rev, 0);
656
657 if (!diff_result_code(&rev.diffopt, diff_files_result)) {
658 print_status(flags, ' ', path, ce_oid,
659 displaypath);
660 } else if (!(flags & OPT_CACHED)) {
661 struct object_id oid;
662 struct ref_store *refs = get_submodule_ref_store(path);
663
664 if (!refs) {
665 print_status(flags, '-', path, ce_oid, displaypath);
666 goto cleanup;
667 }
668 if (refs_head_ref(refs, handle_submodule_head_ref, &oid))
669 die(_("could not resolve HEAD ref inside the "
670 "submodule '%s'"), path);
671
672 print_status(flags, '+', path, &oid, displaypath);
673 } else {
674 print_status(flags, '+', path, ce_oid, displaypath);
675 }
676
677 if (flags & OPT_RECURSIVE) {
678 struct child_process cpr = CHILD_PROCESS_INIT;
679
680 cpr.git_cmd = 1;
681 cpr.dir = path;
682 prepare_submodule_repo_env(&cpr.env);
683
684 strvec_push(&cpr.args, "--super-prefix");
685 strvec_pushf(&cpr.args, "%s/", displaypath);
686 strvec_pushl(&cpr.args, "submodule--helper", "status",
687 "--recursive", NULL);
688
689 if (flags & OPT_CACHED)
690 strvec_push(&cpr.args, "--cached");
691
692 if (flags & OPT_QUIET)
693 strvec_push(&cpr.args, "--quiet");
694
695 if (run_command(&cpr))
696 die(_("failed to recurse into submodule '%s'"), path);
697 }
698
699 cleanup:
700 strvec_clear(&diff_files_args);
701 free(displaypath);
702 release_revisions(&rev);
703 }
704
705 static void status_submodule_cb(const struct cache_entry *list_item,
706 void *cb_data)
707 {
708 struct status_cb *info = cb_data;
709
710 status_submodule(list_item->name, &list_item->oid, list_item->ce_flags,
711 info->prefix, info->flags);
712 }
713
714 static int module_status(int argc, const char **argv, const char *prefix)
715 {
716 struct status_cb info = STATUS_CB_INIT;
717 struct pathspec pathspec = { 0 };
718 struct module_list list = MODULE_LIST_INIT;
719 int quiet = 0;
720 struct option module_status_options[] = {
721 OPT__QUIET(&quiet, N_("suppress submodule status output")),
722 OPT_BIT(0, "cached", &info.flags, N_("use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED),
723 OPT_BIT(0, "recursive", &info.flags, N_("recurse into nested submodules"), OPT_RECURSIVE),
724 OPT_END()
725 };
726 const char *const git_submodule_helper_usage[] = {
727 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
728 NULL
729 };
730 int ret = 1;
731
732 argc = parse_options(argc, argv, prefix, module_status_options,
733 git_submodule_helper_usage, 0);
734
735 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
736 goto cleanup;
737
738 info.prefix = prefix;
739 if (quiet)
740 info.flags |= OPT_QUIET;
741
742 for_each_listed_submodule(&list, status_submodule_cb, &info);
743
744 ret = 0;
745 cleanup:
746 module_list_release(&list);
747 clear_pathspec(&pathspec);
748 return ret;
749 }
750
751 struct module_cb {
752 unsigned int mod_src;
753 unsigned int mod_dst;
754 struct object_id oid_src;
755 struct object_id oid_dst;
756 char status;
757 char *sm_path;
758 };
759 #define MODULE_CB_INIT { 0 }
760
761 static void module_cb_release(struct module_cb *mcb)
762 {
763 free(mcb->sm_path);
764 }
765
766 struct module_cb_list {
767 struct module_cb **entries;
768 int alloc, nr;
769 };
770 #define MODULE_CB_LIST_INIT { 0 }
771
772 static void module_cb_list_release(struct module_cb_list *mcbl)
773 {
774 int i;
775
776 for (i = 0; i < mcbl->nr; i++) {
777 struct module_cb *mcb = mcbl->entries[i];
778
779 module_cb_release(mcb);
780 free(mcb);
781 }
782 free(mcbl->entries);
783 }
784
785 struct summary_cb {
786 int argc;
787 const char **argv;
788 const char *prefix;
789 unsigned int cached: 1;
790 unsigned int for_status: 1;
791 unsigned int files: 1;
792 int summary_limit;
793 };
794 #define SUMMARY_CB_INIT { 0 }
795
796 enum diff_cmd {
797 DIFF_INDEX,
798 DIFF_FILES
799 };
800
801 static char *verify_submodule_committish(const char *sm_path,
802 const char *committish)
803 {
804 struct child_process cp_rev_parse = CHILD_PROCESS_INIT;
805 struct strbuf result = STRBUF_INIT;
806
807 cp_rev_parse.git_cmd = 1;
808 cp_rev_parse.dir = sm_path;
809 prepare_submodule_repo_env(&cp_rev_parse.env);
810 strvec_pushl(&cp_rev_parse.args, "rev-parse", "-q", "--short", NULL);
811 strvec_pushf(&cp_rev_parse.args, "%s^0", committish);
812 strvec_push(&cp_rev_parse.args, "--");
813
814 if (capture_command(&cp_rev_parse, &result, 0))
815 return NULL;
816
817 strbuf_trim_trailing_newline(&result);
818 return strbuf_detach(&result, NULL);
819 }
820
821 static void print_submodule_summary(struct summary_cb *info, const char *errmsg,
822 int total_commits, const char *displaypath,
823 const char *src_abbrev, const char *dst_abbrev,
824 struct module_cb *p)
825 {
826 if (p->status == 'T') {
827 if (S_ISGITLINK(p->mod_dst))
828 printf(_("* %s %s(blob)->%s(submodule)"),
829 displaypath, src_abbrev, dst_abbrev);
830 else
831 printf(_("* %s %s(submodule)->%s(blob)"),
832 displaypath, src_abbrev, dst_abbrev);
833 } else {
834 printf("* %s %s...%s",
835 displaypath, src_abbrev, dst_abbrev);
836 }
837
838 if (total_commits < 0)
839 printf(":\n");
840 else
841 printf(" (%d):\n", total_commits);
842
843 if (errmsg) {
844 printf(_("%s"), errmsg);
845 } else if (total_commits > 0) {
846 struct child_process cp_log = CHILD_PROCESS_INIT;
847
848 cp_log.git_cmd = 1;
849 cp_log.dir = p->sm_path;
850 prepare_submodule_repo_env(&cp_log.env);
851 strvec_pushl(&cp_log.args, "log", NULL);
852
853 if (S_ISGITLINK(p->mod_src) && S_ISGITLINK(p->mod_dst)) {
854 if (info->summary_limit > 0)
855 strvec_pushf(&cp_log.args, "-%d",
856 info->summary_limit);
857
858 strvec_pushl(&cp_log.args, "--pretty= %m %s",
859 "--first-parent", NULL);
860 strvec_pushf(&cp_log.args, "%s...%s",
861 src_abbrev, dst_abbrev);
862 } else if (S_ISGITLINK(p->mod_dst)) {
863 strvec_pushl(&cp_log.args, "--pretty= > %s",
864 "-1", dst_abbrev, NULL);
865 } else {
866 strvec_pushl(&cp_log.args, "--pretty= < %s",
867 "-1", src_abbrev, NULL);
868 }
869 run_command(&cp_log);
870 }
871 printf("\n");
872 }
873
874 static void generate_submodule_summary(struct summary_cb *info,
875 struct module_cb *p)
876 {
877 char *displaypath, *src_abbrev = NULL, *dst_abbrev;
878 int missing_src = 0, missing_dst = 0;
879 struct strbuf errmsg = STRBUF_INIT;
880 int total_commits = -1;
881
882 if (!info->cached && oideq(&p->oid_dst, null_oid())) {
883 if (S_ISGITLINK(p->mod_dst)) {
884 struct ref_store *refs = get_submodule_ref_store(p->sm_path);
885
886 if (refs)
887 refs_head_ref(refs, handle_submodule_head_ref, &p->oid_dst);
888 } else if (S_ISLNK(p->mod_dst) || S_ISREG(p->mod_dst)) {
889 struct stat st;
890 int fd = open(p->sm_path, O_RDONLY);
891
892 if (fd < 0 || fstat(fd, &st) < 0 ||
893 index_fd(&the_index, &p->oid_dst, fd, &st, OBJ_BLOB,
894 p->sm_path, 0))
895 error(_("couldn't hash object from '%s'"), p->sm_path);
896 } else {
897 /* for a submodule removal (mode:0000000), don't warn */
898 if (p->mod_dst)
899 warning(_("unexpected mode %o\n"), p->mod_dst);
900 }
901 }
902
903 if (S_ISGITLINK(p->mod_src)) {
904 if (p->status != 'D')
905 src_abbrev = verify_submodule_committish(p->sm_path,
906 oid_to_hex(&p->oid_src));
907 if (!src_abbrev) {
908 missing_src = 1;
909 /*
910 * As `rev-parse` failed, we fallback to getting
911 * the abbreviated hash using oid_src. We do
912 * this as we might still need the abbreviated
913 * hash in cases like a submodule type change, etc.
914 */
915 src_abbrev = xstrndup(oid_to_hex(&p->oid_src), 7);
916 }
917 } else {
918 /*
919 * The source does not point to a submodule.
920 * So, we fallback to getting the abbreviation using
921 * oid_src as we might still need the abbreviated
922 * hash in cases like submodule add, etc.
923 */
924 src_abbrev = xstrndup(oid_to_hex(&p->oid_src), 7);
925 }
926
927 if (S_ISGITLINK(p->mod_dst)) {
928 dst_abbrev = verify_submodule_committish(p->sm_path,
929 oid_to_hex(&p->oid_dst));
930 if (!dst_abbrev) {
931 missing_dst = 1;
932 /*
933 * As `rev-parse` failed, we fallback to getting
934 * the abbreviated hash using oid_dst. We do
935 * this as we might still need the abbreviated
936 * hash in cases like a submodule type change, etc.
937 */
938 dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7);
939 }
940 } else {
941 /*
942 * The destination does not point to a submodule.
943 * So, we fallback to getting the abbreviation using
944 * oid_dst as we might still need the abbreviated
945 * hash in cases like a submodule removal, etc.
946 */
947 dst_abbrev = xstrndup(oid_to_hex(&p->oid_dst), 7);
948 }
949
950 displaypath = get_submodule_displaypath(p->sm_path, info->prefix);
951
952 if (!missing_src && !missing_dst) {
953 struct child_process cp_rev_list = CHILD_PROCESS_INIT;
954 struct strbuf sb_rev_list = STRBUF_INIT;
955
956 strvec_pushl(&cp_rev_list.args, "rev-list",
957 "--first-parent", "--count", NULL);
958 if (S_ISGITLINK(p->mod_src) && S_ISGITLINK(p->mod_dst))
959 strvec_pushf(&cp_rev_list.args, "%s...%s",
960 src_abbrev, dst_abbrev);
961 else
962 strvec_push(&cp_rev_list.args, S_ISGITLINK(p->mod_src) ?
963 src_abbrev : dst_abbrev);
964 strvec_push(&cp_rev_list.args, "--");
965
966 cp_rev_list.git_cmd = 1;
967 cp_rev_list.dir = p->sm_path;
968 prepare_submodule_repo_env(&cp_rev_list.env);
969
970 if (!capture_command(&cp_rev_list, &sb_rev_list, 0))
971 total_commits = atoi(sb_rev_list.buf);
972
973 strbuf_release(&sb_rev_list);
974 } else {
975 /*
976 * Don't give error msg for modification whose dst is not
977 * submodule, i.e., deleted or changed to blob
978 */
979 if (S_ISGITLINK(p->mod_dst)) {
980 if (missing_src && missing_dst) {
981 strbuf_addf(&errmsg, " Warn: %s doesn't contain commits %s and %s\n",
982 displaypath, oid_to_hex(&p->oid_src),
983 oid_to_hex(&p->oid_dst));
984 } else {
985 strbuf_addf(&errmsg, " Warn: %s doesn't contain commit %s\n",
986 displaypath, missing_src ?
987 oid_to_hex(&p->oid_src) :
988 oid_to_hex(&p->oid_dst));
989 }
990 }
991 }
992
993 print_submodule_summary(info, errmsg.len ? errmsg.buf : NULL,
994 total_commits, displaypath, src_abbrev,
995 dst_abbrev, p);
996
997 free(displaypath);
998 free(src_abbrev);
999 free(dst_abbrev);
1000 strbuf_release(&errmsg);
1001 }
1002
1003 static void prepare_submodule_summary(struct summary_cb *info,
1004 struct module_cb_list *list)
1005 {
1006 int i;
1007 for (i = 0; i < list->nr; i++) {
1008 const struct submodule *sub;
1009 struct module_cb *p = list->entries[i];
1010 struct strbuf sm_gitdir = STRBUF_INIT;
1011
1012 if (p->status == 'D' || p->status == 'T') {
1013 generate_submodule_summary(info, p);
1014 continue;
1015 }
1016
1017 if (info->for_status && p->status != 'A' &&
1018 (sub = submodule_from_path(the_repository,
1019 null_oid(), p->sm_path))) {
1020 char *config_key = NULL;
1021 const char *value;
1022 int ignore_all = 0;
1023
1024 config_key = xstrfmt("submodule.%s.ignore",
1025 sub->name);
1026 if (!git_config_get_string_tmp(config_key, &value))
1027 ignore_all = !strcmp(value, "all");
1028 else if (sub->ignore)
1029 ignore_all = !strcmp(sub->ignore, "all");
1030
1031 free(config_key);
1032 if (ignore_all)
1033 continue;
1034 }
1035
1036 /* Also show added or modified modules which are checked out */
1037 strbuf_addstr(&sm_gitdir, p->sm_path);
1038 if (is_nonbare_repository_dir(&sm_gitdir))
1039 generate_submodule_summary(info, p);
1040 strbuf_release(&sm_gitdir);
1041 }
1042 }
1043
1044 static void submodule_summary_callback(struct diff_queue_struct *q,
1045 struct diff_options *options,
1046 void *data)
1047 {
1048 int i;
1049 struct module_cb_list *list = data;
1050 for (i = 0; i < q->nr; i++) {
1051 struct diff_filepair *p = q->queue[i];
1052 struct module_cb *temp;
1053
1054 if (!S_ISGITLINK(p->one->mode) && !S_ISGITLINK(p->two->mode))
1055 continue;
1056 temp = (struct module_cb*)malloc(sizeof(struct module_cb));
1057 temp->mod_src = p->one->mode;
1058 temp->mod_dst = p->two->mode;
1059 temp->oid_src = p->one->oid;
1060 temp->oid_dst = p->two->oid;
1061 temp->status = p->status;
1062 temp->sm_path = xstrdup(p->one->path);
1063
1064 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
1065 list->entries[list->nr++] = temp;
1066 }
1067 }
1068
1069 static const char *get_diff_cmd(enum diff_cmd diff_cmd)
1070 {
1071 switch (diff_cmd) {
1072 case DIFF_INDEX: return "diff-index";
1073 case DIFF_FILES: return "diff-files";
1074 default: BUG("bad diff_cmd value %d", diff_cmd);
1075 }
1076 }
1077
1078 static int compute_summary_module_list(struct object_id *head_oid,
1079 struct summary_cb *info,
1080 enum diff_cmd diff_cmd)
1081 {
1082 struct strvec diff_args = STRVEC_INIT;
1083 struct rev_info rev;
1084 struct setup_revision_opt opt = {
1085 .free_removed_argv_elements = 1,
1086 };
1087 struct module_cb_list list = MODULE_CB_LIST_INIT;
1088 int ret = 0;
1089
1090 strvec_push(&diff_args, get_diff_cmd(diff_cmd));
1091 if (info->cached)
1092 strvec_push(&diff_args, "--cached");
1093 strvec_pushl(&diff_args, "--ignore-submodules=dirty", "--raw", NULL);
1094 if (head_oid)
1095 strvec_push(&diff_args, oid_to_hex(head_oid));
1096 strvec_push(&diff_args, "--");
1097 if (info->argc)
1098 strvec_pushv(&diff_args, info->argv);
1099
1100 git_config(git_diff_basic_config, NULL);
1101 init_revisions(&rev, info->prefix);
1102 rev.abbrev = 0;
1103 precompose_argv_prefix(diff_args.nr, diff_args.v, NULL);
1104 setup_revisions(diff_args.nr, diff_args.v, &rev, &opt);
1105 rev.diffopt.output_format = DIFF_FORMAT_NO_OUTPUT | DIFF_FORMAT_CALLBACK;
1106 rev.diffopt.format_callback = submodule_summary_callback;
1107 rev.diffopt.format_callback_data = &list;
1108
1109 if (!info->cached) {
1110 if (diff_cmd == DIFF_INDEX)
1111 setup_work_tree();
1112 if (read_cache_preload(&rev.diffopt.pathspec) < 0) {
1113 perror("read_cache_preload");
1114 ret = -1;
1115 goto cleanup;
1116 }
1117 } else if (read_cache() < 0) {
1118 perror("read_cache");
1119 ret = -1;
1120 goto cleanup;
1121 }
1122
1123 if (diff_cmd == DIFF_INDEX)
1124 run_diff_index(&rev, info->cached);
1125 else
1126 run_diff_files(&rev, 0);
1127 prepare_submodule_summary(info, &list);
1128 cleanup:
1129 strvec_clear(&diff_args);
1130 release_revisions(&rev);
1131 module_cb_list_release(&list);
1132 return ret;
1133 }
1134
1135 static int module_summary(int argc, const char **argv, const char *prefix)
1136 {
1137 struct summary_cb info = SUMMARY_CB_INIT;
1138 int cached = 0;
1139 int for_status = 0;
1140 int files = 0;
1141 int summary_limit = -1;
1142 enum diff_cmd diff_cmd = DIFF_INDEX;
1143 struct object_id head_oid;
1144 int ret;
1145 struct option module_summary_options[] = {
1146 OPT_BOOL(0, "cached", &cached,
1147 N_("use the commit stored in the index instead of the submodule HEAD")),
1148 OPT_BOOL(0, "files", &files,
1149 N_("compare the commit in the index with that in the submodule HEAD")),
1150 OPT_BOOL(0, "for-status", &for_status,
1151 N_("skip submodules with 'ignore_config' value set to 'all'")),
1152 OPT_INTEGER('n', "summary-limit", &summary_limit,
1153 N_("limit the summary size")),
1154 OPT_END()
1155 };
1156 const char *const git_submodule_helper_usage[] = {
1157 N_("git submodule summary [<options>] [<commit>] [--] [<path>]"),
1158 NULL
1159 };
1160
1161 argc = parse_options(argc, argv, prefix, module_summary_options,
1162 git_submodule_helper_usage, 0);
1163
1164 if (!summary_limit)
1165 return 0;
1166
1167 if (!get_oid(argc ? argv[0] : "HEAD", &head_oid)) {
1168 if (argc) {
1169 argv++;
1170 argc--;
1171 }
1172 } else if (!argc || !strcmp(argv[0], "HEAD")) {
1173 /* before the first commit: compare with an empty tree */
1174 oidcpy(&head_oid, the_hash_algo->empty_tree);
1175 if (argc) {
1176 argv++;
1177 argc--;
1178 }
1179 } else {
1180 if (get_oid("HEAD", &head_oid))
1181 die(_("could not fetch a revision for HEAD"));
1182 }
1183
1184 if (files) {
1185 if (cached)
1186 die(_("options '%s' and '%s' cannot be used together"), "--cached", "--files");
1187 diff_cmd = DIFF_FILES;
1188 }
1189
1190 info.argc = argc;
1191 info.argv = argv;
1192 info.prefix = prefix;
1193 info.cached = !!cached;
1194 info.files = !!files;
1195 info.for_status = !!for_status;
1196 info.summary_limit = summary_limit;
1197
1198 ret = compute_summary_module_list((diff_cmd == DIFF_INDEX) ? &head_oid : NULL,
1199 &info, diff_cmd);
1200 return ret;
1201 }
1202
1203 struct sync_cb {
1204 const char *prefix;
1205 unsigned int flags;
1206 };
1207 #define SYNC_CB_INIT { 0 }
1208
1209 static void sync_submodule(const char *path, const char *prefix,
1210 unsigned int flags)
1211 {
1212 const struct submodule *sub;
1213 char *remote_key = NULL;
1214 char *sub_origin_url, *super_config_url, *displaypath, *default_remote;
1215 struct strbuf sb = STRBUF_INIT;
1216 char *sub_config_path = NULL;
1217 int code;
1218
1219 if (!is_submodule_active(the_repository, path))
1220 return;
1221
1222 sub = submodule_from_path(the_repository, null_oid(), path);
1223
1224 if (sub && sub->url) {
1225 if (starts_with_dot_dot_slash(sub->url) ||
1226 starts_with_dot_slash(sub->url)) {
1227 char *up_path = get_up_path(path);
1228
1229 sub_origin_url = resolve_relative_url(sub->url, up_path, 1);
1230 super_config_url = resolve_relative_url(sub->url, NULL, 1);
1231 free(up_path);
1232 } else {
1233 sub_origin_url = xstrdup(sub->url);
1234 super_config_url = xstrdup(sub->url);
1235 }
1236 } else {
1237 sub_origin_url = xstrdup("");
1238 super_config_url = xstrdup("");
1239 }
1240
1241 displaypath = get_submodule_displaypath(path, prefix);
1242
1243 if (!(flags & OPT_QUIET))
1244 printf(_("Synchronizing submodule url for '%s'\n"),
1245 displaypath);
1246
1247 strbuf_reset(&sb);
1248 strbuf_addf(&sb, "submodule.%s.url", sub->name);
1249 if (git_config_set_gently(sb.buf, super_config_url))
1250 die(_("failed to register url for submodule path '%s'"),
1251 displaypath);
1252
1253 if (!is_submodule_populated_gently(path, NULL))
1254 goto cleanup;
1255
1256 strbuf_reset(&sb);
1257 code = get_default_remote_submodule(path, &default_remote);
1258 if (code)
1259 exit(code);
1260
1261 remote_key = xstrfmt("remote.%s.url", default_remote);
1262 free(default_remote);
1263
1264 submodule_to_gitdir(&sb, path);
1265 strbuf_addstr(&sb, "/config");
1266
1267 if (git_config_set_in_file_gently(sb.buf, remote_key, sub_origin_url))
1268 die(_("failed to update remote for submodule '%s'"),
1269 path);
1270
1271 if (flags & OPT_RECURSIVE) {
1272 struct child_process cpr = CHILD_PROCESS_INIT;
1273
1274 cpr.git_cmd = 1;
1275 cpr.dir = path;
1276 prepare_submodule_repo_env(&cpr.env);
1277
1278 strvec_push(&cpr.args, "--super-prefix");
1279 strvec_pushf(&cpr.args, "%s/", displaypath);
1280 strvec_pushl(&cpr.args, "submodule--helper", "sync",
1281 "--recursive", NULL);
1282
1283 if (flags & OPT_QUIET)
1284 strvec_push(&cpr.args, "--quiet");
1285
1286 if (run_command(&cpr))
1287 die(_("failed to recurse into submodule '%s'"),
1288 path);
1289 }
1290
1291 cleanup:
1292 free(super_config_url);
1293 free(sub_origin_url);
1294 strbuf_release(&sb);
1295 free(remote_key);
1296 free(displaypath);
1297 free(sub_config_path);
1298 }
1299
1300 static void sync_submodule_cb(const struct cache_entry *list_item, void *cb_data)
1301 {
1302 struct sync_cb *info = cb_data;
1303
1304 sync_submodule(list_item->name, info->prefix, info->flags);
1305 }
1306
1307 static int module_sync(int argc, const char **argv, const char *prefix)
1308 {
1309 struct sync_cb info = SYNC_CB_INIT;
1310 struct pathspec pathspec = { 0 };
1311 struct module_list list = MODULE_LIST_INIT;
1312 int quiet = 0;
1313 int recursive = 0;
1314 struct option module_sync_options[] = {
1315 OPT__QUIET(&quiet, N_("suppress output of synchronizing submodule url")),
1316 OPT_BOOL(0, "recursive", &recursive,
1317 N_("recurse into nested submodules")),
1318 OPT_END()
1319 };
1320 const char *const git_submodule_helper_usage[] = {
1321 N_("git submodule sync [--quiet] [--recursive] [<path>]"),
1322 NULL
1323 };
1324 int ret = 1;
1325
1326 argc = parse_options(argc, argv, prefix, module_sync_options,
1327 git_submodule_helper_usage, 0);
1328
1329 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1330 goto cleanup;
1331
1332 info.prefix = prefix;
1333 if (quiet)
1334 info.flags |= OPT_QUIET;
1335 if (recursive)
1336 info.flags |= OPT_RECURSIVE;
1337
1338 for_each_listed_submodule(&list, sync_submodule_cb, &info);
1339
1340 ret = 0;
1341 cleanup:
1342 module_list_release(&list);
1343 clear_pathspec(&pathspec);
1344 return ret;
1345 }
1346
1347 struct deinit_cb {
1348 const char *prefix;
1349 unsigned int flags;
1350 };
1351 #define DEINIT_CB_INIT { 0 }
1352
1353 static void deinit_submodule(const char *path, const char *prefix,
1354 unsigned int flags)
1355 {
1356 const struct submodule *sub;
1357 char *displaypath = NULL;
1358 struct child_process cp_config = CHILD_PROCESS_INIT;
1359 struct strbuf sb_config = STRBUF_INIT;
1360 char *sub_git_dir = xstrfmt("%s/.git", path);
1361
1362 sub = submodule_from_path(the_repository, null_oid(), path);
1363
1364 if (!sub || !sub->name)
1365 goto cleanup;
1366
1367 displaypath = get_submodule_displaypath(path, prefix);
1368
1369 /* remove the submodule work tree (unless the user already did it) */
1370 if (is_directory(path)) {
1371 struct strbuf sb_rm = STRBUF_INIT;
1372 const char *format;
1373
1374 if (is_directory(sub_git_dir)) {
1375 if (!(flags & OPT_QUIET))
1376 warning(_("Submodule work tree '%s' contains a .git "
1377 "directory. This will be replaced with a "
1378 ".git file by using absorbgitdirs."),
1379 displaypath);
1380
1381 absorb_git_dir_into_superproject(path,
1382 ABSORB_GITDIR_RECURSE_SUBMODULES);
1383
1384 }
1385
1386 if (!(flags & OPT_FORCE)) {
1387 struct child_process cp_rm = CHILD_PROCESS_INIT;
1388
1389 cp_rm.git_cmd = 1;
1390 strvec_pushl(&cp_rm.args, "rm", "-qn",
1391 path, NULL);
1392
1393 if (run_command(&cp_rm))
1394 die(_("Submodule work tree '%s' contains local "
1395 "modifications; use '-f' to discard them"),
1396 displaypath);
1397 }
1398
1399 strbuf_addstr(&sb_rm, path);
1400
1401 if (!remove_dir_recursively(&sb_rm, 0))
1402 format = _("Cleared directory '%s'\n");
1403 else
1404 format = _("Could not remove submodule work tree '%s'\n");
1405
1406 if (!(flags & OPT_QUIET))
1407 printf(format, displaypath);
1408
1409 submodule_unset_core_worktree(sub);
1410
1411 strbuf_release(&sb_rm);
1412 }
1413
1414 if (mkdir(path, 0777))
1415 printf(_("could not create empty submodule directory %s"),
1416 displaypath);
1417
1418 cp_config.git_cmd = 1;
1419 strvec_pushl(&cp_config.args, "config", "--get-regexp", NULL);
1420 strvec_pushf(&cp_config.args, "submodule.%s\\.", sub->name);
1421
1422 /* remove the .git/config entries (unless the user already did it) */
1423 if (!capture_command(&cp_config, &sb_config, 0) && sb_config.len) {
1424 char *sub_key = xstrfmt("submodule.%s", sub->name);
1425
1426 /*
1427 * remove the whole section so we have a clean state when
1428 * the user later decides to init this submodule again
1429 */
1430 git_config_rename_section_in_file(NULL, sub_key, NULL);
1431 if (!(flags & OPT_QUIET))
1432 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1433 sub->name, sub->url, displaypath);
1434 free(sub_key);
1435 }
1436
1437 cleanup:
1438 free(displaypath);
1439 free(sub_git_dir);
1440 strbuf_release(&sb_config);
1441 }
1442
1443 static void deinit_submodule_cb(const struct cache_entry *list_item,
1444 void *cb_data)
1445 {
1446 struct deinit_cb *info = cb_data;
1447 deinit_submodule(list_item->name, info->prefix, info->flags);
1448 }
1449
1450 static int module_deinit(int argc, const char **argv, const char *prefix)
1451 {
1452 struct deinit_cb info = DEINIT_CB_INIT;
1453 struct pathspec pathspec = { 0 };
1454 struct module_list list = MODULE_LIST_INIT;
1455 int quiet = 0;
1456 int force = 0;
1457 int all = 0;
1458 struct option module_deinit_options[] = {
1459 OPT__QUIET(&quiet, N_("suppress submodule status output")),
1460 OPT__FORCE(&force, N_("remove submodule working trees even if they contain local changes"), 0),
1461 OPT_BOOL(0, "all", &all, N_("unregister all submodules")),
1462 OPT_END()
1463 };
1464 const char *const git_submodule_helper_usage[] = {
1465 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1466 NULL
1467 };
1468 int ret = 1;
1469
1470 argc = parse_options(argc, argv, prefix, module_deinit_options,
1471 git_submodule_helper_usage, 0);
1472
1473 if (all && argc) {
1474 error("pathspec and --all are incompatible");
1475 usage_with_options(git_submodule_helper_usage,
1476 module_deinit_options);
1477 }
1478
1479 if (!argc && !all)
1480 die(_("Use '--all' if you really want to deinitialize all submodules"));
1481
1482 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1483 goto cleanup;
1484
1485 info.prefix = prefix;
1486 if (quiet)
1487 info.flags |= OPT_QUIET;
1488 if (force)
1489 info.flags |= OPT_FORCE;
1490
1491 for_each_listed_submodule(&list, deinit_submodule_cb, &info);
1492
1493 ret = 0;
1494 cleanup:
1495 module_list_release(&list);
1496 clear_pathspec(&pathspec);
1497 return ret;
1498 }
1499
1500 struct module_clone_data {
1501 const char *prefix;
1502 const char *path;
1503 const char *name;
1504 const char *url;
1505 const char *depth;
1506 struct list_objects_filter_options *filter_options;
1507 unsigned int quiet: 1;
1508 unsigned int progress: 1;
1509 unsigned int dissociate: 1;
1510 unsigned int require_init: 1;
1511 int single_branch;
1512 };
1513 #define MODULE_CLONE_DATA_INIT { \
1514 .single_branch = -1, \
1515 }
1516
1517 struct submodule_alternate_setup {
1518 const char *submodule_name;
1519 enum SUBMODULE_ALTERNATE_ERROR_MODE {
1520 SUBMODULE_ALTERNATE_ERROR_DIE,
1521 SUBMODULE_ALTERNATE_ERROR_INFO,
1522 SUBMODULE_ALTERNATE_ERROR_IGNORE
1523 } error_mode;
1524 struct string_list *reference;
1525 };
1526 #define SUBMODULE_ALTERNATE_SETUP_INIT { \
1527 .error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE, \
1528 }
1529
1530 static const char alternate_error_advice[] = N_(
1531 "An alternate computed from a superproject's alternate is invalid.\n"
1532 "To allow Git to clone without an alternate in such a case, set\n"
1533 "submodule.alternateErrorStrategy to 'info' or, equivalently, clone with\n"
1534 "'--reference-if-able' instead of '--reference'."
1535 );
1536
1537 static int add_possible_reference_from_superproject(
1538 struct object_directory *odb, void *sas_cb)
1539 {
1540 struct submodule_alternate_setup *sas = sas_cb;
1541 size_t len;
1542
1543 /*
1544 * If the alternate object store is another repository, try the
1545 * standard layout with .git/(modules/<name>)+/objects
1546 */
1547 if (strip_suffix(odb->path, "/objects", &len)) {
1548 struct repository alternate;
1549 char *sm_alternate;
1550 struct strbuf sb = STRBUF_INIT;
1551 struct strbuf err = STRBUF_INIT;
1552 strbuf_add(&sb, odb->path, len);
1553
1554 if (repo_init(&alternate, sb.buf, NULL) < 0)
1555 die(_("could not get a repository handle for gitdir '%s'"),
1556 sb.buf);
1557
1558 /*
1559 * We need to end the new path with '/' to mark it as a dir,
1560 * otherwise a submodule name containing '/' will be broken
1561 * as the last part of a missing submodule reference would
1562 * be taken as a file name.
1563 */
1564 strbuf_reset(&sb);
1565 submodule_name_to_gitdir(&sb, &alternate, sas->submodule_name);
1566 strbuf_addch(&sb, '/');
1567 repo_clear(&alternate);
1568
1569 sm_alternate = compute_alternate_path(sb.buf, &err);
1570 if (sm_alternate) {
1571 char *p = strbuf_detach(&sb, NULL);
1572
1573 string_list_append(sas->reference, p)->util = p;
1574 free(sm_alternate);
1575 } else {
1576 switch (sas->error_mode) {
1577 case SUBMODULE_ALTERNATE_ERROR_DIE:
1578 if (advice_enabled(ADVICE_SUBMODULE_ALTERNATE_ERROR_STRATEGY_DIE))
1579 advise(_(alternate_error_advice));
1580 die(_("submodule '%s' cannot add alternate: %s"),
1581 sas->submodule_name, err.buf);
1582 case SUBMODULE_ALTERNATE_ERROR_INFO:
1583 fprintf_ln(stderr, _("submodule '%s' cannot add alternate: %s"),
1584 sas->submodule_name, err.buf);
1585 case SUBMODULE_ALTERNATE_ERROR_IGNORE:
1586 ; /* nothing */
1587 }
1588 }
1589 strbuf_release(&sb);
1590 }
1591
1592 return 0;
1593 }
1594
1595 static void prepare_possible_alternates(const char *sm_name,
1596 struct string_list *reference)
1597 {
1598 char *sm_alternate = NULL, *error_strategy = NULL;
1599 struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT;
1600
1601 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1602 if (!sm_alternate)
1603 return;
1604
1605 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1606
1607 if (!error_strategy)
1608 error_strategy = xstrdup("die");
1609
1610 sas.submodule_name = sm_name;
1611 sas.reference = reference;
1612 if (!strcmp(error_strategy, "die"))
1613 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE;
1614 else if (!strcmp(error_strategy, "info"))
1615 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO;
1616 else if (!strcmp(error_strategy, "ignore"))
1617 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE;
1618 else
1619 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
1620
1621 if (!strcmp(sm_alternate, "superproject"))
1622 foreach_alt_odb(add_possible_reference_from_superproject, &sas);
1623 else if (!strcmp(sm_alternate, "no"))
1624 ; /* do nothing */
1625 else
1626 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate);
1627
1628 free(sm_alternate);
1629 free(error_strategy);
1630 }
1631
1632 static char *clone_submodule_sm_gitdir(const char *name)
1633 {
1634 struct strbuf sb = STRBUF_INIT;
1635 char *sm_gitdir;
1636
1637 submodule_name_to_gitdir(&sb, the_repository, name);
1638 sm_gitdir = absolute_pathdup(sb.buf);
1639 strbuf_release(&sb);
1640
1641 return sm_gitdir;
1642 }
1643
1644 static int clone_submodule(const struct module_clone_data *clone_data,
1645 struct string_list *reference)
1646 {
1647 char *p;
1648 char *sm_gitdir = clone_submodule_sm_gitdir(clone_data->name);
1649 char *sm_alternate = NULL, *error_strategy = NULL;
1650 struct child_process cp = CHILD_PROCESS_INIT;
1651 const char *clone_data_path = clone_data->path;
1652 char *to_free = NULL;
1653
1654 if (!is_absolute_path(clone_data->path))
1655 clone_data_path = to_free = xstrfmt("%s/%s", get_git_work_tree(),
1656 clone_data->path);
1657
1658 if (validate_submodule_git_dir(sm_gitdir, clone_data->name) < 0)
1659 die(_("refusing to create/use '%s' in another submodule's "
1660 "git dir"), sm_gitdir);
1661
1662 if (!file_exists(sm_gitdir)) {
1663 if (safe_create_leading_directories_const(sm_gitdir) < 0)
1664 die(_("could not create directory '%s'"), sm_gitdir);
1665
1666 prepare_possible_alternates(clone_data->name, reference);
1667
1668 strvec_push(&cp.args, "clone");
1669 strvec_push(&cp.args, "--no-checkout");
1670 if (clone_data->quiet)
1671 strvec_push(&cp.args, "--quiet");
1672 if (clone_data->progress)
1673 strvec_push(&cp.args, "--progress");
1674 if (clone_data->depth && *(clone_data->depth))
1675 strvec_pushl(&cp.args, "--depth", clone_data->depth, NULL);
1676 if (reference->nr) {
1677 struct string_list_item *item;
1678
1679 for_each_string_list_item(item, reference)
1680 strvec_pushl(&cp.args, "--reference",
1681 item->string, NULL);
1682 }
1683 if (clone_data->dissociate)
1684 strvec_push(&cp.args, "--dissociate");
1685 if (sm_gitdir && *sm_gitdir)
1686 strvec_pushl(&cp.args, "--separate-git-dir", sm_gitdir, NULL);
1687 if (clone_data->filter_options && clone_data->filter_options->choice)
1688 strvec_pushf(&cp.args, "--filter=%s",
1689 expand_list_objects_filter_spec(
1690 clone_data->filter_options));
1691 if (clone_data->single_branch >= 0)
1692 strvec_push(&cp.args, clone_data->single_branch ?
1693 "--single-branch" :
1694 "--no-single-branch");
1695
1696 strvec_push(&cp.args, "--");
1697 strvec_push(&cp.args, clone_data->url);
1698 strvec_push(&cp.args, clone_data_path);
1699
1700 cp.git_cmd = 1;
1701 prepare_submodule_repo_env(&cp.env);
1702 cp.no_stdin = 1;
1703
1704 if(run_command(&cp))
1705 die(_("clone of '%s' into submodule path '%s' failed"),
1706 clone_data->url, clone_data_path);
1707 } else {
1708 char *path;
1709
1710 if (clone_data->require_init && !access(clone_data_path, X_OK) &&
1711 !is_empty_dir(clone_data_path))
1712 die(_("directory not empty: '%s'"), clone_data_path);
1713 if (safe_create_leading_directories_const(clone_data_path) < 0)
1714 die(_("could not create directory '%s'"), clone_data_path);
1715 path = xstrfmt("%s/index", sm_gitdir);
1716 unlink_or_warn(path);
1717 free(path);
1718 }
1719
1720 connect_work_tree_and_git_dir(clone_data_path, sm_gitdir, 0);
1721
1722 p = git_pathdup_submodule(clone_data_path, "config");
1723 if (!p)
1724 die(_("could not get submodule directory for '%s'"), clone_data_path);
1725
1726 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1727 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1728 if (sm_alternate)
1729 git_config_set_in_file(p, "submodule.alternateLocation",
1730 sm_alternate);
1731 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1732 if (error_strategy)
1733 git_config_set_in_file(p, "submodule.alternateErrorStrategy",
1734 error_strategy);
1735
1736 free(sm_alternate);
1737 free(error_strategy);
1738
1739 free(sm_gitdir);
1740 free(p);
1741 free(to_free);
1742 return 0;
1743 }
1744
1745 static int module_clone(int argc, const char **argv, const char *prefix)
1746 {
1747 int dissociate = 0, quiet = 0, progress = 0, require_init = 0;
1748 struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT;
1749 struct string_list reference = STRING_LIST_INIT_NODUP;
1750 struct list_objects_filter_options filter_options =
1751 LIST_OBJECTS_FILTER_INIT;
1752
1753 struct option module_clone_options[] = {
1754 OPT_STRING(0, "prefix", &clone_data.prefix,
1755 N_("path"),
1756 N_("alternative anchor for relative paths")),
1757 OPT_STRING(0, "path", &clone_data.path,
1758 N_("path"),
1759 N_("where the new submodule will be cloned to")),
1760 OPT_STRING(0, "name", &clone_data.name,
1761 N_("string"),
1762 N_("name of the new submodule")),
1763 OPT_STRING(0, "url", &clone_data.url,
1764 N_("string"),
1765 N_("url where to clone the submodule from")),
1766 OPT_STRING_LIST(0, "reference", &reference,
1767 N_("repo"),
1768 N_("reference repository")),
1769 OPT_BOOL(0, "dissociate", &dissociate,
1770 N_("use --reference only while cloning")),
1771 OPT_STRING(0, "depth", &clone_data.depth,
1772 N_("string"),
1773 N_("depth for shallow clones")),
1774 OPT__QUIET(&quiet, "suppress output for cloning a submodule"),
1775 OPT_BOOL(0, "progress", &progress,
1776 N_("force cloning progress")),
1777 OPT_BOOL(0, "require-init", &require_init,
1778 N_("disallow cloning into non-empty directory")),
1779 OPT_BOOL(0, "single-branch", &clone_data.single_branch,
1780 N_("clone only one branch, HEAD or --branch")),
1781 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
1782 OPT_END()
1783 };
1784 const char *const git_submodule_helper_usage[] = {
1785 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1786 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1787 "[--single-branch] [--filter <filter-spec>] "
1788 "--url <url> --path <path>"),
1789 NULL
1790 };
1791
1792 argc = parse_options(argc, argv, prefix, module_clone_options,
1793 git_submodule_helper_usage, 0);
1794
1795 clone_data.dissociate = !!dissociate;
1796 clone_data.quiet = !!quiet;
1797 clone_data.progress = !!progress;
1798 clone_data.require_init = !!require_init;
1799 clone_data.filter_options = &filter_options;
1800
1801 if (argc || !clone_data.url || !clone_data.path || !*(clone_data.path))
1802 usage_with_options(git_submodule_helper_usage,
1803 module_clone_options);
1804
1805 clone_submodule(&clone_data, &reference);
1806 list_objects_filter_release(&filter_options);
1807 string_list_clear(&reference, 1);
1808 return 0;
1809 }
1810
1811 static int determine_submodule_update_strategy(struct repository *r,
1812 int just_cloned,
1813 const char *path,
1814 enum submodule_update_type update,
1815 struct submodule_update_strategy *out)
1816 {
1817 const struct submodule *sub = submodule_from_path(r, null_oid(), path);
1818 char *key;
1819 const char *val;
1820 int ret;
1821
1822 key = xstrfmt("submodule.%s.update", sub->name);
1823
1824 if (update) {
1825 out->type = update;
1826 } else if (!repo_config_get_string_tmp(r, key, &val)) {
1827 if (parse_submodule_update_strategy(val, out) < 0) {
1828 ret = die_message(_("Invalid update mode '%s' configured for submodule path '%s'"),
1829 val, path);
1830 goto cleanup;
1831 }
1832 } else if (sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
1833 if (sub->update_strategy.type == SM_UPDATE_COMMAND)
1834 BUG("how did we read update = !command from .gitmodules?");
1835 out->type = sub->update_strategy.type;
1836 out->command = sub->update_strategy.command;
1837 } else
1838 out->type = SM_UPDATE_CHECKOUT;
1839
1840 if (just_cloned &&
1841 (out->type == SM_UPDATE_MERGE ||
1842 out->type == SM_UPDATE_REBASE ||
1843 out->type == SM_UPDATE_NONE))
1844 out->type = SM_UPDATE_CHECKOUT;
1845
1846 ret = 0;
1847 cleanup:
1848 free(key);
1849 return ret;
1850 }
1851
1852 struct update_clone_data {
1853 const struct submodule *sub;
1854 struct object_id oid;
1855 unsigned just_cloned;
1856 };
1857
1858 struct submodule_update_clone {
1859 /* index into 'update_data.list', the list of submodules to look into for cloning */
1860 int current;
1861
1862 /* configuration parameters which are passed on to the children */
1863 const struct update_data *update_data;
1864
1865 /* to be consumed by update_submodule() */
1866 struct update_clone_data *update_clone;
1867 int update_clone_nr; int update_clone_alloc;
1868
1869 /* If we want to stop as fast as possible and return an error */
1870 unsigned quickstop : 1;
1871
1872 /* failed clones to be retried again */
1873 const struct cache_entry **failed_clones;
1874 int failed_clones_nr, failed_clones_alloc;
1875 };
1876 #define SUBMODULE_UPDATE_CLONE_INIT { 0 }
1877
1878 static void submodule_update_clone_release(struct submodule_update_clone *suc)
1879 {
1880 free(suc->update_clone);
1881 free(suc->failed_clones);
1882 }
1883
1884 struct update_data {
1885 const char *prefix;
1886 char *displaypath;
1887 enum submodule_update_type update_default;
1888 struct object_id suboid;
1889 struct string_list references;
1890 struct submodule_update_strategy update_strategy;
1891 struct list_objects_filter_options *filter_options;
1892 struct module_list list;
1893 int depth;
1894 int max_jobs;
1895 int single_branch;
1896 int recommend_shallow;
1897 unsigned int require_init;
1898 unsigned int force;
1899 unsigned int quiet;
1900 unsigned int nofetch;
1901 unsigned int remote;
1902 unsigned int progress;
1903 unsigned int dissociate;
1904 unsigned int init;
1905 unsigned int warn_if_uninitialized;
1906 unsigned int recursive;
1907
1908 /* copied over from update_clone_data */
1909 struct object_id oid;
1910 unsigned int just_cloned;
1911 const char *sm_path;
1912 };
1913 #define UPDATE_DATA_INIT { \
1914 .update_strategy = SUBMODULE_UPDATE_STRATEGY_INIT, \
1915 .list = MODULE_LIST_INIT, \
1916 .recommend_shallow = -1, \
1917 .references = STRING_LIST_INIT_DUP, \
1918 .single_branch = -1, \
1919 .max_jobs = 1, \
1920 }
1921
1922 static void update_data_release(struct update_data *ud)
1923 {
1924 free(ud->displaypath);
1925 module_list_release(&ud->list);
1926 }
1927
1928 static void next_submodule_warn_missing(struct submodule_update_clone *suc,
1929 struct strbuf *out, const char *displaypath)
1930 {
1931 /*
1932 * Only mention uninitialized submodules when their
1933 * paths have been specified.
1934 */
1935 if (suc->update_data->warn_if_uninitialized) {
1936 strbuf_addf(out,
1937 _("Submodule path '%s' not initialized"),
1938 displaypath);
1939 strbuf_addch(out, '\n');
1940 strbuf_addstr(out,
1941 _("Maybe you want to use 'update --init'?"));
1942 strbuf_addch(out, '\n');
1943 }
1944 }
1945
1946 /**
1947 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
1948 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
1949 */
1950 static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
1951 struct child_process *child,
1952 struct submodule_update_clone *suc,
1953 struct strbuf *out)
1954 {
1955 const struct submodule *sub = NULL;
1956 const char *url = NULL;
1957 const char *update_string;
1958 enum submodule_update_type update_type;
1959 char *key;
1960 const struct update_data *ud = suc->update_data;
1961 char *displaypath = get_submodule_displaypath(ce->name, ud->prefix);
1962 struct strbuf sb = STRBUF_INIT;
1963 int needs_cloning = 0;
1964 int need_free_url = 0;
1965
1966 if (ce_stage(ce)) {
1967 strbuf_addf(out, _("Skipping unmerged submodule %s"), displaypath);
1968 strbuf_addch(out, '\n');
1969 goto cleanup;
1970 }
1971
1972 sub = submodule_from_path(the_repository, null_oid(), ce->name);
1973
1974 if (!sub) {
1975 next_submodule_warn_missing(suc, out, displaypath);
1976 goto cleanup;
1977 }
1978
1979 key = xstrfmt("submodule.%s.update", sub->name);
1980 if (!repo_config_get_string_tmp(the_repository, key, &update_string)) {
1981 update_type = parse_submodule_update_type(update_string);
1982 } else {
1983 update_type = sub->update_strategy.type;
1984 }
1985 free(key);
1986
1987 if (suc->update_data->update_strategy.type == SM_UPDATE_NONE
1988 || (suc->update_data->update_strategy.type == SM_UPDATE_UNSPECIFIED
1989 && update_type == SM_UPDATE_NONE)) {
1990 strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
1991 strbuf_addch(out, '\n');
1992 goto cleanup;
1993 }
1994
1995 /* Check if the submodule has been initialized. */
1996 if (!is_submodule_active(the_repository, ce->name)) {
1997 next_submodule_warn_missing(suc, out, displaypath);
1998 goto cleanup;
1999 }
2000
2001 strbuf_reset(&sb);
2002 strbuf_addf(&sb, "submodule.%s.url", sub->name);
2003 if (repo_config_get_string_tmp(the_repository, sb.buf, &url)) {
2004 if (starts_with_dot_slash(sub->url) ||
2005 starts_with_dot_dot_slash(sub->url)) {
2006 url = resolve_relative_url(sub->url, NULL, 0);
2007 need_free_url = 1;
2008 } else
2009 url = sub->url;
2010 }
2011
2012 strbuf_reset(&sb);
2013 strbuf_addf(&sb, "%s/.git", ce->name);
2014 needs_cloning = !file_exists(sb.buf);
2015
2016 ALLOC_GROW(suc->update_clone, suc->update_clone_nr + 1,
2017 suc->update_clone_alloc);
2018 oidcpy(&suc->update_clone[suc->update_clone_nr].oid, &ce->oid);
2019 suc->update_clone[suc->update_clone_nr].just_cloned = needs_cloning;
2020 suc->update_clone[suc->update_clone_nr].sub = sub;
2021 suc->update_clone_nr++;
2022
2023 if (!needs_cloning)
2024 goto cleanup;
2025
2026 child->git_cmd = 1;
2027 child->no_stdin = 1;
2028 child->stdout_to_stderr = 1;
2029 child->err = -1;
2030 strvec_push(&child->args, "submodule--helper");
2031 strvec_push(&child->args, "clone");
2032 if (suc->update_data->progress)
2033 strvec_push(&child->args, "--progress");
2034 if (suc->update_data->quiet)
2035 strvec_push(&child->args, "--quiet");
2036 if (suc->update_data->prefix)
2037 strvec_pushl(&child->args, "--prefix", suc->update_data->prefix, NULL);
2038 if (suc->update_data->recommend_shallow && sub->recommend_shallow == 1)
2039 strvec_push(&child->args, "--depth=1");
2040 else if (suc->update_data->depth)
2041 strvec_pushf(&child->args, "--depth=%d", suc->update_data->depth);
2042 if (suc->update_data->filter_options && suc->update_data->filter_options->choice)
2043 strvec_pushf(&child->args, "--filter=%s",
2044 expand_list_objects_filter_spec(suc->update_data->filter_options));
2045 if (suc->update_data->require_init)
2046 strvec_push(&child->args, "--require-init");
2047 strvec_pushl(&child->args, "--path", sub->path, NULL);
2048 strvec_pushl(&child->args, "--name", sub->name, NULL);
2049 strvec_pushl(&child->args, "--url", url, NULL);
2050 if (suc->update_data->references.nr) {
2051 struct string_list_item *item;
2052
2053 for_each_string_list_item(item, &suc->update_data->references)
2054 strvec_pushl(&child->args, "--reference", item->string, NULL);
2055 }
2056 if (suc->update_data->dissociate)
2057 strvec_push(&child->args, "--dissociate");
2058 if (suc->update_data->single_branch >= 0)
2059 strvec_push(&child->args, suc->update_data->single_branch ?
2060 "--single-branch" :
2061 "--no-single-branch");
2062
2063 cleanup:
2064 free(displaypath);
2065 strbuf_release(&sb);
2066 if (need_free_url)
2067 free((void*)url);
2068
2069 return needs_cloning;
2070 }
2071
2072 static int update_clone_get_next_task(struct child_process *child,
2073 struct strbuf *err,
2074 void *suc_cb,
2075 void **idx_task_cb)
2076 {
2077 struct submodule_update_clone *suc = suc_cb;
2078 const struct cache_entry *ce;
2079 int index;
2080
2081 for (; suc->current < suc->update_data->list.nr; suc->current++) {
2082 ce = suc->update_data->list.entries[suc->current];
2083 if (prepare_to_clone_next_submodule(ce, child, suc, err)) {
2084 int *p = xmalloc(sizeof(*p));
2085
2086 *p = suc->current;
2087 *idx_task_cb = p;
2088 suc->current++;
2089 return 1;
2090 }
2091 }
2092
2093 /*
2094 * The loop above tried cloning each submodule once, now try the
2095 * stragglers again, which we can imagine as an extension of the
2096 * entry list.
2097 */
2098 index = suc->current - suc->update_data->list.nr;
2099 if (index < suc->failed_clones_nr) {
2100 int *p;
2101
2102 ce = suc->failed_clones[index];
2103 if (!prepare_to_clone_next_submodule(ce, child, suc, err)) {
2104 suc->current ++;
2105 strbuf_addstr(err, "BUG: submodule considered for "
2106 "cloning, doesn't need cloning "
2107 "any more?\n");
2108 return 0;
2109 }
2110 p = xmalloc(sizeof(*p));
2111 *p = suc->current;
2112 *idx_task_cb = p;
2113 suc->current ++;
2114 return 1;
2115 }
2116
2117 return 0;
2118 }
2119
2120 static int update_clone_start_failure(struct strbuf *err,
2121 void *suc_cb,
2122 void *idx_task_cb)
2123 {
2124 struct submodule_update_clone *suc = suc_cb;
2125
2126 suc->quickstop = 1;
2127 return 1;
2128 }
2129
2130 static int update_clone_task_finished(int result,
2131 struct strbuf *err,
2132 void *suc_cb,
2133 void *idx_task_cb)
2134 {
2135 const struct cache_entry *ce;
2136 struct submodule_update_clone *suc = suc_cb;
2137 int *idxP = idx_task_cb;
2138 int idx = *idxP;
2139
2140 free(idxP);
2141
2142 if (!result)
2143 return 0;
2144
2145 if (idx < suc->update_data->list.nr) {
2146 ce = suc->update_data->list.entries[idx];
2147 strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"),
2148 ce->name);
2149 strbuf_addch(err, '\n');
2150 ALLOC_GROW(suc->failed_clones,
2151 suc->failed_clones_nr + 1,
2152 suc->failed_clones_alloc);
2153 suc->failed_clones[suc->failed_clones_nr++] = ce;
2154 return 0;
2155 } else {
2156 idx -= suc->update_data->list.nr;
2157 ce = suc->failed_clones[idx];
2158 strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"),
2159 ce->name);
2160 strbuf_addch(err, '\n');
2161 suc->quickstop = 1;
2162 return 1;
2163 }
2164
2165 return 0;
2166 }
2167
2168 static int git_update_clone_config(const char *var, const char *value,
2169 void *cb)
2170 {
2171 int *max_jobs = cb;
2172
2173 if (!strcmp(var, "submodule.fetchjobs"))
2174 *max_jobs = parse_submodule_fetchjobs(var, value);
2175 return 0;
2176 }
2177
2178 static int is_tip_reachable(const char *path, const struct object_id *oid)
2179 {
2180 struct child_process cp = CHILD_PROCESS_INIT;
2181 struct strbuf rev = STRBUF_INIT;
2182 char *hex = oid_to_hex(oid);
2183
2184 cp.git_cmd = 1;
2185 cp.dir = path;
2186 cp.no_stderr = 1;
2187 strvec_pushl(&cp.args, "rev-list", "-n", "1", hex, "--not", "--all", NULL);
2188
2189 prepare_submodule_repo_env(&cp.env);
2190
2191 if (capture_command(&cp, &rev, GIT_MAX_HEXSZ + 1) || rev.len)
2192 return 0;
2193
2194 return 1;
2195 }
2196
2197 static int fetch_in_submodule(const char *module_path, int depth, int quiet,
2198 const struct object_id *oid)
2199 {
2200 struct child_process cp = CHILD_PROCESS_INIT;
2201
2202 prepare_submodule_repo_env(&cp.env);
2203 cp.git_cmd = 1;
2204 cp.dir = module_path;
2205
2206 strvec_push(&cp.args, "fetch");
2207 if (quiet)
2208 strvec_push(&cp.args, "--quiet");
2209 if (depth)
2210 strvec_pushf(&cp.args, "--depth=%d", depth);
2211 if (oid) {
2212 char *hex = oid_to_hex(oid);
2213 char *remote = get_default_remote();
2214
2215 strvec_pushl(&cp.args, remote, hex, NULL);
2216 free(remote);
2217 }
2218
2219 return run_command(&cp);
2220 }
2221
2222 static int run_update_command(const struct update_data *ud, int subforce)
2223 {
2224 struct child_process cp = CHILD_PROCESS_INIT;
2225 char *oid = oid_to_hex(&ud->oid);
2226 int ret;
2227
2228 switch (ud->update_strategy.type) {
2229 case SM_UPDATE_CHECKOUT:
2230 cp.git_cmd = 1;
2231 strvec_pushl(&cp.args, "checkout", "-q", NULL);
2232 if (subforce)
2233 strvec_push(&cp.args, "-f");
2234 break;
2235 case SM_UPDATE_REBASE:
2236 cp.git_cmd = 1;
2237 strvec_push(&cp.args, "rebase");
2238 if (ud->quiet)
2239 strvec_push(&cp.args, "--quiet");
2240 break;
2241 case SM_UPDATE_MERGE:
2242 cp.git_cmd = 1;
2243 strvec_push(&cp.args, "merge");
2244 if (ud->quiet)
2245 strvec_push(&cp.args, "--quiet");
2246 break;
2247 case SM_UPDATE_COMMAND:
2248 cp.use_shell = 1;
2249 strvec_push(&cp.args, ud->update_strategy.command);
2250 break;
2251 default:
2252 BUG("unexpected update strategy type: %d",
2253 ud->update_strategy.type);
2254 }
2255 strvec_push(&cp.args, oid);
2256
2257 cp.dir = ud->sm_path;
2258 prepare_submodule_repo_env(&cp.env);
2259 if ((ret = run_command(&cp))) {
2260 switch (ud->update_strategy.type) {
2261 case SM_UPDATE_CHECKOUT:
2262 die_message(_("Unable to checkout '%s' in submodule path '%s'"),
2263 oid, ud->displaypath);
2264 /* No "ret" assignment, use "git checkout"'s */
2265 break;
2266 case SM_UPDATE_REBASE:
2267 ret = die_message(_("Unable to rebase '%s' in submodule path '%s'"),
2268 oid, ud->displaypath);
2269 break;
2270 case SM_UPDATE_MERGE:
2271 ret = die_message(_("Unable to merge '%s' in submodule path '%s'"),
2272 oid, ud->displaypath);
2273 break;
2274 case SM_UPDATE_COMMAND:
2275 ret = die_message(_("Execution of '%s %s' failed in submodule path '%s'"),
2276 ud->update_strategy.command, oid, ud->displaypath);
2277 break;
2278 default:
2279 BUG("unexpected update strategy type: %d",
2280 ud->update_strategy.type);
2281 }
2282
2283 return ret;
2284 }
2285
2286 if (ud->quiet)
2287 return 0;
2288
2289 switch (ud->update_strategy.type) {
2290 case SM_UPDATE_CHECKOUT:
2291 printf(_("Submodule path '%s': checked out '%s'\n"),
2292 ud->displaypath, oid);
2293 break;
2294 case SM_UPDATE_REBASE:
2295 printf(_("Submodule path '%s': rebased into '%s'\n"),
2296 ud->displaypath, oid);
2297 break;
2298 case SM_UPDATE_MERGE:
2299 printf(_("Submodule path '%s': merged in '%s'\n"),
2300 ud->displaypath, oid);
2301 break;
2302 case SM_UPDATE_COMMAND:
2303 printf(_("Submodule path '%s': '%s %s'\n"),
2304 ud->displaypath, ud->update_strategy.command, oid);
2305 break;
2306 default:
2307 BUG("unexpected update strategy type: %d",
2308 ud->update_strategy.type);
2309 }
2310
2311 return 0;
2312 }
2313
2314 static int run_update_procedure(const struct update_data *ud)
2315 {
2316 int subforce = is_null_oid(&ud->suboid) || ud->force;
2317
2318 if (!ud->nofetch) {
2319 /*
2320 * Run fetch only if `oid` isn't present or it
2321 * is not reachable from a ref.
2322 */
2323 if (!is_tip_reachable(ud->sm_path, &ud->oid) &&
2324 fetch_in_submodule(ud->sm_path, ud->depth, ud->quiet, NULL) &&
2325 !ud->quiet)
2326 fprintf_ln(stderr,
2327 _("Unable to fetch in submodule path '%s'; "
2328 "trying to directly fetch %s:"),
2329 ud->displaypath, oid_to_hex(&ud->oid));
2330 /*
2331 * Now we tried the usual fetch, but `oid` may
2332 * not be reachable from any of the refs.
2333 */
2334 if (!is_tip_reachable(ud->sm_path, &ud->oid) &&
2335 fetch_in_submodule(ud->sm_path, ud->depth, ud->quiet, &ud->oid))
2336 return die_message(_("Fetched in submodule path '%s', but it did not "
2337 "contain %s. Direct fetching of that commit failed."),
2338 ud->displaypath, oid_to_hex(&ud->oid));
2339 }
2340
2341 return run_update_command(ud, subforce);
2342 }
2343
2344 static int remote_submodule_branch(const char *path, const char **branch)
2345 {
2346 const struct submodule *sub;
2347 char *key;
2348 *branch = NULL;
2349
2350 sub = submodule_from_path(the_repository, null_oid(), path);
2351 if (!sub)
2352 return die_message(_("could not initialize submodule at path '%s'"),
2353 path);
2354
2355 key = xstrfmt("submodule.%s.branch", sub->name);
2356 if (repo_config_get_string_tmp(the_repository, key, branch))
2357 *branch = sub->branch;
2358 free(key);
2359
2360 if (!*branch) {
2361 *branch = "HEAD";
2362 return 0;
2363 }
2364
2365 if (!strcmp(*branch, ".")) {
2366 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
2367
2368 if (!refname)
2369 return die_message(_("No such ref: %s"), "HEAD");
2370
2371 /* detached HEAD */
2372 if (!strcmp(refname, "HEAD"))
2373 return die_message(_("Submodule (%s) branch configured to inherit "
2374 "branch from superproject, but the superproject "
2375 "is not on any branch"), sub->name);
2376
2377 if (!skip_prefix(refname, "refs/heads/", &refname))
2378 return die_message(_("Expecting a full ref name, got %s"),
2379 refname);
2380
2381 *branch = refname;
2382 return 0;
2383 }
2384
2385 /* Our "branch" is coming from repo_config_get_string_tmp() */
2386 return 0;
2387 }
2388
2389 static int ensure_core_worktree(const char *path)
2390 {
2391 const char *cw;
2392 struct repository subrepo;
2393
2394 if (repo_submodule_init(&subrepo, the_repository, path, null_oid()))
2395 return die_message(_("could not get a repository handle for submodule '%s'"),
2396 path);
2397
2398 if (!repo_config_get_string_tmp(&subrepo, "core.worktree", &cw)) {
2399 char *cfg_file, *abs_path;
2400 const char *rel_path;
2401 struct strbuf sb = STRBUF_INIT;
2402
2403 cfg_file = repo_git_path(&subrepo, "config");
2404
2405 abs_path = absolute_pathdup(path);
2406 rel_path = relative_path(abs_path, subrepo.gitdir, &sb);
2407
2408 git_config_set_in_file(cfg_file, "core.worktree", rel_path);
2409
2410 free(cfg_file);
2411 free(abs_path);
2412 strbuf_release(&sb);
2413 }
2414
2415 repo_clear(&subrepo);
2416 return 0;
2417 }
2418
2419 static const char *submodule_update_type_to_label(enum submodule_update_type type)
2420 {
2421 switch (type) {
2422 case SM_UPDATE_CHECKOUT:
2423 return "checkout";
2424 case SM_UPDATE_MERGE:
2425 return "merge";
2426 case SM_UPDATE_REBASE:
2427 return "rebase";
2428 case SM_UPDATE_UNSPECIFIED:
2429 case SM_UPDATE_NONE:
2430 case SM_UPDATE_COMMAND:
2431 break;
2432 }
2433 BUG("unreachable with type %d", type);
2434 }
2435
2436 static void update_data_to_args(const struct update_data *update_data,
2437 struct strvec *args)
2438 {
2439 enum submodule_update_type update_type = update_data->update_default;
2440
2441 if (update_data->displaypath) {
2442 strvec_push(args, "--super-prefix");
2443 strvec_pushf(args, "%s/", update_data->displaypath);
2444 }
2445 strvec_pushl(args, "submodule--helper", "update", "--recursive", NULL);
2446 strvec_pushf(args, "--jobs=%d", update_data->max_jobs);
2447 if (update_data->quiet)
2448 strvec_push(args, "--quiet");
2449 if (update_data->force)
2450 strvec_push(args, "--force");
2451 if (update_data->init)
2452 strvec_push(args, "--init");
2453 if (update_data->remote)
2454 strvec_push(args, "--remote");
2455 if (update_data->nofetch)
2456 strvec_push(args, "--no-fetch");
2457 if (update_data->dissociate)
2458 strvec_push(args, "--dissociate");
2459 if (update_data->progress)
2460 strvec_push(args, "--progress");
2461 if (update_data->require_init)
2462 strvec_push(args, "--require-init");
2463 if (update_data->depth)
2464 strvec_pushf(args, "--depth=%d", update_data->depth);
2465 if (update_type != SM_UPDATE_UNSPECIFIED)
2466 strvec_pushf(args, "--%s",
2467 submodule_update_type_to_label(update_type));
2468
2469 if (update_data->references.nr) {
2470 struct string_list_item *item;
2471
2472 for_each_string_list_item(item, &update_data->references)
2473 strvec_pushl(args, "--reference", item->string, NULL);
2474 }
2475 if (update_data->filter_options && update_data->filter_options->choice)
2476 strvec_pushf(args, "--filter=%s",
2477 expand_list_objects_filter_spec(
2478 update_data->filter_options));
2479 if (update_data->recommend_shallow == 0)
2480 strvec_push(args, "--no-recommend-shallow");
2481 else if (update_data->recommend_shallow == 1)
2482 strvec_push(args, "--recommend-shallow");
2483 if (update_data->single_branch >= 0)
2484 strvec_push(args, update_data->single_branch ?
2485 "--single-branch" :
2486 "--no-single-branch");
2487 }
2488
2489 static int update_submodule(struct update_data *update_data)
2490 {
2491 int ret;
2492
2493 ret = determine_submodule_update_strategy(the_repository,
2494 update_data->just_cloned,
2495 update_data->sm_path,
2496 update_data->update_default,
2497 &update_data->update_strategy);
2498 if (ret)
2499 return ret;
2500
2501 if (update_data->just_cloned)
2502 oidcpy(&update_data->suboid, null_oid());
2503 else if (resolve_gitlink_ref(update_data->sm_path, "HEAD", &update_data->suboid))
2504 return die_message(_("Unable to find current revision in submodule path '%s'"),
2505 update_data->displaypath);
2506
2507 if (update_data->remote) {
2508 char *remote_name;
2509 const char *branch;
2510 char *remote_ref;
2511 int code;
2512
2513 code = get_default_remote_submodule(update_data->sm_path, &remote_name);
2514 if (code)
2515 return code;
2516 code = remote_submodule_branch(update_data->sm_path, &branch);
2517 if (code)
2518 return code;
2519 remote_ref = xstrfmt("refs/remotes/%s/%s", remote_name, branch);
2520
2521 free(remote_name);
2522
2523 if (!update_data->nofetch) {
2524 if (fetch_in_submodule(update_data->sm_path, update_data->depth,
2525 0, NULL))
2526 return die_message(_("Unable to fetch in submodule path '%s'"),
2527 update_data->sm_path);
2528 }
2529
2530 if (resolve_gitlink_ref(update_data->sm_path, remote_ref, &update_data->oid))
2531 return die_message(_("Unable to find %s revision in submodule path '%s'"),
2532 remote_ref, update_data->sm_path);
2533
2534 free(remote_ref);
2535 }
2536
2537 if (!oideq(&update_data->oid, &update_data->suboid) || update_data->force) {
2538 ret = run_update_procedure(update_data);
2539 if (ret)
2540 return ret;
2541 }
2542
2543 if (update_data->recursive) {
2544 struct child_process cp = CHILD_PROCESS_INIT;
2545 struct update_data next = *update_data;
2546
2547 next.prefix = NULL;
2548 oidcpy(&next.oid, null_oid());
2549 oidcpy(&next.suboid, null_oid());
2550
2551 cp.dir = update_data->sm_path;
2552 cp.git_cmd = 1;
2553 prepare_submodule_repo_env(&cp.env);
2554 update_data_to_args(&next, &cp.args);
2555
2556 ret = run_command(&cp);
2557 if (ret)
2558 die_message(_("Failed to recurse into submodule path '%s'"),
2559 update_data->displaypath);
2560 return ret;
2561 }
2562
2563 return 0;
2564 }
2565
2566 static int update_submodules(struct update_data *update_data)
2567 {
2568 int i, ret = 0;
2569 struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
2570
2571 suc.update_data = update_data;
2572 run_processes_parallel_tr2(suc.update_data->max_jobs, update_clone_get_next_task,
2573 update_clone_start_failure,
2574 update_clone_task_finished, &suc, "submodule",
2575 "parallel/update");
2576
2577 /*
2578 * We saved the output and put it out all at once now.
2579 * That means:
2580 * - the listener does not have to interleave their (checkout)
2581 * work with our fetching. The writes involved in a
2582 * checkout involve more straightforward sequential I/O.
2583 * - the listener can avoid doing any work if fetching failed.
2584 */
2585 if (suc.quickstop) {
2586 ret = 1;
2587 goto cleanup;
2588 }
2589
2590 for (i = 0; i < suc.update_clone_nr; i++) {
2591 struct update_clone_data ucd = suc.update_clone[i];
2592 int code;
2593
2594 oidcpy(&update_data->oid, &ucd.oid);
2595 update_data->just_cloned = ucd.just_cloned;
2596 update_data->sm_path = ucd.sub->path;
2597
2598 code = ensure_core_worktree(update_data->sm_path);
2599 if (code)
2600 goto fail;
2601
2602 update_data->displaypath = get_submodule_displaypath(
2603 update_data->sm_path, update_data->prefix);
2604 code = update_submodule(update_data);
2605 FREE_AND_NULL(update_data->displaypath);
2606 fail:
2607 if (!code)
2608 continue;
2609 ret = code;
2610 if (ret == 128)
2611 goto cleanup;
2612 }
2613
2614 cleanup:
2615 submodule_update_clone_release(&suc);
2616 string_list_clear(&update_data->references, 0);
2617 return ret;
2618 }
2619
2620 static int module_update(int argc, const char **argv, const char *prefix)
2621 {
2622 struct pathspec pathspec = { 0 };
2623 struct pathspec pathspec2 = { 0 };
2624 struct update_data opt = UPDATE_DATA_INIT;
2625 struct list_objects_filter_options filter_options =
2626 LIST_OBJECTS_FILTER_INIT;
2627 int ret;
2628 struct option module_update_options[] = {
2629 OPT__FORCE(&opt.force, N_("force checkout updates"), 0),
2630 OPT_BOOL(0, "init", &opt.init,
2631 N_("initialize uninitialized submodules before update")),
2632 OPT_BOOL(0, "remote", &opt.remote,
2633 N_("use SHA-1 of submodule's remote tracking branch")),
2634 OPT_BOOL(0, "recursive", &opt.recursive,
2635 N_("traverse submodules recursively")),
2636 OPT_BOOL('N', "no-fetch", &opt.nofetch,
2637 N_("don't fetch new objects from the remote site")),
2638 OPT_STRING(0, "prefix", &opt.prefix,
2639 N_("path"),
2640 N_("path into the working tree")),
2641 OPT_SET_INT(0, "checkout", &opt.update_default,
2642 N_("use the 'checkout' update strategy (default)"),
2643 SM_UPDATE_CHECKOUT),
2644 OPT_SET_INT('m', "merge", &opt.update_default,
2645 N_("use the 'merge' update strategy"),
2646 SM_UPDATE_MERGE),
2647 OPT_SET_INT('r', "rebase", &opt.update_default,
2648 N_("use the 'rebase' update strategy"),
2649 SM_UPDATE_REBASE),
2650 OPT_STRING_LIST(0, "reference", &opt.references, N_("repo"),
2651 N_("reference repository")),
2652 OPT_BOOL(0, "dissociate", &opt.dissociate,
2653 N_("use --reference only while cloning")),
2654 OPT_INTEGER(0, "depth", &opt.depth,
2655 N_("create a shallow clone truncated to the "
2656 "specified number of revisions")),
2657 OPT_INTEGER('j', "jobs", &opt.max_jobs,
2658 N_("parallel jobs")),
2659 OPT_BOOL(0, "recommend-shallow", &opt.recommend_shallow,
2660 N_("whether the initial clone should follow the shallow recommendation")),
2661 OPT__QUIET(&opt.quiet, N_("don't print cloning progress")),
2662 OPT_BOOL(0, "progress", &opt.progress,
2663 N_("force cloning progress")),
2664 OPT_BOOL(0, "require-init", &opt.require_init,
2665 N_("disallow cloning into non-empty directory, implies --init")),
2666 OPT_BOOL(0, "single-branch", &opt.single_branch,
2667 N_("clone only one branch, HEAD or --branch")),
2668 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
2669 OPT_END()
2670 };
2671 const char *const git_submodule_helper_usage[] = {
2672 N_("git submodule [--quiet] update"
2673 " [--init [--filter=<filter-spec>]] [--remote]"
2674 " [-N|--no-fetch] [-f|--force]"
2675 " [--checkout|--merge|--rebase]"
2676 " [--[no-]recommend-shallow] [--reference <repository>]"
2677 " [--recursive] [--[no-]single-branch] [--] [<path>...]"),
2678 NULL
2679 };
2680
2681 update_clone_config_from_gitmodules(&opt.max_jobs);
2682 git_config(git_update_clone_config, &opt.max_jobs);
2683
2684 argc = parse_options(argc, argv, prefix, module_update_options,
2685 git_submodule_helper_usage, 0);
2686
2687 if (opt.require_init)
2688 opt.init = 1;
2689
2690 if (filter_options.choice && !opt.init) {
2691 usage_with_options(git_submodule_helper_usage,
2692 module_update_options);
2693 }
2694
2695 opt.filter_options = &filter_options;
2696
2697 if (opt.update_default)
2698 opt.update_strategy.type = opt.update_default;
2699
2700 if (module_list_compute(argc, argv, prefix, &pathspec, &opt.list) < 0) {
2701 ret = 1;
2702 goto cleanup;
2703 }
2704
2705 if (pathspec.nr)
2706 opt.warn_if_uninitialized = 1;
2707
2708 if (opt.init) {
2709 struct module_list list = MODULE_LIST_INIT;
2710 struct init_cb info = INIT_CB_INIT;
2711
2712 if (module_list_compute(argc, argv, opt.prefix,
2713 &pathspec2, &list) < 0) {
2714 module_list_release(&list);
2715 ret = 1;
2716 goto cleanup;
2717 }
2718
2719 /*
2720 * If there are no path args and submodule.active is set then,
2721 * by default, only initialize 'active' modules.
2722 */
2723 if (!argc && git_config_get_value_multi("submodule.active"))
2724 module_list_active(&list);
2725
2726 info.prefix = opt.prefix;
2727 if (opt.quiet)
2728 info.flags |= OPT_QUIET;
2729
2730 for_each_listed_submodule(&list, init_submodule_cb, &info);
2731 module_list_release(&list);
2732 }
2733
2734 ret = update_submodules(&opt);
2735 cleanup:
2736 update_data_release(&opt);
2737 list_objects_filter_release(&filter_options);
2738 clear_pathspec(&pathspec);
2739 clear_pathspec(&pathspec2);
2740 return ret;
2741 }
2742
2743 static int push_check(int argc, const char **argv, const char *prefix)
2744 {
2745 struct remote *remote;
2746 const char *superproject_head;
2747 char *head;
2748 int detached_head = 0;
2749 struct object_id head_oid;
2750
2751 if (argc < 3)
2752 die("submodule--helper push-check requires at least 2 arguments");
2753
2754 /*
2755 * superproject's resolved head ref.
2756 * if HEAD then the superproject is in a detached head state, otherwise
2757 * it will be the resolved head ref.
2758 */
2759 superproject_head = argv[1];
2760 argv++;
2761 argc--;
2762 /* Get the submodule's head ref and determine if it is detached */
2763 head = resolve_refdup("HEAD", 0, &head_oid, NULL);
2764 if (!head)
2765 die(_("Failed to resolve HEAD as a valid ref."));
2766 if (!strcmp(head, "HEAD"))
2767 detached_head = 1;
2768
2769 /*
2770 * The remote must be configured.
2771 * This is to avoid pushing to the exact same URL as the parent.
2772 */
2773 remote = pushremote_get(argv[1]);
2774 if (!remote || remote->origin == REMOTE_UNCONFIGURED)
2775 die("remote '%s' not configured", argv[1]);
2776
2777 /* Check the refspec */
2778 if (argc > 2) {
2779 int i;
2780 struct ref *local_refs = get_local_heads();
2781 struct refspec refspec = REFSPEC_INIT_PUSH;
2782
2783 refspec_appendn(&refspec, argv + 2, argc - 2);
2784
2785 for (i = 0; i < refspec.nr; i++) {
2786 const struct refspec_item *rs = &refspec.items[i];
2787
2788 if (rs->pattern || rs->matching)
2789 continue;
2790
2791 /* LHS must match a single ref */
2792 switch (count_refspec_match(rs->src, local_refs, NULL)) {
2793 case 1:
2794 break;
2795 case 0:
2796 /*
2797 * If LHS matches 'HEAD' then we need to ensure
2798 * that it matches the same named branch
2799 * checked out in the superproject.
2800 */
2801 if (!strcmp(rs->src, "HEAD")) {
2802 if (!detached_head &&
2803 !strcmp(head, superproject_head))
2804 break;
2805 die("HEAD does not match the named branch in the superproject");
2806 }
2807 /* fallthrough */
2808 default:
2809 die("src refspec '%s' must name a ref",
2810 rs->src);
2811 }
2812 }
2813 refspec_clear(&refspec);
2814 }
2815 free(head);
2816
2817 return 0;
2818 }
2819
2820 static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
2821 {
2822 int i;
2823 struct pathspec pathspec = { 0 };
2824 struct module_list list = MODULE_LIST_INIT;
2825 unsigned flags = ABSORB_GITDIR_RECURSE_SUBMODULES;
2826 struct option embed_gitdir_options[] = {
2827 OPT_STRING(0, "prefix", &prefix,
2828 N_("path"),
2829 N_("path into the working tree")),
2830 OPT_BIT(0, "--recursive", &flags, N_("recurse into submodules"),
2831 ABSORB_GITDIR_RECURSE_SUBMODULES),
2832 OPT_END()
2833 };
2834 const char *const git_submodule_helper_usage[] = {
2835 N_("git submodule absorbgitdirs [<options>] [<path>...]"),
2836 NULL
2837 };
2838 int ret = 1;
2839
2840 argc = parse_options(argc, argv, prefix, embed_gitdir_options,
2841 git_submodule_helper_usage, 0);
2842
2843 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
2844 goto cleanup;
2845
2846 for (i = 0; i < list.nr; i++)
2847 absorb_git_dir_into_superproject(list.entries[i]->name, flags);
2848
2849 ret = 0;
2850 cleanup:
2851 clear_pathspec(&pathspec);
2852 module_list_release(&list);
2853 return ret;
2854 }
2855
2856 static int module_config(int argc, const char **argv, const char *prefix)
2857 {
2858 enum {
2859 CHECK_WRITEABLE = 1,
2860 DO_UNSET = 2
2861 } command = 0;
2862 struct option module_config_options[] = {
2863 OPT_CMDMODE(0, "check-writeable", &command,
2864 N_("check if it is safe to write to the .gitmodules file"),
2865 CHECK_WRITEABLE),
2866 OPT_CMDMODE(0, "unset", &command,
2867 N_("unset the config in the .gitmodules file"),
2868 DO_UNSET),
2869 OPT_END()
2870 };
2871 const char *const git_submodule_helper_usage[] = {
2872 N_("git submodule--helper config <name> [<value>]"),
2873 N_("git submodule--helper config --unset <name>"),
2874 "git submodule--helper config --check-writeable",
2875 NULL
2876 };
2877
2878 argc = parse_options(argc, argv, prefix, module_config_options,
2879 git_submodule_helper_usage, PARSE_OPT_KEEP_ARGV0);
2880
2881 if (argc == 1 && command == CHECK_WRITEABLE)
2882 return is_writing_gitmodules_ok() ? 0 : -1;
2883
2884 /* Equivalent to ACTION_GET in builtin/config.c */
2885 if (argc == 2 && command != DO_UNSET)
2886 return print_config_from_gitmodules(the_repository, argv[1]);
2887
2888 /* Equivalent to ACTION_SET in builtin/config.c */
2889 if (argc == 3 || (argc == 2 && command == DO_UNSET)) {
2890 const char *value = (argc == 3) ? argv[2] : NULL;
2891
2892 if (!is_writing_gitmodules_ok())
2893 die(_("please make sure that the .gitmodules file is in the working tree"));
2894
2895 return config_set_in_gitmodules_file_gently(argv[1], value);
2896 }
2897
2898 usage_with_options(git_submodule_helper_usage, module_config_options);
2899 }
2900
2901 static int module_set_url(int argc, const char **argv, const char *prefix)
2902 {
2903 int quiet = 0;
2904 const char *newurl;
2905 const char *path;
2906 char *config_name;
2907 struct option options[] = {
2908 OPT__QUIET(&quiet, N_("suppress output for setting url of a submodule")),
2909 OPT_END()
2910 };
2911 const char *const usage[] = {
2912 N_("git submodule set-url [--quiet] <path> <newurl>"),
2913 NULL
2914 };
2915
2916 argc = parse_options(argc, argv, prefix, options, usage, 0);
2917
2918 if (argc != 2 || !(path = argv[0]) || !(newurl = argv[1]))
2919 usage_with_options(usage, options);
2920
2921 config_name = xstrfmt("submodule.%s.url", path);
2922
2923 config_set_in_gitmodules_file_gently(config_name, newurl);
2924 sync_submodule(path, prefix, quiet ? OPT_QUIET : 0);
2925
2926 free(config_name);
2927
2928 return 0;
2929 }
2930
2931 static int module_set_branch(int argc, const char **argv, const char *prefix)
2932 {
2933 int opt_default = 0, ret;
2934 const char *opt_branch = NULL;
2935 const char *path;
2936 char *config_name;
2937 struct option options[] = {
2938 /*
2939 * We accept the `quiet` option for uniformity across subcommands,
2940 * though there is nothing to make less verbose in this subcommand.
2941 */
2942 OPT_NOOP_NOARG('q', "quiet"),
2943
2944 OPT_BOOL('d', "default", &opt_default,
2945 N_("set the default tracking branch to master")),
2946 OPT_STRING('b', "branch", &opt_branch, N_("branch"),
2947 N_("set the default tracking branch")),
2948 OPT_END()
2949 };
2950 const char *const usage[] = {
2951 N_("git submodule set-branch [-q|--quiet] (-d|--default) <path>"),
2952 N_("git submodule set-branch [-q|--quiet] (-b|--branch) <branch> <path>"),
2953 NULL
2954 };
2955
2956 argc = parse_options(argc, argv, prefix, options, usage, 0);
2957
2958 if (!opt_branch && !opt_default)
2959 die(_("--branch or --default required"));
2960
2961 if (opt_branch && opt_default)
2962 die(_("options '%s' and '%s' cannot be used together"), "--branch", "--default");
2963
2964 if (argc != 1 || !(path = argv[0]))
2965 usage_with_options(usage, options);
2966
2967 config_name = xstrfmt("submodule.%s.branch", path);
2968 ret = config_set_in_gitmodules_file_gently(config_name, opt_branch);
2969
2970 free(config_name);
2971 return !!ret;
2972 }
2973
2974 static int module_create_branch(int argc, const char **argv, const char *prefix)
2975 {
2976 enum branch_track track;
2977 int quiet = 0, force = 0, reflog = 0, dry_run = 0;
2978 struct option options[] = {
2979 OPT__QUIET(&quiet, N_("print only error messages")),
2980 OPT__FORCE(&force, N_("force creation"), 0),
2981 OPT_BOOL(0, "create-reflog", &reflog,
2982 N_("create the branch's reflog")),
2983 OPT_CALLBACK_F('t', "track", &track, "(direct|inherit)",
2984 N_("set branch tracking configuration"),
2985 PARSE_OPT_OPTARG,
2986 parse_opt_tracking_mode),
2987 OPT__DRY_RUN(&dry_run,
2988 N_("show whether the branch would be created")),
2989 OPT_END()
2990 };
2991 const char *const usage[] = {
2992 N_("git submodule--helper create-branch [-f|--force] [--create-reflog] [-q|--quiet] [-t|--track] [-n|--dry-run] <name> <start-oid> <start-name>"),
2993 NULL
2994 };
2995
2996 git_config(git_default_config, NULL);
2997 track = git_branch_track;
2998 argc = parse_options(argc, argv, prefix, options, usage, 0);
2999
3000 if (argc != 3)
3001 usage_with_options(usage, options);
3002
3003 if (!quiet && !dry_run)
3004 printf_ln(_("creating branch '%s'"), argv[0]);
3005
3006 create_branches_recursively(the_repository, argv[0], argv[1], argv[2],
3007 force, reflog, quiet, track, dry_run);
3008 return 0;
3009 }
3010
3011 struct add_data {
3012 const char *prefix;
3013 const char *branch;
3014 const char *reference_path;
3015 char *sm_path;
3016 const char *sm_name;
3017 const char *repo;
3018 const char *realrepo;
3019 int depth;
3020 unsigned int force: 1;
3021 unsigned int quiet: 1;
3022 unsigned int progress: 1;
3023 unsigned int dissociate: 1;
3024 };
3025 #define ADD_DATA_INIT { .depth = -1 }
3026
3027 static void append_fetch_remotes(struct strbuf *msg, const char *git_dir_path)
3028 {
3029 struct child_process cp_remote = CHILD_PROCESS_INIT;
3030 struct strbuf sb_remote_out = STRBUF_INIT;
3031
3032 cp_remote.git_cmd = 1;
3033 strvec_pushf(&cp_remote.env,
3034 "GIT_DIR=%s", git_dir_path);
3035 strvec_push(&cp_remote.env, "GIT_WORK_TREE=.");
3036 strvec_pushl(&cp_remote.args, "remote", "-v", NULL);
3037 if (!capture_command(&cp_remote, &sb_remote_out, 0)) {
3038 char *next_line;
3039 char *line = sb_remote_out.buf;
3040
3041 while ((next_line = strchr(line, '\n')) != NULL) {
3042 size_t len = next_line - line;
3043
3044 if (strip_suffix_mem(line, &len, " (fetch)"))
3045 strbuf_addf(msg, " %.*s\n", (int)len, line);
3046 line = next_line + 1;
3047 }
3048 }
3049
3050 strbuf_release(&sb_remote_out);
3051 }
3052
3053 static int add_submodule(const struct add_data *add_data)
3054 {
3055 char *submod_gitdir_path;
3056 struct module_clone_data clone_data = MODULE_CLONE_DATA_INIT;
3057 struct string_list reference = STRING_LIST_INIT_NODUP;
3058 int ret = -1;
3059
3060 /* perhaps the path already exists and is already a git repo, else clone it */
3061 if (is_directory(add_data->sm_path)) {
3062 struct strbuf sm_path = STRBUF_INIT;
3063 strbuf_addstr(&sm_path, add_data->sm_path);
3064 submod_gitdir_path = xstrfmt("%s/.git", add_data->sm_path);
3065 if (is_nonbare_repository_dir(&sm_path))
3066 printf(_("Adding existing repo at '%s' to the index\n"),
3067 add_data->sm_path);
3068 else
3069 die(_("'%s' already exists and is not a valid git repo"),
3070 add_data->sm_path);
3071 strbuf_release(&sm_path);
3072 free(submod_gitdir_path);
3073 } else {
3074 struct child_process cp = CHILD_PROCESS_INIT;
3075
3076 submod_gitdir_path = xstrfmt(".git/modules/%s", add_data->sm_name);
3077
3078 if (is_directory(submod_gitdir_path)) {
3079 if (!add_data->force) {
3080 struct strbuf msg = STRBUF_INIT;
3081 char *die_msg;
3082
3083 strbuf_addf(&msg, _("A git directory for '%s' is found "
3084 "locally with remote(s):\n"),
3085 add_data->sm_name);
3086
3087 append_fetch_remotes(&msg, submod_gitdir_path);
3088 free(submod_gitdir_path);
3089
3090 strbuf_addf(&msg, _("If you want to reuse this local git "
3091 "directory instead of cloning again from\n"
3092 " %s\n"
3093 "use the '--force' option. If the local git "
3094 "directory is not the correct repo\n"
3095 "or you are unsure what this means choose "
3096 "another name with the '--name' option."),
3097 add_data->realrepo);
3098
3099 die_msg = strbuf_detach(&msg, NULL);
3100 die("%s", die_msg);
3101 } else {
3102 printf(_("Reactivating local git directory for "
3103 "submodule '%s'\n"), add_data->sm_name);
3104 }
3105 }
3106 free(submod_gitdir_path);
3107
3108 clone_data.prefix = add_data->prefix;
3109 clone_data.path = add_data->sm_path;
3110 clone_data.name = add_data->sm_name;
3111 clone_data.url = add_data->realrepo;
3112 clone_data.quiet = add_data->quiet;
3113 clone_data.progress = add_data->progress;
3114 if (add_data->reference_path) {
3115 char *p = xstrdup(add_data->reference_path);
3116
3117 string_list_append(&reference, p)->util = p;
3118 }
3119 clone_data.dissociate = add_data->dissociate;
3120 if (add_data->depth >= 0)
3121 clone_data.depth = xstrfmt("%d", add_data->depth);
3122
3123 if (clone_submodule(&clone_data, &reference))
3124 goto cleanup;
3125
3126 prepare_submodule_repo_env(&cp.env);
3127 cp.git_cmd = 1;
3128 cp.dir = add_data->sm_path;
3129 /*
3130 * NOTE: we only get here if add_data->force is true, so
3131 * passing --force to checkout is reasonable.
3132 */
3133 strvec_pushl(&cp.args, "checkout", "-f", "-q", NULL);
3134
3135 if (add_data->branch) {
3136 strvec_pushl(&cp.args, "-B", add_data->branch, NULL);
3137 strvec_pushf(&cp.args, "origin/%s", add_data->branch);
3138 }
3139
3140 if (run_command(&cp))
3141 die(_("unable to checkout submodule '%s'"), add_data->sm_path);
3142 }
3143 ret = 0;
3144 cleanup:
3145 string_list_clear(&reference, 1);
3146 return ret;
3147 }
3148
3149 static int config_submodule_in_gitmodules(const char *name, const char *var, const char *value)
3150 {
3151 char *key;
3152 int ret;
3153
3154 if (!is_writing_gitmodules_ok())
3155 die(_("please make sure that the .gitmodules file is in the working tree"));
3156
3157 key = xstrfmt("submodule.%s.%s", name, var);
3158 ret = config_set_in_gitmodules_file_gently(key, value);
3159 free(key);
3160
3161 return ret;
3162 }
3163
3164 static void configure_added_submodule(struct add_data *add_data)
3165 {
3166 char *key;
3167 const char *val;
3168 struct child_process add_submod = CHILD_PROCESS_INIT;
3169 struct child_process add_gitmodules = CHILD_PROCESS_INIT;
3170
3171 key = xstrfmt("submodule.%s.url", add_data->sm_name);
3172 git_config_set_gently(key, add_data->realrepo);
3173 free(key);
3174
3175 add_submod.git_cmd = 1;
3176 strvec_pushl(&add_submod.args, "add",
3177 "--no-warn-embedded-repo", NULL);
3178 if (add_data->force)
3179 strvec_push(&add_submod.args, "--force");
3180 strvec_pushl(&add_submod.args, "--", add_data->sm_path, NULL);
3181
3182 if (run_command(&add_submod))
3183 die(_("Failed to add submodule '%s'"), add_data->sm_path);
3184
3185 if (config_submodule_in_gitmodules(add_data->sm_name, "path", add_data->sm_path) ||
3186 config_submodule_in_gitmodules(add_data->sm_name, "url", add_data->repo))
3187 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3188
3189 if (add_data->branch) {
3190 if (config_submodule_in_gitmodules(add_data->sm_name,
3191 "branch", add_data->branch))
3192 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3193 }
3194
3195 add_gitmodules.git_cmd = 1;
3196 strvec_pushl(&add_gitmodules.args,
3197 "add", "--force", "--", ".gitmodules", NULL);
3198
3199 if (run_command(&add_gitmodules))
3200 die(_("Failed to register submodule '%s'"), add_data->sm_path);
3201
3202 /*
3203 * NEEDSWORK: In a multi-working-tree world this needs to be
3204 * set in the per-worktree config.
3205 */
3206 /*
3207 * NEEDSWORK: In the longer run, we need to get rid of this
3208 * pattern of querying "submodule.active" before calling
3209 * is_submodule_active(), since that function needs to find
3210 * out the value of "submodule.active" again anyway.
3211 */
3212 if (!git_config_get_string_tmp("submodule.active", &val)) {
3213 /*
3214 * If the submodule being added isn't already covered by the
3215 * current configured pathspec, set the submodule's active flag
3216 */
3217 if (!is_submodule_active(the_repository, add_data->sm_path)) {
3218 key = xstrfmt("submodule.%s.active", add_data->sm_name);
3219 git_config_set_gently(key, "true");
3220 free(key);
3221 }
3222 } else {
3223 key = xstrfmt("submodule.%s.active", add_data->sm_name);
3224 git_config_set_gently(key, "true");
3225 free(key);
3226 }
3227 }
3228
3229 static void die_on_index_match(const char *path, int force)
3230 {
3231 struct pathspec ps;
3232 const char *args[] = { path, NULL };
3233 parse_pathspec(&ps, 0, PATHSPEC_PREFER_CWD, NULL, args);
3234
3235 if (read_cache_preload(NULL) < 0)
3236 die(_("index file corrupt"));
3237
3238 if (ps.nr) {
3239 int i;
3240 char *ps_matched = xcalloc(ps.nr, 1);
3241
3242 /* TODO: audit for interaction with sparse-index. */
3243 ensure_full_index(&the_index);
3244
3245 /*
3246 * Since there is only one pathspec, we just need
3247 * need to check ps_matched[0] to know if a cache
3248 * entry matched.
3249 */
3250 for (i = 0; i < active_nr; i++) {
3251 ce_path_match(&the_index, active_cache[i], &ps,
3252 ps_matched);
3253
3254 if (ps_matched[0]) {
3255 if (!force)
3256 die(_("'%s' already exists in the index"),
3257 path);
3258 if (!S_ISGITLINK(active_cache[i]->ce_mode))
3259 die(_("'%s' already exists in the index "
3260 "and is not a submodule"), path);
3261 break;
3262 }
3263 }
3264 free(ps_matched);
3265 }
3266 clear_pathspec(&ps);
3267 }
3268
3269 static void die_on_repo_without_commits(const char *path)
3270 {
3271 struct strbuf sb = STRBUF_INIT;
3272 strbuf_addstr(&sb, path);
3273 if (is_nonbare_repository_dir(&sb)) {
3274 struct object_id oid;
3275 if (resolve_gitlink_ref(path, "HEAD", &oid) < 0)
3276 die(_("'%s' does not have a commit checked out"), path);
3277 }
3278 strbuf_release(&sb);
3279 }
3280
3281 static int module_add(int argc, const char **argv, const char *prefix)
3282 {
3283 int force = 0, quiet = 0, progress = 0, dissociate = 0;
3284 struct add_data add_data = ADD_DATA_INIT;
3285 char *to_free = NULL;
3286 struct option options[] = {
3287 OPT_STRING('b', "branch", &add_data.branch, N_("branch"),
3288 N_("branch of repository to add as submodule")),
3289 OPT__FORCE(&force, N_("allow adding an otherwise ignored submodule path"),
3290 PARSE_OPT_NOCOMPLETE),
3291 OPT__QUIET(&quiet, N_("print only error messages")),
3292 OPT_BOOL(0, "progress", &progress, N_("force cloning progress")),
3293 OPT_STRING(0, "reference", &add_data.reference_path, N_("repository"),
3294 N_("reference repository")),
3295 OPT_BOOL(0, "dissociate", &dissociate, N_("borrow the objects from reference repositories")),
3296 OPT_STRING(0, "name", &add_data.sm_name, N_("name"),
3297 N_("sets the submodule's name to the given string "
3298 "instead of defaulting to its path")),
3299 OPT_INTEGER(0, "depth", &add_data.depth, N_("depth for shallow clones")),
3300 OPT_END()
3301 };
3302 const char *const usage[] = {
3303 N_("git submodule add [<options>] [--] <repository> [<path>]"),
3304 NULL
3305 };
3306 struct strbuf sb = STRBUF_INIT;
3307 int ret = 1;
3308
3309 argc = parse_options(argc, argv, prefix, options, usage, 0);
3310
3311 if (!is_writing_gitmodules_ok())
3312 die(_("please make sure that the .gitmodules file is in the working tree"));
3313
3314 if (prefix && *prefix &&
3315 add_data.reference_path && !is_absolute_path(add_data.reference_path))
3316 add_data.reference_path = xstrfmt("%s%s", prefix, add_data.reference_path);
3317
3318 if (argc == 0 || argc > 2)
3319 usage_with_options(usage, options);
3320
3321 add_data.repo = argv[0];
3322 if (argc == 1)
3323 add_data.sm_path = git_url_basename(add_data.repo, 0, 0);
3324 else
3325 add_data.sm_path = xstrdup(argv[1]);
3326
3327 if (prefix && *prefix && !is_absolute_path(add_data.sm_path)) {
3328 char *sm_path = add_data.sm_path;
3329
3330 add_data.sm_path = xstrfmt("%s%s", prefix, sm_path);
3331 free(sm_path);
3332 }
3333
3334 if (starts_with_dot_dot_slash(add_data.repo) ||
3335 starts_with_dot_slash(add_data.repo)) {
3336 if (prefix)
3337 die(_("Relative path can only be used from the toplevel "
3338 "of the working tree"));
3339
3340 /* dereference source url relative to parent's url */
3341 to_free = resolve_relative_url(add_data.repo, NULL, 1);
3342 add_data.realrepo = to_free;
3343 } else if (is_dir_sep(add_data.repo[0]) || strchr(add_data.repo, ':')) {
3344 add_data.realrepo = add_data.repo;
3345 } else {
3346 die(_("repo URL: '%s' must be absolute or begin with ./|../"),
3347 add_data.repo);
3348 }
3349
3350 /*
3351 * normalize path:
3352 * multiple //; leading ./; /./; /../;
3353 */
3354 normalize_path_copy(add_data.sm_path, add_data.sm_path);
3355 strip_dir_trailing_slashes(add_data.sm_path);
3356
3357 die_on_index_match(add_data.sm_path, force);
3358 die_on_repo_without_commits(add_data.sm_path);
3359
3360 if (!force) {
3361 struct child_process cp = CHILD_PROCESS_INIT;
3362
3363 cp.git_cmd = 1;
3364 cp.no_stdout = 1;
3365 strvec_pushl(&cp.args, "add", "--dry-run", "--ignore-missing",
3366 "--no-warn-embedded-repo", add_data.sm_path, NULL);
3367 if ((ret = pipe_command(&cp, NULL, 0, NULL, 0, &sb, 0))) {
3368 strbuf_complete_line(&sb);
3369 fputs(sb.buf, stderr);
3370 goto cleanup;
3371 }
3372 }
3373
3374 if(!add_data.sm_name)
3375 add_data.sm_name = add_data.sm_path;
3376
3377 if (check_submodule_name(add_data.sm_name))
3378 die(_("'%s' is not a valid submodule name"), add_data.sm_name);
3379
3380 add_data.prefix = prefix;
3381 add_data.force = !!force;
3382 add_data.quiet = !!quiet;
3383 add_data.progress = !!progress;
3384 add_data.dissociate = !!dissociate;
3385
3386 if (add_submodule(&add_data))
3387 goto cleanup;
3388 configure_added_submodule(&add_data);
3389
3390 ret = 0;
3391 cleanup:
3392 free(add_data.sm_path);
3393 free(to_free);
3394 strbuf_release(&sb);
3395
3396 return ret;
3397 }
3398
3399 #define SUPPORT_SUPER_PREFIX (1<<0)
3400
3401 struct cmd_struct {
3402 const char *cmd;
3403 int (*fn)(int, const char **, const char *);
3404 unsigned option;
3405 };
3406
3407 static struct cmd_struct commands[] = {
3408 {"clone", module_clone, SUPPORT_SUPER_PREFIX},
3409 {"add", module_add, 0},
3410 {"update", module_update, SUPPORT_SUPER_PREFIX},
3411 {"foreach", module_foreach, SUPPORT_SUPER_PREFIX},
3412 {"init", module_init, 0},
3413 {"status", module_status, SUPPORT_SUPER_PREFIX},
3414 {"sync", module_sync, SUPPORT_SUPER_PREFIX},
3415 {"deinit", module_deinit, 0},
3416 {"summary", module_summary, 0},
3417 {"push-check", push_check, 0},
3418 {"absorbgitdirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
3419 {"config", module_config, 0},
3420 {"set-url", module_set_url, 0},
3421 {"set-branch", module_set_branch, 0},
3422 {"create-branch", module_create_branch, 0},
3423 };
3424
3425 int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
3426 {
3427 int i;
3428 if (argc < 2 || !strcmp(argv[1], "-h"))
3429 usage("git submodule--helper <command>");
3430
3431 for (i = 0; i < ARRAY_SIZE(commands); i++) {
3432 if (!strcmp(argv[1], commands[i].cmd)) {
3433 if (get_super_prefix() &&
3434 !(commands[i].option & SUPPORT_SUPER_PREFIX))
3435 die(_("%s doesn't support --super-prefix"),
3436 commands[i].cmd);
3437 return commands[i].fn(argc - 1, argv + 1, prefix);
3438 }
3439 }
3440
3441 die(_("'%s' is not a valid submodule--helper "
3442 "subcommand"), argv[1]);
3443 }