3 #include "parse-options.h"
9 #include "submodule-config.h"
10 #include "string-list.h"
11 #include "run-command.h"
14 const struct cache_entry
**entries
;
17 #define MODULE_LIST_INIT { NULL, 0, 0 }
19 static int module_list_compute(int argc
, const char **argv
,
21 struct pathspec
*pathspec
,
22 struct module_list
*list
)
25 char *ps_matched
= NULL
;
26 parse_pathspec(pathspec
, 0,
27 PATHSPEC_PREFER_FULL
|
28 PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP
,
32 ps_matched
= xcalloc(pathspec
->nr
, 1);
35 die(_("index file corrupt"));
37 for (i
= 0; i
< active_nr
; i
++) {
38 const struct cache_entry
*ce
= active_cache
[i
];
40 if (!match_pathspec(pathspec
, ce
->name
, ce_namelen(ce
),
42 !S_ISGITLINK(ce
->ce_mode
))
45 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
46 list
->entries
[list
->nr
++] = ce
;
47 while (i
+ 1 < active_nr
&&
48 !strcmp(ce
->name
, active_cache
[i
+ 1]->name
))
50 * Skip entries with the same name in different stages
51 * to make sure an entry is returned only once.
56 if (ps_matched
&& report_path_error(ps_matched
, pathspec
, prefix
))
64 static int module_list(int argc
, const char **argv
, const char *prefix
)
67 struct pathspec pathspec
;
68 struct module_list list
= MODULE_LIST_INIT
;
70 struct option module_list_options
[] = {
71 OPT_STRING(0, "prefix", &prefix
,
73 N_("alternative anchor for relative paths")),
77 const char *const git_submodule_helper_usage
[] = {
78 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
82 argc
= parse_options(argc
, argv
, prefix
, module_list_options
,
83 git_submodule_helper_usage
, 0);
85 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0) {
86 printf("#unmatched\n");
90 for (i
= 0; i
< list
.nr
; i
++) {
91 const struct cache_entry
*ce
= list
.entries
[i
];
94 printf("%06o %s U\t", ce
->ce_mode
, sha1_to_hex(null_sha1
));
96 printf("%06o %s %d\t", ce
->ce_mode
, sha1_to_hex(ce
->sha1
), ce_stage(ce
));
98 utf8_fprintf(stdout
, "%s\n", ce
->name
);
103 static int module_name(int argc
, const char **argv
, const char *prefix
)
105 const struct submodule
*sub
;
108 usage(_("git submodule--helper name <path>"));
111 sub
= submodule_from_path(null_sha1
, argv
[1]);
114 die(_("no submodule mapping found in .gitmodules for path '%s'"),
117 printf("%s\n", sub
->name
);
123 * Rules to sanitize configuration variables that are Ok to be passed into
124 * submodule operations from the parent project using "-c". Should only
125 * include keys which are both (a) safe and (b) necessary for proper
128 static int submodule_config_ok(const char *var
)
130 if (starts_with(var
, "credential."))
135 static int sanitize_submodule_config(const char *var
, const char *value
, void *data
)
137 struct strbuf
*out
= data
;
139 if (submodule_config_ok(var
)) {
141 strbuf_addch(out
, ' ');
144 sq_quotef(out
, "%s=%s", var
, value
);
146 sq_quote_buf(out
, var
);
152 static void prepare_submodule_repo_env(struct argv_array
*out
)
154 const char * const *var
;
156 for (var
= local_repo_env
; *var
; var
++) {
157 if (!strcmp(*var
, CONFIG_DATA_ENVIRONMENT
)) {
158 struct strbuf sanitized_config
= STRBUF_INIT
;
159 git_config_from_parameters(sanitize_submodule_config
,
161 argv_array_pushf(out
, "%s=%s", *var
, sanitized_config
.buf
);
162 strbuf_release(&sanitized_config
);
164 argv_array_push(out
, *var
);
170 static int clone_submodule(const char *path
, const char *gitdir
, const char *url
,
171 const char *depth
, const char *reference
, int quiet
)
173 struct child_process cp
;
174 child_process_init(&cp
);
176 argv_array_push(&cp
.args
, "clone");
177 argv_array_push(&cp
.args
, "--no-checkout");
179 argv_array_push(&cp
.args
, "--quiet");
181 argv_array_pushl(&cp
.args
, "--depth", depth
, NULL
);
182 if (reference
&& *reference
)
183 argv_array_pushl(&cp
.args
, "--reference", reference
, NULL
);
184 if (gitdir
&& *gitdir
)
185 argv_array_pushl(&cp
.args
, "--separate-git-dir", gitdir
, NULL
);
187 argv_array_push(&cp
.args
, url
);
188 argv_array_push(&cp
.args
, path
);
191 prepare_submodule_repo_env(&cp
.env_array
);
194 return run_command(&cp
);
197 static int module_clone(int argc
, const char **argv
, const char *prefix
)
199 const char *name
= NULL
, *url
= NULL
;
200 const char *reference
= NULL
, *depth
= NULL
;
202 FILE *submodule_dot_git
;
203 char *p
, *path
= NULL
, *sm_gitdir
;
204 struct strbuf rel_path
= STRBUF_INIT
;
205 struct strbuf sb
= STRBUF_INIT
;
207 struct option module_clone_options
[] = {
208 OPT_STRING(0, "prefix", &prefix
,
210 N_("alternative anchor for relative paths")),
211 OPT_STRING(0, "path", &path
,
213 N_("where the new submodule will be cloned to")),
214 OPT_STRING(0, "name", &name
,
216 N_("name of the new submodule")),
217 OPT_STRING(0, "url", &url
,
219 N_("url where to clone the submodule from")),
220 OPT_STRING(0, "reference", &reference
,
222 N_("reference repository")),
223 OPT_STRING(0, "depth", &depth
,
225 N_("depth for shallow clones")),
226 OPT__QUIET(&quiet
, "Suppress output for cloning a submodule"),
230 const char *const git_submodule_helper_usage
[] = {
231 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
232 "[--reference <repository>] [--name <name>] [--depth <depth>] "
233 "--url <url> --path <path>"),
237 argc
= parse_options(argc
, argv
, prefix
, module_clone_options
,
238 git_submodule_helper_usage
, 0);
240 if (argc
|| !url
|| !path
|| !*path
)
241 usage_with_options(git_submodule_helper_usage
,
242 module_clone_options
);
244 strbuf_addf(&sb
, "%s/modules/%s", get_git_dir(), name
);
245 sm_gitdir
= xstrdup(absolute_path(sb
.buf
));
248 if (!is_absolute_path(path
)) {
249 strbuf_addf(&sb
, "%s/%s", get_git_work_tree(), path
);
250 path
= strbuf_detach(&sb
, NULL
);
252 path
= xstrdup(path
);
254 if (!file_exists(sm_gitdir
)) {
255 if (safe_create_leading_directories_const(sm_gitdir
) < 0)
256 die(_("could not create directory '%s'"), sm_gitdir
);
257 if (clone_submodule(path
, sm_gitdir
, url
, depth
, reference
, quiet
))
258 die(_("clone of '%s' into submodule path '%s' failed"),
261 if (safe_create_leading_directories_const(path
) < 0)
262 die(_("could not create directory '%s'"), path
);
263 strbuf_addf(&sb
, "%s/index", sm_gitdir
);
264 unlink_or_warn(sb
.buf
);
268 /* Write a .git file in the submodule to redirect to the superproject. */
269 strbuf_addf(&sb
, "%s/.git", path
);
270 if (safe_create_leading_directories_const(sb
.buf
) < 0)
271 die(_("could not create leading directories of '%s'"), sb
.buf
);
272 submodule_dot_git
= fopen(sb
.buf
, "w");
273 if (!submodule_dot_git
)
274 die_errno(_("cannot open file '%s'"), sb
.buf
);
276 fprintf_or_die(submodule_dot_git
, "gitdir: %s\n",
277 relative_path(sm_gitdir
, path
, &rel_path
));
278 if (fclose(submodule_dot_git
))
279 die(_("could not close file %s"), sb
.buf
);
281 strbuf_reset(&rel_path
);
283 /* Redirect the worktree of the submodule in the superproject's config */
284 p
= git_pathdup_submodule(path
, "config");
286 die(_("could not get submodule directory for '%s'"), path
);
287 git_config_set_in_file(p
, "core.worktree",
288 relative_path(path
, sm_gitdir
, &rel_path
));
290 strbuf_release(&rel_path
);
297 static int module_sanitize_config(int argc
, const char **argv
, const char *prefix
)
299 struct strbuf sanitized_config
= STRBUF_INIT
;
302 usage(_("git submodule--helper sanitize-config"));
304 git_config_from_parameters(sanitize_submodule_config
, &sanitized_config
);
305 if (sanitized_config
.len
)
306 printf("%s\n", sanitized_config
.buf
);
308 strbuf_release(&sanitized_config
);
313 struct submodule_update_clone
{
314 /* index into 'list', the list of submodules to look into for cloning */
316 struct module_list list
;
317 unsigned warn_if_uninitialized
: 1;
319 /* update parameter passed via commandline */
320 struct submodule_update_strategy update
;
322 /* configuration parameters which are passed on to the children */
324 const char *reference
;
326 const char *recursive_prefix
;
329 /* Machine-readable status lines to be consumed by git-submodule.sh */
330 struct string_list projectlines
;
332 /* If we want to stop as fast as possible and return an error */
333 unsigned quickstop
: 1;
335 #define SUBMODULE_UPDATE_CLONE_INIT {0, MODULE_LIST_INIT, 0, \
336 SUBMODULE_UPDATE_STRATEGY_INIT, 0, NULL, NULL, NULL, NULL, \
337 STRING_LIST_INIT_DUP, 0}
340 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
341 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
343 static int prepare_to_clone_next_submodule(const struct cache_entry
*ce
,
344 struct child_process
*child
,
345 struct submodule_update_clone
*suc
,
348 const struct submodule
*sub
= NULL
;
349 struct strbuf displaypath_sb
= STRBUF_INIT
;
350 struct strbuf sb
= STRBUF_INIT
;
351 const char *displaypath
= NULL
;
353 int needs_cloning
= 0;
356 if (suc
->recursive_prefix
)
357 strbuf_addf(&sb
, "%s/%s", suc
->recursive_prefix
, ce
->name
);
359 strbuf_addf(&sb
, "%s", ce
->name
);
360 strbuf_addf(out
, _("Skipping unmerged submodule %s"), sb
.buf
);
361 strbuf_addch(out
, '\n');
365 sub
= submodule_from_path(null_sha1
, ce
->name
);
367 if (suc
->recursive_prefix
)
368 displaypath
= relative_path(suc
->recursive_prefix
,
369 ce
->name
, &displaypath_sb
);
371 displaypath
= ce
->name
;
373 if (suc
->update
.type
== SM_UPDATE_NONE
374 || (suc
->update
.type
== SM_UPDATE_UNSPECIFIED
375 && sub
->update_strategy
.type
== SM_UPDATE_NONE
)) {
376 strbuf_addf(out
, _("Skipping submodule '%s'"), displaypath
);
377 strbuf_addch(out
, '\n');
382 * Looking up the url in .git/config.
383 * We must not fall back to .gitmodules as we only want
384 * to process configured submodules.
387 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
388 git_config_get_string(sb
.buf
, &url
);
391 * Only mention uninitialized submodules when their
392 * path have been specified
394 if (suc
->warn_if_uninitialized
) {
396 _("Submodule path '%s' not initialized"),
398 strbuf_addch(out
, '\n');
400 _("Maybe you want to use 'update --init'?"));
401 strbuf_addch(out
, '\n');
407 strbuf_addf(&sb
, "%s/.git", ce
->name
);
408 needs_cloning
= !file_exists(sb
.buf
);
411 strbuf_addf(&sb
, "%06o %s %d %d\t%s\n", ce
->ce_mode
,
412 sha1_to_hex(ce
->sha1
), ce_stage(ce
),
413 needs_cloning
, ce
->name
);
414 string_list_append(&suc
->projectlines
, sb
.buf
);
421 child
->stdout_to_stderr
= 1;
423 argv_array_push(&child
->args
, "submodule--helper");
424 argv_array_push(&child
->args
, "clone");
426 argv_array_push(&child
->args
, "--quiet");
428 argv_array_pushl(&child
->args
, "--prefix", suc
->prefix
, NULL
);
429 argv_array_pushl(&child
->args
, "--path", sub
->path
, NULL
);
430 argv_array_pushl(&child
->args
, "--name", sub
->name
, NULL
);
431 argv_array_pushl(&child
->args
, "--url", url
, NULL
);
433 argv_array_push(&child
->args
, suc
->reference
);
435 argv_array_push(&child
->args
, suc
->depth
);
439 strbuf_reset(&displaypath_sb
);
442 return needs_cloning
;
445 static int update_clone_get_next_task(struct child_process
*child
,
450 struct submodule_update_clone
*suc
= suc_cb
;
452 for (; suc
->current
< suc
->list
.nr
; suc
->current
++) {
453 const struct cache_entry
*ce
= suc
->list
.entries
[suc
->current
];
454 if (prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
462 static int update_clone_start_failure(struct strbuf
*err
,
466 struct submodule_update_clone
*suc
= suc_cb
;
471 static int update_clone_task_finished(int result
,
476 struct submodule_update_clone
*suc
= suc_cb
;
485 static int update_clone(int argc
, const char **argv
, const char *prefix
)
487 const char *update
= NULL
;
489 struct string_list_item
*item
;
490 struct pathspec pathspec
;
491 struct submodule_update_clone suc
= SUBMODULE_UPDATE_CLONE_INIT
;
493 struct option module_update_clone_options
[] = {
494 OPT_STRING(0, "prefix", &prefix
,
496 N_("path into the working tree")),
497 OPT_STRING(0, "recursive-prefix", &suc
.recursive_prefix
,
499 N_("path into the working tree, across nested "
500 "submodule boundaries")),
501 OPT_STRING(0, "update", &update
,
503 N_("rebase, merge, checkout or none")),
504 OPT_STRING(0, "reference", &suc
.reference
, N_("repo"),
505 N_("reference repository")),
506 OPT_STRING(0, "depth", &suc
.depth
, "<depth>",
507 N_("Create a shallow clone truncated to the "
508 "specified number of revisions")),
509 OPT_INTEGER('j', "jobs", &max_jobs
,
510 N_("parallel jobs")),
511 OPT__QUIET(&suc
.quiet
, N_("don't print cloning progress")),
515 const char *const git_submodule_helper_usage
[] = {
516 N_("git submodule--helper update_clone [--prefix=<path>] [<path>...]"),
521 argc
= parse_options(argc
, argv
, prefix
, module_update_clone_options
,
522 git_submodule_helper_usage
, 0);
525 if (parse_submodule_update_strategy(update
, &suc
.update
) < 0)
526 die(_("bad value for update parameter"));
528 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &suc
.list
) < 0)
532 suc
.warn_if_uninitialized
= 1;
534 /* Overlay the parsed .gitmodules file with .git/config */
536 git_config(submodule_config
, NULL
);
539 max_jobs
= parallel_submodules();
541 run_processes_parallel(max_jobs
,
542 update_clone_get_next_task
,
543 update_clone_start_failure
,
544 update_clone_task_finished
,
548 * We saved the output and put it out all at once now.
550 * - the listener does not have to interleave their (checkout)
551 * work with our fetching. The writes involved in a
552 * checkout involve more straightforward sequential I/O.
553 * - the listener can avoid doing any work if fetching failed.
558 for_each_string_list_item(item
, &suc
.projectlines
)
559 utf8_fprintf(stdout
, "%s", item
->string
);
566 int (*fn
)(int, const char **, const char *);
569 static struct cmd_struct commands
[] = {
570 {"list", module_list
},
571 {"name", module_name
},
572 {"clone", module_clone
},
573 {"sanitize-config", module_sanitize_config
},
574 {"update-clone", update_clone
}
577 int cmd_submodule__helper(int argc
, const char **argv
, const char *prefix
)
581 die(_("submodule--helper subcommand must be "
582 "called with a subcommand"));
584 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++)
585 if (!strcmp(argv
[1], commands
[i
].cmd
))
586 return commands
[i
].fn(argc
- 1, argv
+ 1, prefix
);
588 die(_("'%s' is not a valid submodule--helper "
589 "subcommand"), argv
[1]);