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 "refspec.h"
16#include "connect.h"
17#include "revision.h"
18#include "diffcore.h"
19#include "diff.h"
20#include "object-store.h"
21
22#define OPT_QUIET (1 << 0)
23#define OPT_CACHED (1 << 1)
24#define OPT_RECURSIVE (1 << 2)
25#define OPT_FORCE (1 << 3)
26
27typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
28 void *cb_data);
29
30static char *get_default_remote(void)
31{
32 char *dest = NULL, *ret;
33 struct strbuf sb = STRBUF_INIT;
34 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
35
36 if (!refname)
37 die(_("No such ref: %s"), "HEAD");
38
39 /* detached HEAD */
40 if (!strcmp(refname, "HEAD"))
41 return xstrdup("origin");
42
43 if (!skip_prefix(refname, "refs/heads/", &refname))
44 die(_("Expecting a full ref name, got %s"), refname);
45
46 strbuf_addf(&sb, "branch.%s.remote", refname);
47 if (git_config_get_string(sb.buf, &dest))
48 ret = xstrdup("origin");
49 else
50 ret = dest;
51
52 strbuf_release(&sb);
53 return ret;
54}
55
56static int print_default_remote(int argc, const char **argv, const char *prefix)
57{
58 char *remote;
59
60 if (argc != 1)
61 die(_("submodule--helper print-default-remote takes no arguments"));
62
63 remote = get_default_remote();
64 if (remote)
65 printf("%s\n", remote);
66
67 free(remote);
68 return 0;
69}
70
71static int starts_with_dot_slash(const char *str)
72{
73 return str[0] == '.' && is_dir_sep(str[1]);
74}
75
76static int starts_with_dot_dot_slash(const char *str)
77{
78 return str[0] == '.' && str[1] == '.' && is_dir_sep(str[2]);
79}
80
81/*
82 * Returns 1 if it was the last chop before ':'.
83 */
84static int chop_last_dir(char **remoteurl, int is_relative)
85{
86 char *rfind = find_last_dir_sep(*remoteurl);
87 if (rfind) {
88 *rfind = '\0';
89 return 0;
90 }
91
92 rfind = strrchr(*remoteurl, ':');
93 if (rfind) {
94 *rfind = '\0';
95 return 1;
96 }
97
98 if (is_relative || !strcmp(".", *remoteurl))
99 die(_("cannot strip one component off url '%s'"),
100 *remoteurl);
101
102 free(*remoteurl);
103 *remoteurl = xstrdup(".");
104 return 0;
105}
106
107/*
108 * The `url` argument is the URL that navigates to the submodule origin
109 * repo. When relative, this URL is relative to the superproject origin
110 * URL repo. The `up_path` argument, if specified, is the relative
111 * path that navigates from the submodule working tree to the superproject
112 * working tree. Returns the origin URL of the submodule.
113 *
114 * Return either an absolute URL or filesystem path (if the superproject
115 * origin URL is an absolute URL or filesystem path, respectively) or a
116 * relative file system path (if the superproject origin URL is a relative
117 * file system path).
118 *
119 * When the output is a relative file system path, the path is either
120 * relative to the submodule working tree, if up_path is specified, or to
121 * the superproject working tree otherwise.
122 *
123 * NEEDSWORK: This works incorrectly on the domain and protocol part.
124 * remote_url url outcome expectation
125 * http://a.com/b ../c http://a.com/c as is
126 * http://a.com/b/ ../c http://a.com/c same as previous line, but
127 * ignore trailing slash in url
128 * http://a.com/b ../../c http://c error out
129 * http://a.com/b ../../../c http:/c error out
130 * http://a.com/b ../../../../c http:c error out
131 * http://a.com/b ../../../../../c .:c error out
132 * NEEDSWORK: Given how chop_last_dir() works, this function is broken
133 * when a local part has a colon in its path component, too.
134 */
135static char *relative_url(const char *remote_url,
136 const char *url,
137 const char *up_path)
138{
139 int is_relative = 0;
140 int colonsep = 0;
141 char *out;
142 char *remoteurl = xstrdup(remote_url);
143 struct strbuf sb = STRBUF_INIT;
144 size_t len = strlen(remoteurl);
145
146 if (is_dir_sep(remoteurl[len-1]))
147 remoteurl[len-1] = '\0';
148
149 if (!url_is_local_not_ssh(remoteurl) || is_absolute_path(remoteurl))
150 is_relative = 0;
151 else {
152 is_relative = 1;
153 /*
154 * Prepend a './' to ensure all relative
155 * remoteurls start with './' or '../'
156 */
157 if (!starts_with_dot_slash(remoteurl) &&
158 !starts_with_dot_dot_slash(remoteurl)) {
159 strbuf_reset(&sb);
160 strbuf_addf(&sb, "./%s", remoteurl);
161 free(remoteurl);
162 remoteurl = strbuf_detach(&sb, NULL);
163 }
164 }
165 /*
166 * When the url starts with '../', remove that and the
167 * last directory in remoteurl.
168 */
169 while (url) {
170 if (starts_with_dot_dot_slash(url)) {
171 url += 3;
172 colonsep |= chop_last_dir(&remoteurl, is_relative);
173 } else if (starts_with_dot_slash(url))
174 url += 2;
175 else
176 break;
177 }
178 strbuf_reset(&sb);
179 strbuf_addf(&sb, "%s%s%s", remoteurl, colonsep ? ":" : "/", url);
180 if (ends_with(url, "/"))
181 strbuf_setlen(&sb, sb.len - 1);
182 free(remoteurl);
183
184 if (starts_with_dot_slash(sb.buf))
185 out = xstrdup(sb.buf + 2);
186 else
187 out = xstrdup(sb.buf);
188 strbuf_reset(&sb);
189
190 if (!up_path || !is_relative)
191 return out;
192
193 strbuf_addf(&sb, "%s%s", up_path, out);
194 free(out);
195 return strbuf_detach(&sb, NULL);
196}
197
198static int resolve_relative_url(int argc, const char **argv, const char *prefix)
199{
200 char *remoteurl = NULL;
201 char *remote = get_default_remote();
202 const char *up_path = NULL;
203 char *res;
204 const char *url;
205 struct strbuf sb = STRBUF_INIT;
206
207 if (argc != 2 && argc != 3)
208 die("resolve-relative-url only accepts one or two arguments");
209
210 url = argv[1];
211 strbuf_addf(&sb, "remote.%s.url", remote);
212 free(remote);
213
214 if (git_config_get_string(sb.buf, &remoteurl))
215 /* the repository is its own authoritative upstream */
216 remoteurl = xgetcwd();
217
218 if (argc == 3)
219 up_path = argv[2];
220
221 res = relative_url(remoteurl, url, up_path);
222 puts(res);
223 free(res);
224 free(remoteurl);
225 return 0;
226}
227
228static int resolve_relative_url_test(int argc, const char **argv, const char *prefix)
229{
230 char *remoteurl, *res;
231 const char *up_path, *url;
232
233 if (argc != 4)
234 die("resolve-relative-url-test only accepts three arguments: <up_path> <remoteurl> <url>");
235
236 up_path = argv[1];
237 remoteurl = xstrdup(argv[2]);
238 url = argv[3];
239
240 if (!strcmp(up_path, "(null)"))
241 up_path = NULL;
242
243 res = relative_url(remoteurl, url, up_path);
244 puts(res);
245 free(res);
246 free(remoteurl);
247 return 0;
248}
249
250/* the result should be freed by the caller. */
251static char *get_submodule_displaypath(const char *path, const char *prefix)
252{
253 const char *super_prefix = get_super_prefix();
254
255 if (prefix && super_prefix) {
256 BUG("cannot have prefix '%s' and superprefix '%s'",
257 prefix, super_prefix);
258 } else if (prefix) {
259 struct strbuf sb = STRBUF_INIT;
260 char *displaypath = xstrdup(relative_path(path, prefix, &sb));
261 strbuf_release(&sb);
262 return displaypath;
263 } else if (super_prefix) {
264 return xstrfmt("%s%s", super_prefix, path);
265 } else {
266 return xstrdup(path);
267 }
268}
269
270static char *compute_rev_name(const char *sub_path, const char* object_id)
271{
272 struct strbuf sb = STRBUF_INIT;
273 const char ***d;
274
275 static const char *describe_bare[] = { NULL };
276
277 static const char *describe_tags[] = { "--tags", NULL };
278
279 static const char *describe_contains[] = { "--contains", NULL };
280
281 static const char *describe_all_always[] = { "--all", "--always", NULL };
282
283 static const char **describe_argv[] = { describe_bare, describe_tags,
284 describe_contains,
285 describe_all_always, NULL };
286
287 for (d = describe_argv; *d; d++) {
288 struct child_process cp = CHILD_PROCESS_INIT;
289 prepare_submodule_repo_env(&cp.env_array);
290 cp.dir = sub_path;
291 cp.git_cmd = 1;
292 cp.no_stderr = 1;
293
294 argv_array_push(&cp.args, "describe");
295 argv_array_pushv(&cp.args, *d);
296 argv_array_push(&cp.args, object_id);
297
298 if (!capture_command(&cp, &sb, 0)) {
299 strbuf_strip_suffix(&sb, "\n");
300 return strbuf_detach(&sb, NULL);
301 }
302 }
303
304 strbuf_release(&sb);
305 return NULL;
306}
307
308struct module_list {
309 const struct cache_entry **entries;
310 int alloc, nr;
311};
312#define MODULE_LIST_INIT { NULL, 0, 0 }
313
314static int module_list_compute(int argc, const char **argv,
315 const char *prefix,
316 struct pathspec *pathspec,
317 struct module_list *list)
318{
319 int i, result = 0;
320 char *ps_matched = NULL;
321 parse_pathspec(pathspec, 0,
322 PATHSPEC_PREFER_FULL,
323 prefix, argv);
324
325 if (pathspec->nr)
326 ps_matched = xcalloc(pathspec->nr, 1);
327
328 if (read_cache() < 0)
329 die(_("index file corrupt"));
330
331 for (i = 0; i < active_nr; i++) {
332 const struct cache_entry *ce = active_cache[i];
333
334 if (!match_pathspec(&the_index, pathspec, ce->name, ce_namelen(ce),
335 0, ps_matched, 1) ||
336 !S_ISGITLINK(ce->ce_mode))
337 continue;
338
339 ALLOC_GROW(list->entries, list->nr + 1, list->alloc);
340 list->entries[list->nr++] = ce;
341 while (i + 1 < active_nr &&
342 !strcmp(ce->name, active_cache[i + 1]->name))
343 /*
344 * Skip entries with the same name in different stages
345 * to make sure an entry is returned only once.
346 */
347 i++;
348 }
349
350 if (ps_matched && report_path_error(ps_matched, pathspec, prefix))
351 result = -1;
352
353 free(ps_matched);
354
355 return result;
356}
357
358static void module_list_active(struct module_list *list)
359{
360 int i;
361 struct module_list active_modules = MODULE_LIST_INIT;
362
363 for (i = 0; i < list->nr; i++) {
364 const struct cache_entry *ce = list->entries[i];
365
366 if (!is_submodule_active(the_repository, ce->name))
367 continue;
368
369 ALLOC_GROW(active_modules.entries,
370 active_modules.nr + 1,
371 active_modules.alloc);
372 active_modules.entries[active_modules.nr++] = ce;
373 }
374
375 free(list->entries);
376 *list = active_modules;
377}
378
379static char *get_up_path(const char *path)
380{
381 int i;
382 struct strbuf sb = STRBUF_INIT;
383
384 for (i = count_slashes(path); i; i--)
385 strbuf_addstr(&sb, "../");
386
387 /*
388 * Check if 'path' ends with slash or not
389 * for having the same output for dir/sub_dir
390 * and dir/sub_dir/
391 */
392 if (!is_dir_sep(path[strlen(path) - 1]))
393 strbuf_addstr(&sb, "../");
394
395 return strbuf_detach(&sb, NULL);
396}
397
398static int module_list(int argc, const char **argv, const char *prefix)
399{
400 int i;
401 struct pathspec pathspec;
402 struct module_list list = MODULE_LIST_INIT;
403
404 struct option module_list_options[] = {
405 OPT_STRING(0, "prefix", &prefix,
406 N_("path"),
407 N_("alternative anchor for relative paths")),
408 OPT_END()
409 };
410
411 const char *const git_submodule_helper_usage[] = {
412 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
413 NULL
414 };
415
416 argc = parse_options(argc, argv, prefix, module_list_options,
417 git_submodule_helper_usage, 0);
418
419 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
420 return 1;
421
422 for (i = 0; i < list.nr; i++) {
423 const struct cache_entry *ce = list.entries[i];
424
425 if (ce_stage(ce))
426 printf("%06o %s U\t", ce->ce_mode, sha1_to_hex(null_sha1));
427 else
428 printf("%06o %s %d\t", ce->ce_mode,
429 oid_to_hex(&ce->oid), ce_stage(ce));
430
431 fprintf(stdout, "%s\n", ce->name);
432 }
433 return 0;
434}
435
436static void for_each_listed_submodule(const struct module_list *list,
437 each_submodule_fn fn, void *cb_data)
438{
439 int i;
440 for (i = 0; i < list->nr; i++)
441 fn(list->entries[i], cb_data);
442}
443
444struct cb_foreach {
445 int argc;
446 const char **argv;
447 const char *prefix;
448 int quiet;
449 int recursive;
450};
451#define CB_FOREACH_INIT { 0 }
452
453static void runcommand_in_submodule_cb(const struct cache_entry *list_item,
454 void *cb_data)
455{
456 struct cb_foreach *info = cb_data;
457 const char *path = list_item->name;
458 const struct object_id *ce_oid = &list_item->oid;
459
460 const struct submodule *sub;
461 struct child_process cp = CHILD_PROCESS_INIT;
462 char *displaypath;
463
464 displaypath = get_submodule_displaypath(path, info->prefix);
465
466 sub = submodule_from_path(the_repository, &null_oid, path);
467
468 if (!sub)
469 die(_("No url found for submodule path '%s' in .gitmodules"),
470 displaypath);
471
472 if (!is_submodule_populated_gently(path, NULL))
473 goto cleanup;
474
475 prepare_submodule_repo_env(&cp.env_array);
476
477 /*
478 * For the purpose of executing <command> in the submodule,
479 * separate shell is used for the purpose of running the
480 * child process.
481 */
482 cp.use_shell = 1;
483 cp.dir = path;
484
485 /*
486 * NEEDSWORK: the command currently has access to the variables $name,
487 * $sm_path, $displaypath, $sha1 and $toplevel only when the command
488 * contains a single argument. This is done for maintaining a faithful
489 * translation from shell script.
490 */
491 if (info->argc == 1) {
492 char *toplevel = xgetcwd();
493 struct strbuf sb = STRBUF_INIT;
494
495 argv_array_pushf(&cp.env_array, "name=%s", sub->name);
496 argv_array_pushf(&cp.env_array, "sm_path=%s", path);
497 argv_array_pushf(&cp.env_array, "displaypath=%s", displaypath);
498 argv_array_pushf(&cp.env_array, "sha1=%s",
499 oid_to_hex(ce_oid));
500 argv_array_pushf(&cp.env_array, "toplevel=%s", toplevel);
501
502 /*
503 * Since the path variable was accessible from the script
504 * before porting, it is also made available after porting.
505 * The environment variable "PATH" has a very special purpose
506 * on windows. And since environment variables are
507 * case-insensitive in windows, it interferes with the
508 * existing PATH variable. Hence, to avoid that, we expose
509 * path via the args argv_array and not via env_array.
510 */
511 sq_quote_buf(&sb, path);
512 argv_array_pushf(&cp.args, "path=%s; %s",
513 sb.buf, info->argv[0]);
514 strbuf_release(&sb);
515 free(toplevel);
516 } else {
517 argv_array_pushv(&cp.args, info->argv);
518 }
519
520 if (!info->quiet)
521 printf(_("Entering '%s'\n"), displaypath);
522
523 if (info->argv[0] && run_command(&cp))
524 die(_("run_command returned non-zero status for %s\n."),
525 displaypath);
526
527 if (info->recursive) {
528 struct child_process cpr = CHILD_PROCESS_INIT;
529
530 cpr.git_cmd = 1;
531 cpr.dir = path;
532 prepare_submodule_repo_env(&cpr.env_array);
533
534 argv_array_pushl(&cpr.args, "--super-prefix", NULL);
535 argv_array_pushf(&cpr.args, "%s/", displaypath);
536 argv_array_pushl(&cpr.args, "submodule--helper", "foreach", "--recursive",
537 NULL);
538
539 if (info->quiet)
540 argv_array_push(&cpr.args, "--quiet");
541
542 argv_array_pushv(&cpr.args, info->argv);
543
544 if (run_command(&cpr))
545 die(_("run_command returned non-zero status while "
546 "recursing in the nested submodules of %s\n."),
547 displaypath);
548 }
549
550cleanup:
551 free(displaypath);
552}
553
554static int module_foreach(int argc, const char **argv, const char *prefix)
555{
556 struct cb_foreach info = CB_FOREACH_INIT;
557 struct pathspec pathspec;
558 struct module_list list = MODULE_LIST_INIT;
559
560 struct option module_foreach_options[] = {
561 OPT__QUIET(&info.quiet, N_("Suppress output of entering each submodule command")),
562 OPT_BOOL(0, "recursive", &info.recursive,
563 N_("Recurse into nested submodules")),
564 OPT_END()
565 };
566
567 const char *const git_submodule_helper_usage[] = {
568 N_("git submodule--helper foreach [--quiet] [--recursive] <command>"),
569 NULL
570 };
571
572 argc = parse_options(argc, argv, prefix, module_foreach_options,
573 git_submodule_helper_usage, PARSE_OPT_KEEP_UNKNOWN);
574
575 if (module_list_compute(0, NULL, prefix, &pathspec, &list) < 0)
576 return 1;
577
578 info.argc = argc;
579 info.argv = argv;
580 info.prefix = prefix;
581
582 for_each_listed_submodule(&list, runcommand_in_submodule_cb, &info);
583
584 return 0;
585}
586
587struct init_cb {
588 const char *prefix;
589 unsigned int flags;
590};
591
592#define INIT_CB_INIT { NULL, 0 }
593
594static void init_submodule(const char *path, const char *prefix,
595 unsigned int flags)
596{
597 const struct submodule *sub;
598 struct strbuf sb = STRBUF_INIT;
599 char *upd = NULL, *url = NULL, *displaypath;
600
601 displaypath = get_submodule_displaypath(path, prefix);
602
603 sub = submodule_from_path(the_repository, &null_oid, path);
604
605 if (!sub)
606 die(_("No url found for submodule path '%s' in .gitmodules"),
607 displaypath);
608
609 /*
610 * NEEDSWORK: In a multi-working-tree world, this needs to be
611 * set in the per-worktree config.
612 *
613 * Set active flag for the submodule being initialized
614 */
615 if (!is_submodule_active(the_repository, path)) {
616 strbuf_addf(&sb, "submodule.%s.active", sub->name);
617 git_config_set_gently(sb.buf, "true");
618 strbuf_reset(&sb);
619 }
620
621 /*
622 * Copy url setting when it is not set yet.
623 * To look up the url in .git/config, we must not fall back to
624 * .gitmodules, so look it up directly.
625 */
626 strbuf_addf(&sb, "submodule.%s.url", sub->name);
627 if (git_config_get_string(sb.buf, &url)) {
628 if (!sub->url)
629 die(_("No url found for submodule path '%s' in .gitmodules"),
630 displaypath);
631
632 url = xstrdup(sub->url);
633
634 /* Possibly a url relative to parent */
635 if (starts_with_dot_dot_slash(url) ||
636 starts_with_dot_slash(url)) {
637 char *remoteurl, *relurl;
638 char *remote = get_default_remote();
639 struct strbuf remotesb = STRBUF_INIT;
640 strbuf_addf(&remotesb, "remote.%s.url", remote);
641 free(remote);
642
643 if (git_config_get_string(remotesb.buf, &remoteurl)) {
644 warning(_("could not lookup configuration '%s'. Assuming this repository is its own authoritative upstream."), remotesb.buf);
645 remoteurl = xgetcwd();
646 }
647 relurl = relative_url(remoteurl, url, NULL);
648 strbuf_release(&remotesb);
649 free(remoteurl);
650 free(url);
651 url = relurl;
652 }
653
654 if (git_config_set_gently(sb.buf, url))
655 die(_("Failed to register url for submodule path '%s'"),
656 displaypath);
657 if (!(flags & OPT_QUIET))
658 fprintf(stderr,
659 _("Submodule '%s' (%s) registered for path '%s'\n"),
660 sub->name, url, displaypath);
661 }
662 strbuf_reset(&sb);
663
664 /* Copy "update" setting when it is not set yet */
665 strbuf_addf(&sb, "submodule.%s.update", sub->name);
666 if (git_config_get_string(sb.buf, &upd) &&
667 sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
668 if (sub->update_strategy.type == SM_UPDATE_COMMAND) {
669 fprintf(stderr, _("warning: command update mode suggested for submodule '%s'\n"),
670 sub->name);
671 upd = xstrdup("none");
672 } else
673 upd = xstrdup(submodule_strategy_to_string(&sub->update_strategy));
674
675 if (git_config_set_gently(sb.buf, upd))
676 die(_("Failed to register update mode for submodule path '%s'"), displaypath);
677 }
678 strbuf_release(&sb);
679 free(displaypath);
680 free(url);
681 free(upd);
682}
683
684static void init_submodule_cb(const struct cache_entry *list_item, void *cb_data)
685{
686 struct init_cb *info = cb_data;
687 init_submodule(list_item->name, info->prefix, info->flags);
688}
689
690static int module_init(int argc, const char **argv, const char *prefix)
691{
692 struct init_cb info = INIT_CB_INIT;
693 struct pathspec pathspec;
694 struct module_list list = MODULE_LIST_INIT;
695 int quiet = 0;
696
697 struct option module_init_options[] = {
698 OPT__QUIET(&quiet, N_("Suppress output for initializing a submodule")),
699 OPT_END()
700 };
701
702 const char *const git_submodule_helper_usage[] = {
703 N_("git submodule--helper init [<path>]"),
704 NULL
705 };
706
707 argc = parse_options(argc, argv, prefix, module_init_options,
708 git_submodule_helper_usage, 0);
709
710 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
711 return 1;
712
713 /*
714 * If there are no path args and submodule.active is set then,
715 * by default, only initialize 'active' modules.
716 */
717 if (!argc && git_config_get_value_multi("submodule.active"))
718 module_list_active(&list);
719
720 info.prefix = prefix;
721 if (quiet)
722 info.flags |= OPT_QUIET;
723
724 for_each_listed_submodule(&list, init_submodule_cb, &info);
725
726 return 0;
727}
728
729struct status_cb {
730 const char *prefix;
731 unsigned int flags;
732};
733
734#define STATUS_CB_INIT { NULL, 0 }
735
736static void print_status(unsigned int flags, char state, const char *path,
737 const struct object_id *oid, const char *displaypath)
738{
739 if (flags & OPT_QUIET)
740 return;
741
742 printf("%c%s %s", state, oid_to_hex(oid), displaypath);
743
744 if (state == ' ' || state == '+') {
745 const char *name = compute_rev_name(path, oid_to_hex(oid));
746
747 if (name)
748 printf(" (%s)", name);
749 }
750
751 printf("\n");
752}
753
754static int handle_submodule_head_ref(const char *refname,
755 const struct object_id *oid, int flags,
756 void *cb_data)
757{
758 struct object_id *output = cb_data;
759 if (oid)
760 oidcpy(output, oid);
761
762 return 0;
763}
764
765static void status_submodule(const char *path, const struct object_id *ce_oid,
766 unsigned int ce_flags, const char *prefix,
767 unsigned int flags)
768{
769 char *displaypath;
770 struct argv_array diff_files_args = ARGV_ARRAY_INIT;
771 struct rev_info rev;
772 int diff_files_result;
773
774 if (!submodule_from_path(the_repository, &null_oid, path))
775 die(_("no submodule mapping found in .gitmodules for path '%s'"),
776 path);
777
778 displaypath = get_submodule_displaypath(path, prefix);
779
780 if ((CE_STAGEMASK & ce_flags) >> CE_STAGESHIFT) {
781 print_status(flags, 'U', path, &null_oid, displaypath);
782 goto cleanup;
783 }
784
785 if (!is_submodule_active(the_repository, path)) {
786 print_status(flags, '-', path, ce_oid, displaypath);
787 goto cleanup;
788 }
789
790 argv_array_pushl(&diff_files_args, "diff-files",
791 "--ignore-submodules=dirty", "--quiet", "--",
792 path, NULL);
793
794 git_config(git_diff_basic_config, NULL);
795 init_revisions(&rev, prefix);
796 rev.abbrev = 0;
797 diff_files_args.argc = setup_revisions(diff_files_args.argc,
798 diff_files_args.argv,
799 &rev, NULL);
800 diff_files_result = run_diff_files(&rev, 0);
801
802 if (!diff_result_code(&rev.diffopt, diff_files_result)) {
803 print_status(flags, ' ', path, ce_oid,
804 displaypath);
805 } else if (!(flags & OPT_CACHED)) {
806 struct object_id oid;
807 struct ref_store *refs = get_submodule_ref_store(path);
808
809 if (!refs) {
810 print_status(flags, '-', path, ce_oid, displaypath);
811 goto cleanup;
812 }
813 if (refs_head_ref(refs, handle_submodule_head_ref, &oid))
814 die(_("could not resolve HEAD ref inside the "
815 "submodule '%s'"), path);
816
817 print_status(flags, '+', path, &oid, displaypath);
818 } else {
819 print_status(flags, '+', path, ce_oid, displaypath);
820 }
821
822 if (flags & OPT_RECURSIVE) {
823 struct child_process cpr = CHILD_PROCESS_INIT;
824
825 cpr.git_cmd = 1;
826 cpr.dir = path;
827 prepare_submodule_repo_env(&cpr.env_array);
828
829 argv_array_push(&cpr.args, "--super-prefix");
830 argv_array_pushf(&cpr.args, "%s/", displaypath);
831 argv_array_pushl(&cpr.args, "submodule--helper", "status",
832 "--recursive", NULL);
833
834 if (flags & OPT_CACHED)
835 argv_array_push(&cpr.args, "--cached");
836
837 if (flags & OPT_QUIET)
838 argv_array_push(&cpr.args, "--quiet");
839
840 if (run_command(&cpr))
841 die(_("failed to recurse into submodule '%s'"), path);
842 }
843
844cleanup:
845 argv_array_clear(&diff_files_args);
846 free(displaypath);
847}
848
849static void status_submodule_cb(const struct cache_entry *list_item,
850 void *cb_data)
851{
852 struct status_cb *info = cb_data;
853 status_submodule(list_item->name, &list_item->oid, list_item->ce_flags,
854 info->prefix, info->flags);
855}
856
857static int module_status(int argc, const char **argv, const char *prefix)
858{
859 struct status_cb info = STATUS_CB_INIT;
860 struct pathspec pathspec;
861 struct module_list list = MODULE_LIST_INIT;
862 int quiet = 0;
863
864 struct option module_status_options[] = {
865 OPT__QUIET(&quiet, N_("Suppress submodule status output")),
866 OPT_BIT(0, "cached", &info.flags, N_("Use commit stored in the index instead of the one stored in the submodule HEAD"), OPT_CACHED),
867 OPT_BIT(0, "recursive", &info.flags, N_("recurse into nested submodules"), OPT_RECURSIVE),
868 OPT_END()
869 };
870
871 const char *const git_submodule_helper_usage[] = {
872 N_("git submodule status [--quiet] [--cached] [--recursive] [<path>...]"),
873 NULL
874 };
875
876 argc = parse_options(argc, argv, prefix, module_status_options,
877 git_submodule_helper_usage, 0);
878
879 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
880 return 1;
881
882 info.prefix = prefix;
883 if (quiet)
884 info.flags |= OPT_QUIET;
885
886 for_each_listed_submodule(&list, status_submodule_cb, &info);
887
888 return 0;
889}
890
891static int module_name(int argc, const char **argv, const char *prefix)
892{
893 const struct submodule *sub;
894
895 if (argc != 2)
896 usage(_("git submodule--helper name <path>"));
897
898 sub = submodule_from_path(the_repository, &null_oid, argv[1]);
899
900 if (!sub)
901 die(_("no submodule mapping found in .gitmodules for path '%s'"),
902 argv[1]);
903
904 printf("%s\n", sub->name);
905
906 return 0;
907}
908
909struct sync_cb {
910 const char *prefix;
911 unsigned int flags;
912};
913
914#define SYNC_CB_INIT { NULL, 0 }
915
916static void sync_submodule(const char *path, const char *prefix,
917 unsigned int flags)
918{
919 const struct submodule *sub;
920 char *remote_key = NULL;
921 char *sub_origin_url, *super_config_url, *displaypath;
922 struct strbuf sb = STRBUF_INIT;
923 struct child_process cp = CHILD_PROCESS_INIT;
924 char *sub_config_path = NULL;
925
926 if (!is_submodule_active(the_repository, path))
927 return;
928
929 sub = submodule_from_path(the_repository, &null_oid, path);
930
931 if (sub && sub->url) {
932 if (starts_with_dot_dot_slash(sub->url) ||
933 starts_with_dot_slash(sub->url)) {
934 char *remote_url, *up_path;
935 char *remote = get_default_remote();
936 strbuf_addf(&sb, "remote.%s.url", remote);
937
938 if (git_config_get_string(sb.buf, &remote_url))
939 remote_url = xgetcwd();
940
941 up_path = get_up_path(path);
942 sub_origin_url = relative_url(remote_url, sub->url, up_path);
943 super_config_url = relative_url(remote_url, sub->url, NULL);
944
945 free(remote);
946 free(up_path);
947 free(remote_url);
948 } else {
949 sub_origin_url = xstrdup(sub->url);
950 super_config_url = xstrdup(sub->url);
951 }
952 } else {
953 sub_origin_url = xstrdup("");
954 super_config_url = xstrdup("");
955 }
956
957 displaypath = get_submodule_displaypath(path, prefix);
958
959 if (!(flags & OPT_QUIET))
960 printf(_("Synchronizing submodule url for '%s'\n"),
961 displaypath);
962
963 strbuf_reset(&sb);
964 strbuf_addf(&sb, "submodule.%s.url", sub->name);
965 if (git_config_set_gently(sb.buf, super_config_url))
966 die(_("failed to register url for submodule path '%s'"),
967 displaypath);
968
969 if (!is_submodule_populated_gently(path, NULL))
970 goto cleanup;
971
972 prepare_submodule_repo_env(&cp.env_array);
973 cp.git_cmd = 1;
974 cp.dir = path;
975 argv_array_pushl(&cp.args, "submodule--helper",
976 "print-default-remote", NULL);
977
978 strbuf_reset(&sb);
979 if (capture_command(&cp, &sb, 0))
980 die(_("failed to get the default remote for submodule '%s'"),
981 path);
982
983 strbuf_strip_suffix(&sb, "\n");
984 remote_key = xstrfmt("remote.%s.url", sb.buf);
985
986 strbuf_reset(&sb);
987 submodule_to_gitdir(&sb, path);
988 strbuf_addstr(&sb, "/config");
989
990 if (git_config_set_in_file_gently(sb.buf, remote_key, sub_origin_url))
991 die(_("failed to update remote for submodule '%s'"),
992 path);
993
994 if (flags & OPT_RECURSIVE) {
995 struct child_process cpr = CHILD_PROCESS_INIT;
996
997 cpr.git_cmd = 1;
998 cpr.dir = path;
999 prepare_submodule_repo_env(&cpr.env_array);
1000
1001 argv_array_push(&cpr.args, "--super-prefix");
1002 argv_array_pushf(&cpr.args, "%s/", displaypath);
1003 argv_array_pushl(&cpr.args, "submodule--helper", "sync",
1004 "--recursive", NULL);
1005
1006 if (flags & OPT_QUIET)
1007 argv_array_push(&cpr.args, "--quiet");
1008
1009 if (run_command(&cpr))
1010 die(_("failed to recurse into submodule '%s'"),
1011 path);
1012 }
1013
1014cleanup:
1015 free(super_config_url);
1016 free(sub_origin_url);
1017 strbuf_release(&sb);
1018 free(remote_key);
1019 free(displaypath);
1020 free(sub_config_path);
1021}
1022
1023static void sync_submodule_cb(const struct cache_entry *list_item, void *cb_data)
1024{
1025 struct sync_cb *info = cb_data;
1026 sync_submodule(list_item->name, info->prefix, info->flags);
1027}
1028
1029static int module_sync(int argc, const char **argv, const char *prefix)
1030{
1031 struct sync_cb info = SYNC_CB_INIT;
1032 struct pathspec pathspec;
1033 struct module_list list = MODULE_LIST_INIT;
1034 int quiet = 0;
1035 int recursive = 0;
1036
1037 struct option module_sync_options[] = {
1038 OPT__QUIET(&quiet, N_("Suppress output of synchronizing submodule url")),
1039 OPT_BOOL(0, "recursive", &recursive,
1040 N_("Recurse into nested submodules")),
1041 OPT_END()
1042 };
1043
1044 const char *const git_submodule_helper_usage[] = {
1045 N_("git submodule--helper sync [--quiet] [--recursive] [<path>]"),
1046 NULL
1047 };
1048
1049 argc = parse_options(argc, argv, prefix, module_sync_options,
1050 git_submodule_helper_usage, 0);
1051
1052 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1053 return 1;
1054
1055 info.prefix = prefix;
1056 if (quiet)
1057 info.flags |= OPT_QUIET;
1058 if (recursive)
1059 info.flags |= OPT_RECURSIVE;
1060
1061 for_each_listed_submodule(&list, sync_submodule_cb, &info);
1062
1063 return 0;
1064}
1065
1066struct deinit_cb {
1067 const char *prefix;
1068 unsigned int flags;
1069};
1070#define DEINIT_CB_INIT { NULL, 0 }
1071
1072static void deinit_submodule(const char *path, const char *prefix,
1073 unsigned int flags)
1074{
1075 const struct submodule *sub;
1076 char *displaypath = NULL;
1077 struct child_process cp_config = CHILD_PROCESS_INIT;
1078 struct strbuf sb_config = STRBUF_INIT;
1079 char *sub_git_dir = xstrfmt("%s/.git", path);
1080
1081 sub = submodule_from_path(the_repository, &null_oid, path);
1082
1083 if (!sub || !sub->name)
1084 goto cleanup;
1085
1086 displaypath = get_submodule_displaypath(path, prefix);
1087
1088 /* remove the submodule work tree (unless the user already did it) */
1089 if (is_directory(path)) {
1090 struct strbuf sb_rm = STRBUF_INIT;
1091 const char *format;
1092
1093 /*
1094 * protect submodules containing a .git directory
1095 * NEEDSWORK: instead of dying, automatically call
1096 * absorbgitdirs and (possibly) warn.
1097 */
1098 if (is_directory(sub_git_dir))
1099 die(_("Submodule work tree '%s' contains a .git "
1100 "directory (use 'rm -rf' if you really want "
1101 "to remove it including all of its history)"),
1102 displaypath);
1103
1104 if (!(flags & OPT_FORCE)) {
1105 struct child_process cp_rm = CHILD_PROCESS_INIT;
1106 cp_rm.git_cmd = 1;
1107 argv_array_pushl(&cp_rm.args, "rm", "-qn",
1108 path, NULL);
1109
1110 if (run_command(&cp_rm))
1111 die(_("Submodule work tree '%s' contains local "
1112 "modifications; use '-f' to discard them"),
1113 displaypath);
1114 }
1115
1116 strbuf_addstr(&sb_rm, path);
1117
1118 if (!remove_dir_recursively(&sb_rm, 0))
1119 format = _("Cleared directory '%s'\n");
1120 else
1121 format = _("Could not remove submodule work tree '%s'\n");
1122
1123 if (!(flags & OPT_QUIET))
1124 printf(format, displaypath);
1125
1126 strbuf_release(&sb_rm);
1127 }
1128
1129 if (mkdir(path, 0777))
1130 printf(_("could not create empty submodule directory %s"),
1131 displaypath);
1132
1133 cp_config.git_cmd = 1;
1134 argv_array_pushl(&cp_config.args, "config", "--get-regexp", NULL);
1135 argv_array_pushf(&cp_config.args, "submodule.%s\\.", sub->name);
1136
1137 /* remove the .git/config entries (unless the user already did it) */
1138 if (!capture_command(&cp_config, &sb_config, 0) && sb_config.len) {
1139 char *sub_key = xstrfmt("submodule.%s", sub->name);
1140 /*
1141 * remove the whole section so we have a clean state when
1142 * the user later decides to init this submodule again
1143 */
1144 git_config_rename_section_in_file(NULL, sub_key, NULL);
1145 if (!(flags & OPT_QUIET))
1146 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1147 sub->name, sub->url, displaypath);
1148 free(sub_key);
1149 }
1150
1151cleanup:
1152 free(displaypath);
1153 free(sub_git_dir);
1154 strbuf_release(&sb_config);
1155}
1156
1157static void deinit_submodule_cb(const struct cache_entry *list_item,
1158 void *cb_data)
1159{
1160 struct deinit_cb *info = cb_data;
1161 deinit_submodule(list_item->name, info->prefix, info->flags);
1162}
1163
1164static int module_deinit(int argc, const char **argv, const char *prefix)
1165{
1166 struct deinit_cb info = DEINIT_CB_INIT;
1167 struct pathspec pathspec;
1168 struct module_list list = MODULE_LIST_INIT;
1169 int quiet = 0;
1170 int force = 0;
1171 int all = 0;
1172
1173 struct option module_deinit_options[] = {
1174 OPT__QUIET(&quiet, N_("Suppress submodule status output")),
1175 OPT__FORCE(&force, N_("Remove submodule working trees even if they contain local changes"), 0),
1176 OPT_BOOL(0, "all", &all, N_("Unregister all submodules")),
1177 OPT_END()
1178 };
1179
1180 const char *const git_submodule_helper_usage[] = {
1181 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1182 NULL
1183 };
1184
1185 argc = parse_options(argc, argv, prefix, module_deinit_options,
1186 git_submodule_helper_usage, 0);
1187
1188 if (all && argc) {
1189 error("pathspec and --all are incompatible");
1190 usage_with_options(git_submodule_helper_usage,
1191 module_deinit_options);
1192 }
1193
1194 if (!argc && !all)
1195 die(_("Use '--all' if you really want to deinitialize all submodules"));
1196
1197 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1198 return 1;
1199
1200 info.prefix = prefix;
1201 if (quiet)
1202 info.flags |= OPT_QUIET;
1203 if (force)
1204 info.flags |= OPT_FORCE;
1205
1206 for_each_listed_submodule(&list, deinit_submodule_cb, &info);
1207
1208 return 0;
1209}
1210
1211static int clone_submodule(const char *path, const char *gitdir, const char *url,
1212 const char *depth, struct string_list *reference, int dissociate,
1213 int quiet, int progress)
1214{
1215 struct child_process cp = CHILD_PROCESS_INIT;
1216
1217 argv_array_push(&cp.args, "clone");
1218 argv_array_push(&cp.args, "--no-checkout");
1219 if (quiet)
1220 argv_array_push(&cp.args, "--quiet");
1221 if (progress)
1222 argv_array_push(&cp.args, "--progress");
1223 if (depth && *depth)
1224 argv_array_pushl(&cp.args, "--depth", depth, NULL);
1225 if (reference->nr) {
1226 struct string_list_item *item;
1227 for_each_string_list_item(item, reference)
1228 argv_array_pushl(&cp.args, "--reference",
1229 item->string, NULL);
1230 }
1231 if (dissociate)
1232 argv_array_push(&cp.args, "--dissociate");
1233 if (gitdir && *gitdir)
1234 argv_array_pushl(&cp.args, "--separate-git-dir", gitdir, NULL);
1235
1236 argv_array_push(&cp.args, url);
1237 argv_array_push(&cp.args, path);
1238
1239 cp.git_cmd = 1;
1240 prepare_submodule_repo_env(&cp.env_array);
1241 cp.no_stdin = 1;
1242
1243 return run_command(&cp);
1244}
1245
1246struct submodule_alternate_setup {
1247 const char *submodule_name;
1248 enum SUBMODULE_ALTERNATE_ERROR_MODE {
1249 SUBMODULE_ALTERNATE_ERROR_DIE,
1250 SUBMODULE_ALTERNATE_ERROR_INFO,
1251 SUBMODULE_ALTERNATE_ERROR_IGNORE
1252 } error_mode;
1253 struct string_list *reference;
1254};
1255#define SUBMODULE_ALTERNATE_SETUP_INIT { NULL, \
1256 SUBMODULE_ALTERNATE_ERROR_IGNORE, NULL }
1257
1258static int add_possible_reference_from_superproject(
1259 struct alternate_object_database *alt, void *sas_cb)
1260{
1261 struct submodule_alternate_setup *sas = sas_cb;
1262
1263 /*
1264 * If the alternate object store is another repository, try the
1265 * standard layout with .git/(modules/<name>)+/objects
1266 */
1267 if (ends_with(alt->path, "/objects")) {
1268 char *sm_alternate;
1269 struct strbuf sb = STRBUF_INIT;
1270 struct strbuf err = STRBUF_INIT;
1271 strbuf_add(&sb, alt->path, strlen(alt->path) - strlen("objects"));
1272
1273 /*
1274 * We need to end the new path with '/' to mark it as a dir,
1275 * otherwise a submodule name containing '/' will be broken
1276 * as the last part of a missing submodule reference would
1277 * be taken as a file name.
1278 */
1279 strbuf_addf(&sb, "modules/%s/", sas->submodule_name);
1280
1281 sm_alternate = compute_alternate_path(sb.buf, &err);
1282 if (sm_alternate) {
1283 string_list_append(sas->reference, xstrdup(sb.buf));
1284 free(sm_alternate);
1285 } else {
1286 switch (sas->error_mode) {
1287 case SUBMODULE_ALTERNATE_ERROR_DIE:
1288 die(_("submodule '%s' cannot add alternate: %s"),
1289 sas->submodule_name, err.buf);
1290 case SUBMODULE_ALTERNATE_ERROR_INFO:
1291 fprintf(stderr, _("submodule '%s' cannot add alternate: %s"),
1292 sas->submodule_name, err.buf);
1293 case SUBMODULE_ALTERNATE_ERROR_IGNORE:
1294 ; /* nothing */
1295 }
1296 }
1297 strbuf_release(&sb);
1298 }
1299
1300 return 0;
1301}
1302
1303static void prepare_possible_alternates(const char *sm_name,
1304 struct string_list *reference)
1305{
1306 char *sm_alternate = NULL, *error_strategy = NULL;
1307 struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT;
1308
1309 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1310 if (!sm_alternate)
1311 return;
1312
1313 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1314
1315 if (!error_strategy)
1316 error_strategy = xstrdup("die");
1317
1318 sas.submodule_name = sm_name;
1319 sas.reference = reference;
1320 if (!strcmp(error_strategy, "die"))
1321 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE;
1322 else if (!strcmp(error_strategy, "info"))
1323 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO;
1324 else if (!strcmp(error_strategy, "ignore"))
1325 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE;
1326 else
1327 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
1328
1329 if (!strcmp(sm_alternate, "superproject"))
1330 foreach_alt_odb(add_possible_reference_from_superproject, &sas);
1331 else if (!strcmp(sm_alternate, "no"))
1332 ; /* do nothing */
1333 else
1334 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate);
1335
1336 free(sm_alternate);
1337 free(error_strategy);
1338}
1339
1340static int module_clone(int argc, const char **argv, const char *prefix)
1341{
1342 const char *name = NULL, *url = NULL, *depth = NULL;
1343 int quiet = 0;
1344 int progress = 0;
1345 char *p, *path = NULL, *sm_gitdir;
1346 struct strbuf sb = STRBUF_INIT;
1347 struct string_list reference = STRING_LIST_INIT_NODUP;
1348 int dissociate = 0;
1349 char *sm_alternate = NULL, *error_strategy = NULL;
1350
1351 struct option module_clone_options[] = {
1352 OPT_STRING(0, "prefix", &prefix,
1353 N_("path"),
1354 N_("alternative anchor for relative paths")),
1355 OPT_STRING(0, "path", &path,
1356 N_("path"),
1357 N_("where the new submodule will be cloned to")),
1358 OPT_STRING(0, "name", &name,
1359 N_("string"),
1360 N_("name of the new submodule")),
1361 OPT_STRING(0, "url", &url,
1362 N_("string"),
1363 N_("url where to clone the submodule from")),
1364 OPT_STRING_LIST(0, "reference", &reference,
1365 N_("repo"),
1366 N_("reference repository")),
1367 OPT_BOOL(0, "dissociate", &dissociate,
1368 N_("use --reference only while cloning")),
1369 OPT_STRING(0, "depth", &depth,
1370 N_("string"),
1371 N_("depth for shallow clones")),
1372 OPT__QUIET(&quiet, "Suppress output for cloning a submodule"),
1373 OPT_BOOL(0, "progress", &progress,
1374 N_("force cloning progress")),
1375 OPT_END()
1376 };
1377
1378 const char *const git_submodule_helper_usage[] = {
1379 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
1380 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1381 "--url <url> --path <path>"),
1382 NULL
1383 };
1384
1385 argc = parse_options(argc, argv, prefix, module_clone_options,
1386 git_submodule_helper_usage, 0);
1387
1388 if (argc || !url || !path || !*path)
1389 usage_with_options(git_submodule_helper_usage,
1390 module_clone_options);
1391
1392 strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
1393 sm_gitdir = absolute_pathdup(sb.buf);
1394 strbuf_reset(&sb);
1395
1396 if (!is_absolute_path(path)) {
1397 strbuf_addf(&sb, "%s/%s", get_git_work_tree(), path);
1398 path = strbuf_detach(&sb, NULL);
1399 } else
1400 path = xstrdup(path);
1401
1402 if (!file_exists(sm_gitdir)) {
1403 if (safe_create_leading_directories_const(sm_gitdir) < 0)
1404 die(_("could not create directory '%s'"), sm_gitdir);
1405
1406 prepare_possible_alternates(name, &reference);
1407
1408 if (clone_submodule(path, sm_gitdir, url, depth, &reference, dissociate,
1409 quiet, progress))
1410 die(_("clone of '%s' into submodule path '%s' failed"),
1411 url, path);
1412 } else {
1413 if (safe_create_leading_directories_const(path) < 0)
1414 die(_("could not create directory '%s'"), path);
1415 strbuf_addf(&sb, "%s/index", sm_gitdir);
1416 unlink_or_warn(sb.buf);
1417 strbuf_reset(&sb);
1418 }
1419
1420 connect_work_tree_and_git_dir(path, sm_gitdir, 0);
1421
1422 p = git_pathdup_submodule(path, "config");
1423 if (!p)
1424 die(_("could not get submodule directory for '%s'"), path);
1425
1426 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1427 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1428 if (sm_alternate)
1429 git_config_set_in_file(p, "submodule.alternateLocation",
1430 sm_alternate);
1431 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1432 if (error_strategy)
1433 git_config_set_in_file(p, "submodule.alternateErrorStrategy",
1434 error_strategy);
1435
1436 free(sm_alternate);
1437 free(error_strategy);
1438
1439 strbuf_release(&sb);
1440 free(sm_gitdir);
1441 free(path);
1442 free(p);
1443 return 0;
1444}
1445
1446static void determine_submodule_update_strategy(struct repository *r,
1447 int just_cloned,
1448 const char *path,
1449 const char *update,
1450 struct submodule_update_strategy *out)
1451{
1452 const struct submodule *sub = submodule_from_path(r, &null_oid, path);
1453 char *key;
1454 const char *val;
1455
1456 key = xstrfmt("submodule.%s.update", sub->name);
1457
1458 if (update) {
1459 trace_printf("parsing update");
1460 if (parse_submodule_update_strategy(update, out) < 0)
1461 die(_("Invalid update mode '%s' for submodule path '%s'"),
1462 update, path);
1463 } else if (!repo_config_get_string_const(r, key, &val)) {
1464 if (parse_submodule_update_strategy(val, out) < 0)
1465 die(_("Invalid update mode '%s' configured for submodule path '%s'"),
1466 val, path);
1467 } else if (sub->update_strategy.type != SM_UPDATE_UNSPECIFIED) {
1468 trace_printf("loaded thing");
1469 out->type = sub->update_strategy.type;
1470 out->command = sub->update_strategy.command;
1471 } else
1472 out->type = SM_UPDATE_CHECKOUT;
1473
1474 if (just_cloned &&
1475 (out->type == SM_UPDATE_MERGE ||
1476 out->type == SM_UPDATE_REBASE ||
1477 out->type == SM_UPDATE_NONE))
1478 out->type = SM_UPDATE_CHECKOUT;
1479
1480 free(key);
1481}
1482
1483static int module_update_module_mode(int argc, const char **argv, const char *prefix)
1484{
1485 const char *path, *update = NULL;
1486 int just_cloned;
1487 struct submodule_update_strategy update_strategy = { .type = SM_UPDATE_CHECKOUT };
1488
1489 if (argc < 3 || argc > 4)
1490 die("submodule--helper update-module-clone expects <just-cloned> <path> [<update>]");
1491
1492 just_cloned = git_config_int("just_cloned", argv[1]);
1493 path = argv[2];
1494
1495 if (argc == 4)
1496 update = argv[3];
1497
1498 determine_submodule_update_strategy(the_repository,
1499 just_cloned, path, update,
1500 &update_strategy);
1501 fputs(submodule_strategy_to_string(&update_strategy), stdout);
1502
1503 return 0;
1504}
1505
1506struct update_clone_data {
1507 const struct submodule *sub;
1508 struct object_id oid;
1509 unsigned just_cloned;
1510};
1511
1512struct submodule_update_clone {
1513 /* index into 'list', the list of submodules to look into for cloning */
1514 int current;
1515 struct module_list list;
1516 unsigned warn_if_uninitialized : 1;
1517
1518 /* update parameter passed via commandline */
1519 struct submodule_update_strategy update;
1520
1521 /* configuration parameters which are passed on to the children */
1522 int progress;
1523 int quiet;
1524 int recommend_shallow;
1525 struct string_list references;
1526 int dissociate;
1527 const char *depth;
1528 const char *recursive_prefix;
1529 const char *prefix;
1530
1531 /* to be consumed by git-submodule.sh */
1532 struct update_clone_data *update_clone;
1533 int update_clone_nr; int update_clone_alloc;
1534
1535 /* If we want to stop as fast as possible and return an error */
1536 unsigned quickstop : 1;
1537
1538 /* failed clones to be retried again */
1539 const struct cache_entry **failed_clones;
1540 int failed_clones_nr, failed_clones_alloc;
1541
1542 int max_jobs;
1543};
1544#define SUBMODULE_UPDATE_CLONE_INIT {0, MODULE_LIST_INIT, 0, \
1545 SUBMODULE_UPDATE_STRATEGY_INIT, 0, 0, -1, STRING_LIST_INIT_DUP, 0, \
1546 NULL, NULL, NULL, \
1547 NULL, 0, 0, 0, NULL, 0, 0, 0}
1548
1549
1550static void next_submodule_warn_missing(struct submodule_update_clone *suc,
1551 struct strbuf *out, const char *displaypath)
1552{
1553 /*
1554 * Only mention uninitialized submodules when their
1555 * paths have been specified.
1556 */
1557 if (suc->warn_if_uninitialized) {
1558 strbuf_addf(out,
1559 _("Submodule path '%s' not initialized"),
1560 displaypath);
1561 strbuf_addch(out, '\n');
1562 strbuf_addstr(out,
1563 _("Maybe you want to use 'update --init'?"));
1564 strbuf_addch(out, '\n');
1565 }
1566}
1567
1568/**
1569 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
1570 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
1571 */
1572static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
1573 struct child_process *child,
1574 struct submodule_update_clone *suc,
1575 struct strbuf *out)
1576{
1577 const struct submodule *sub = NULL;
1578 const char *url = NULL;
1579 const char *update_string;
1580 enum submodule_update_type update_type;
1581 char *key;
1582 struct strbuf displaypath_sb = STRBUF_INIT;
1583 struct strbuf sb = STRBUF_INIT;
1584 const char *displaypath = NULL;
1585 int needs_cloning = 0;
1586
1587 if (ce_stage(ce)) {
1588 if (suc->recursive_prefix)
1589 strbuf_addf(&sb, "%s/%s", suc->recursive_prefix, ce->name);
1590 else
1591 strbuf_addstr(&sb, ce->name);
1592 strbuf_addf(out, _("Skipping unmerged submodule %s"), sb.buf);
1593 strbuf_addch(out, '\n');
1594 goto cleanup;
1595 }
1596
1597 sub = submodule_from_path(the_repository, &null_oid, ce->name);
1598
1599 if (suc->recursive_prefix)
1600 displaypath = relative_path(suc->recursive_prefix,
1601 ce->name, &displaypath_sb);
1602 else
1603 displaypath = ce->name;
1604
1605 if (!sub) {
1606 next_submodule_warn_missing(suc, out, displaypath);
1607 goto cleanup;
1608 }
1609
1610 key = xstrfmt("submodule.%s.update", sub->name);
1611 if (!repo_config_get_string_const(the_repository, key, &update_string)) {
1612 update_type = parse_submodule_update_type(update_string);
1613 } else {
1614 update_type = sub->update_strategy.type;
1615 }
1616 free(key);
1617
1618 if (suc->update.type == SM_UPDATE_NONE
1619 || (suc->update.type == SM_UPDATE_UNSPECIFIED
1620 && update_type == SM_UPDATE_NONE)) {
1621 strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
1622 strbuf_addch(out, '\n');
1623 goto cleanup;
1624 }
1625
1626 /* Check if the submodule has been initialized. */
1627 if (!is_submodule_active(the_repository, ce->name)) {
1628 next_submodule_warn_missing(suc, out, displaypath);
1629 goto cleanup;
1630 }
1631
1632 strbuf_reset(&sb);
1633 strbuf_addf(&sb, "submodule.%s.url", sub->name);
1634 if (repo_config_get_string_const(the_repository, sb.buf, &url))
1635 url = sub->url;
1636
1637 strbuf_reset(&sb);
1638 strbuf_addf(&sb, "%s/.git", ce->name);
1639 needs_cloning = !file_exists(sb.buf);
1640
1641 ALLOC_GROW(suc->update_clone, suc->update_clone_nr + 1,
1642 suc->update_clone_alloc);
1643 oidcpy(&suc->update_clone[suc->update_clone_nr].oid, &ce->oid);
1644 suc->update_clone[suc->update_clone_nr].just_cloned = needs_cloning;
1645 suc->update_clone[suc->update_clone_nr].sub = sub;
1646 suc->update_clone_nr++;
1647
1648 if (!needs_cloning)
1649 goto cleanup;
1650
1651 child->git_cmd = 1;
1652 child->no_stdin = 1;
1653 child->stdout_to_stderr = 1;
1654 child->err = -1;
1655 argv_array_push(&child->args, "submodule--helper");
1656 argv_array_push(&child->args, "clone");
1657 if (suc->progress)
1658 argv_array_push(&child->args, "--progress");
1659 if (suc->quiet)
1660 argv_array_push(&child->args, "--quiet");
1661 if (suc->prefix)
1662 argv_array_pushl(&child->args, "--prefix", suc->prefix, NULL);
1663 if (suc->recommend_shallow && sub->recommend_shallow == 1)
1664 argv_array_push(&child->args, "--depth=1");
1665 argv_array_pushl(&child->args, "--path", sub->path, NULL);
1666 argv_array_pushl(&child->args, "--name", sub->name, NULL);
1667 argv_array_pushl(&child->args, "--url", url, NULL);
1668 if (suc->references.nr) {
1669 struct string_list_item *item;
1670 for_each_string_list_item(item, &suc->references)
1671 argv_array_pushl(&child->args, "--reference", item->string, NULL);
1672 }
1673 if (suc->dissociate)
1674 argv_array_push(&child->args, "--dissociate");
1675 if (suc->depth)
1676 argv_array_push(&child->args, suc->depth);
1677
1678cleanup:
1679 strbuf_reset(&displaypath_sb);
1680 strbuf_reset(&sb);
1681
1682 return needs_cloning;
1683}
1684
1685static int update_clone_get_next_task(struct child_process *child,
1686 struct strbuf *err,
1687 void *suc_cb,
1688 void **idx_task_cb)
1689{
1690 struct submodule_update_clone *suc = suc_cb;
1691 const struct cache_entry *ce;
1692 int index;
1693
1694 for (; suc->current < suc->list.nr; suc->current++) {
1695 ce = suc->list.entries[suc->current];
1696 if (prepare_to_clone_next_submodule(ce, child, suc, err)) {
1697 int *p = xmalloc(sizeof(*p));
1698 *p = suc->current;
1699 *idx_task_cb = p;
1700 suc->current++;
1701 return 1;
1702 }
1703 }
1704
1705 /*
1706 * The loop above tried cloning each submodule once, now try the
1707 * stragglers again, which we can imagine as an extension of the
1708 * entry list.
1709 */
1710 index = suc->current - suc->list.nr;
1711 if (index < suc->failed_clones_nr) {
1712 int *p;
1713 ce = suc->failed_clones[index];
1714 if (!prepare_to_clone_next_submodule(ce, child, suc, err)) {
1715 suc->current ++;
1716 strbuf_addstr(err, "BUG: submodule considered for "
1717 "cloning, doesn't need cloning "
1718 "any more?\n");
1719 return 0;
1720 }
1721 p = xmalloc(sizeof(*p));
1722 *p = suc->current;
1723 *idx_task_cb = p;
1724 suc->current ++;
1725 return 1;
1726 }
1727
1728 return 0;
1729}
1730
1731static int update_clone_start_failure(struct strbuf *err,
1732 void *suc_cb,
1733 void *idx_task_cb)
1734{
1735 struct submodule_update_clone *suc = suc_cb;
1736 suc->quickstop = 1;
1737 return 1;
1738}
1739
1740static int update_clone_task_finished(int result,
1741 struct strbuf *err,
1742 void *suc_cb,
1743 void *idx_task_cb)
1744{
1745 const struct cache_entry *ce;
1746 struct submodule_update_clone *suc = suc_cb;
1747
1748 int *idxP = idx_task_cb;
1749 int idx = *idxP;
1750 free(idxP);
1751
1752 if (!result)
1753 return 0;
1754
1755 if (idx < suc->list.nr) {
1756 ce = suc->list.entries[idx];
1757 strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"),
1758 ce->name);
1759 strbuf_addch(err, '\n');
1760 ALLOC_GROW(suc->failed_clones,
1761 suc->failed_clones_nr + 1,
1762 suc->failed_clones_alloc);
1763 suc->failed_clones[suc->failed_clones_nr++] = ce;
1764 return 0;
1765 } else {
1766 idx -= suc->list.nr;
1767 ce = suc->failed_clones[idx];
1768 strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"),
1769 ce->name);
1770 strbuf_addch(err, '\n');
1771 suc->quickstop = 1;
1772 return 1;
1773 }
1774
1775 return 0;
1776}
1777
1778static int git_update_clone_config(const char *var, const char *value,
1779 void *cb)
1780{
1781 int *max_jobs = cb;
1782 if (!strcmp(var, "submodule.fetchjobs"))
1783 *max_jobs = parse_submodule_fetchjobs(var, value);
1784 return 0;
1785}
1786
1787static void update_submodule(struct update_clone_data *ucd)
1788{
1789 fprintf(stdout, "dummy %s %d\t%s\n",
1790 oid_to_hex(&ucd->oid),
1791 ucd->just_cloned,
1792 ucd->sub->path);
1793}
1794
1795static int update_submodules(struct submodule_update_clone *suc)
1796{
1797 int i;
1798
1799 run_processes_parallel(suc->max_jobs,
1800 update_clone_get_next_task,
1801 update_clone_start_failure,
1802 update_clone_task_finished,
1803 suc);
1804
1805 /*
1806 * We saved the output and put it out all at once now.
1807 * That means:
1808 * - the listener does not have to interleave their (checkout)
1809 * work with our fetching. The writes involved in a
1810 * checkout involve more straightforward sequential I/O.
1811 * - the listener can avoid doing any work if fetching failed.
1812 */
1813 if (suc->quickstop)
1814 return 1;
1815
1816 for (i = 0; i < suc->update_clone_nr; i++)
1817 update_submodule(&suc->update_clone[i]);
1818
1819 return 0;
1820}
1821
1822static int update_clone(int argc, const char **argv, const char *prefix)
1823{
1824 const char *update = NULL;
1825 struct pathspec pathspec;
1826 struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
1827
1828 struct option module_update_clone_options[] = {
1829 OPT_STRING(0, "prefix", &prefix,
1830 N_("path"),
1831 N_("path into the working tree")),
1832 OPT_STRING(0, "recursive-prefix", &suc.recursive_prefix,
1833 N_("path"),
1834 N_("path into the working tree, across nested "
1835 "submodule boundaries")),
1836 OPT_STRING(0, "update", &update,
1837 N_("string"),
1838 N_("rebase, merge, checkout or none")),
1839 OPT_STRING_LIST(0, "reference", &suc.references, N_("repo"),
1840 N_("reference repository")),
1841 OPT_BOOL(0, "dissociate", &suc.dissociate,
1842 N_("use --reference only while cloning")),
1843 OPT_STRING(0, "depth", &suc.depth, "<depth>",
1844 N_("Create a shallow clone truncated to the "
1845 "specified number of revisions")),
1846 OPT_INTEGER('j', "jobs", &suc.max_jobs,
1847 N_("parallel jobs")),
1848 OPT_BOOL(0, "recommend-shallow", &suc.recommend_shallow,
1849 N_("whether the initial clone should follow the shallow recommendation")),
1850 OPT__QUIET(&suc.quiet, N_("don't print cloning progress")),
1851 OPT_BOOL(0, "progress", &suc.progress,
1852 N_("force cloning progress")),
1853 OPT_END()
1854 };
1855
1856 const char *const git_submodule_helper_usage[] = {
1857 N_("git submodule--helper update_clone [--prefix=<path>] [<path>...]"),
1858 NULL
1859 };
1860 suc.prefix = prefix;
1861
1862 update_clone_config_from_gitmodules(&suc.max_jobs);
1863 git_config(git_update_clone_config, &suc.max_jobs);
1864
1865 argc = parse_options(argc, argv, prefix, module_update_clone_options,
1866 git_submodule_helper_usage, 0);
1867
1868 if (update)
1869 if (parse_submodule_update_strategy(update, &suc.update) < 0)
1870 die(_("bad value for update parameter"));
1871
1872 if (module_list_compute(argc, argv, prefix, &pathspec, &suc.list) < 0)
1873 return 1;
1874
1875 if (pathspec.nr)
1876 suc.warn_if_uninitialized = 1;
1877
1878 return update_submodules(&suc);
1879}
1880
1881static int resolve_relative_path(int argc, const char **argv, const char *prefix)
1882{
1883 struct strbuf sb = STRBUF_INIT;
1884 if (argc != 3)
1885 die("submodule--helper relative-path takes exactly 2 arguments, got %d", argc);
1886
1887 printf("%s", relative_path(argv[1], argv[2], &sb));
1888 strbuf_release(&sb);
1889 return 0;
1890}
1891
1892static const char *remote_submodule_branch(const char *path)
1893{
1894 const struct submodule *sub;
1895 const char *branch = NULL;
1896 char *key;
1897
1898 sub = submodule_from_path(the_repository, &null_oid, path);
1899 if (!sub)
1900 return NULL;
1901
1902 key = xstrfmt("submodule.%s.branch", sub->name);
1903 if (repo_config_get_string_const(the_repository, key, &branch))
1904 branch = sub->branch;
1905 free(key);
1906
1907 if (!branch)
1908 return "master";
1909
1910 if (!strcmp(branch, ".")) {
1911 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
1912
1913 if (!refname)
1914 die(_("No such ref: %s"), "HEAD");
1915
1916 /* detached HEAD */
1917 if (!strcmp(refname, "HEAD"))
1918 die(_("Submodule (%s) branch configured to inherit "
1919 "branch from superproject, but the superproject "
1920 "is not on any branch"), sub->name);
1921
1922 if (!skip_prefix(refname, "refs/heads/", &refname))
1923 die(_("Expecting a full ref name, got %s"), refname);
1924 return refname;
1925 }
1926
1927 return branch;
1928}
1929
1930static int resolve_remote_submodule_branch(int argc, const char **argv,
1931 const char *prefix)
1932{
1933 const char *ret;
1934 struct strbuf sb = STRBUF_INIT;
1935 if (argc != 2)
1936 die("submodule--helper remote-branch takes exactly one arguments, got %d", argc);
1937
1938 ret = remote_submodule_branch(argv[1]);
1939 if (!ret)
1940 die("submodule %s doesn't exist", argv[1]);
1941
1942 printf("%s", ret);
1943 strbuf_release(&sb);
1944 return 0;
1945}
1946
1947static int push_check(int argc, const char **argv, const char *prefix)
1948{
1949 struct remote *remote;
1950 const char *superproject_head;
1951 char *head;
1952 int detached_head = 0;
1953 struct object_id head_oid;
1954
1955 if (argc < 3)
1956 die("submodule--helper push-check requires at least 2 arguments");
1957
1958 /*
1959 * superproject's resolved head ref.
1960 * if HEAD then the superproject is in a detached head state, otherwise
1961 * it will be the resolved head ref.
1962 */
1963 superproject_head = argv[1];
1964 argv++;
1965 argc--;
1966 /* Get the submodule's head ref and determine if it is detached */
1967 head = resolve_refdup("HEAD", 0, &head_oid, NULL);
1968 if (!head)
1969 die(_("Failed to resolve HEAD as a valid ref."));
1970 if (!strcmp(head, "HEAD"))
1971 detached_head = 1;
1972
1973 /*
1974 * The remote must be configured.
1975 * This is to avoid pushing to the exact same URL as the parent.
1976 */
1977 remote = pushremote_get(argv[1]);
1978 if (!remote || remote->origin == REMOTE_UNCONFIGURED)
1979 die("remote '%s' not configured", argv[1]);
1980
1981 /* Check the refspec */
1982 if (argc > 2) {
1983 int i;
1984 struct ref *local_refs = get_local_heads();
1985 struct refspec refspec = REFSPEC_INIT_PUSH;
1986
1987 refspec_appendn(&refspec, argv + 2, argc - 2);
1988
1989 for (i = 0; i < refspec.nr; i++) {
1990 const struct refspec_item *rs = &refspec.items[i];
1991
1992 if (rs->pattern || rs->matching)
1993 continue;
1994
1995 /* LHS must match a single ref */
1996 switch (count_refspec_match(rs->src, local_refs, NULL)) {
1997 case 1:
1998 break;
1999 case 0:
2000 /*
2001 * If LHS matches 'HEAD' then we need to ensure
2002 * that it matches the same named branch
2003 * checked out in the superproject.
2004 */
2005 if (!strcmp(rs->src, "HEAD")) {
2006 if (!detached_head &&
2007 !strcmp(head, superproject_head))
2008 break;
2009 die("HEAD does not match the named branch in the superproject");
2010 }
2011 /* fallthrough */
2012 default:
2013 die("src refspec '%s' must name a ref",
2014 rs->src);
2015 }
2016 }
2017 refspec_clear(&refspec);
2018 }
2019 free(head);
2020
2021 return 0;
2022}
2023
2024static int ensure_core_worktree(int argc, const char **argv, const char *prefix)
2025{
2026 const struct submodule *sub;
2027 const char *path;
2028 char *cw;
2029 struct repository subrepo;
2030
2031 if (argc != 2)
2032 BUG("submodule--helper connect-gitdir-workingtree <name> <path>");
2033
2034 path = argv[1];
2035
2036 sub = submodule_from_path(the_repository, &null_oid, path);
2037 if (!sub)
2038 BUG("We could get the submodule handle before?");
2039
2040 if (repo_submodule_init(&subrepo, the_repository, path))
2041 die(_("could not get a repository handle for submodule '%s'"), path);
2042
2043 if (!repo_config_get_string(&subrepo, "core.worktree", &cw)) {
2044 char *cfg_file, *abs_path;
2045 const char *rel_path;
2046 struct strbuf sb = STRBUF_INIT;
2047
2048 cfg_file = repo_git_path(&subrepo, "config");
2049
2050 abs_path = absolute_pathdup(path);
2051 rel_path = relative_path(abs_path, subrepo.gitdir, &sb);
2052
2053 git_config_set_in_file(cfg_file, "core.worktree", rel_path);
2054
2055 free(cfg_file);
2056 free(abs_path);
2057 strbuf_release(&sb);
2058 }
2059
2060 return 0;
2061}
2062
2063static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
2064{
2065 int i;
2066 struct pathspec pathspec;
2067 struct module_list list = MODULE_LIST_INIT;
2068 unsigned flags = ABSORB_GITDIR_RECURSE_SUBMODULES;
2069
2070 struct option embed_gitdir_options[] = {
2071 OPT_STRING(0, "prefix", &prefix,
2072 N_("path"),
2073 N_("path into the working tree")),
2074 OPT_BIT(0, "--recursive", &flags, N_("recurse into submodules"),
2075 ABSORB_GITDIR_RECURSE_SUBMODULES),
2076 OPT_END()
2077 };
2078
2079 const char *const git_submodule_helper_usage[] = {
2080 N_("git submodule--helper embed-git-dir [<path>...]"),
2081 NULL
2082 };
2083
2084 argc = parse_options(argc, argv, prefix, embed_gitdir_options,
2085 git_submodule_helper_usage, 0);
2086
2087 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
2088 return 1;
2089
2090 for (i = 0; i < list.nr; i++)
2091 absorb_git_dir_into_superproject(prefix,
2092 list.entries[i]->name, flags);
2093
2094 return 0;
2095}
2096
2097static int is_active(int argc, const char **argv, const char *prefix)
2098{
2099 if (argc != 2)
2100 die("submodule--helper is-active takes exactly 1 argument");
2101
2102 return !is_submodule_active(the_repository, argv[1]);
2103}
2104
2105/*
2106 * Exit non-zero if any of the submodule names given on the command line is
2107 * invalid. If no names are given, filter stdin to print only valid names
2108 * (which is primarily intended for testing).
2109 */
2110static int check_name(int argc, const char **argv, const char *prefix)
2111{
2112 if (argc > 1) {
2113 while (*++argv) {
2114 if (check_submodule_name(*argv) < 0)
2115 return 1;
2116 }
2117 } else {
2118 struct strbuf buf = STRBUF_INIT;
2119 while (strbuf_getline(&buf, stdin) != EOF) {
2120 if (!check_submodule_name(buf.buf))
2121 printf("%s\n", buf.buf);
2122 }
2123 strbuf_release(&buf);
2124 }
2125 return 0;
2126}
2127
2128#define SUPPORT_SUPER_PREFIX (1<<0)
2129
2130struct cmd_struct {
2131 const char *cmd;
2132 int (*fn)(int, const char **, const char *);
2133 unsigned option;
2134};
2135
2136static struct cmd_struct commands[] = {
2137 {"list", module_list, 0},
2138 {"name", module_name, 0},
2139 {"clone", module_clone, 0},
2140 {"update-module-mode", module_update_module_mode, 0},
2141 {"update-clone", update_clone, 0},
2142 {"ensure-core-worktree", ensure_core_worktree, 0},
2143 {"relative-path", resolve_relative_path, 0},
2144 {"resolve-relative-url", resolve_relative_url, 0},
2145 {"resolve-relative-url-test", resolve_relative_url_test, 0},
2146 {"foreach", module_foreach, SUPPORT_SUPER_PREFIX},
2147 {"init", module_init, SUPPORT_SUPER_PREFIX},
2148 {"status", module_status, SUPPORT_SUPER_PREFIX},
2149 {"print-default-remote", print_default_remote, 0},
2150 {"sync", module_sync, SUPPORT_SUPER_PREFIX},
2151 {"deinit", module_deinit, 0},
2152 {"remote-branch", resolve_remote_submodule_branch, 0},
2153 {"push-check", push_check, 0},
2154 {"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
2155 {"is-active", is_active, 0},
2156 {"check-name", check_name, 0},
2157};
2158
2159int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
2160{
2161 int i;
2162 if (argc < 2 || !strcmp(argv[1], "-h"))
2163 usage("git submodule--helper <command>");
2164
2165 for (i = 0; i < ARRAY_SIZE(commands); i++) {
2166 if (!strcmp(argv[1], commands[i].cmd)) {
2167 if (get_super_prefix() &&
2168 !(commands[i].option & SUPPORT_SUPER_PREFIX))
2169 die(_("%s doesn't support --super-prefix"),
2170 commands[i].cmd);
2171 return commands[i].fn(argc - 1, argv + 1, prefix);
2172 }
2173 }
2174
2175 die(_("'%s' is not a valid submodule--helper "
2176 "subcommand"), argv[1]);
2177}