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