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