1#include "builtin.h"
2#include "repository.h"
3#include "cache.h"
4#include "config.h"
5#include "parse-options.h"
6#include "quote.h"
7#include "pathspec.h"
8#include "dir.h"
9#include "submodule.h"
10#include "submodule-config.h"
11#include "string-list.h"
12#include "run-command.h"
13#include "remote.h"
14#include "refs.h"
15#include "connect.h"
16
17static char *get_default_remote(void)
18{
19 char *dest = NULL, *ret;
20 struct strbuf sb = STRBUF_INIT;
21 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
22
23 if (!refname)
24 die(_("No such ref: %s"), "HEAD");
25
26 /* detached HEAD */
27 if (!strcmp(refname, "HEAD"))
28 return xstrdup("origin");
29
30 if (!skip_prefix(refname, "refs/heads/", &refname))
31 die(_("Expecting a full ref name, got %s"), refname);
32
33 strbuf_addf(&sb, "branch.%s.remote", refname);
34 if (git_config_get_string(sb.buf, &dest))
35 ret = xstrdup("origin");
36 else
37 ret = dest;
38
39 strbuf_release(&sb);
40 return ret;
41}
42
43static int starts_with_dot_slash(const char *str)
44{
45 return str[0] == '.' && is_dir_sep(str[1]);
46}
47
48static int starts_with_dot_dot_slash(const char *str)
49{
50 return str[0] == '.' && str[1] == '.' && is_dir_sep(str[2]);
51}
52
53/*
54 * Returns 1 if it was the last chop before ':'.
55 */
56static int chop_last_dir(char **remoteurl, int is_relative)
57{
58 char *rfind = find_last_dir_sep(*remoteurl);
59 if (rfind) {
60 *rfind = '\0';
61 return 0;
62 }
63
64 rfind = strrchr(*remoteurl, ':');
65 if (rfind) {
66 *rfind = '\0';
67 return 1;
68 }
69
70 if (is_relative || !strcmp(".", *remoteurl))
71 die(_("cannot strip one component off url '%s'"),
72 *remoteurl);
73
74 free(*remoteurl);
75 *remoteurl = xstrdup(".");
76 return 0;
77}
78
79/*
80 * The `url` argument is the URL that navigates to the submodule origin
81 * repo. When relative, this URL is relative to the superproject origin
82 * URL repo. The `up_path` argument, if specified, is the relative
83 * path that navigates from the submodule working tree to the superproject
84 * working tree. Returns the origin URL of the submodule.
85 *
86 * Return either an absolute URL or filesystem path (if the superproject
87 * origin URL is an absolute URL or filesystem path, respectively) or a
88 * relative file system path (if the superproject origin URL is a relative
89 * file system path).
90 *
91 * When the output is a relative file system path, the path is either
92 * relative to the submodule working tree, if up_path is specified, or to
93 * the superproject working tree otherwise.
94 *
95 * NEEDSWORK: This works incorrectly on the domain and protocol part.
96 * remote_url url outcome expectation
97 * http://a.com/b ../c http://a.com/c as is
98 * http://a.com/b/ ../c http://a.com/c same as previous line, but
99 * ignore trailing slash in url
100 * http://a.com/b ../../c http://c error out
101 * http://a.com/b ../../../c http:/c error out
102 * http://a.com/b ../../../../c http:c error out
103 * http://a.com/b ../../../../../c .:c error out
104 * NEEDSWORK: Given how chop_last_dir() works, this function is broken
105 * when a local part has a colon in its path component, too.
106 */
107static char *relative_url(const char *remote_url,
108 const char *url,
109 const char *up_path)
110{
111 int is_relative = 0;
112 int colonsep = 0;
113 char *out;
114 char *remoteurl = xstrdup(remote_url);
115 struct strbuf sb = STRBUF_INIT;
116 size_t len = strlen(remoteurl);
117
118 if (is_dir_sep(remoteurl[len-1]))
119 remoteurl[len-1] = '\0';
120
121 if (!url_is_local_not_ssh(remoteurl) || is_absolute_path(remoteurl))
122 is_relative = 0;
123 else {
124 is_relative = 1;
125 /*
126 * Prepend a './' to ensure all relative
127 * remoteurls start with './' or '../'
128 */
129 if (!starts_with_dot_slash(remoteurl) &&
130 !starts_with_dot_dot_slash(remoteurl)) {
131 strbuf_reset(&sb);
132 strbuf_addf(&sb, "./%s", remoteurl);
133 free(remoteurl);
134 remoteurl = strbuf_detach(&sb, NULL);
135 }
136 }
137 /*
138 * When the url starts with '../', remove that and the
139 * last directory in remoteurl.
140 */
141 while (url) {
142 if (starts_with_dot_dot_slash(url)) {
143 url += 3;
144 colonsep |= chop_last_dir(&remoteurl, is_relative);
145 } else if (starts_with_dot_slash(url))
146 url += 2;
147 else
148 break;
149 }
150 strbuf_reset(&sb);
151 strbuf_addf(&sb, "%s%s%s", remoteurl, colonsep ? ":" : "/", url);
152 if (ends_with(url, "/"))
153 strbuf_setlen(&sb, sb.len - 1);
154 free(remoteurl);
155
156 if (starts_with_dot_slash(sb.buf))
157 out = xstrdup(sb.buf + 2);
158 else
159 out = xstrdup(sb.buf);
160 strbuf_reset(&sb);
161
162 if (!up_path || !is_relative)
163 return out;
164
165 strbuf_addf(&sb, "%s%s", up_path, out);
166 free(out);
167 return strbuf_detach(&sb, NULL);
168}
169
170static int resolve_relative_url(int argc, const char **argv, const char *prefix)
171{
172 char *remoteurl = NULL;
173 char *remote = get_default_remote();
174 const char *up_path = NULL;
175 char *res;
176 const char *url;
177 struct strbuf sb = STRBUF_INIT;
178
179 if (argc != 2 && argc != 3)
180 die("resolve-relative-url only accepts one or two arguments");
181
182 url = argv[1];
183 strbuf_addf(&sb, "remote.%s.url", remote);
184 free(remote);
185
186 if (git_config_get_string(sb.buf, &remoteurl))
187 /* the repository is its own authoritative upstream */
188 remoteurl = xgetcwd();
189
190 if (argc == 3)
191 up_path = argv[2];
192
193 res = relative_url(remoteurl, url, up_path);
194 puts(res);
195 free(res);
196 free(remoteurl);
197 return 0;
198}
199
200static int resolve_relative_url_test(int argc, const char **argv, const char *prefix)
201{
202 char *remoteurl, *res;
203 const char *up_path, *url;
204
205 if (argc != 4)
206 die("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>");
207
208 up_path = argv[1];
209 remoteurl = xstrdup(argv[2]);
210 url = argv[3];
211
212 if (!strcmp(up_path, "(null)"))
213 up_path = NULL;
214
215 res = relative_url(remoteurl, url, up_path);
216 puts(res);
217 free(res);
218 free(remoteurl);
219 return 0;
220}
221
222struct module_list {
223 const struct cache_entry **entries;
224 int alloc, nr;
225};
226#define MODULE_LIST_INIT { NULL, 0, 0 }
227
228static int module_list_compute(int argc, const char **argv,
229 const char *prefix,
230 struct pathspec *pathspec,
231 struct module_list *list)
232{
233 int i, result = 0;
234 char *ps_matched = NULL;
235 parse_pathspec(pathspec, 0,
236 PATHSPEC_PREFER_FULL,
237 prefix, argv);
238
239 if (pathspec->nr)
240 ps_matched = xcalloc(pathspec->nr, 1);
241
242 if (read_cache() < 0)
243 die(_("index file corrupt"));
244
245 for (i = 0; i < active_nr; i++) {
246 const struct cache_entry *ce = active_cache[i];
247
248 if (!match_pathspec(pathspec, ce->name, ce_namelen(ce),
249 0, ps_matched, 1) ||
250 !S_ISGITLINK(ce->ce_mode))
251 continue;
252
253 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
254 list->entries[list->nr++] = ce;
255 while (i + 1 < active_nr &&
256 !strcmp(ce->name, active_cache[i + 1]->name))
257 /*
258 * Skip entries with the same name in different stages
259 * to make sure an entry is returned only once.
260 */
261 i++;
262 }
263
264 if (ps_matched && report_path_error(ps_matched, pathspec, prefix))
265 result = -1;
266
267 free(ps_matched);
268
269 return result;
270}
271
272static void module_list_active(struct module_list *list)
273{
274 int i;
275 struct module_list active_modules = MODULE_LIST_INIT;
276
277 for (i = 0; i < list->nr; i++) {
278 const struct cache_entry *ce = list->entries[i];
279
280 if (!is_submodule_active(the_repository, ce->name))
281 continue;
282
283 ALLOC_GROW(active_modules.entries,
284 active_modules.nr + 1,
285 active_modules.alloc);
286 active_modules.entries[active_modules.nr++] = ce;
287 }
288
289 free(list->entries);
290 *list = active_modules;
291}
292
293static int module_list(int argc, const char **argv, const char *prefix)
294{
295 int i;
296 struct pathspec pathspec;
297 struct module_list list = MODULE_LIST_INIT;
298
299 struct option module_list_options[] = {
300 OPT_STRING(0, "prefix", &prefix,
301 N_("path"),
302 N_("alternative anchor for relative paths")),
303 OPT_END()
304 };
305
306 const char *const git_submodule_helper_usage[] = {
307 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
308 NULL
309 };
310
311 argc = parse_options(argc, argv, prefix, module_list_options,
312 git_submodule_helper_usage, 0);
313
314 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
315 return 1;
316
317 for (i = 0; i < list.nr; i++) {
318 const struct cache_entry *ce = list.entries[i];
319
320 if (ce_stage(ce))
321 printf("%06o %s U\t", ce->ce_mode, sha1_to_hex(null_sha1));
322 else
323 printf("%06o %s %d\t", ce->ce_mode,
324 oid_to_hex(&ce->oid), ce_stage(ce));
325
326 fprintf(stdout, "%s\n", ce->name);
327 }
328 return 0;
329}
330
331static void init_submodule(const char *path, const char *prefix, int quiet)
332{
333 const struct submodule *sub;
334 struct strbuf sb = STRBUF_INIT;
335 char *upd = NULL, *url = NULL, *displaypath;
336
337 if (prefix && get_super_prefix())
338 die("BUG: cannot have prefix and superprefix");
339 else if (prefix)
340 displaypath = xstrdup(relative_path(path, prefix, &sb));
341 else if (get_super_prefix()) {
342 strbuf_addf(&sb, "%s%s", get_super_prefix(), path);
343 displaypath = strbuf_detach(&sb, NULL);
344 } else
345 displaypath = xstrdup(path);
346
347 sub = submodule_from_path(&null_oid, path);
348
349 if (!sub)
350 die(_("No url found for submodule path '%s' in .gitmodules"),
351 displaypath);
352
353 /*
354 * NEEDSWORK: In a multi-working-tree world, this needs to be
355 * set in the per-worktree config.
356 *
357 * Set active flag for the submodule being initialized
358 */
359 if (!is_submodule_active(the_repository, path)) {
360 strbuf_reset(&sb);
361 strbuf_addf(&sb, "submodule.%s.active", sub->name);
362 git_config_set_gently(sb.buf, "true");
363 }
364
365 /*
366 * Copy url setting when it is not set yet.
367 * To look up the url in .git/config, we must not fall back to
368 * .gitmodules, so look it up directly.
369 */
370 strbuf_reset(&sb);
371 strbuf_addf(&sb, "submodule.%s.url", sub->name);
372 if (git_config_get_string(sb.buf, &url)) {
373 if (!sub->url)
374 die(_("No url found for submodule path '%s' in .gitmodules"),
375 displaypath);
376
377 url = xstrdup(sub->url);
378
379 /* Possibly a url relative to parent */
380 if (starts_with_dot_dot_slash(url) ||
381 starts_with_dot_slash(url)) {
382 char *remoteurl, *relurl;
383 char *remote = get_default_remote();
384 struct strbuf remotesb = STRBUF_INIT;
385 strbuf_addf(&remotesb, "remote.%s.url", remote);
386 free(remote);
387
388 if (git_config_get_string(remotesb.buf, &remoteurl)) {
389 warning(_("could not lookup configuration '%s'. Assuming this repository is its own authoritative upstream."), remotesb.buf);
390 remoteurl = xgetcwd();
391 }
392 relurl = relative_url(remoteurl, url, NULL);
393 strbuf_release(&remotesb);
394 free(remoteurl);
395 free(url);
396 url = relurl;
397 }
398
399 if (git_config_set_gently(sb.buf, url))
400 die(_("Failed to register url for submodule path '%s'"),
401 displaypath);
402 if (!quiet)
403 fprintf(stderr,
404 _("Submodule '%s' (%s) registered for path '%s'\n"),
405 sub->name, url, displaypath);
406 }
407
408 /* Copy "update" setting when it is not set yet */
409 strbuf_reset(&sb);
410 strbuf_addf(&sb, "submodule.%s.update", sub->name);
411 if (git_config_get_string(sb.buf, &upd) &&
412 sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
413 if (sub->update_strategy.type == SM_UPDATE_COMMAND) {
414 fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"),
415 sub->name);
416 upd = xstrdup("none");
417 } else
418 upd = xstrdup(submodule_strategy_to_string(&sub->update_strategy));
419
420 if (git_config_set_gently(sb.buf, upd))
421 die(_("Failed to register update mode for submodule path '%s'"), displaypath);
422 }
423 strbuf_release(&sb);
424 free(displaypath);
425 free(url);
426 free(upd);
427}
428
429static int module_init(int argc, const char **argv, const char *prefix)
430{
431 struct pathspec pathspec;
432 struct module_list list = MODULE_LIST_INIT;
433 int quiet = 0;
434 int i;
435
436 struct option module_init_options[] = {
437 OPT__QUIET(&quiet, N_("Suppress output for initializing a submodule")),
438 OPT_END()
439 };
440
441 const char *const git_submodule_helper_usage[] = {
442 N_("git submodule--helper init [<path>]"),
443 NULL
444 };
445
446 argc = parse_options(argc, argv, prefix, module_init_options,
447 git_submodule_helper_usage, 0);
448
449 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
450 return 1;
451
452 /*
453 * If there are no path args and submodule.active is set then,
454 * by default, only initialize 'active' modules.
455 */
456 if (!argc && git_config_get_value_multi("submodule.active"))
457 module_list_active(&list);
458
459 for (i = 0; i < list.nr; i++)
460 init_submodule(list.entries[i]->name, prefix, quiet);
461
462 return 0;
463}
464
465static int module_name(int argc, const char **argv, const char *prefix)
466{
467 const struct submodule *sub;
468
469 if (argc != 2)
470 usage(_("git submodule--helper name <path>"));
471
472 sub = submodule_from_path(&null_oid, argv[1]);
473
474 if (!sub)
475 die(_("no submodule mapping found in .gitmodules for path '%s'"),
476 argv[1]);
477
478 printf("%s\n", sub->name);
479
480 return 0;
481}
482
483static int clone_submodule(const char *path, const char *gitdir, const char *url,
484 const char *depth, struct string_list *reference,
485 int quiet, int progress)
486{
487 struct child_process cp = CHILD_PROCESS_INIT;
488
489 argv_array_push(&cp.args, "clone");
490 argv_array_push(&cp.args, "--no-checkout");
491 if (quiet)
492 argv_array_push(&cp.args, "--quiet");
493 if (progress)
494 argv_array_push(&cp.args, "--progress");
495 if (depth && *depth)
496 argv_array_pushl(&cp.args, "--depth", depth, NULL);
497 if (reference->nr) {
498 struct string_list_item *item;
499 for_each_string_list_item(item, reference)
500 argv_array_pushl(&cp.args, "--reference",
501 item->string, NULL);
502 }
503 if (gitdir && *gitdir)
504 argv_array_pushl(&cp.args, "--separate-git-dir", gitdir, NULL);
505
506 argv_array_push(&cp.args, url);
507 argv_array_push(&cp.args, path);
508
509 cp.git_cmd = 1;
510 prepare_submodule_repo_env(&cp.env_array);
511 cp.no_stdin = 1;
512
513 return run_command(&cp);
514}
515
516struct submodule_alternate_setup {
517 const char *submodule_name;
518 enum SUBMODULE_ALTERNATE_ERROR_MODE {
519 SUBMODULE_ALTERNATE_ERROR_DIE,
520 SUBMODULE_ALTERNATE_ERROR_INFO,
521 SUBMODULE_ALTERNATE_ERROR_IGNORE
522 } error_mode;
523 struct string_list *reference;
524};
525#define SUBMODULE_ALTERNATE_SETUP_INIT { NULL, \
526 SUBMODULE_ALTERNATE_ERROR_IGNORE, NULL }
527
528static int add_possible_reference_from_superproject(
529 struct alternate_object_database *alt, void *sas_cb)
530{
531 struct submodule_alternate_setup *sas = sas_cb;
532
533 /*
534 * If the alternate object store is another repository, try the
535 * standard layout with .git/(modules/<name>)+/objects
536 */
537 if (ends_with(alt->path, "/objects")) {
538 char *sm_alternate;
539 struct strbuf sb = STRBUF_INIT;
540 struct strbuf err = STRBUF_INIT;
541 strbuf_add(&sb, alt->path, strlen(alt->path) - strlen("objects"));
542
543 /*
544 * We need to end the new path with '/' to mark it as a dir,
545 * otherwise a submodule name containing '/' will be broken
546 * as the last part of a missing submodule reference would
547 * be taken as a file name.
548 */
549 strbuf_addf(&sb, "modules/%s/", sas->submodule_name);
550
551 sm_alternate = compute_alternate_path(sb.buf, &err);
552 if (sm_alternate) {
553 string_list_append(sas->reference, xstrdup(sb.buf));
554 free(sm_alternate);
555 } else {
556 switch (sas->error_mode) {
557 case SUBMODULE_ALTERNATE_ERROR_DIE:
558 die(_("submodule '%s' cannot add alternate: %s"),
559 sas->submodule_name, err.buf);
560 case SUBMODULE_ALTERNATE_ERROR_INFO:
561 fprintf(stderr, _("submodule '%s' cannot add alternate: %s"),
562 sas->submodule_name, err.buf);
563 case SUBMODULE_ALTERNATE_ERROR_IGNORE:
564 ; /* nothing */
565 }
566 }
567 strbuf_release(&sb);
568 }
569
570 return 0;
571}
572
573static void prepare_possible_alternates(const char *sm_name,
574 struct string_list *reference)
575{
576 char *sm_alternate = NULL, *error_strategy = NULL;
577 struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT;
578
579 git_config_get_string("submodule.alternateLocation", &sm_alternate);
580 if (!sm_alternate)
581 return;
582
583 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
584
585 if (!error_strategy)
586 error_strategy = xstrdup("die");
587
588 sas.submodule_name = sm_name;
589 sas.reference = reference;
590 if (!strcmp(error_strategy, "die"))
591 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE;
592 else if (!strcmp(error_strategy, "info"))
593 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO;
594 else if (!strcmp(error_strategy, "ignore"))
595 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE;
596 else
597 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
598
599 if (!strcmp(sm_alternate, "superproject"))
600 foreach_alt_odb(add_possible_reference_from_superproject, &sas);
601 else if (!strcmp(sm_alternate, "no"))
602 ; /* do nothing */
603 else
604 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate);
605
606 free(sm_alternate);
607 free(error_strategy);
608}
609
610static int module_clone(int argc, const char **argv, const char *prefix)
611{
612 const char *name = NULL, *url = NULL, *depth = NULL;
613 int quiet = 0;
614 int progress = 0;
615 char *p, *path = NULL, *sm_gitdir;
616 struct strbuf sb = STRBUF_INIT;
617 struct string_list reference = STRING_LIST_INIT_NODUP;
618 char *sm_alternate = NULL, *error_strategy = NULL;
619
620 struct option module_clone_options[] = {
621 OPT_STRING(0, "prefix", &prefix,
622 N_("path"),
623 N_("alternative anchor for relative paths")),
624 OPT_STRING(0, "path", &path,
625 N_("path"),
626 N_("where the new submodule will be cloned to")),
627 OPT_STRING(0, "name", &name,
628 N_("string"),
629 N_("name of the new submodule")),
630 OPT_STRING(0, "url", &url,
631 N_("string"),
632 N_("url where to clone the submodule from")),
633 OPT_STRING_LIST(0, "reference", &reference,
634 N_("repo"),
635 N_("reference repository")),
636 OPT_STRING(0, "depth", &depth,
637 N_("string"),
638 N_("depth for shallow clones")),
639 OPT__QUIET(&quiet, "Suppress output for cloning a submodule"),
640 OPT_BOOL(0, "progress", &progress,
641 N_("force cloning progress")),
642 OPT_END()
643 };
644
645 const char *const git_submodule_helper_usage[] = {
646 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
647 "[--reference <repository>] [--name <name>] [--depth <depth>] "
648 "--url <url> --path <path>"),
649 NULL
650 };
651
652 argc = parse_options(argc, argv, prefix, module_clone_options,
653 git_submodule_helper_usage, 0);
654
655 if (argc || !url || !path || !*path)
656 usage_with_options(git_submodule_helper_usage,
657 module_clone_options);
658
659 strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
660 sm_gitdir = absolute_pathdup(sb.buf);
661 strbuf_reset(&sb);
662
663 if (!is_absolute_path(path)) {
664 strbuf_addf(&sb, "%s/%s", get_git_work_tree(), path);
665 path = strbuf_detach(&sb, NULL);
666 } else
667 path = xstrdup(path);
668
669 if (!file_exists(sm_gitdir)) {
670 if (safe_create_leading_directories_const(sm_gitdir) < 0)
671 die(_("could not create directory '%s'"), sm_gitdir);
672
673 prepare_possible_alternates(name, &reference);
674
675 if (clone_submodule(path, sm_gitdir, url, depth, &reference,
676 quiet, progress))
677 die(_("clone of '%s' into submodule path '%s' failed"),
678 url, path);
679 } else {
680 if (safe_create_leading_directories_const(path) < 0)
681 die(_("could not create directory '%s'"), path);
682 strbuf_addf(&sb, "%s/index", sm_gitdir);
683 unlink_or_warn(sb.buf);
684 strbuf_reset(&sb);
685 }
686
687 /* Connect module worktree and git dir */
688 connect_work_tree_and_git_dir(path, sm_gitdir);
689
690 p = git_pathdup_submodule(path, "config");
691 if (!p)
692 die(_("could not get submodule directory for '%s'"), path);
693
694 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
695 git_config_get_string("submodule.alternateLocation", &sm_alternate);
696 if (sm_alternate)
697 git_config_set_in_file(p, "submodule.alternateLocation",
698 sm_alternate);
699 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
700 if (error_strategy)
701 git_config_set_in_file(p, "submodule.alternateErrorStrategy",
702 error_strategy);
703
704 free(sm_alternate);
705 free(error_strategy);
706
707 strbuf_release(&sb);
708 free(sm_gitdir);
709 free(path);
710 free(p);
711 return 0;
712}
713
714struct submodule_update_clone {
715 /* index into 'list', the list of submodules to look into for cloning */
716 int current;
717 struct module_list list;
718 unsigned warn_if_uninitialized : 1;
719
720 /* update parameter passed via commandline */
721 struct submodule_update_strategy update;
722
723 /* configuration parameters which are passed on to the children */
724 int progress;
725 int quiet;
726 int recommend_shallow;
727 struct string_list references;
728 const char *depth;
729 const char *recursive_prefix;
730 const char *prefix;
731
732 /* Machine-readable status lines to be consumed by git-submodule.sh */
733 struct string_list projectlines;
734
735 /* If we want to stop as fast as possible and return an error */
736 unsigned quickstop : 1;
737
738 /* failed clones to be retried again */
739 const struct cache_entry **failed_clones;
740 int failed_clones_nr, failed_clones_alloc;
741};
742#define SUBMODULE_UPDATE_CLONE_INIT {0, MODULE_LIST_INIT, 0, \
743 SUBMODULE_UPDATE_STRATEGY_INIT, 0, 0, -1, STRING_LIST_INIT_DUP, \
744 NULL, NULL, NULL, \
745 STRING_LIST_INIT_DUP, 0, NULL, 0, 0}
746
747
748static void next_submodule_warn_missing(struct submodule_update_clone *suc,
749 struct strbuf *out, const char *displaypath)
750{
751 /*
752 * Only mention uninitialized submodules when their
753 * paths have been specified.
754 */
755 if (suc->warn_if_uninitialized) {
756 strbuf_addf(out,
757 _("Submodule path '%s' not initialized"),
758 displaypath);
759 strbuf_addch(out, '\n');
760 strbuf_addstr(out,
761 _("Maybe you want to use 'update --init'?"));
762 strbuf_addch(out, '\n');
763 }
764}
765
766/**
767 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
768 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
769 */
770static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
771 struct child_process *child,
772 struct submodule_update_clone *suc,
773 struct strbuf *out)
774{
775 const struct submodule *sub = NULL;
776 const char *url = NULL;
777 const char *update_string;
778 enum submodule_update_type update_type;
779 char *key;
780 struct strbuf displaypath_sb = STRBUF_INIT;
781 struct strbuf sb = STRBUF_INIT;
782 const char *displaypath = NULL;
783 int needs_cloning = 0;
784
785 if (ce_stage(ce)) {
786 if (suc->recursive_prefix)
787 strbuf_addf(&sb, "%s/%s", suc->recursive_prefix, ce->name);
788 else
789 strbuf_addstr(&sb, ce->name);
790 strbuf_addf(out, _("Skipping unmerged submodule %s"), sb.buf);
791 strbuf_addch(out, '\n');
792 goto cleanup;
793 }
794
795 sub = submodule_from_path(&null_oid, ce->name);
796
797 if (suc->recursive_prefix)
798 displaypath = relative_path(suc->recursive_prefix,
799 ce->name, &displaypath_sb);
800 else
801 displaypath = ce->name;
802
803 if (!sub) {
804 next_submodule_warn_missing(suc, out, displaypath);
805 goto cleanup;
806 }
807
808 key = xstrfmt("submodule.%s.update", sub->name);
809 if (!repo_config_get_string_const(the_repository, key, &update_string)) {
810 update_type = parse_submodule_update_type(update_string);
811 } else {
812 update_type = sub->update_strategy.type;
813 }
814 free(key);
815
816 if (suc->update.type == SM_UPDATE_NONE
817 || (suc->update.type == SM_UPDATE_UNSPECIFIED
818 && update_type == SM_UPDATE_NONE)) {
819 strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
820 strbuf_addch(out, '\n');
821 goto cleanup;
822 }
823
824 /* Check if the submodule has been initialized. */
825 if (!is_submodule_active(the_repository, ce->name)) {
826 next_submodule_warn_missing(suc, out, displaypath);
827 goto cleanup;
828 }
829
830 strbuf_reset(&sb);
831 strbuf_addf(&sb, "submodule.%s.url", sub->name);
832 if (repo_config_get_string_const(the_repository, sb.buf, &url))
833 url = sub->url;
834
835 strbuf_reset(&sb);
836 strbuf_addf(&sb, "%s/.git", ce->name);
837 needs_cloning = !file_exists(sb.buf);
838
839 strbuf_reset(&sb);
840 strbuf_addf(&sb, "%06o %s %d %d\t%s\n", ce->ce_mode,
841 oid_to_hex(&ce->oid), ce_stage(ce),
842 needs_cloning, ce->name);
843 string_list_append(&suc->projectlines, sb.buf);
844
845 if (!needs_cloning)
846 goto cleanup;
847
848 child->git_cmd = 1;
849 child->no_stdin = 1;
850 child->stdout_to_stderr = 1;
851 child->err = -1;
852 argv_array_push(&child->args, "submodule--helper");
853 argv_array_push(&child->args, "clone");
854 if (suc->progress)
855 argv_array_push(&child->args, "--progress");
856 if (suc->quiet)
857 argv_array_push(&child->args, "--quiet");
858 if (suc->prefix)
859 argv_array_pushl(&child->args, "--prefix", suc->prefix, NULL);
860 if (suc->recommend_shallow && sub->recommend_shallow == 1)
861 argv_array_push(&child->args, "--depth=1");
862 argv_array_pushl(&child->args, "--path", sub->path, NULL);
863 argv_array_pushl(&child->args, "--name", sub->name, NULL);
864 argv_array_pushl(&child->args, "--url", url, NULL);
865 if (suc->references.nr) {
866 struct string_list_item *item;
867 for_each_string_list_item(item, &suc->references)
868 argv_array_pushl(&child->args, "--reference", item->string, NULL);
869 }
870 if (suc->depth)
871 argv_array_push(&child->args, suc->depth);
872
873cleanup:
874 strbuf_reset(&displaypath_sb);
875 strbuf_reset(&sb);
876
877 return needs_cloning;
878}
879
880static int update_clone_get_next_task(struct child_process *child,
881 struct strbuf *err,
882 void *suc_cb,
883 void **idx_task_cb)
884{
885 struct submodule_update_clone *suc = suc_cb;
886 const struct cache_entry *ce;
887 int index;
888
889 for (; suc->current < suc->list.nr; suc->current++) {
890 ce = suc->list.entries[suc->current];
891 if (prepare_to_clone_next_submodule(ce, child, suc, err)) {
892 int *p = xmalloc(sizeof(*p));
893 *p = suc->current;
894 *idx_task_cb = p;
895 suc->current++;
896 return 1;
897 }
898 }
899
900 /*
901 * The loop above tried cloning each submodule once, now try the
902 * stragglers again, which we can imagine as an extension of the
903 * entry list.
904 */
905 index = suc->current - suc->list.nr;
906 if (index < suc->failed_clones_nr) {
907 int *p;
908 ce = suc->failed_clones[index];
909 if (!prepare_to_clone_next_submodule(ce, child, suc, err)) {
910 suc->current ++;
911 strbuf_addstr(err, "BUG: submodule considered for "
912 "cloning, doesn't need cloning "
913 "any more?\n");
914 return 0;
915 }
916 p = xmalloc(sizeof(*p));
917 *p = suc->current;
918 *idx_task_cb = p;
919 suc->current ++;
920 return 1;
921 }
922
923 return 0;
924}
925
926static int update_clone_start_failure(struct strbuf *err,
927 void *suc_cb,
928 void *idx_task_cb)
929{
930 struct submodule_update_clone *suc = suc_cb;
931 suc->quickstop = 1;
932 return 1;
933}
934
935static int update_clone_task_finished(int result,
936 struct strbuf *err,
937 void *suc_cb,
938 void *idx_task_cb)
939{
940 const struct cache_entry *ce;
941 struct submodule_update_clone *suc = suc_cb;
942
943 int *idxP = idx_task_cb;
944 int idx = *idxP;
945 free(idxP);
946
947 if (!result)
948 return 0;
949
950 if (idx < suc->list.nr) {
951 ce = suc->list.entries[idx];
952 strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"),
953 ce->name);
954 strbuf_addch(err, '\n');
955 ALLOC_GROW(suc->failed_clones,
956 suc->failed_clones_nr + 1,
957 suc->failed_clones_alloc);
958 suc->failed_clones[suc->failed_clones_nr++] = ce;
959 return 0;
960 } else {
961 idx -= suc->list.nr;
962 ce = suc->failed_clones[idx];
963 strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"),
964 ce->name);
965 strbuf_addch(err, '\n');
966 suc->quickstop = 1;
967 return 1;
968 }
969
970 return 0;
971}
972
973static int gitmodules_update_clone_config(const char *var, const char *value,
974 void *cb)
975{
976 int *max_jobs = cb;
977 if (!strcmp(var, "submodule.fetchjobs"))
978 *max_jobs = parse_submodule_fetchjobs(var, value);
979 return 0;
980}
981
982static int update_clone(int argc, const char **argv, const char *prefix)
983{
984 const char *update = NULL;
985 int max_jobs = 1;
986 struct string_list_item *item;
987 struct pathspec pathspec;
988 struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
989
990 struct option module_update_clone_options[] = {
991 OPT_STRING(0, "prefix", &prefix,
992 N_("path"),
993 N_("path into the working tree")),
994 OPT_STRING(0, "recursive-prefix", &suc.recursive_prefix,
995 N_("path"),
996 N_("path into the working tree, across nested "
997 "submodule boundaries")),
998 OPT_STRING(0, "update", &update,
999 N_("string"),
1000 N_("rebase, merge, checkout or none")),
1001 OPT_STRING_LIST(0, "reference", &suc.references, N_("repo"),
1002 N_("reference repository")),
1003 OPT_STRING(0, "depth", &suc.depth, "<depth>",
1004 N_("Create a shallow clone truncated to the "
1005 "specified number of revisions")),
1006 OPT_INTEGER('j', "jobs", &max_jobs,
1007 N_("parallel jobs")),
1008 OPT_BOOL(0, "recommend-shallow", &suc.recommend_shallow,
1009 N_("whether the initial clone should follow the shallow recommendation")),
1010 OPT__QUIET(&suc.quiet, N_("don't print cloning progress")),
1011 OPT_BOOL(0, "progress", &suc.progress,
1012 N_("force cloning progress")),
1013 OPT_END()
1014 };
1015
1016 const char *const git_submodule_helper_usage[] = {
1017 N_("git submodule--helper update_clone [--prefix=<path>] [<path>...]"),
1018 NULL
1019 };
1020 suc.prefix = prefix;
1021
1022 config_from_gitmodules(gitmodules_update_clone_config, &max_jobs);
1023 git_config(gitmodules_update_clone_config, &max_jobs);
1024
1025 argc = parse_options(argc, argv, prefix, module_update_clone_options,
1026 git_submodule_helper_usage, 0);
1027
1028 if (update)
1029 if (parse_submodule_update_strategy(update, &suc.update) < 0)
1030 die(_("bad value for update parameter"));
1031
1032 if (module_list_compute(argc, argv, prefix, &pathspec, &suc.list) < 0)
1033 return 1;
1034
1035 if (pathspec.nr)
1036 suc.warn_if_uninitialized = 1;
1037
1038 run_processes_parallel(max_jobs,
1039 update_clone_get_next_task,
1040 update_clone_start_failure,
1041 update_clone_task_finished,
1042 &suc);
1043
1044 /*
1045 * We saved the output and put it out all at once now.
1046 * That means:
1047 * - the listener does not have to interleave their (checkout)
1048 * work with our fetching. The writes involved in a
1049 * checkout involve more straightforward sequential I/O.
1050 * - the listener can avoid doing any work if fetching failed.
1051 */
1052 if (suc.quickstop)
1053 return 1;
1054
1055 for_each_string_list_item(item, &suc.projectlines)
1056 fprintf(stdout, "%s", item->string);
1057
1058 return 0;
1059}
1060
1061static int resolve_relative_path(int argc, const char **argv, const char *prefix)
1062{
1063 struct strbuf sb = STRBUF_INIT;
1064 if (argc != 3)
1065 die("submodule--helper relative-path takes exactly 2 arguments, got %d", argc);
1066
1067 printf("%s", relative_path(argv[1], argv[2], &sb));
1068 strbuf_release(&sb);
1069 return 0;
1070}
1071
1072static const char *remote_submodule_branch(const char *path)
1073{
1074 const struct submodule *sub;
1075 const char *branch = NULL;
1076 char *key;
1077
1078 sub = submodule_from_path(&null_oid, path);
1079 if (!sub)
1080 return NULL;
1081
1082 key = xstrfmt("submodule.%s.branch", sub->name);
1083 if (repo_config_get_string_const(the_repository, key, &branch))
1084 branch = sub->branch;
1085 free(key);
1086
1087 if (!branch)
1088 return "master";
1089
1090 if (!strcmp(branch, ".")) {
1091 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
1092
1093 if (!refname)
1094 die(_("No such ref: %s"), "HEAD");
1095
1096 /* detached HEAD */
1097 if (!strcmp(refname, "HEAD"))
1098 die(_("Submodule (%s) branch configured to inherit "
1099 "branch from superproject, but the superproject "
1100 "is not on any branch"), sub->name);
1101
1102 if (!skip_prefix(refname, "refs/heads/", &refname))
1103 die(_("Expecting a full ref name, got %s"), refname);
1104 return refname;
1105 }
1106
1107 return branch;
1108}
1109
1110static int resolve_remote_submodule_branch(int argc, const char **argv,
1111 const char *prefix)
1112{
1113 const char *ret;
1114 struct strbuf sb = STRBUF_INIT;
1115 if (argc != 2)
1116 die("submodule--helper remote-branch takes exactly one arguments, got %d", argc);
1117
1118 ret = remote_submodule_branch(argv[1]);
1119 if (!ret)
1120 die("submodule %s doesn't exist", argv[1]);
1121
1122 printf("%s", ret);
1123 strbuf_release(&sb);
1124 return 0;
1125}
1126
1127static int push_check(int argc, const char **argv, const char *prefix)
1128{
1129 struct remote *remote;
1130 const char *superproject_head;
1131 char *head;
1132 int detached_head = 0;
1133 struct object_id head_oid;
1134
1135 if (argc < 3)
1136 die("submodule--helper push-check requires at least 2 arguments");
1137
1138 /*
1139 * superproject's resolved head ref.
1140 * if HEAD then the superproject is in a detached head state, otherwise
1141 * it will be the resolved head ref.
1142 */
1143 superproject_head = argv[1];
1144 argv++;
1145 argc--;
1146 /* Get the submodule's head ref and determine if it is detached */
1147 head = resolve_refdup("HEAD", 0, head_oid.hash, NULL);
1148 if (!head)
1149 die(_("Failed to resolve HEAD as a valid ref."));
1150 if (!strcmp(head, "HEAD"))
1151 detached_head = 1;
1152
1153 /*
1154 * The remote must be configured.
1155 * This is to avoid pushing to the exact same URL as the parent.
1156 */
1157 remote = pushremote_get(argv[1]);
1158 if (!remote || remote->origin == REMOTE_UNCONFIGURED)
1159 die("remote '%s' not configured", argv[1]);
1160
1161 /* Check the refspec */
1162 if (argc > 2) {
1163 int i, refspec_nr = argc - 2;
1164 struct ref *local_refs = get_local_heads();
1165 struct refspec *refspec = parse_push_refspec(refspec_nr,
1166 argv + 2);
1167
1168 for (i = 0; i < refspec_nr; i++) {
1169 struct refspec *rs = refspec + i;
1170
1171 if (rs->pattern || rs->matching)
1172 continue;
1173
1174 /* LHS must match a single ref */
1175 switch (count_refspec_match(rs->src, local_refs, NULL)) {
1176 case 1:
1177 break;
1178 case 0:
1179 /*
1180 * If LHS matches 'HEAD' then we need to ensure
1181 * that it matches the same named branch
1182 * checked out in the superproject.
1183 */
1184 if (!strcmp(rs->src, "HEAD")) {
1185 if (!detached_head &&
1186 !strcmp(head, superproject_head))
1187 break;
1188 die("HEAD does not match the named branch in the superproject");
1189 }
1190 /* fallthrough */
1191 default:
1192 die("src refspec '%s' must name a ref",
1193 rs->src);
1194 }
1195 }
1196 free_refspec(refspec_nr, refspec);
1197 }
1198 free(head);
1199
1200 return 0;
1201}
1202
1203static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
1204{
1205 int i;
1206 struct pathspec pathspec;
1207 struct module_list list = MODULE_LIST_INIT;
1208 unsigned flags = ABSORB_GITDIR_RECURSE_SUBMODULES;
1209
1210 struct option embed_gitdir_options[] = {
1211 OPT_STRING(0, "prefix", &prefix,
1212 N_("path"),
1213 N_("path into the working tree")),
1214 OPT_BIT(0, "--recursive", &flags, N_("recurse into submodules"),
1215 ABSORB_GITDIR_RECURSE_SUBMODULES),
1216 OPT_END()
1217 };
1218
1219 const char *const git_submodule_helper_usage[] = {
1220 N_("git submodule--helper embed-git-dir [<path>...]"),
1221 NULL
1222 };
1223
1224 argc = parse_options(argc, argv, prefix, embed_gitdir_options,
1225 git_submodule_helper_usage, 0);
1226
1227 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1228 return 1;
1229
1230 for (i = 0; i < list.nr; i++)
1231 absorb_git_dir_into_superproject(prefix,
1232 list.entries[i]->name, flags);
1233
1234 return 0;
1235}
1236
1237static int is_active(int argc, const char **argv, const char *prefix)
1238{
1239 if (argc != 2)
1240 die("submodule--helper is-active takes exactly 1 argument");
1241
1242 return !is_submodule_active(the_repository, argv[1]);
1243}
1244
1245/*
1246 * Exit non-zero if any of the submodule names given on the command line is
1247 * invalid. If no names are given, filter stdin to print only valid names
1248 * (which is primarily intended for testing).
1249 */
1250static int check_name(int argc, const char **argv, const char *prefix)
1251{
1252 if (argc > 1) {
1253 while (*++argv) {
1254 if (check_submodule_name(*argv) < 0)
1255 return 1;
1256 }
1257 } else {
1258 struct strbuf buf = STRBUF_INIT;
1259 while (strbuf_getline(&buf, stdin) != EOF) {
1260 if (!check_submodule_name(buf.buf))
1261 printf("%s\n", buf.buf);
1262 }
1263 strbuf_release(&buf);
1264 }
1265 return 0;
1266}
1267
1268#define SUPPORT_SUPER_PREFIX (1<<0)
1269
1270struct cmd_struct {
1271 const char *cmd;
1272 int (*fn)(int, const char **, const char *);
1273 unsigned option;
1274};
1275
1276static struct cmd_struct commands[] = {
1277 {"list", module_list, 0},
1278 {"name", module_name, 0},
1279 {"clone", module_clone, 0},
1280 {"update-clone", update_clone, 0},
1281 {"relative-path", resolve_relative_path, 0},
1282 {"resolve-relative-url", resolve_relative_url, 0},
1283 {"resolve-relative-url-test", resolve_relative_url_test, 0},
1284 {"init", module_init, SUPPORT_SUPER_PREFIX},
1285 {"remote-branch", resolve_remote_submodule_branch, 0},
1286 {"push-check", push_check, 0},
1287 {"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
1288 {"is-active", is_active, 0},
1289 {"check-name", check_name, 0},
1290};
1291
1292int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
1293{
1294 int i;
1295 if (argc < 2 || !strcmp(argv[1], "-h"))
1296 usage("git submodule--helper <command>");
1297
1298 for (i = 0; i < ARRAY_SIZE(commands); i++) {
1299 if (!strcmp(argv[1], commands[i].cmd)) {
1300 if (get_super_prefix() &&
1301 !(commands[i].option & SUPPORT_SUPER_PREFIX))
1302 die(_("%s doesn't support --super-prefix"),
1303 commands[i].cmd);
1304 return commands[i].fn(argc - 1, argv + 1, prefix);
1305 }
1306 }
1307
1308 die(_("'%s' is not a valid submodule--helper "
1309 "subcommand"), argv[1]);
1310}