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