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