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