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