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