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