]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/submodule--helper.c
builtin/submodule--helper: store update_clone information in a struct
[thirdparty/git.git] / builtin / submodule--helper.c
CommitLineData
74703a1e 1#include "builtin.h"
627d9342 2#include "repository.h"
74703a1e 3#include "cache.h"
b2141fc1 4#include "config.h"
74703a1e
SB
5#include "parse-options.h"
6#include "quote.h"
7#include "pathspec.h"
8#include "dir.h"
0ea306ef
SB
9#include "submodule.h"
10#include "submodule-config.h"
11#include "string-list.h"
ee8838d1 12#include "run-command.h"
63e95beb
SB
13#include "remote.h"
14#include "refs.h"
ec0cb496 15#include "refspec.h"
63e95beb 16#include "connect.h"
a9f8a375
PC
17#include "revision.h"
18#include "diffcore.h"
19#include "diff.h"
0d4a1321 20#include "object-store.h"
63e95beb 21
9f580a62 22#define OPT_QUIET (1 << 0)
a9f8a375
PC
23#define OPT_CACHED (1 << 1)
24#define OPT_RECURSIVE (1 << 2)
2e612731 25#define OPT_FORCE (1 << 3)
9f580a62
PC
26
27typedef void (*each_submodule_fn)(const struct cache_entry *list_item,
28 void *cb_data);
63e95beb
SB
29
30static char *get_default_remote(void)
31{
32 char *dest = NULL, *ret;
63e95beb 33 struct strbuf sb = STRBUF_INIT;
744c040b 34 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
63e95beb
SB
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
13424764
PC
56static int print_default_remote(int argc, const char **argv, const char *prefix)
57{
e6be8e2f 58 char *remote;
13424764
PC
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
e6be8e2f 67 free(remote);
13424764
PC
68 return 0;
69}
70
63e95beb
SB
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
08788504
SB
126 * http://a.com/b/ ../c http://a.com/c same as previous line, but
127 * ignore trailing slash in url
63e95beb
SB
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
08788504
SB
146 if (is_dir_sep(remoteurl[len-1]))
147 remoteurl[len-1] = '\0';
63e95beb
SB
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);
3389e78e
SB
180 if (ends_with(url, "/"))
181 strbuf_setlen(&sb, sb.len - 1);
63e95beb
SB
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}
74703a1e 249
74a10642
PC
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
a9f8a375
PC
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
74703a1e
SB
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;
2b56bb7a 320 char *ps_matched = NULL;
74703a1e 321 parse_pathspec(pathspec, 0,
2249d4db 322 PATHSPEC_PREFER_FULL,
74703a1e
SB
323 prefix, argv);
324
74703a1e
SB
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
84ba959b
SB
334 if (!match_pathspec(pathspec, ce->name, ce_namelen(ce),
335 0, ps_matched, 1) ||
336 !S_ISGITLINK(ce->ce_mode))
74703a1e
SB
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 }
74703a1e
SB
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
3e7eaed0
BW
358static void module_list_active(struct module_list *list)
359{
360 int i;
361 struct module_list active_modules = MODULE_LIST_INIT;
362
3e7eaed0
BW
363 for (i = 0; i < list->nr; i++) {
364 const struct cache_entry *ce = list->entries[i];
365
627d9342 366 if (!is_submodule_active(the_repository, ce->name))
3e7eaed0
BW
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
13424764
PC
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
74703a1e
SB
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
b0f4b408 419 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
74703a1e 420 return 1;
74703a1e
SB
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
99d1a986 428 printf("%06o %s %d\t", ce->ce_mode,
429 oid_to_hex(&ce->oid), ce_stage(ce));
74703a1e 430
dc4b4a61 431 fprintf(stdout, "%s\n", ce->name);
74703a1e
SB
432 }
433 return 0;
434}
435
9f580a62
PC
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
fc1b9243
PC
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
9f580a62
PC
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)
3604242f
SB
596{
597 const struct submodule *sub;
598 struct strbuf sb = STRBUF_INIT;
599 char *upd = NULL, *url = NULL, *displaypath;
600
74a10642 601 displaypath = get_submodule_displaypath(path, prefix);
d92028a5 602
3b8fb393 603 sub = submodule_from_path(the_repository, &null_oid, path);
d92028a5
SB
604
605 if (!sub)
606 die(_("No url found for submodule path '%s' in .gitmodules"),
607 displaypath);
3604242f 608
1f8d7115
BW
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 */
627d9342 615 if (!is_submodule_active(the_repository, path)) {
1f8d7115
BW
616 strbuf_addf(&sb, "submodule.%s.active", sub->name);
617 git_config_set_gently(sb.buf, "true");
74a10642 618 strbuf_reset(&sb);
1f8d7115
BW
619 }
620
3604242f
SB
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 */
3604242f
SB
626 strbuf_addf(&sb, "submodule.%s.url", sub->name);
627 if (git_config_get_string(sb.buf, &url)) {
627fde10 628 if (!sub->url)
3604242f
SB
629 die(_("No url found for submodule path '%s' in .gitmodules"),
630 displaypath);
631
627fde10
JK
632 url = xstrdup(sub->url);
633
3604242f
SB
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
d1b3b81a
SB
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);
3604242f 645 remoteurl = xgetcwd();
d1b3b81a 646 }
3604242f
SB
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);
9f580a62 657 if (!(flags & OPT_QUIET))
c66410ed
SB
658 fprintf(stderr,
659 _("Submodule '%s' (%s) registered for path '%s'\n"),
3604242f
SB
660 sub->name, url, displaypath);
661 }
74a10642 662 strbuf_reset(&sb);
3604242f
SB
663
664 /* Copy "update" setting when it is not set yet */
3604242f
SB
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
9f580a62
PC
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
3604242f
SB
690static int module_init(int argc, const char **argv, const char *prefix)
691{
9f580a62 692 struct init_cb info = INIT_CB_INIT;
3604242f
SB
693 struct pathspec pathspec;
694 struct module_list list = MODULE_LIST_INIT;
695 int quiet = 0;
3604242f
SB
696
697 struct option module_init_options[] = {
3604242f
SB
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
3e7eaed0
BW
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
9f580a62
PC
720 info.prefix = prefix;
721 if (quiet)
722 info.flags |= OPT_QUIET;
723
724 for_each_listed_submodule(&list, init_submodule_cb, &info);
3604242f
SB
725
726 return 0;
727}
728
a9f8a375
PC
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
0b5e2ea7
NTND
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 }
a9f8a375
PC
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
3b8fb393 774 if (!submodule_from_path(the_repository, &null_oid, path))
a9f8a375
PC
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;
74b6bda3 807 struct ref_store *refs = get_submodule_ref_store(path);
a9f8a375 808
74b6bda3
RS
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))
ed5bdd5b 814 die(_("could not resolve HEAD ref inside the "
a9f8a375
PC
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);
3604242f
SB
887
888 return 0;
889}
890
0ea306ef
SB
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
3b8fb393 898 sub = submodule_from_path(the_repository, &null_oid, argv[1]);
0ea306ef
SB
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}
14111fc4 908
13424764
PC
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
3b8fb393 929 sub = submodule_from_path(the_repository, &null_oid, path);
13424764
PC
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}
1029
1030static int module_sync(int argc, const char **argv, const char *prefix)
1031{
1032 struct sync_cb info = SYNC_CB_INIT;
1033 struct pathspec pathspec;
1034 struct module_list list = MODULE_LIST_INIT;
1035 int quiet = 0;
1036 int recursive = 0;
1037
1038 struct option module_sync_options[] = {
1039 OPT__QUIET(&quiet, N_("Suppress output of synchronizing submodule url")),
1040 OPT_BOOL(0, "recursive", &recursive,
1041 N_("Recurse into nested submodules")),
1042 OPT_END()
1043 };
1044
1045 const char *const git_submodule_helper_usage[] = {
1046 N_("git submodule--helper sync [--quiet] [--recursive] [<path>]"),
1047 NULL
1048 };
1049
1050 argc = parse_options(argc, argv, prefix, module_sync_options,
1051 git_submodule_helper_usage, 0);
1052
1053 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1054 return 1;
1055
1056 info.prefix = prefix;
1057 if (quiet)
1058 info.flags |= OPT_QUIET;
1059 if (recursive)
1060 info.flags |= OPT_RECURSIVE;
1061
1062 for_each_listed_submodule(&list, sync_submodule_cb, &info);
1063
1064 return 0;
1065}
1066
2e612731
PC
1067struct deinit_cb {
1068 const char *prefix;
1069 unsigned int flags;
1070};
1071#define DEINIT_CB_INIT { NULL, 0 }
1072
1073static void deinit_submodule(const char *path, const char *prefix,
1074 unsigned int flags)
1075{
1076 const struct submodule *sub;
1077 char *displaypath = NULL;
1078 struct child_process cp_config = CHILD_PROCESS_INIT;
1079 struct strbuf sb_config = STRBUF_INIT;
1080 char *sub_git_dir = xstrfmt("%s/.git", path);
1081
3b8fb393 1082 sub = submodule_from_path(the_repository, &null_oid, path);
2e612731
PC
1083
1084 if (!sub || !sub->name)
1085 goto cleanup;
1086
1087 displaypath = get_submodule_displaypath(path, prefix);
1088
1089 /* remove the submodule work tree (unless the user already did it) */
1090 if (is_directory(path)) {
1091 struct strbuf sb_rm = STRBUF_INIT;
1092 const char *format;
1093
1094 /*
1095 * protect submodules containing a .git directory
1096 * NEEDSWORK: instead of dying, automatically call
1097 * absorbgitdirs and (possibly) warn.
1098 */
1099 if (is_directory(sub_git_dir))
1100 die(_("Submodule work tree '%s' contains a .git "
1101 "directory (use 'rm -rf' if you really want "
1102 "to remove it including all of its history)"),
1103 displaypath);
1104
1105 if (!(flags & OPT_FORCE)) {
1106 struct child_process cp_rm = CHILD_PROCESS_INIT;
1107 cp_rm.git_cmd = 1;
1108 argv_array_pushl(&cp_rm.args, "rm", "-qn",
1109 path, NULL);
1110
1111 if (run_command(&cp_rm))
1112 die(_("Submodule work tree '%s' contains local "
1113 "modifications; use '-f' to discard them"),
1114 displaypath);
1115 }
1116
1117 strbuf_addstr(&sb_rm, path);
1118
1119 if (!remove_dir_recursively(&sb_rm, 0))
1120 format = _("Cleared directory '%s'\n");
1121 else
1122 format = _("Could not remove submodule work tree '%s'\n");
1123
1124 if (!(flags & OPT_QUIET))
1125 printf(format, displaypath);
1126
984cd77d
SB
1127 submodule_unset_core_worktree(sub);
1128
2e612731
PC
1129 strbuf_release(&sb_rm);
1130 }
1131
1132 if (mkdir(path, 0777))
1133 printf(_("could not create empty submodule directory %s"),
1134 displaypath);
1135
1136 cp_config.git_cmd = 1;
1137 argv_array_pushl(&cp_config.args, "config", "--get-regexp", NULL);
1138 argv_array_pushf(&cp_config.args, "submodule.%s\\.", sub->name);
1139
1140 /* remove the .git/config entries (unless the user already did it) */
1141 if (!capture_command(&cp_config, &sb_config, 0) && sb_config.len) {
1142 char *sub_key = xstrfmt("submodule.%s", sub->name);
1143 /*
1144 * remove the whole section so we have a clean state when
1145 * the user later decides to init this submodule again
1146 */
1147 git_config_rename_section_in_file(NULL, sub_key, NULL);
1148 if (!(flags & OPT_QUIET))
1149 printf(_("Submodule '%s' (%s) unregistered for path '%s'\n"),
1150 sub->name, sub->url, displaypath);
1151 free(sub_key);
1152 }
1153
1154cleanup:
1155 free(displaypath);
1156 free(sub_git_dir);
1157 strbuf_release(&sb_config);
1158}
1159
1160static void deinit_submodule_cb(const struct cache_entry *list_item,
1161 void *cb_data)
1162{
1163 struct deinit_cb *info = cb_data;
1164 deinit_submodule(list_item->name, info->prefix, info->flags);
1165}
1166
1167static int module_deinit(int argc, const char **argv, const char *prefix)
1168{
1169 struct deinit_cb info = DEINIT_CB_INIT;
1170 struct pathspec pathspec;
1171 struct module_list list = MODULE_LIST_INIT;
1172 int quiet = 0;
1173 int force = 0;
1174 int all = 0;
1175
1176 struct option module_deinit_options[] = {
1177 OPT__QUIET(&quiet, N_("Suppress submodule status output")),
7fb6aefd 1178 OPT__FORCE(&force, N_("Remove submodule working trees even if they contain local changes"), 0),
2e612731
PC
1179 OPT_BOOL(0, "all", &all, N_("Unregister all submodules")),
1180 OPT_END()
1181 };
1182
1183 const char *const git_submodule_helper_usage[] = {
1184 N_("git submodule deinit [--quiet] [-f | --force] [--all | [--] [<path>...]]"),
1185 NULL
1186 };
1187
1188 argc = parse_options(argc, argv, prefix, module_deinit_options,
1189 git_submodule_helper_usage, 0);
1190
1191 if (all && argc) {
1192 error("pathspec and --all are incompatible");
1193 usage_with_options(git_submodule_helper_usage,
1194 module_deinit_options);
1195 }
1196
1197 if (!argc && !all)
1198 die(_("Use '--all' if you really want to deinitialize all submodules"));
1199
1200 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
9748e39d 1201 return 1;
2e612731
PC
1202
1203 info.prefix = prefix;
1204 if (quiet)
1205 info.flags |= OPT_QUIET;
1206 if (force)
1207 info.flags |= OPT_FORCE;
1208
1209 for_each_listed_submodule(&list, deinit_submodule_cb, &info);
1210
1211 return 0;
1212}
1213
ee8838d1 1214static int clone_submodule(const char *path, const char *gitdir, const char *url,
a0ef2934 1215 const char *depth, struct string_list *reference, int dissociate,
72c5f883 1216 int quiet, int progress)
ee8838d1 1217{
542aa25d 1218 struct child_process cp = CHILD_PROCESS_INIT;
ee8838d1
SB
1219
1220 argv_array_push(&cp.args, "clone");
1221 argv_array_push(&cp.args, "--no-checkout");
1222 if (quiet)
1223 argv_array_push(&cp.args, "--quiet");
72c5f883
JK
1224 if (progress)
1225 argv_array_push(&cp.args, "--progress");
ee8838d1
SB
1226 if (depth && *depth)
1227 argv_array_pushl(&cp.args, "--depth", depth, NULL);
965dbea0
SB
1228 if (reference->nr) {
1229 struct string_list_item *item;
1230 for_each_string_list_item(item, reference)
1231 argv_array_pushl(&cp.args, "--reference",
1232 item->string, NULL);
1233 }
a0ef2934
CF
1234 if (dissociate)
1235 argv_array_push(&cp.args, "--dissociate");
ee8838d1
SB
1236 if (gitdir && *gitdir)
1237 argv_array_pushl(&cp.args, "--separate-git-dir", gitdir, NULL);
1238
1239 argv_array_push(&cp.args, url);
1240 argv_array_push(&cp.args, path);
1241
1242 cp.git_cmd = 1;
14111fc4 1243 prepare_submodule_repo_env(&cp.env_array);
ee8838d1
SB
1244 cp.no_stdin = 1;
1245
1246 return run_command(&cp);
1247}
1248
31224cbd
SB
1249struct submodule_alternate_setup {
1250 const char *submodule_name;
1251 enum SUBMODULE_ALTERNATE_ERROR_MODE {
1252 SUBMODULE_ALTERNATE_ERROR_DIE,
1253 SUBMODULE_ALTERNATE_ERROR_INFO,
1254 SUBMODULE_ALTERNATE_ERROR_IGNORE
1255 } error_mode;
1256 struct string_list *reference;
1257};
1258#define SUBMODULE_ALTERNATE_SETUP_INIT { NULL, \
1259 SUBMODULE_ALTERNATE_ERROR_IGNORE, NULL }
1260
1261static int add_possible_reference_from_superproject(
1262 struct alternate_object_database *alt, void *sas_cb)
1263{
1264 struct submodule_alternate_setup *sas = sas_cb;
1265
31224cbd
SB
1266 /*
1267 * If the alternate object store is another repository, try the
bf03b790 1268 * standard layout with .git/(modules/<name>)+/objects
31224cbd 1269 */
bf03b790 1270 if (ends_with(alt->path, "/objects")) {
31224cbd
SB
1271 char *sm_alternate;
1272 struct strbuf sb = STRBUF_INIT;
1273 struct strbuf err = STRBUF_INIT;
597f9134
JK
1274 strbuf_add(&sb, alt->path, strlen(alt->path) - strlen("objects"));
1275
31224cbd
SB
1276 /*
1277 * We need to end the new path with '/' to mark it as a dir,
1278 * otherwise a submodule name containing '/' will be broken
1279 * as the last part of a missing submodule reference would
1280 * be taken as a file name.
1281 */
1282 strbuf_addf(&sb, "modules/%s/", sas->submodule_name);
1283
1284 sm_alternate = compute_alternate_path(sb.buf, &err);
1285 if (sm_alternate) {
1286 string_list_append(sas->reference, xstrdup(sb.buf));
1287 free(sm_alternate);
1288 } else {
1289 switch (sas->error_mode) {
1290 case SUBMODULE_ALTERNATE_ERROR_DIE:
1291 die(_("submodule '%s' cannot add alternate: %s"),
1292 sas->submodule_name, err.buf);
1293 case SUBMODULE_ALTERNATE_ERROR_INFO:
1294 fprintf(stderr, _("submodule '%s' cannot add alternate: %s"),
1295 sas->submodule_name, err.buf);
1296 case SUBMODULE_ALTERNATE_ERROR_IGNORE:
1297 ; /* nothing */
1298 }
1299 }
1300 strbuf_release(&sb);
1301 }
1302
31224cbd
SB
1303 return 0;
1304}
1305
1306static void prepare_possible_alternates(const char *sm_name,
1307 struct string_list *reference)
1308{
1309 char *sm_alternate = NULL, *error_strategy = NULL;
1310 struct submodule_alternate_setup sas = SUBMODULE_ALTERNATE_SETUP_INIT;
1311
1312 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1313 if (!sm_alternate)
1314 return;
1315
1316 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1317
1318 if (!error_strategy)
1319 error_strategy = xstrdup("die");
1320
1321 sas.submodule_name = sm_name;
1322 sas.reference = reference;
1323 if (!strcmp(error_strategy, "die"))
1324 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_DIE;
1325 else if (!strcmp(error_strategy, "info"))
1326 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_INFO;
1327 else if (!strcmp(error_strategy, "ignore"))
1328 sas.error_mode = SUBMODULE_ALTERNATE_ERROR_IGNORE;
1329 else
1330 die(_("Value '%s' for submodule.alternateErrorStrategy is not recognized"), error_strategy);
1331
1332 if (!strcmp(sm_alternate, "superproject"))
1333 foreach_alt_odb(add_possible_reference_from_superproject, &sas);
1334 else if (!strcmp(sm_alternate, "no"))
1335 ; /* do nothing */
1336 else
1337 die(_("Value '%s' for submodule.alternateLocation is not recognized"), sm_alternate);
1338
1339 free(sm_alternate);
1340 free(error_strategy);
1341}
1342
ee8838d1
SB
1343static int module_clone(int argc, const char **argv, const char *prefix)
1344{
965dbea0 1345 const char *name = NULL, *url = NULL, *depth = NULL;
ee8838d1 1346 int quiet = 0;
72c5f883 1347 int progress = 0;
1ea4d9b7 1348 char *p, *path = NULL, *sm_gitdir;
ee8838d1 1349 struct strbuf sb = STRBUF_INIT;
965dbea0 1350 struct string_list reference = STRING_LIST_INIT_NODUP;
a0ef2934 1351 int dissociate = 0;
bf03b790 1352 char *sm_alternate = NULL, *error_strategy = NULL;
ee8838d1
SB
1353
1354 struct option module_clone_options[] = {
1355 OPT_STRING(0, "prefix", &prefix,
1356 N_("path"),
1357 N_("alternative anchor for relative paths")),
1358 OPT_STRING(0, "path", &path,
1359 N_("path"),
1360 N_("where the new submodule will be cloned to")),
1361 OPT_STRING(0, "name", &name,
1362 N_("string"),
1363 N_("name of the new submodule")),
1364 OPT_STRING(0, "url", &url,
1365 N_("string"),
1366 N_("url where to clone the submodule from")),
965dbea0
SB
1367 OPT_STRING_LIST(0, "reference", &reference,
1368 N_("repo"),
ee8838d1 1369 N_("reference repository")),
a0ef2934
CF
1370 OPT_BOOL(0, "dissociate", &dissociate,
1371 N_("use --reference only while cloning")),
ee8838d1
SB
1372 OPT_STRING(0, "depth", &depth,
1373 N_("string"),
1374 N_("depth for shallow clones")),
1375 OPT__QUIET(&quiet, "Suppress output for cloning a submodule"),
72c5f883
JK
1376 OPT_BOOL(0, "progress", &progress,
1377 N_("force cloning progress")),
ee8838d1
SB
1378 OPT_END()
1379 };
1380
1381 const char *const git_submodule_helper_usage[] = {
1382 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
7dad2633
JK
1383 "[--reference <repository>] [--name <name>] [--depth <depth>] "
1384 "--url <url> --path <path>"),
ee8838d1
SB
1385 NULL
1386 };
1387
1388 argc = parse_options(argc, argv, prefix, module_clone_options,
1389 git_submodule_helper_usage, 0);
1390
deef3cdc 1391 if (argc || !url || !path || !*path)
08e0970a
JK
1392 usage_with_options(git_submodule_helper_usage,
1393 module_clone_options);
1394
ee8838d1 1395 strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
0aaad415 1396 sm_gitdir = absolute_pathdup(sb.buf);
1ea4d9b7 1397 strbuf_reset(&sb);
f8eaa0ba
SB
1398
1399 if (!is_absolute_path(path)) {
1400 strbuf_addf(&sb, "%s/%s", get_git_work_tree(), path);
1401 path = strbuf_detach(&sb, NULL);
1402 } else
1403 path = xstrdup(path);
ee8838d1
SB
1404
1405 if (!file_exists(sm_gitdir)) {
1406 if (safe_create_leading_directories_const(sm_gitdir) < 0)
1407 die(_("could not create directory '%s'"), sm_gitdir);
31224cbd
SB
1408
1409 prepare_possible_alternates(name, &reference);
1410
a0ef2934 1411 if (clone_submodule(path, sm_gitdir, url, depth, &reference, dissociate,
72c5f883 1412 quiet, progress))
ee8838d1
SB
1413 die(_("clone of '%s' into submodule path '%s' failed"),
1414 url, path);
1415 } else {
1416 if (safe_create_leading_directories_const(path) < 0)
1417 die(_("could not create directory '%s'"), path);
1418 strbuf_addf(&sb, "%s/index", sm_gitdir);
1419 unlink_or_warn(sb.buf);
1420 strbuf_reset(&sb);
1421 }
1422
da62f786 1423 connect_work_tree_and_git_dir(path, sm_gitdir, 0);
ee8838d1 1424
ee8838d1
SB
1425 p = git_pathdup_submodule(path, "config");
1426 if (!p)
1427 die(_("could not get submodule directory for '%s'"), path);
bf03b790
VS
1428
1429 /* setup alternateLocation and alternateErrorStrategy in the cloned submodule if needed */
1430 git_config_get_string("submodule.alternateLocation", &sm_alternate);
1431 if (sm_alternate)
1432 git_config_set_in_file(p, "submodule.alternateLocation",
1433 sm_alternate);
1434 git_config_get_string("submodule.alternateErrorStrategy", &error_strategy);
1435 if (error_strategy)
1436 git_config_set_in_file(p, "submodule.alternateErrorStrategy",
1437 error_strategy);
1438
1439 free(sm_alternate);
1440 free(error_strategy);
1441
ee8838d1 1442 strbuf_release(&sb);
ee8838d1 1443 free(sm_gitdir);
f8eaa0ba 1444 free(path);
ee8838d1
SB
1445 free(p);
1446 return 0;
1447}
74703a1e 1448
f1d15713
SB
1449struct update_clone_data {
1450 const struct submodule *sub;
1451 struct object_id oid;
1452 unsigned just_cloned;
1453};
1454
48308681
SB
1455struct submodule_update_clone {
1456 /* index into 'list', the list of submodules to look into for cloning */
1457 int current;
1458 struct module_list list;
1459 unsigned warn_if_uninitialized : 1;
1460
1461 /* update parameter passed via commandline */
1462 struct submodule_update_strategy update;
1463
1464 /* configuration parameters which are passed on to the children */
72c5f883 1465 int progress;
48308681 1466 int quiet;
abed000a 1467 int recommend_shallow;
5f50f33e 1468 struct string_list references;
a0ef2934 1469 int dissociate;
48308681
SB
1470 const char *depth;
1471 const char *recursive_prefix;
1472 const char *prefix;
1473
f1d15713
SB
1474 /* to be consumed by git-submodule.sh */
1475 struct update_clone_data *update_clone;
1476 int update_clone_nr; int update_clone_alloc;
48308681
SB
1477
1478 /* If we want to stop as fast as possible and return an error */
1479 unsigned quickstop : 1;
665b35ec
SB
1480
1481 /* failed clones to be retried again */
1482 const struct cache_entry **failed_clones;
1483 int failed_clones_nr, failed_clones_alloc;
90efe595
SB
1484
1485 int max_jobs;
48308681
SB
1486};
1487#define SUBMODULE_UPDATE_CLONE_INIT {0, MODULE_LIST_INIT, 0, \
a0ef2934 1488 SUBMODULE_UPDATE_STRATEGY_INIT, 0, 0, -1, STRING_LIST_INIT_DUP, 0, \
5f50f33e 1489 NULL, NULL, NULL, \
f1d15713 1490 NULL, 0, 0, 0, NULL, 0, 0, 0}
48308681 1491
08fdbdb1
SB
1492
1493static void next_submodule_warn_missing(struct submodule_update_clone *suc,
1494 struct strbuf *out, const char *displaypath)
1495{
1496 /*
1497 * Only mention uninitialized submodules when their
1498 * paths have been specified.
1499 */
1500 if (suc->warn_if_uninitialized) {
1501 strbuf_addf(out,
1502 _("Submodule path '%s' not initialized"),
1503 displaypath);
1504 strbuf_addch(out, '\n');
1505 strbuf_addstr(out,
1506 _("Maybe you want to use 'update --init'?"));
1507 strbuf_addch(out, '\n');
1508 }
1509}
1510
48308681
SB
1511/**
1512 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
1513 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
1514 */
1515static int prepare_to_clone_next_submodule(const struct cache_entry *ce,
1516 struct child_process *child,
1517 struct submodule_update_clone *suc,
1518 struct strbuf *out)
1519{
1520 const struct submodule *sub = NULL;
ec6141a0
BW
1521 const char *url = NULL;
1522 const char *update_string;
1523 enum submodule_update_type update_type;
1524 char *key;
48308681
SB
1525 struct strbuf displaypath_sb = STRBUF_INIT;
1526 struct strbuf sb = STRBUF_INIT;
1527 const char *displaypath = NULL;
48308681
SB
1528 int needs_cloning = 0;
1529
1530 if (ce_stage(ce)) {
1531 if (suc->recursive_prefix)
1532 strbuf_addf(&sb, "%s/%s", suc->recursive_prefix, ce->name);
1533 else
92d52fab 1534 strbuf_addstr(&sb, ce->name);
48308681
SB
1535 strbuf_addf(out, _("Skipping unmerged submodule %s"), sb.buf);
1536 strbuf_addch(out, '\n');
1537 goto cleanup;
1538 }
1539
3b8fb393 1540 sub = submodule_from_path(the_repository, &null_oid, ce->name);
48308681
SB
1541
1542 if (suc->recursive_prefix)
1543 displaypath = relative_path(suc->recursive_prefix,
1544 ce->name, &displaypath_sb);
1545 else
1546 displaypath = ce->name;
1547
08fdbdb1
SB
1548 if (!sub) {
1549 next_submodule_warn_missing(suc, out, displaypath);
1550 goto cleanup;
1551 }
1552
ec6141a0
BW
1553 key = xstrfmt("submodule.%s.update", sub->name);
1554 if (!repo_config_get_string_const(the_repository, key, &update_string)) {
1555 update_type = parse_submodule_update_type(update_string);
1556 } else {
1557 update_type = sub->update_strategy.type;
1558 }
1559 free(key);
1560
48308681
SB
1561 if (suc->update.type == SM_UPDATE_NONE
1562 || (suc->update.type == SM_UPDATE_UNSPECIFIED
ec6141a0 1563 && update_type == SM_UPDATE_NONE)) {
48308681
SB
1564 strbuf_addf(out, _("Skipping submodule '%s'"), displaypath);
1565 strbuf_addch(out, '\n');
1566 goto cleanup;
1567 }
1568
ee92ab99 1569 /* Check if the submodule has been initialized. */
627d9342 1570 if (!is_submodule_active(the_repository, ce->name)) {
08fdbdb1 1571 next_submodule_warn_missing(suc, out, displaypath);
48308681
SB
1572 goto cleanup;
1573 }
1574
ec6141a0
BW
1575 strbuf_reset(&sb);
1576 strbuf_addf(&sb, "submodule.%s.url", sub->name);
1577 if (repo_config_get_string_const(the_repository, sb.buf, &url))
1578 url = sub->url;
1579
48308681
SB
1580 strbuf_reset(&sb);
1581 strbuf_addf(&sb, "%s/.git", ce->name);
1582 needs_cloning = !file_exists(sb.buf);
1583
f1d15713
SB
1584 ALLOC_GROW(suc->update_clone, suc->update_clone_nr + 1,
1585 suc->update_clone_alloc);
1586 oidcpy(&suc->update_clone[suc->update_clone_nr].oid, &ce->oid);
1587 suc->update_clone[suc->update_clone_nr].just_cloned = needs_cloning;
1588 suc->update_clone[suc->update_clone_nr].sub = sub;
1589 suc->update_clone_nr++;
48308681
SB
1590
1591 if (!needs_cloning)
1592 goto cleanup;
1593
1594 child->git_cmd = 1;
1595 child->no_stdin = 1;
1596 child->stdout_to_stderr = 1;
1597 child->err = -1;
1598 argv_array_push(&child->args, "submodule--helper");
1599 argv_array_push(&child->args, "clone");
72c5f883
JK
1600 if (suc->progress)
1601 argv_array_push(&child->args, "--progress");
48308681
SB
1602 if (suc->quiet)
1603 argv_array_push(&child->args, "--quiet");
1604 if (suc->prefix)
1605 argv_array_pushl(&child->args, "--prefix", suc->prefix, NULL);
abed000a
SB
1606 if (suc->recommend_shallow && sub->recommend_shallow == 1)
1607 argv_array_push(&child->args, "--depth=1");
48308681
SB
1608 argv_array_pushl(&child->args, "--path", sub->path, NULL);
1609 argv_array_pushl(&child->args, "--name", sub->name, NULL);
ec6141a0 1610 argv_array_pushl(&child->args, "--url", url, NULL);
5f50f33e
SB
1611 if (suc->references.nr) {
1612 struct string_list_item *item;
1613 for_each_string_list_item(item, &suc->references)
1614 argv_array_pushl(&child->args, "--reference", item->string, NULL);
1615 }
a0ef2934
CF
1616 if (suc->dissociate)
1617 argv_array_push(&child->args, "--dissociate");
48308681
SB
1618 if (suc->depth)
1619 argv_array_push(&child->args, suc->depth);
1620
1621cleanup:
48308681
SB
1622 strbuf_reset(&displaypath_sb);
1623 strbuf_reset(&sb);
1624
1625 return needs_cloning;
1626}
1627
1628static int update_clone_get_next_task(struct child_process *child,
1629 struct strbuf *err,
1630 void *suc_cb,
665b35ec 1631 void **idx_task_cb)
48308681
SB
1632{
1633 struct submodule_update_clone *suc = suc_cb;
665b35ec
SB
1634 const struct cache_entry *ce;
1635 int index;
48308681
SB
1636
1637 for (; suc->current < suc->list.nr; suc->current++) {
665b35ec 1638 ce = suc->list.entries[suc->current];
48308681 1639 if (prepare_to_clone_next_submodule(ce, child, suc, err)) {
665b35ec
SB
1640 int *p = xmalloc(sizeof(*p));
1641 *p = suc->current;
1642 *idx_task_cb = p;
48308681
SB
1643 suc->current++;
1644 return 1;
1645 }
1646 }
665b35ec
SB
1647
1648 /*
1649 * The loop above tried cloning each submodule once, now try the
1650 * stragglers again, which we can imagine as an extension of the
1651 * entry list.
1652 */
1653 index = suc->current - suc->list.nr;
1654 if (index < suc->failed_clones_nr) {
1655 int *p;
1656 ce = suc->failed_clones[index];
2201ee09
SB
1657 if (!prepare_to_clone_next_submodule(ce, child, suc, err)) {
1658 suc->current ++;
a22ae753
RS
1659 strbuf_addstr(err, "BUG: submodule considered for "
1660 "cloning, doesn't need cloning "
1661 "any more?\n");
2201ee09
SB
1662 return 0;
1663 }
665b35ec
SB
1664 p = xmalloc(sizeof(*p));
1665 *p = suc->current;
1666 *idx_task_cb = p;
1667 suc->current ++;
1668 return 1;
1669 }
1670
48308681
SB
1671 return 0;
1672}
1673
1674static int update_clone_start_failure(struct strbuf *err,
1675 void *suc_cb,
665b35ec 1676 void *idx_task_cb)
48308681
SB
1677{
1678 struct submodule_update_clone *suc = suc_cb;
1679 suc->quickstop = 1;
1680 return 1;
1681}
1682
1683static int update_clone_task_finished(int result,
1684 struct strbuf *err,
1685 void *suc_cb,
665b35ec 1686 void *idx_task_cb)
48308681 1687{
665b35ec 1688 const struct cache_entry *ce;
48308681
SB
1689 struct submodule_update_clone *suc = suc_cb;
1690
c1e860f1 1691 int *idxP = idx_task_cb;
665b35ec
SB
1692 int idx = *idxP;
1693 free(idxP);
1694
48308681
SB
1695 if (!result)
1696 return 0;
1697
665b35ec
SB
1698 if (idx < suc->list.nr) {
1699 ce = suc->list.entries[idx];
1700 strbuf_addf(err, _("Failed to clone '%s'. Retry scheduled"),
1701 ce->name);
1702 strbuf_addch(err, '\n');
1703 ALLOC_GROW(suc->failed_clones,
1704 suc->failed_clones_nr + 1,
1705 suc->failed_clones_alloc);
1706 suc->failed_clones[suc->failed_clones_nr++] = ce;
1707 return 0;
1708 } else {
eb09121b 1709 idx -= suc->list.nr;
665b35ec
SB
1710 ce = suc->failed_clones[idx];
1711 strbuf_addf(err, _("Failed to clone '%s' a second time, aborting"),
1712 ce->name);
1713 strbuf_addch(err, '\n');
1714 suc->quickstop = 1;
1715 return 1;
1716 }
1717
1718 return 0;
48308681
SB
1719}
1720
05744997
AO
1721static int git_update_clone_config(const char *var, const char *value,
1722 void *cb)
f20e7c1e
BW
1723{
1724 int *max_jobs = cb;
1725 if (!strcmp(var, "submodule.fetchjobs"))
1726 *max_jobs = parse_submodule_fetchjobs(var, value);
1727 return 0;
1728}
1729
90efe595
SB
1730static int update_submodules(struct submodule_update_clone *suc)
1731{
f1d15713
SB
1732 int i;
1733 struct strbuf sb = STRBUF_INIT;
90efe595
SB
1734
1735 run_processes_parallel(suc->max_jobs,
1736 update_clone_get_next_task,
1737 update_clone_start_failure,
1738 update_clone_task_finished,
1739 suc);
1740
1741 /*
1742 * We saved the output and put it out all at once now.
1743 * That means:
1744 * - the listener does not have to interleave their (checkout)
1745 * work with our fetching. The writes involved in a
1746 * checkout involve more straightforward sequential I/O.
1747 * - the listener can avoid doing any work if fetching failed.
1748 */
1749 if (suc->quickstop)
1750 return 1;
1751
f1d15713
SB
1752 for (i = 0; i < suc->update_clone_nr; i++) {
1753 strbuf_addf(&sb, "dummy %s %d\t%s\n",
1754 oid_to_hex(&suc->update_clone[i].oid),
1755 suc->update_clone[i].just_cloned,
1756 suc->update_clone[i].sub->path);
1757 fprintf(stdout, "%s", sb.buf);
1758 strbuf_reset(&sb);
1759 }
90efe595 1760
f1d15713 1761 strbuf_release(&sb);
90efe595
SB
1762 return 0;
1763}
1764
48308681
SB
1765static int update_clone(int argc, const char **argv, const char *prefix)
1766{
1767 const char *update = NULL;
48308681
SB
1768 struct pathspec pathspec;
1769 struct submodule_update_clone suc = SUBMODULE_UPDATE_CLONE_INIT;
1770
1771 struct option module_update_clone_options[] = {
1772 OPT_STRING(0, "prefix", &prefix,
1773 N_("path"),
1774 N_("path into the working tree")),
1775 OPT_STRING(0, "recursive-prefix", &suc.recursive_prefix,
1776 N_("path"),
1777 N_("path into the working tree, across nested "
1778 "submodule boundaries")),
1779 OPT_STRING(0, "update", &update,
1780 N_("string"),
1781 N_("rebase, merge, checkout or none")),
5f50f33e 1782 OPT_STRING_LIST(0, "reference", &suc.references, N_("repo"),
48308681 1783 N_("reference repository")),
a0ef2934
CF
1784 OPT_BOOL(0, "dissociate", &suc.dissociate,
1785 N_("use --reference only while cloning")),
48308681
SB
1786 OPT_STRING(0, "depth", &suc.depth, "<depth>",
1787 N_("Create a shallow clone truncated to the "
1788 "specified number of revisions")),
90efe595 1789 OPT_INTEGER('j', "jobs", &suc.max_jobs,
2335b870 1790 N_("parallel jobs")),
abed000a
SB
1791 OPT_BOOL(0, "recommend-shallow", &suc.recommend_shallow,
1792 N_("whether the initial clone should follow the shallow recommendation")),
48308681 1793 OPT__QUIET(&suc.quiet, N_("don't print cloning progress")),
72c5f883
JK
1794 OPT_BOOL(0, "progress", &suc.progress,
1795 N_("force cloning progress")),
48308681
SB
1796 OPT_END()
1797 };
1798
1799 const char *const git_submodule_helper_usage[] = {
1800 N_("git submodule--helper update_clone [--prefix=<path>] [<path>...]"),
1801 NULL
1802 };
1803 suc.prefix = prefix;
1804
90efe595
SB
1805 update_clone_config_from_gitmodules(&suc.max_jobs);
1806 git_config(git_update_clone_config, &suc.max_jobs);
f20e7c1e 1807
48308681
SB
1808 argc = parse_options(argc, argv, prefix, module_update_clone_options,
1809 git_submodule_helper_usage, 0);
1810
1811 if (update)
1812 if (parse_submodule_update_strategy(update, &suc.update) < 0)
1813 die(_("bad value for update parameter"));
1814
1815 if (module_list_compute(argc, argv, prefix, &pathspec, &suc.list) < 0)
1816 return 1;
1817
1818 if (pathspec.nr)
1819 suc.warn_if_uninitialized = 1;
1820
90efe595 1821 return update_submodules(&suc);
48308681
SB
1822}
1823
44431df0
SB
1824static int resolve_relative_path(int argc, const char **argv, const char *prefix)
1825{
1826 struct strbuf sb = STRBUF_INIT;
1827 if (argc != 3)
2de26ae1 1828 die("submodule--helper relative-path takes exactly 2 arguments, got %d", argc);
44431df0
SB
1829
1830 printf("%s", relative_path(argv[1], argv[2], &sb));
1831 strbuf_release(&sb);
1832 return 0;
1833}
1834
92bbe7cc
SB
1835static const char *remote_submodule_branch(const char *path)
1836{
1837 const struct submodule *sub;
177257cc
BW
1838 const char *branch = NULL;
1839 char *key;
92bbe7cc 1840
3b8fb393 1841 sub = submodule_from_path(the_repository, &null_oid, path);
92bbe7cc
SB
1842 if (!sub)
1843 return NULL;
1844
177257cc
BW
1845 key = xstrfmt("submodule.%s.branch", sub->name);
1846 if (repo_config_get_string_const(the_repository, key, &branch))
1847 branch = sub->branch;
1848 free(key);
1849
1850 if (!branch)
92bbe7cc
SB
1851 return "master";
1852
177257cc 1853 if (!strcmp(branch, ".")) {
744c040b 1854 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
4d7bc52b
SB
1855
1856 if (!refname)
1857 die(_("No such ref: %s"), "HEAD");
1858
1859 /* detached HEAD */
1860 if (!strcmp(refname, "HEAD"))
1861 die(_("Submodule (%s) branch configured to inherit "
1862 "branch from superproject, but the superproject "
1863 "is not on any branch"), sub->name);
1864
1865 if (!skip_prefix(refname, "refs/heads/", &refname))
1866 die(_("Expecting a full ref name, got %s"), refname);
1867 return refname;
1868 }
1869
177257cc 1870 return branch;
92bbe7cc
SB
1871}
1872
1873static int resolve_remote_submodule_branch(int argc, const char **argv,
1874 const char *prefix)
1875{
1876 const char *ret;
1877 struct strbuf sb = STRBUF_INIT;
1878 if (argc != 2)
1879 die("submodule--helper remote-branch takes exactly one arguments, got %d", argc);
1880
1881 ret = remote_submodule_branch(argv[1]);
1882 if (!ret)
1883 die("submodule %s doesn't exist", argv[1]);
1884
1885 printf("%s", ret);
1886 strbuf_release(&sb);
1887 return 0;
1888}
1889
93481a6b
BW
1890static int push_check(int argc, const char **argv, const char *prefix)
1891{
1892 struct remote *remote;
c7be7201
BW
1893 const char *superproject_head;
1894 char *head;
1895 int detached_head = 0;
1896 struct object_id head_oid;
93481a6b 1897
c7be7201
BW
1898 if (argc < 3)
1899 die("submodule--helper push-check requires at least 2 arguments");
1900
1901 /*
1902 * superproject's resolved head ref.
1903 * if HEAD then the superproject is in a detached head state, otherwise
1904 * it will be the resolved head ref.
1905 */
1906 superproject_head = argv[1];
1907 argv++;
1908 argc--;
1909 /* Get the submodule's head ref and determine if it is detached */
0f2dc722 1910 head = resolve_refdup("HEAD", 0, &head_oid, NULL);
c7be7201
BW
1911 if (!head)
1912 die(_("Failed to resolve HEAD as a valid ref."));
1913 if (!strcmp(head, "HEAD"))
1914 detached_head = 1;
93481a6b
BW
1915
1916 /*
1917 * The remote must be configured.
1918 * This is to avoid pushing to the exact same URL as the parent.
1919 */
1920 remote = pushremote_get(argv[1]);
1921 if (!remote || remote->origin == REMOTE_UNCONFIGURED)
1922 die("remote '%s' not configured", argv[1]);
1923
1924 /* Check the refspec */
1925 if (argc > 2) {
9c8361b2 1926 int i;
93481a6b 1927 struct ref *local_refs = get_local_heads();
9c8361b2 1928 struct refspec refspec = REFSPEC_INIT_PUSH;
93481a6b 1929
9c8361b2
BW
1930 refspec_appendn(&refspec, argv + 2, argc - 2);
1931
1932 for (i = 0; i < refspec.nr; i++) {
1933 const struct refspec_item *rs = &refspec.items[i];
93481a6b
BW
1934
1935 if (rs->pattern || rs->matching)
1936 continue;
1937
c7be7201
BW
1938 /* LHS must match a single ref */
1939 switch (count_refspec_match(rs->src, local_refs, NULL)) {
1940 case 1:
1941 break;
1942 case 0:
1943 /*
1944 * If LHS matches 'HEAD' then we need to ensure
1945 * that it matches the same named branch
1946 * checked out in the superproject.
1947 */
1948 if (!strcmp(rs->src, "HEAD")) {
1949 if (!detached_head &&
1950 !strcmp(head, superproject_head))
1951 break;
1952 die("HEAD does not match the named branch in the superproject");
1953 }
1cf01a34 1954 /* fallthrough */
c7be7201 1955 default:
93481a6b
BW
1956 die("src refspec '%s' must name a ref",
1957 rs->src);
c7be7201 1958 }
93481a6b 1959 }
9c8361b2 1960 refspec_clear(&refspec);
93481a6b 1961 }
c7be7201 1962 free(head);
93481a6b
BW
1963
1964 return 0;
1965}
1966
f6f85861
SB
1967static int absorb_git_dirs(int argc, const char **argv, const char *prefix)
1968{
1969 int i;
1970 struct pathspec pathspec;
1971 struct module_list list = MODULE_LIST_INIT;
1972 unsigned flags = ABSORB_GITDIR_RECURSE_SUBMODULES;
1973
1974 struct option embed_gitdir_options[] = {
1975 OPT_STRING(0, "prefix", &prefix,
1976 N_("path"),
1977 N_("path into the working tree")),
1978 OPT_BIT(0, "--recursive", &flags, N_("recurse into submodules"),
1979 ABSORB_GITDIR_RECURSE_SUBMODULES),
1980 OPT_END()
1981 };
1982
1983 const char *const git_submodule_helper_usage[] = {
1984 N_("git submodule--helper embed-git-dir [<path>...]"),
1985 NULL
1986 };
1987
1988 argc = parse_options(argc, argv, prefix, embed_gitdir_options,
1989 git_submodule_helper_usage, 0);
1990
f6f85861
SB
1991 if (module_list_compute(argc, argv, prefix, &pathspec, &list) < 0)
1992 return 1;
1993
1994 for (i = 0; i < list.nr; i++)
1995 absorb_git_dir_into_superproject(prefix,
1996 list.entries[i]->name, flags);
1997
1998 return 0;
1999}
2000
5c2bd8b7
BW
2001static int is_active(int argc, const char **argv, const char *prefix)
2002{
2003 if (argc != 2)
9af7ec30 2004 die("submodule--helper is-active takes exactly 1 argument");
5c2bd8b7 2005
627d9342 2006 return !is_submodule_active(the_repository, argv[1]);
5c2bd8b7
BW
2007}
2008
0383bbb9
JK
2009/*
2010 * Exit non-zero if any of the submodule names given on the command line is
2011 * invalid. If no names are given, filter stdin to print only valid names
2012 * (which is primarily intended for testing).
2013 */
2014static int check_name(int argc, const char **argv, const char *prefix)
2015{
2016 if (argc > 1) {
2017 while (*++argv) {
2018 if (check_submodule_name(*argv) < 0)
2019 return 1;
2020 }
2021 } else {
2022 struct strbuf buf = STRBUF_INIT;
2023 while (strbuf_getline(&buf, stdin) != EOF) {
2024 if (!check_submodule_name(buf.buf))
2025 printf("%s\n", buf.buf);
2026 }
2027 strbuf_release(&buf);
2028 }
2029 return 0;
2030}
2031
e9831750
SB
2032static int connect_gitdir_workingtree(int argc, const char **argv, const char *prefix)
2033{
2034 struct strbuf sb = STRBUF_INIT;
2035 const char *name, *path;
2036 char *sm_gitdir;
2037
2038 if (argc != 3)
2039 BUG("submodule--helper connect-gitdir-workingtree <name> <path>");
2040
2041 name = argv[1];
2042 path = argv[2];
2043
2044 strbuf_addf(&sb, "%s/modules/%s", get_git_dir(), name);
2045 sm_gitdir = absolute_pathdup(sb.buf);
2046
2047 connect_work_tree_and_git_dir(path, sm_gitdir, 0);
2048
2049 strbuf_release(&sb);
2050 free(sm_gitdir);
2051
2052 return 0;
2053}
2054
89c86265
SB
2055#define SUPPORT_SUPER_PREFIX (1<<0)
2056
74703a1e
SB
2057struct cmd_struct {
2058 const char *cmd;
2059 int (*fn)(int, const char **, const char *);
89c86265 2060 unsigned option;
74703a1e
SB
2061};
2062
2063static struct cmd_struct commands[] = {
89c86265
SB
2064 {"list", module_list, 0},
2065 {"name", module_name, 0},
2066 {"clone", module_clone, 0},
2067 {"update-clone", update_clone, 0},
e9831750 2068 {"connect-gitdir-workingtree", connect_gitdir_workingtree, 0},
89c86265
SB
2069 {"relative-path", resolve_relative_path, 0},
2070 {"resolve-relative-url", resolve_relative_url, 0},
2071 {"resolve-relative-url-test", resolve_relative_url_test, 0},
fc1b9243 2072 {"foreach", module_foreach, SUPPORT_SUPER_PREFIX},
6e7c14e6 2073 {"init", module_init, SUPPORT_SUPER_PREFIX},
a9f8a375 2074 {"status", module_status, SUPPORT_SUPER_PREFIX},
13424764
PC
2075 {"print-default-remote", print_default_remote, 0},
2076 {"sync", module_sync, SUPPORT_SUPER_PREFIX},
2e612731 2077 {"deinit", module_deinit, 0},
89c86265 2078 {"remote-branch", resolve_remote_submodule_branch, 0},
93481a6b 2079 {"push-check", push_check, 0},
f6f85861 2080 {"absorb-git-dirs", absorb_git_dirs, SUPPORT_SUPER_PREFIX},
5c2bd8b7 2081 {"is-active", is_active, 0},
0383bbb9 2082 {"check-name", check_name, 0},
74703a1e
SB
2083};
2084
2085int cmd_submodule__helper(int argc, const char **argv, const char *prefix)
2086{
2087 int i;
f0994fa8
JK
2088 if (argc < 2 || !strcmp(argv[1], "-h"))
2089 usage("git submodule--helper <command>");
74703a1e 2090
89c86265
SB
2091 for (i = 0; i < ARRAY_SIZE(commands); i++) {
2092 if (!strcmp(argv[1], commands[i].cmd)) {
2093 if (get_super_prefix() &&
2094 !(commands[i].option & SUPPORT_SUPER_PREFIX))
2095 die(_("%s doesn't support --super-prefix"),
2096 commands[i].cmd);
74703a1e 2097 return commands[i].fn(argc - 1, argv + 1, prefix);
89c86265
SB
2098 }
2099 }
74703a1e 2100
cdc04b65 2101 die(_("'%s' is not a valid submodule--helper "
74703a1e
SB
2102 "subcommand"), argv[1]);
2103}