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