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 *max_prefix
, *ps_matched
= NULL
;
27 parse_pathspec(pathspec
, 0,
28 PATHSPEC_PREFER_FULL
|
29 PATHSPEC_STRIP_SUBMODULE_SLASH_CHEAP
,
32 /* Find common prefix for all pathspec's */
33 max_prefix
= common_prefix(pathspec
);
34 max_prefix_len
= max_prefix
? strlen(max_prefix
) : 0;
37 ps_matched
= xcalloc(pathspec
->nr
, 1);
40 die(_("index file corrupt"));
42 for (i
= 0; i
< active_nr
; i
++) {
43 const struct cache_entry
*ce
= active_cache
[i
];
45 if (!S_ISGITLINK(ce
->ce_mode
) ||
46 !match_pathspec(pathspec
, ce
->name
, ce_namelen(ce
),
47 max_prefix_len
, ps_matched
, 1))
50 ALLOC_GROW(list
->entries
, list
->nr
+ 1, list
->alloc
);
51 list
->entries
[list
->nr
++] = ce
;
52 while (i
+ 1 < active_nr
&&
53 !strcmp(ce
->name
, active_cache
[i
+ 1]->name
))
55 * Skip entries with the same name in different stages
56 * to make sure an entry is returned only once.
62 if (ps_matched
&& report_path_error(ps_matched
, pathspec
, prefix
))
70 static int module_list(int argc
, const char **argv
, const char *prefix
)
73 struct pathspec pathspec
;
74 struct module_list list
= MODULE_LIST_INIT
;
76 struct option module_list_options
[] = {
77 OPT_STRING(0, "prefix", &prefix
,
79 N_("alternative anchor for relative paths")),
83 const char *const git_submodule_helper_usage
[] = {
84 N_("git submodule--helper list [--prefix=<path>] [<path>...]"),
88 argc
= parse_options(argc
, argv
, prefix
, module_list_options
,
89 git_submodule_helper_usage
, 0);
91 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &list
) < 0) {
92 printf("#unmatched\n");
96 for (i
= 0; i
< list
.nr
; i
++) {
97 const struct cache_entry
*ce
= list
.entries
[i
];
100 printf("%06o %s U\t", ce
->ce_mode
, sha1_to_hex(null_sha1
));
102 printf("%06o %s %d\t", ce
->ce_mode
, sha1_to_hex(ce
->sha1
), ce_stage(ce
));
104 utf8_fprintf(stdout
, "%s\n", ce
->name
);
109 static int module_name(int argc
, const char **argv
, const char *prefix
)
111 const struct submodule
*sub
;
114 usage(_("git submodule--helper name <path>"));
117 sub
= submodule_from_path(null_sha1
, argv
[1]);
120 die(_("no submodule mapping found in .gitmodules for path '%s'"),
123 printf("%s\n", sub
->name
);
127 static int clone_submodule(const char *path
, const char *gitdir
, const char *url
,
128 const char *depth
, const char *reference
, int quiet
)
130 struct child_process cp
;
131 child_process_init(&cp
);
133 argv_array_push(&cp
.args
, "clone");
134 argv_array_push(&cp
.args
, "--no-checkout");
136 argv_array_push(&cp
.args
, "--quiet");
138 argv_array_pushl(&cp
.args
, "--depth", depth
, NULL
);
139 if (reference
&& *reference
)
140 argv_array_pushl(&cp
.args
, "--reference", reference
, NULL
);
141 if (gitdir
&& *gitdir
)
142 argv_array_pushl(&cp
.args
, "--separate-git-dir", gitdir
, NULL
);
144 argv_array_push(&cp
.args
, url
);
145 argv_array_push(&cp
.args
, path
);
148 cp
.env
= local_repo_env
;
151 return run_command(&cp
);
154 static int module_clone(int argc
, const char **argv
, const char *prefix
)
156 const char *path
= NULL
, *name
= NULL
, *url
= NULL
;
157 const char *reference
= NULL
, *depth
= NULL
;
159 FILE *submodule_dot_git
;
160 char *sm_gitdir
, *cwd
, *p
;
161 struct strbuf rel_path
= STRBUF_INIT
;
162 struct strbuf sb
= STRBUF_INIT
;
164 struct option module_clone_options
[] = {
165 OPT_STRING(0, "prefix", &prefix
,
167 N_("alternative anchor for relative paths")),
168 OPT_STRING(0, "path", &path
,
170 N_("where the new submodule will be cloned to")),
171 OPT_STRING(0, "name", &name
,
173 N_("name of the new submodule")),
174 OPT_STRING(0, "url", &url
,
176 N_("url where to clone the submodule from")),
177 OPT_STRING(0, "reference", &reference
,
179 N_("reference repository")),
180 OPT_STRING(0, "depth", &depth
,
182 N_("depth for shallow clones")),
183 OPT__QUIET(&quiet
, "Suppress output for cloning a submodule"),
187 const char *const git_submodule_helper_usage
[] = {
188 N_("git submodule--helper clone [--prefix=<path>] [--quiet] "
189 "[--reference <repository>] [--name <name>] [--url <url>]"
190 "[--depth <depth>] [--] [<path>...]"),
194 argc
= parse_options(argc
, argv
, prefix
, module_clone_options
,
195 git_submodule_helper_usage
, 0);
197 strbuf_addf(&sb
, "%s/modules/%s", get_git_dir(), name
);
198 sm_gitdir
= strbuf_detach(&sb
, NULL
);
200 if (!file_exists(sm_gitdir
)) {
201 if (safe_create_leading_directories_const(sm_gitdir
) < 0)
202 die(_("could not create directory '%s'"), sm_gitdir
);
203 if (clone_submodule(path
, sm_gitdir
, url
, depth
, reference
, quiet
))
204 die(_("clone of '%s' into submodule path '%s' failed"),
207 if (safe_create_leading_directories_const(path
) < 0)
208 die(_("could not create directory '%s'"), path
);
209 strbuf_addf(&sb
, "%s/index", sm_gitdir
);
210 unlink_or_warn(sb
.buf
);
214 /* Write a .git file in the submodule to redirect to the superproject. */
215 if (safe_create_leading_directories_const(path
) < 0)
216 die(_("could not create directory '%s'"), path
);
219 strbuf_addf(&sb
, "%s/.git", path
);
221 strbuf_addstr(&sb
, ".git");
223 if (safe_create_leading_directories_const(sb
.buf
) < 0)
224 die(_("could not create leading directories of '%s'"), sb
.buf
);
225 submodule_dot_git
= fopen(sb
.buf
, "w");
226 if (!submodule_dot_git
)
227 die_errno(_("cannot open file '%s'"), sb
.buf
);
229 fprintf(submodule_dot_git
, "gitdir: %s\n",
230 relative_path(sm_gitdir
, path
, &rel_path
));
231 if (fclose(submodule_dot_git
))
232 die(_("could not close file %s"), sb
.buf
);
234 strbuf_reset(&rel_path
);
237 /* Redirect the worktree of the submodule in the superproject's config */
238 if (!is_absolute_path(sm_gitdir
)) {
239 strbuf_addf(&sb
, "%s/%s", cwd
, sm_gitdir
);
241 sm_gitdir
= strbuf_detach(&sb
, NULL
);
244 strbuf_addf(&sb
, "%s/%s", cwd
, path
);
245 p
= git_pathdup_submodule(path
, "config");
247 die(_("could not get submodule directory for '%s'"), path
);
248 git_config_set_in_file(p
, "core.worktree",
249 relative_path(sb
.buf
, sm_gitdir
, &rel_path
));
251 strbuf_release(&rel_path
);
258 struct submodule_update_clone
{
259 /* index into 'list', the list of submodules to look into for cloning */
261 struct module_list list
;
262 unsigned warn_if_uninitialized
: 1;
264 /* update parameter passed via commandline */
265 struct submodule_update_strategy update
;
267 /* configuration parameters which are passed on to the children */
269 const char *reference
;
271 const char *recursive_prefix
;
274 /* Machine-readable status lines to be consumed by git-submodule.sh */
275 struct string_list projectlines
;
277 /* If we want to stop as fast as possible and return an error */
278 unsigned quickstop
: 1;
280 #define SUBMODULE_UPDATE_CLONE_INIT {0, MODULE_LIST_INIT, 0, \
281 SUBMODULE_UPDATE_STRATEGY_INIT, 0, NULL, NULL, NULL, NULL, \
282 STRING_LIST_INIT_DUP, 0}
285 * Determine whether 'ce' needs to be cloned. If so, prepare the 'child' to
286 * run the clone. Returns 1 if 'ce' needs to be cloned, 0 otherwise.
288 static int prepare_to_clone_next_submodule(const struct cache_entry
*ce
,
289 struct child_process
*child
,
290 struct submodule_update_clone
*suc
,
293 const struct submodule
*sub
= NULL
;
294 struct strbuf displaypath_sb
= STRBUF_INIT
;
295 struct strbuf sb
= STRBUF_INIT
;
296 const char *displaypath
= NULL
;
298 int needs_cloning
= 0;
301 if (suc
->recursive_prefix
)
302 strbuf_addf(&sb
, "%s/%s", suc
->recursive_prefix
, ce
->name
);
304 strbuf_addf(&sb
, "%s", ce
->name
);
305 strbuf_addf(out
, _("Skipping unmerged submodule %s"), sb
.buf
);
306 strbuf_addch(out
, '\n');
310 sub
= submodule_from_path(null_sha1
, ce
->name
);
312 if (suc
->recursive_prefix
)
313 displaypath
= relative_path(suc
->recursive_prefix
,
314 ce
->name
, &displaypath_sb
);
316 displaypath
= ce
->name
;
318 if (suc
->update
.type
== SM_UPDATE_NONE
319 || (suc
->update
.type
== SM_UPDATE_UNSPECIFIED
320 && sub
->update_strategy
.type
== SM_UPDATE_NONE
)) {
321 strbuf_addf(out
, _("Skipping submodule '%s'"), displaypath
);
322 strbuf_addch(out
, '\n');
327 * Looking up the url in .git/config.
328 * We must not fall back to .gitmodules as we only want
329 * to process configured submodules.
332 strbuf_addf(&sb
, "submodule.%s.url", sub
->name
);
333 git_config_get_string(sb
.buf
, &url
);
336 * Only mention uninitialized submodules when their
337 * path have been specified
339 if (suc
->warn_if_uninitialized
) {
341 _("Submodule path '%s' not initialized"),
343 strbuf_addch(out
, '\n');
345 _("Maybe you want to use 'update --init'?"));
346 strbuf_addch(out
, '\n');
352 strbuf_addf(&sb
, "%s/.git", ce
->name
);
353 needs_cloning
= !file_exists(sb
.buf
);
356 strbuf_addf(&sb
, "%06o %s %d %d\t%s\n", ce
->ce_mode
,
357 sha1_to_hex(ce
->sha1
), ce_stage(ce
),
358 needs_cloning
, ce
->name
);
359 string_list_append(&suc
->projectlines
, sb
.buf
);
366 child
->stdout_to_stderr
= 1;
368 argv_array_push(&child
->args
, "submodule--helper");
369 argv_array_push(&child
->args
, "clone");
371 argv_array_push(&child
->args
, "--quiet");
373 argv_array_pushl(&child
->args
, "--prefix", suc
->prefix
, NULL
);
374 argv_array_pushl(&child
->args
, "--path", sub
->path
, NULL
);
375 argv_array_pushl(&child
->args
, "--name", sub
->name
, NULL
);
376 argv_array_pushl(&child
->args
, "--url", url
, NULL
);
378 argv_array_push(&child
->args
, suc
->reference
);
380 argv_array_push(&child
->args
, suc
->depth
);
384 strbuf_reset(&displaypath_sb
);
387 return needs_cloning
;
390 static int update_clone_get_next_task(struct child_process
*child
,
395 struct submodule_update_clone
*suc
= suc_cb
;
397 for (; suc
->current
< suc
->list
.nr
; suc
->current
++) {
398 const struct cache_entry
*ce
= suc
->list
.entries
[suc
->current
];
399 if (prepare_to_clone_next_submodule(ce
, child
, suc
, err
)) {
407 static int update_clone_start_failure(struct strbuf
*err
,
411 struct submodule_update_clone
*suc
= suc_cb
;
416 static int update_clone_task_finished(int result
,
421 struct submodule_update_clone
*suc
= suc_cb
;
430 static int update_clone(int argc
, const char **argv
, const char *prefix
)
432 const char *update
= NULL
;
433 struct string_list_item
*item
;
434 struct pathspec pathspec
;
435 struct submodule_update_clone suc
= SUBMODULE_UPDATE_CLONE_INIT
;
437 struct option module_update_clone_options
[] = {
438 OPT_STRING(0, "prefix", &prefix
,
440 N_("path into the working tree")),
441 OPT_STRING(0, "recursive-prefix", &suc
.recursive_prefix
,
443 N_("path into the working tree, across nested "
444 "submodule boundaries")),
445 OPT_STRING(0, "update", &update
,
447 N_("rebase, merge, checkout or none")),
448 OPT_STRING(0, "reference", &suc
.reference
, N_("repo"),
449 N_("reference repository")),
450 OPT_STRING(0, "depth", &suc
.depth
, "<depth>",
451 N_("Create a shallow clone truncated to the "
452 "specified number of revisions")),
453 OPT__QUIET(&suc
.quiet
, N_("don't print cloning progress")),
457 const char *const git_submodule_helper_usage
[] = {
458 N_("git submodule--helper update_clone [--prefix=<path>] [<path>...]"),
463 argc
= parse_options(argc
, argv
, prefix
, module_update_clone_options
,
464 git_submodule_helper_usage
, 0);
467 if (parse_submodule_update_strategy(update
, &suc
.update
) < 0)
468 die(_("bad value for update parameter"));
470 if (module_list_compute(argc
, argv
, prefix
, &pathspec
, &suc
.list
) < 0)
474 suc
.warn_if_uninitialized
= 1;
476 /* Overlay the parsed .gitmodules file with .git/config */
478 git_config(submodule_config
, NULL
);
480 run_processes_parallel(1,
481 update_clone_get_next_task
,
482 update_clone_start_failure
,
483 update_clone_task_finished
,
487 * We saved the output and put it out all at once now.
489 * - the listener does not have to interleave their (checkout)
490 * work with our fetching. The writes involved in a
491 * checkout involve more straightforward sequential I/O.
492 * - the listener can avoid doing any work if fetching failed.
497 for_each_string_list_item(item
, &suc
.projectlines
)
498 utf8_fprintf(stdout
, "%s", item
->string
);
505 int (*fn
)(int, const char **, const char *);
508 static struct cmd_struct commands
[] = {
509 {"list", module_list
},
510 {"name", module_name
},
511 {"clone", module_clone
},
512 {"update-clone", update_clone
}
515 int cmd_submodule__helper(int argc
, const char **argv
, const char *prefix
)
519 die(_("fatal: submodule--helper subcommand must be "
520 "called with a subcommand"));
522 for (i
= 0; i
< ARRAY_SIZE(commands
); i
++)
523 if (!strcmp(argv
[1], commands
[i
].cmd
))
524 return commands
[i
].fn(argc
- 1, argv
+ 1, prefix
);
526 die(_("fatal: '%s' is not a valid submodule--helper "
527 "subcommand"), argv
[1]);