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