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