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