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