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