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/submodule.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 / submodule.c` 2773 lines ยท 75862 bytes Raw Blame History 1 #define USE_THE_REPOSITORY_VARIABLE 2 #define DISABLE_SIGN_COMPARE_WARNINGS 3 4 #include "git-compat-util.h" 5 #include "abspath.h" 6 #include "repository.h" 7 #include "config.h" 8 #include "submodule-config.h" 9 #include "submodule.h" 10 #include "dir.h" 11 #include "diff.h" 12 #include "commit.h" 13 #include "environment.h" 14 #include "gettext.h" 15 #include "hex.h" 16 #include "revision.h" 17 #include "run-command.h" 18 #include "diffcore.h" 19 #include "refs.h" 20 #include "string-list.h" 21 #include "oid-array.h" 22 #include "strvec.h" 23 #include "thread-utils.h" 24 #include "path.h" 25 #include "remote.h" 26 #include "worktree.h" 27 #include "parse-options.h" 28 #include "object-file.h" 29 #include "object-name.h" 30 #include "odb.h" 31 #include "commit-reach.h" 32 #include "read-cache-ll.h" 33 #include "setup.h" 34 #include "advice.h" 35 #include "url.h" 36 37 static int config_update_recurse_submodules = RECURSE_SUBMODULES_OFF; 38 static int initialized_fetch_ref_tips; 39 static struct oid_array ref_tips_before_fetch; 40 static struct oid_array ref_tips_after_fetch; 41 42 /* 43 * Check if the .gitmodules file is unmerged. Parsing of the .gitmodules file 44 * will be disabled because we can't guess what might be configured in 45 * .gitmodules unless the user resolves the conflict. 46 */ 47 int is_gitmodules_unmerged(struct index_state *istate) 48 { 49 int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE)); 50 if (pos < 0) { /* .gitmodules not found or isn't merged */ 51 pos = -1 - pos; 52 if (istate->cache_nr > pos) { /* there is a .gitmodules */ 53 const struct cache_entry *ce = istate->cache[pos]; 54 if (ce_namelen(ce) == strlen(GITMODULES_FILE) && 55 !strcmp(ce->name, GITMODULES_FILE)) 56 return 1; 57 } 58 } 59 60 return 0; 61 } 62 63 /* 64 * Check if the .gitmodules file is safe to write. 65 * 66 * Writing to the .gitmodules file requires that the file exists in the 67 * working tree or, if it doesn't, that a brand new .gitmodules file is going 68 * to be created (i.e. it's neither in the index nor in the current branch). 69 * 70 * It is not safe to write to .gitmodules if it's not in the working tree but 71 * it is in the index or in the current branch, because writing new values 72 * (and staging them) would blindly overwrite ALL the old content. 73 */ 74 int is_writing_gitmodules_ok(void) 75 { 76 struct object_id oid; 77 return file_exists(GITMODULES_FILE) || 78 (repo_get_oid(the_repository, GITMODULES_INDEX, &oid) < 0 && repo_get_oid(the_repository, GITMODULES_HEAD, &oid) < 0); 79 } 80 81 /* 82 * Check if the .gitmodules file has unstaged modifications. This must be 83 * checked before allowing modifications to the .gitmodules file with the 84 * intention to stage them later, because when continuing we would stage the 85 * modifications the user didn't stage herself too. That might change in a 86 * future version when we learn to stage the changes we do ourselves without 87 * staging any previous modifications. 88 */ 89 int is_staging_gitmodules_ok(struct index_state *istate) 90 { 91 int pos = index_name_pos(istate, GITMODULES_FILE, strlen(GITMODULES_FILE)); 92 93 if ((pos >= 0) && (pos < istate->cache_nr)) { 94 struct stat st; 95 if (lstat(GITMODULES_FILE, &st) == 0 && 96 ie_modified(istate, istate->cache[pos], &st, 0) & DATA_CHANGED) 97 return 0; 98 } 99 100 return 1; 101 } 102 103 static int for_each_remote_ref_submodule(const char *submodule, 104 refs_for_each_cb fn, void *cb_data) 105 { 106 return refs_for_each_remote_ref(repo_get_submodule_ref_store(the_repository, 107 submodule), 108 fn, cb_data); 109 } 110 111 /* 112 * Try to update the "path" entry in the "submodule." section of the 113 * .gitmodules file. Return 0 only if a .gitmodules file was found, a section 114 * with the correct path= setting was found and we could update it. 115 */ 116 int update_path_in_gitmodules(const char *oldpath, const char *newpath) 117 { 118 struct strbuf entry = STRBUF_INIT; 119 const struct submodule *submodule; 120 int ret; 121 122 if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */ 123 return -1; 124 125 if (is_gitmodules_unmerged(the_repository->index)) 126 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first")); 127 128 submodule = submodule_from_path(the_repository, null_oid(the_hash_algo), oldpath); 129 if (!submodule || !submodule->name) { 130 warning(_("Could not find section in .gitmodules where path=%s"), oldpath); 131 return -1; 132 } 133 strbuf_addstr(&entry, "submodule."); 134 strbuf_addstr(&entry, submodule->name); 135 strbuf_addstr(&entry, ".path"); 136 ret = config_set_in_gitmodules_file_gently(the_repository, entry.buf, newpath); 137 strbuf_release(&entry); 138 return ret; 139 } 140 141 /* 142 * Try to remove the "submodule." section from .gitmodules where the given 143 * path is configured. Return 0 only if a .gitmodules file was found, a section 144 * with the correct path= setting was found and we could remove it. 145 */ 146 int remove_path_from_gitmodules(const char *path) 147 { 148 struct strbuf sect = STRBUF_INIT; 149 const struct submodule *submodule; 150 151 if (!file_exists(GITMODULES_FILE)) /* Do nothing without .gitmodules */ 152 return -1; 153 154 if (is_gitmodules_unmerged(the_repository->index)) 155 die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first")); 156 157 submodule = submodule_from_path(the_repository, null_oid(the_hash_algo), path); 158 if (!submodule || !submodule->name) { 159 warning(_("Could not find section in .gitmodules where path=%s"), path); 160 return -1; 161 } 162 strbuf_addstr(§, "submodule."); 163 strbuf_addstr(§, submodule->name); 164 if (repo_config_rename_section_in_file(the_repository, GITMODULES_FILE, sect.buf, NULL) < 0) { 165 /* Maybe the user already did that, don't error out here */ 166 warning(_("Could not remove .gitmodules entry for %s"), path); 167 strbuf_release(§); 168 return -1; 169 } 170 strbuf_release(§); 171 return 0; 172 } 173 174 void stage_updated_gitmodules(struct index_state *istate) 175 { 176 if (add_file_to_index(istate, GITMODULES_FILE, 0)) 177 die(_("staging updated .gitmodules failed")); 178 } 179 180 void set_diffopt_flags_from_submodule_config(struct diff_options *diffopt, 181 const char *path) 182 { 183 const struct submodule *submodule = submodule_from_path(the_repository, 184 null_oid(the_hash_algo), 185 path); 186 if (submodule) { 187 const char *ignore; 188 char *key; 189 190 key = xstrfmt("submodule.%s.ignore", submodule->name); 191 if (repo_config_get_string_tmp(the_repository, key, &ignore)) 192 ignore = submodule->ignore; 193 free(key); 194 195 if (ignore) 196 handle_ignore_submodules_arg(diffopt, ignore); 197 else if (is_gitmodules_unmerged(the_repository->index)) 198 diffopt->flags.ignore_submodules = 1; 199 } 200 } 201 202 /* Cheap function that only determines if we're interested in submodules at all */ 203 int git_default_submodule_config(const char *var, const char *value, 204 void *cb UNUSED) 205 { 206 if (!strcmp(var, "submodule.recurse")) { 207 int v = git_config_bool(var, value) ? 208 RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF; 209 config_update_recurse_submodules = v; 210 } 211 return 0; 212 } 213 214 int option_parse_recurse_submodules_worktree_updater(const struct option *opt, 215 const char *arg, int unset) 216 { 217 if (unset) { 218 config_update_recurse_submodules = RECURSE_SUBMODULES_OFF; 219 return 0; 220 } 221 if (arg) 222 config_update_recurse_submodules = 223 parse_update_recurse_submodules_arg(opt->long_name, 224 arg); 225 else 226 config_update_recurse_submodules = RECURSE_SUBMODULES_ON; 227 228 return 0; 229 } 230 231 /* 232 * Determine if a submodule has been initialized at a given 'path' 233 */ 234 /* 235 * NEEDSWORK: Emit a warning if submodule.active exists, but is valueless, 236 * ie, the config looks like: "[submodule] active\n". 237 * Since that is an invalid pathspec, we should inform the user. 238 */ 239 int is_tree_submodule_active(struct repository *repo, 240 const struct object_id *treeish_name, 241 const char *path) 242 { 243 int ret = 0; 244 char *key = NULL; 245 char *value = NULL; 246 const struct string_list *sl; 247 const struct submodule *module; 248 249 module = submodule_from_path(repo, treeish_name, path); 250 251 /* early return if there isn't a path->module mapping */ 252 if (!module) 253 return 0; 254 255 /* submodule..active is set */ 256 key = xstrfmt("submodule.%s.active", module->name); 257 if (!repo_config_get_bool(repo, key, &ret)) { 258 free(key); 259 return ret; 260 } 261 free(key); 262 263 /* submodule.active is set */ 264 if (!repo_config_get_string_multi(repo, "submodule.active", &sl)) { 265 struct pathspec ps; 266 struct strvec args = STRVEC_INIT; 267 const struct string_list_item *item; 268 269 for_each_string_list_item(item, sl) { 270 strvec_push(&args, item->string); 271 } 272 273 parse_pathspec(&ps, 0, 0, NULL, args.v); 274 ret = match_pathspec(repo->index, &ps, path, strlen(path), 0, NULL, 1); 275 276 strvec_clear(&args); 277 clear_pathspec(&ps); 278 return ret; 279 } 280 281 /* fallback to checking if the URL is set */ 282 key = xstrfmt("submodule.%s.url", module->name); 283 ret = !repo_config_get_string(repo, key, &value); 284 285 free(value); 286 free(key); 287 return ret; 288 } 289 290 int is_submodule_active(struct repository *repo, const char *path) 291 { 292 return is_tree_submodule_active(repo, null_oid(the_hash_algo), path); 293 } 294 295 int is_submodule_populated_gently(const char *path, int *return_error_code) 296 { 297 int ret = 0; 298 char *gitdir = xstrfmt("%s/.git", path); 299 300 if (resolve_gitdir_gently(gitdir, return_error_code)) 301 ret = 1; 302 303 free(gitdir); 304 return ret; 305 } 306 307 /* 308 * Dies if the provided 'prefix' corresponds to an unpopulated submodule 309 */ 310 void die_in_unpopulated_submodule(struct index_state *istate, 311 const char *prefix) 312 { 313 int i, prefixlen; 314 315 if (!prefix) 316 return; 317 318 prefixlen = strlen(prefix); 319 320 for (i = 0; i < istate->cache_nr; i++) { 321 struct cache_entry *ce = istate->cache[i]; 322 int ce_len = ce_namelen(ce); 323 324 if (!S_ISGITLINK(ce->ce_mode)) 325 continue; 326 if (prefixlen <= ce_len) 327 continue; 328 if (strncmp(ce->name, prefix, ce_len)) 329 continue; 330 if (prefix[ce_len] != '/') 331 continue; 332 333 die(_("in unpopulated submodule '%s'"), ce->name); 334 } 335 } 336 337 /* 338 * Dies if any paths in the provided pathspec descends into a submodule 339 */ 340 void die_path_inside_submodule(struct index_state *istate, 341 const struct pathspec *ps) 342 { 343 int i, j; 344 345 for (i = 0; i < istate->cache_nr; i++) { 346 struct cache_entry *ce = istate->cache[i]; 347 int ce_len = ce_namelen(ce); 348 349 if (!S_ISGITLINK(ce->ce_mode)) 350 continue; 351 352 for (j = 0; j < ps->nr ; j++) { 353 const struct pathspec_item *item = &ps->items[j]; 354 355 if (item->len <= ce_len) 356 continue; 357 if (item->match[ce_len] != '/') 358 continue; 359 if (strncmp(ce->name, item->match, ce_len)) 360 continue; 361 if (item->len == ce_len + 1) 362 continue; 363 364 die(_("Pathspec '%s' is in submodule '%.*s'"), 365 item->original, ce_len, ce->name); 366 } 367 } 368 } 369 370 enum submodule_update_type parse_submodule_update_type(const char *value) 371 { 372 if (!strcmp(value, "none")) 373 return SM_UPDATE_NONE; 374 else if (!strcmp(value, "checkout")) 375 return SM_UPDATE_CHECKOUT; 376 else if (!strcmp(value, "rebase")) 377 return SM_UPDATE_REBASE; 378 else if (!strcmp(value, "merge")) 379 return SM_UPDATE_MERGE; 380 else if (*value == '!') 381 return SM_UPDATE_COMMAND; 382 else 383 return SM_UPDATE_UNSPECIFIED; 384 } 385 386 int parse_submodule_update_strategy(const char *value, 387 struct submodule_update_strategy *dst) 388 { 389 enum submodule_update_type type; 390 391 free((void*)dst->command); 392 dst->command = NULL; 393 394 type = parse_submodule_update_type(value); 395 if (type == SM_UPDATE_UNSPECIFIED) 396 return -1; 397 398 dst->type = type; 399 if (type == SM_UPDATE_COMMAND) 400 dst->command = xstrdup(value + 1); 401 402 return 0; 403 } 404 405 void submodule_update_strategy_release(struct submodule_update_strategy *strategy) 406 { 407 free((char *) strategy->command); 408 } 409 410 const char *submodule_update_type_to_string(enum submodule_update_type type) 411 { 412 switch (type) { 413 case SM_UPDATE_CHECKOUT: 414 return "checkout"; 415 case SM_UPDATE_MERGE: 416 return "merge"; 417 case SM_UPDATE_REBASE: 418 return "rebase"; 419 case SM_UPDATE_NONE: 420 return "none"; 421 case SM_UPDATE_UNSPECIFIED: 422 case SM_UPDATE_COMMAND: 423 BUG("init_submodule() should handle type %d", type); 424 default: 425 BUG("unexpected update strategy type: %d", type); 426 } 427 } 428 429 void handle_ignore_submodules_arg(struct diff_options *diffopt, 430 const char *arg) 431 { 432 diffopt->flags.ignore_submodule_set = 1; 433 diffopt->flags.ignore_submodules = 0; 434 diffopt->flags.ignore_untracked_in_submodules = 0; 435 diffopt->flags.ignore_dirty_submodules = 0; 436 437 if (!strcmp(arg, "all")) 438 diffopt->flags.ignore_submodules = 1; 439 else if (!strcmp(arg, "untracked")) 440 diffopt->flags.ignore_untracked_in_submodules = 1; 441 else if (!strcmp(arg, "dirty")) 442 diffopt->flags.ignore_dirty_submodules = 1; 443 else if (strcmp(arg, "none")) 444 die(_("bad --ignore-submodules argument: %s"), arg); 445 /* 446 * Please update _git_status() in git-completion.bash when you 447 * add new options 448 */ 449 } 450 451 static int prepare_submodule_diff_summary(struct repository *r, struct rev_info *rev, 452 const char *path, 453 struct commit *left, struct commit *right, 454 struct commit_list *merge_bases) 455 { 456 struct commit_list *list; 457 458 repo_init_revisions(r, rev, NULL); 459 setup_revisions(0, NULL, rev, NULL); 460 rev->left_right = 1; 461 rev->first_parent_only = 1; 462 left->object.flags |= SYMMETRIC_LEFT; 463 add_pending_object(rev, &left->object, path); 464 add_pending_object(rev, &right->object, path); 465 for (list = merge_bases; list; list = list->next) { 466 list->item->object.flags |= UNINTERESTING; 467 add_pending_object(rev, &list->item->object, 468 oid_to_hex(&list->item->object.oid)); 469 } 470 return prepare_revision_walk(rev); 471 } 472 473 static void print_submodule_diff_summary(struct repository *r, struct rev_info *rev, struct diff_options *o) 474 { 475 static const char format[] = " %m %s"; 476 struct strbuf sb = STRBUF_INIT; 477 struct commit *commit; 478 479 while ((commit = get_revision(rev))) { 480 struct pretty_print_context ctx = {0}; 481 ctx.date_mode = rev->date_mode; 482 ctx.output_encoding = get_log_output_encoding(); 483 strbuf_setlen(&sb, 0); 484 repo_format_commit_message(r, commit, format, &sb, 485 &ctx); 486 strbuf_addch(&sb, '\n'); 487 if (commit->object.flags & SYMMETRIC_LEFT) 488 diff_emit_submodule_del(o, sb.buf); 489 else 490 diff_emit_submodule_add(o, sb.buf); 491 } 492 strbuf_release(&sb); 493 } 494 495 void prepare_submodule_repo_env(struct strvec *out) 496 { 497 prepare_other_repo_env(out, DEFAULT_GIT_DIR_ENVIRONMENT); 498 } 499 500 static void prepare_submodule_repo_env_in_gitdir(struct strvec *out) 501 { 502 prepare_other_repo_env(out, "."); 503 } 504 505 /* 506 * Initialize a repository struct for a submodule based on the provided 'path'. 507 * 508 * Returns the repository struct on success, 509 * NULL when the submodule is not present. 510 */ 511 static struct repository *open_submodule(const char *path) 512 { 513 struct strbuf sb = STRBUF_INIT; 514 struct repository *out = xmalloc(sizeof(*out)); 515 516 if (submodule_to_gitdir(the_repository, &sb, path) || 517 repo_init(out, sb.buf, NULL)) { 518 strbuf_release(&sb); 519 free(out); 520 return NULL; 521 } 522 523 /* Mark it as a submodule */ 524 out->submodule_prefix = xstrdup(path); 525 526 strbuf_release(&sb); 527 return out; 528 } 529 530 /* 531 * Helper function to display the submodule header line prior to the full 532 * summary output. 533 * 534 * If it can locate the submodule git directory it will create a repository 535 * handle for the submodule and lookup both the left and right commits and 536 * put them into the left and right pointers. 537 */ 538 static void show_submodule_header(struct diff_options *o, 539 const char *path, 540 struct object_id *one, struct object_id *two, 541 unsigned dirty_submodule, 542 struct repository *sub, 543 struct commit **left, struct commit **right, 544 struct commit_list **merge_bases) 545 { 546 const char *message = NULL; 547 struct strbuf sb = STRBUF_INIT; 548 int fast_forward = 0, fast_backward = 0; 549 550 if (dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) 551 diff_emit_submodule_untracked(o, path); 552 553 if (dirty_submodule & DIRTY_SUBMODULE_MODIFIED) 554 diff_emit_submodule_modified(o, path); 555 556 if (is_null_oid(one)) 557 message = "(new submodule)"; 558 else if (is_null_oid(two)) 559 message = "(submodule deleted)"; 560 561 if (!sub) { 562 if (!message) 563 message = "(commits not present)"; 564 goto output_header; 565 } 566 567 /* 568 * Attempt to lookup the commit references, and determine if this is 569 * a fast forward or fast backwards update. 570 */ 571 *left = lookup_commit_reference(sub, one); 572 *right = lookup_commit_reference(sub, two); 573 574 /* 575 * Warn about missing commits in the submodule project, but only if 576 * they aren't null. 577 */ 578 if ((!is_null_oid(one) && !*left) || 579 (!is_null_oid(two) && !*right)) 580 message = "(commits not present)"; 581 582 *merge_bases = NULL; 583 if (repo_get_merge_bases(sub, *left, *right, merge_bases) < 0) { 584 message = "(corrupt repository)"; 585 goto output_header; 586 } 587 588 if (*merge_bases) { 589 if ((*merge_bases)->item == *left) 590 fast_forward = 1; 591 else if ((*merge_bases)->item == *right) 592 fast_backward = 1; 593 } 594 595 if (oideq(one, two)) { 596 strbuf_release(&sb); 597 return; 598 } 599 600 output_header: 601 strbuf_addf(&sb, "Submodule %s ", path); 602 strbuf_add_unique_abbrev(&sb, one, DEFAULT_ABBREV); 603 strbuf_addstr(&sb, (fast_backward || fast_forward) ? ".." : "..."); 604 strbuf_add_unique_abbrev(&sb, two, DEFAULT_ABBREV); 605 if (message) 606 strbuf_addf(&sb, " %s\n", message); 607 else 608 strbuf_addf(&sb, "%s:\n", fast_backward ? " (rewind)" : ""); 609 diff_emit_submodule_header(o, sb.buf); 610 611 strbuf_release(&sb); 612 } 613 614 void show_submodule_diff_summary(struct diff_options *o, const char *path, 615 struct object_id *one, struct object_id *two, 616 unsigned dirty_submodule) 617 { 618 struct rev_info rev = REV_INFO_INIT; 619 struct commit *left = NULL, *right = NULL; 620 struct commit_list *merge_bases = NULL; 621 struct repository *sub; 622 623 sub = open_submodule(path); 624 show_submodule_header(o, path, one, two, dirty_submodule, 625 sub, &left, &right, &merge_bases); 626 627 /* 628 * If we don't have both a left and a right pointer, there is no 629 * reason to try and display a summary. The header line should contain 630 * all the information the user needs. 631 */ 632 if (!left || !right || !sub) 633 goto out; 634 635 /* Treat revision walker failure the same as missing commits */ 636 if (prepare_submodule_diff_summary(sub, &rev, path, left, right, merge_bases)) { 637 diff_emit_submodule_error(o, "(revision walker failed)\n"); 638 goto out; 639 } 640 641 print_submodule_diff_summary(sub, &rev, o); 642 643 out: 644 commit_list_free(merge_bases); 645 release_revisions(&rev); 646 clear_commit_marks(left, ~0); 647 clear_commit_marks(right, ~0); 648 if (sub) { 649 repo_clear(sub); 650 free(sub); 651 } 652 } 653 654 void show_submodule_inline_diff(struct diff_options *o, const char *path, 655 struct object_id *one, struct object_id *two, 656 unsigned dirty_submodule) 657 { 658 const struct object_id *old_oid = the_hash_algo->empty_tree, *new_oid = the_hash_algo->empty_tree; 659 struct commit *left = NULL, *right = NULL; 660 struct commit_list *merge_bases = NULL; 661 struct child_process cp = CHILD_PROCESS_INIT; 662 struct strbuf sb = STRBUF_INIT; 663 struct repository *sub; 664 665 sub = open_submodule(path); 666 show_submodule_header(o, path, one, two, dirty_submodule, 667 sub, &left, &right, &merge_bases); 668 669 /* We need a valid left and right commit to display a difference */ 670 if (!(left || is_null_oid(one)) || 671 !(right || is_null_oid(two))) 672 goto done; 673 674 if (left) 675 old_oid = one; 676 if (right) 677 new_oid = two; 678 679 cp.git_cmd = 1; 680 cp.dir = path; 681 cp.out = -1; 682 cp.no_stdin = 1; 683 684 /* TODO: other options may need to be passed here. */ 685 strvec_pushl(&cp.args, "diff", "--submodule=diff", NULL); 686 strvec_pushf(&cp.args, "--color=%s", want_color(o->use_color) ? 687 "always" : "never"); 688 689 if (o->flags.reverse_diff) { 690 strvec_pushf(&cp.args, "--src-prefix=%s%s/", 691 o->b_prefix, path); 692 strvec_pushf(&cp.args, "--dst-prefix=%s%s/", 693 o->a_prefix, path); 694 } else { 695 strvec_pushf(&cp.args, "--src-prefix=%s%s/", 696 o->a_prefix, path); 697 strvec_pushf(&cp.args, "--dst-prefix=%s%s/", 698 o->b_prefix, path); 699 } 700 strvec_push(&cp.args, oid_to_hex(old_oid)); 701 /* 702 * If the submodule has modified content, we will diff against the 703 * work tree, under the assumption that the user has asked for the 704 * diff format and wishes to actually see all differences even if they 705 * haven't yet been committed to the submodule yet. 706 */ 707 if (!(dirty_submodule & DIRTY_SUBMODULE_MODIFIED)) 708 strvec_push(&cp.args, oid_to_hex(new_oid)); 709 710 prepare_submodule_repo_env(&cp.env); 711 712 if (!is_directory(path)) { 713 /* fall back to absorbed git dir, if any */ 714 if (!sub) 715 goto done; 716 cp.dir = sub->gitdir; 717 strvec_push(&cp.env, GIT_DIR_ENVIRONMENT "=."); 718 strvec_push(&cp.env, GIT_WORK_TREE_ENVIRONMENT "=."); 719 } 720 721 if (start_command(&cp)) { 722 diff_emit_submodule_error(o, "(diff failed)\n"); 723 goto done; 724 } 725 726 while (strbuf_getwholeline_fd(&sb, cp.out, '\n') != EOF) 727 diff_emit_submodule_pipethrough(o, sb.buf, sb.len); 728 729 if (finish_command(&cp)) 730 diff_emit_submodule_error(o, "(diff failed)\n"); 731 732 done: 733 strbuf_release(&sb); 734 commit_list_free(merge_bases); 735 if (left) 736 clear_commit_marks(left, ~0); 737 if (right) 738 clear_commit_marks(right, ~0); 739 if (sub) { 740 repo_clear(sub); 741 free(sub); 742 } 743 } 744 745 int should_update_submodules(void) 746 { 747 return config_update_recurse_submodules == RECURSE_SUBMODULES_ON; 748 } 749 750 const struct submodule *submodule_from_ce(const struct cache_entry *ce) 751 { 752 if (!S_ISGITLINK(ce->ce_mode)) 753 return NULL; 754 755 if (!should_update_submodules()) 756 return NULL; 757 758 return submodule_from_path(the_repository, null_oid(the_hash_algo), ce->name); 759 } 760 761 762 struct collect_changed_submodules_cb_data { 763 struct repository *repo; 764 struct string_list *changed; 765 const struct object_id *commit_oid; 766 }; 767 768 /* 769 * this would normally be two functions: default_name_from_path() and 770 * path_from_default_name(). Since the default name is the same as 771 * the submodule path we can get away with just one function which only 772 * checks whether there is a submodule in the working directory at that 773 * location. 774 */ 775 static const char *default_name_or_path(const char *path_or_name) 776 { 777 int error_code; 778 779 if (!is_submodule_populated_gently(path_or_name, &error_code)) 780 return NULL; 781 782 return path_or_name; 783 } 784 785 /* 786 * Holds relevant information for a changed submodule. Used as the .util 787 * member of the changed submodule name string_list_item. 788 * 789 * (super_oid, path) allows the submodule config to be read from _some_ 790 * .gitmodules file. We store this information the first time we find a 791 * superproject commit that points to the submodule, but this is 792 * arbitrary - we can choose any (super_oid, path) that matches the 793 * submodule's name. 794 * 795 * NEEDSWORK: Storing an arbitrary commit is undesirable because we can't 796 * guarantee that we're reading the commit that the user would expect. A better 797 * scheme would be to just fetch a submodule by its name. This requires two 798 * steps: 799 * - Create a function that behaves like repo_submodule_init(), but accepts a 800 * submodule name instead of treeish_name and path. This should be easy 801 * because repo_submodule_init() internally uses the submodule's name. 802 * 803 * - Replace most instances of 'struct submodule' (which is the .gitmodules 804 * config) with just the submodule name. This is OK because we expect 805 * submodule settings to be stored in .git/config (via "git submodule init"), 806 * not .gitmodules. This also lets us delete get_non_gitmodules_submodule(), 807 * which constructs a bogus 'struct submodule' for the sake of giving a 808 * placeholder name to a gitlink. 809 */ 810 struct changed_submodule_data { 811 /* 812 * The first superproject commit in the rev walk that points to 813 * the submodule. 814 */ 815 const struct object_id *super_oid; 816 /* 817 * Path to the submodule in the superproject commit referenced 818 * by 'super_oid'. 819 */ 820 char *path; 821 /* The submodule commits that have changed in the rev walk. */ 822 struct oid_array new_commits; 823 }; 824 825 static void changed_submodule_data_clear(struct changed_submodule_data *cs_data) 826 { 827 oid_array_clear(&cs_data->new_commits); 828 free(cs_data->path); 829 } 830 831 static void collect_changed_submodules_cb(struct diff_queue_struct *q, 832 struct diff_options *options UNUSED, 833 void *data) 834 { 835 struct collect_changed_submodules_cb_data *me = data; 836 struct string_list *changed = me->changed; 837 const struct object_id *commit_oid = me->commit_oid; 838 int i; 839 840 for (i = 0; i < q->nr; i++) { 841 struct diff_filepair *p = q->queue[i]; 842 const struct submodule *submodule; 843 const char *name; 844 struct string_list_item *item; 845 struct changed_submodule_data *cs_data; 846 847 if (!S_ISGITLINK(p->two->mode)) 848 continue; 849 850 submodule = submodule_from_path(me->repo, 851 commit_oid, p->two->path); 852 if (submodule) 853 name = submodule->name; 854 else { 855 name = default_name_or_path(p->two->path); 856 /* make sure name does not collide with existing one */ 857 if (name) 858 submodule = submodule_from_name(me->repo, 859 commit_oid, name); 860 if (submodule) { 861 warning(_("Submodule in commit %s at path: " 862 "'%s' collides with a submodule named " 863 "the same. Skipping it."), 864 oid_to_hex(commit_oid), p->two->path); 865 name = NULL; 866 } 867 } 868 869 if (!name) 870 continue; 871 872 item = string_list_insert(changed, name); 873 if (item->util) 874 cs_data = item->util; 875 else { 876 item->util = xcalloc(1, sizeof(struct changed_submodule_data)); 877 cs_data = item->util; 878 cs_data->super_oid = commit_oid; 879 cs_data->path = xstrdup(p->two->path); 880 } 881 oid_array_append(&cs_data->new_commits, &p->two->oid); 882 } 883 } 884 885 /* 886 * Collect the paths of submodules in 'changed' which have changed based on 887 * the revisions as specified in 'argv'. Each entry in 'changed' will also 888 * have a corresponding 'struct oid_array' (in the 'util' field) which lists 889 * what the submodule pointers were updated to during the change. 890 */ 891 static void collect_changed_submodules(struct repository *r, 892 struct string_list *changed, 893 struct strvec *argv) 894 { 895 struct rev_info rev; 896 const struct commit *commit; 897 int save_warning; 898 struct setup_revision_opt s_r_opt = { 899 .assume_dashdash = 1, 900 }; 901 struct repo_config_values *cfg = repo_config_values(the_repository); 902 903 save_warning = cfg->warn_on_object_refname_ambiguity; 904 cfg->warn_on_object_refname_ambiguity = 0; 905 repo_init_revisions(r, &rev, NULL); 906 setup_revisions_from_strvec(argv, &rev, &s_r_opt); 907 cfg->warn_on_object_refname_ambiguity = save_warning; 908 if (prepare_revision_walk(&rev)) 909 die(_("revision walk setup failed")); 910 911 while ((commit = get_revision(&rev))) { 912 struct rev_info diff_rev; 913 struct collect_changed_submodules_cb_data data; 914 data.repo = r; 915 data.changed = changed; 916 data.commit_oid = &commit->object.oid; 917 918 repo_init_revisions(r, &diff_rev, NULL); 919 diff_rev.diffopt.output_format |= DIFF_FORMAT_CALLBACK; 920 diff_rev.diffopt.format_callback = collect_changed_submodules_cb; 921 diff_rev.diffopt.format_callback_data = &data; 922 diff_rev.dense_combined_merges = 1; 923 diff_tree_combined_merge(commit, &diff_rev); 924 release_revisions(&diff_rev); 925 } 926 927 reset_revision_walk(); 928 release_revisions(&rev); 929 } 930 931 static void free_submodules_data(struct string_list *submodules) 932 { 933 struct string_list_item *item; 934 for_each_string_list_item(item, submodules) 935 changed_submodule_data_clear(item->util); 936 937 string_list_clear(submodules, 1); 938 } 939 940 static int has_remote(const struct reference *ref UNUSED, void *cb_data UNUSED) 941 { 942 return 1; 943 } 944 945 static int append_oid_to_argv(const struct object_id *oid, void *data) 946 { 947 struct strvec *argv = data; 948 strvec_push(argv, oid_to_hex(oid)); 949 return 0; 950 } 951 952 struct has_commit_data { 953 struct repository *repo; 954 int result; 955 const char *path; 956 const struct object_id *super_oid; 957 }; 958 959 static int check_has_commit(const struct object_id *oid, void *data) 960 { 961 struct has_commit_data *cb = data; 962 struct repository subrepo; 963 enum object_type type; 964 965 if (repo_submodule_init(&subrepo, cb->repo, cb->path, cb->super_oid)) { 966 cb->result = 0; 967 /* subrepo failed to init, so don't clean it up. */ 968 return 0; 969 } 970 971 type = odb_read_object_info(subrepo.objects, oid, NULL); 972 973 switch (type) { 974 case OBJ_COMMIT: 975 goto cleanup; 976 case OBJ_BAD: 977 /* 978 * Object is missing or invalid. If invalid, an error message 979 * has already been printed. 980 */ 981 cb->result = 0; 982 goto cleanup; 983 default: 984 die(_("submodule entry '%s' (%s) is a %s, not a commit"), 985 cb->path, oid_to_hex(oid), type_name(type)); 986 } 987 cleanup: 988 repo_clear(&subrepo); 989 return 0; 990 } 991 992 static int submodule_has_commits(struct repository *r, 993 const char *path, 994 const struct object_id *super_oid, 995 struct oid_array *commits) 996 { 997 struct has_commit_data has_commit = { 998 .repo = r, 999 .result = 1, 1000 .path = path, 1001 .super_oid = super_oid 1002 }; 1003 1004 if (validate_submodule_path(path) < 0) 1005 exit(128); 1006 1007 oid_array_for_each_unique(commits, check_has_commit, &has_commit); 1008 1009 if (has_commit.result) { 1010 /* 1011 * Even if the submodule is checked out and the commit is 1012 * present, make sure it exists in the submodule's object store 1013 * and that it is reachable from a ref. 1014 */ 1015 struct child_process cp = CHILD_PROCESS_INIT; 1016 struct strbuf out = STRBUF_INIT; 1017 1018 strvec_pushl(&cp.args, "rev-list", "-n", "1", NULL); 1019 oid_array_for_each_unique(commits, append_oid_to_argv, &cp.args); 1020 strvec_pushl(&cp.args, "--not", "--all", NULL); 1021 1022 prepare_submodule_repo_env(&cp.env); 1023 cp.git_cmd = 1; 1024 cp.no_stdin = 1; 1025 cp.dir = path; 1026 1027 if (capture_command(&cp, &out, GIT_MAX_HEXSZ + 1) || out.len) 1028 has_commit.result = 0; 1029 1030 strbuf_release(&out); 1031 } 1032 1033 return has_commit.result; 1034 } 1035 1036 static int submodule_needs_pushing(struct repository *r, 1037 const char *path, 1038 struct oid_array *commits) 1039 { 1040 if (!submodule_has_commits(r, path, null_oid(the_hash_algo), commits)) 1041 /* 1042 * NOTE: We do consider it safe to return "no" here. The 1043 * correct answer would be "We do not know" instead of 1044 * "No push needed", but it is quite hard to change 1045 * the submodule pointer without having the submodule 1046 * around. If a user did however change the submodules 1047 * without having the submodule around, this indicates 1048 * an expert who knows what they are doing or a 1049 * maintainer integrating work from other people. In 1050 * both cases it should be safe to skip this check. 1051 */ 1052 return 0; 1053 1054 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) { 1055 struct child_process cp = CHILD_PROCESS_INIT; 1056 struct strbuf buf = STRBUF_INIT; 1057 int needs_pushing = 0; 1058 1059 strvec_push(&cp.args, "rev-list"); 1060 oid_array_for_each_unique(commits, append_oid_to_argv, &cp.args); 1061 strvec_pushl(&cp.args, "--not", "--remotes", "-n", "1" , NULL); 1062 1063 prepare_submodule_repo_env(&cp.env); 1064 cp.git_cmd = 1; 1065 cp.no_stdin = 1; 1066 cp.out = -1; 1067 cp.dir = path; 1068 if (start_command(&cp)) 1069 die(_("Could not run 'git rev-list --not --remotes -n 1' command in submodule %s"), 1070 path); 1071 if (strbuf_read(&buf, cp.out, the_hash_algo->hexsz + 1)) 1072 needs_pushing = 1; 1073 finish_command(&cp); 1074 close(cp.out); 1075 strbuf_release(&buf); 1076 return needs_pushing; 1077 } 1078 1079 return 0; 1080 } 1081 1082 int find_unpushed_submodules(struct repository *r, 1083 struct oid_array *commits, 1084 const char *remotes_name, 1085 struct string_list *needs_pushing) 1086 { 1087 struct string_list submodules = STRING_LIST_INIT_DUP; 1088 struct string_list_item *name; 1089 struct strvec argv = STRVEC_INIT; 1090 1091 /* argv.v[0] will be ignored by setup_revisions */ 1092 strvec_push(&argv, "find_unpushed_submodules"); 1093 oid_array_for_each_unique(commits, append_oid_to_argv, &argv); 1094 strvec_push(&argv, "--not"); 1095 strvec_pushf(&argv, "--remotes=%s", remotes_name); 1096 1097 collect_changed_submodules(r, &submodules, &argv); 1098 1099 for_each_string_list_item(name, &submodules) { 1100 struct changed_submodule_data *cs_data = name->util; 1101 const struct submodule *submodule; 1102 const char *path = NULL; 1103 1104 submodule = submodule_from_name(r, null_oid(the_hash_algo), name->string); 1105 if (submodule) 1106 path = submodule->path; 1107 else 1108 path = default_name_or_path(name->string); 1109 1110 if (!path) 1111 continue; 1112 1113 if (submodule_needs_pushing(r, path, &cs_data->new_commits)) 1114 string_list_insert(needs_pushing, path); 1115 } 1116 1117 free_submodules_data(&submodules); 1118 strvec_clear(&argv); 1119 1120 return needs_pushing->nr; 1121 } 1122 1123 static int push_submodule(const char *path, 1124 const struct remote *remote, 1125 const struct refspec *rs, 1126 const struct string_list *push_options, 1127 int dry_run) 1128 { 1129 if (validate_submodule_path(path) < 0) 1130 exit(128); 1131 1132 if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) { 1133 struct child_process cp = CHILD_PROCESS_INIT; 1134 strvec_push(&cp.args, "push"); 1135 /* 1136 * When recursing into a submodule, treat any "only" configurations as "on- 1137 * demand", since "only" would not work (we need all submodules to be pushed 1138 * in order to be able to push the superproject). 1139 */ 1140 strvec_push(&cp.args, "--recurse-submodules=only-is-on-demand"); 1141 if (dry_run) 1142 strvec_push(&cp.args, "--dry-run"); 1143 1144 if (push_options && push_options->nr) { 1145 const struct string_list_item *item; 1146 for_each_string_list_item(item, push_options) 1147 strvec_pushf(&cp.args, "--push-option=%s", 1148 item->string); 1149 } 1150 1151 if (remote->origin != REMOTE_UNCONFIGURED) { 1152 int i; 1153 strvec_push(&cp.args, remote->name); 1154 for (i = 0; i < rs->nr; i++) 1155 strvec_push(&cp.args, rs->items[i].raw); 1156 } 1157 1158 prepare_submodule_repo_env(&cp.env); 1159 cp.git_cmd = 1; 1160 cp.no_stdin = 1; 1161 cp.dir = path; 1162 if (run_command(&cp)) 1163 return 0; 1164 close(cp.out); 1165 } 1166 1167 return 1; 1168 } 1169 1170 /* 1171 * Perform a check in the submodule to see if the remote and refspec work. 1172 * Die if the submodule can't be pushed. 1173 */ 1174 static void submodule_push_check(const char *path, const char *head, 1175 const struct remote *remote, 1176 const struct refspec *rs) 1177 { 1178 struct child_process cp = CHILD_PROCESS_INIT; 1179 int i; 1180 1181 if (validate_submodule_path(path) < 0) 1182 exit(128); 1183 1184 strvec_push(&cp.args, "submodule--helper"); 1185 strvec_push(&cp.args, "push-check"); 1186 strvec_push(&cp.args, head); 1187 strvec_push(&cp.args, remote->name); 1188 1189 for (i = 0; i < rs->nr; i++) 1190 strvec_push(&cp.args, rs->items[i].raw); 1191 1192 prepare_submodule_repo_env(&cp.env); 1193 cp.git_cmd = 1; 1194 cp.no_stdin = 1; 1195 cp.no_stdout = 1; 1196 cp.dir = path; 1197 1198 /* 1199 * Simply indicate if 'submodule--helper push-check' failed. 1200 * More detailed error information will be provided by the 1201 * child process. 1202 */ 1203 if (run_command(&cp)) 1204 die(_("process for submodule '%s' failed"), path); 1205 } 1206 1207 int push_unpushed_submodules(struct repository *r, 1208 struct oid_array *commits, 1209 const struct remote *remote, 1210 const struct refspec *rs, 1211 const struct string_list *push_options, 1212 int dry_run) 1213 { 1214 int i, ret = 1; 1215 struct string_list needs_pushing = STRING_LIST_INIT_DUP; 1216 1217 if (!find_unpushed_submodules(r, commits, 1218 remote->name, &needs_pushing)) 1219 return 1; 1220 1221 /* 1222 * Verify that the remote and refspec can be propagated to all 1223 * submodules. This check can be skipped if the remote and refspec 1224 * won't be propagated due to the remote being unconfigured (e.g. a URL 1225 * instead of a remote name). 1226 */ 1227 if (remote->origin != REMOTE_UNCONFIGURED) { 1228 char *head; 1229 struct object_id head_oid; 1230 1231 head = refs_resolve_refdup(get_main_ref_store(the_repository), 1232 "HEAD", 0, &head_oid, NULL); 1233 if (!head) 1234 die(_("Failed to resolve HEAD as a valid ref.")); 1235 1236 for (i = 0; i < needs_pushing.nr; i++) 1237 submodule_push_check(needs_pushing.items[i].string, 1238 head, remote, rs); 1239 free(head); 1240 } 1241 1242 /* Actually push the submodules */ 1243 for (i = 0; i < needs_pushing.nr; i++) { 1244 const char *path = needs_pushing.items[i].string; 1245 fprintf(stderr, _("Pushing submodule '%s'\n"), path); 1246 if (!push_submodule(path, remote, rs, 1247 push_options, dry_run)) { 1248 fprintf(stderr, _("Unable to push submodule '%s'\n"), path); 1249 ret = 0; 1250 } 1251 } 1252 1253 string_list_clear(&needs_pushing, 0); 1254 1255 return ret; 1256 } 1257 1258 static int append_oid_to_array(const struct reference *ref, void *data) 1259 { 1260 struct oid_array *array = data; 1261 oid_array_append(array, ref->oid); 1262 return 0; 1263 } 1264 1265 void check_for_new_submodule_commits(struct object_id *oid) 1266 { 1267 if (!initialized_fetch_ref_tips) { 1268 refs_for_each_ref(get_main_ref_store(the_repository), 1269 append_oid_to_array, &ref_tips_before_fetch); 1270 initialized_fetch_ref_tips = 1; 1271 } 1272 1273 oid_array_append(&ref_tips_after_fetch, oid); 1274 } 1275 1276 /* 1277 * Returns 1 if there is at least one submodule gitdir in 1278 * $GIT_DIR/modules and 0 otherwise. This follows 1279 * submodule_name_to_gitdir(), which looks for submodules in 1280 * $GIT_DIR/modules, not $GIT_COMMON_DIR. 1281 * 1282 * A submodule can be moved to $GIT_DIR/modules manually by running "git 1283 * submodule absorbgitdirs", or it may be initialized there by "git 1284 * submodule update". 1285 */ 1286 static int repo_has_absorbed_submodules(struct repository *r) 1287 { 1288 int ret; 1289 struct strbuf buf = STRBUF_INIT; 1290 1291 repo_git_path_append(r, &buf, "modules/"); 1292 ret = file_exists(buf.buf) && !is_empty_dir(buf.buf); 1293 strbuf_release(&buf); 1294 return ret; 1295 } 1296 1297 static void calculate_changed_submodule_paths(struct repository *r, 1298 struct string_list *changed_submodule_names) 1299 { 1300 struct strvec argv = STRVEC_INIT; 1301 struct string_list_item *name; 1302 1303 /* No need to check if no submodules would be fetched */ 1304 if (!submodule_from_path(r, NULL, NULL) && 1305 !repo_has_absorbed_submodules(r)) 1306 return; 1307 1308 strvec_push(&argv, "--"); /* argv[0] program name */ 1309 oid_array_for_each_unique(&ref_tips_after_fetch, 1310 append_oid_to_argv, &argv); 1311 strvec_push(&argv, "--not"); 1312 oid_array_for_each_unique(&ref_tips_before_fetch, 1313 append_oid_to_argv, &argv); 1314 1315 /* 1316 * Collect all submodules (whether checked out or not) for which new 1317 * commits have been recorded upstream in "changed_submodule_names". 1318 */ 1319 collect_changed_submodules(r, changed_submodule_names, &argv); 1320 1321 for_each_string_list_item(name, changed_submodule_names) { 1322 struct changed_submodule_data *cs_data = name->util; 1323 const struct submodule *submodule; 1324 const char *path = NULL; 1325 1326 submodule = submodule_from_name(r, null_oid(the_hash_algo), name->string); 1327 if (submodule) 1328 path = submodule->path; 1329 else 1330 path = default_name_or_path(name->string); 1331 1332 if (!path) 1333 continue; 1334 1335 if (submodule_has_commits(r, path, null_oid(the_hash_algo), &cs_data->new_commits)) { 1336 changed_submodule_data_clear(cs_data); 1337 *name->string = '\0'; 1338 } 1339 } 1340 1341 string_list_remove_empty_items(changed_submodule_names, 1); 1342 1343 strvec_clear(&argv); 1344 oid_array_clear(&ref_tips_before_fetch); 1345 oid_array_clear(&ref_tips_after_fetch); 1346 initialized_fetch_ref_tips = 0; 1347 } 1348 1349 int submodule_touches_in_range(struct repository *r, 1350 struct object_id *excl_oid, 1351 struct object_id *incl_oid) 1352 { 1353 struct string_list subs = STRING_LIST_INIT_DUP; 1354 struct strvec args = STRVEC_INIT; 1355 int ret; 1356 1357 /* No need to check if there are no submodules configured */ 1358 if (!submodule_from_path(r, NULL, NULL)) 1359 return 0; 1360 1361 strvec_push(&args, "--"); /* args[0] program name */ 1362 strvec_push(&args, oid_to_hex(incl_oid)); 1363 if (!is_null_oid(excl_oid)) { 1364 strvec_push(&args, "--not"); 1365 strvec_push(&args, oid_to_hex(excl_oid)); 1366 } 1367 1368 collect_changed_submodules(r, &subs, &args); 1369 ret = subs.nr; 1370 1371 strvec_clear(&args); 1372 1373 free_submodules_data(&subs); 1374 return ret; 1375 } 1376 1377 struct submodule_parallel_fetch { 1378 /* 1379 * The index of the last index entry processed by 1380 * get_fetch_task_from_index(). 1381 */ 1382 int index_count; 1383 /* 1384 * The index of the last string_list entry processed by 1385 * get_fetch_task_from_changed(). 1386 */ 1387 int changed_count; 1388 struct strvec args; 1389 struct repository *r; 1390 const char *prefix; 1391 int command_line_option; 1392 int default_option; 1393 int quiet; 1394 int result; 1395 1396 /* 1397 * Names of submodules that have new commits. Generated by 1398 * walking the newly fetched superproject commits. 1399 */ 1400 struct string_list changed_submodule_names; 1401 /* 1402 * Names of submodules that have already been processed. Lets us 1403 * avoid fetching the same submodule more than once. 1404 */ 1405 struct string_list seen_submodule_names; 1406 1407 /* Pending fetches by OIDs */ 1408 struct fetch_task **oid_fetch_tasks; 1409 int oid_fetch_tasks_nr, oid_fetch_tasks_alloc; 1410 1411 struct strbuf submodules_with_errors; 1412 }; 1413 #define SPF_INIT { \ 1414 .args = STRVEC_INIT, \ 1415 .changed_submodule_names = STRING_LIST_INIT_DUP, \ 1416 .seen_submodule_names = STRING_LIST_INIT_DUP, \ 1417 .submodules_with_errors = STRBUF_INIT, \ 1418 } 1419 1420 static int get_fetch_recurse_config(const struct submodule *submodule, 1421 struct submodule_parallel_fetch *spf) 1422 { 1423 if (spf->command_line_option != RECURSE_SUBMODULES_DEFAULT) 1424 return spf->command_line_option; 1425 1426 if (submodule) { 1427 char *key; 1428 const char *value; 1429 1430 int fetch_recurse = submodule->fetch_recurse; 1431 key = xstrfmt("submodule.%s.fetchRecurseSubmodules", submodule->name); 1432 if (!repo_config_get_string_tmp(spf->r, key, &value)) { 1433 fetch_recurse = parse_fetch_recurse_submodules_arg(key, value); 1434 } 1435 free(key); 1436 1437 if (fetch_recurse != RECURSE_SUBMODULES_NONE) 1438 /* local config overrules everything except commandline */ 1439 return fetch_recurse; 1440 } 1441 1442 return spf->default_option; 1443 } 1444 1445 /* 1446 * Fetch in progress (if callback data) or 1447 * pending (if in oid_fetch_tasks in struct submodule_parallel_fetch) 1448 */ 1449 struct fetch_task { 1450 struct repository *repo; 1451 const struct submodule *sub; 1452 unsigned free_sub : 1; /* Do we need to free the submodule? */ 1453 const char *default_argv; /* The default fetch mode. */ 1454 struct strvec git_args; /* Args for the child git process. */ 1455 1456 struct oid_array *commits; /* Ensure these commits are fetched */ 1457 }; 1458 1459 /** 1460 * When a submodule is not defined in .gitmodules, we cannot access it 1461 * via the regular submodule-config. Create a fake submodule, which we can 1462 * work on. 1463 */ 1464 static const struct submodule *get_non_gitmodules_submodule(const char *path) 1465 { 1466 struct submodule *ret; 1467 const char *name = default_name_or_path(path); 1468 1469 if (!name) 1470 return NULL; 1471 1472 CALLOC_ARRAY(ret, 1); 1473 ret->path = name; 1474 ret->name = name; 1475 1476 return (const struct submodule *) ret; 1477 } 1478 1479 static void fetch_task_free(struct fetch_task *p) 1480 { 1481 if (p->free_sub) 1482 free((void*)p->sub); 1483 p->free_sub = 0; 1484 p->sub = NULL; 1485 1486 if (p->repo) 1487 repo_clear(p->repo); 1488 FREE_AND_NULL(p->repo); 1489 1490 strvec_clear(&p->git_args); 1491 free(p); 1492 } 1493 1494 static struct repository *get_submodule_repo_for(struct repository *r, 1495 const char *path, 1496 const struct object_id *treeish_name) 1497 { 1498 struct repository *ret = xmalloc(sizeof(*ret)); 1499 1500 if (repo_submodule_init(ret, r, path, treeish_name)) { 1501 free(ret); 1502 return NULL; 1503 } 1504 1505 return ret; 1506 } 1507 1508 static struct fetch_task *fetch_task_create(struct submodule_parallel_fetch *spf, 1509 const char *path, 1510 const struct object_id *treeish_name) 1511 { 1512 struct fetch_task *task; 1513 1514 CALLOC_ARRAY(task, 1); 1515 1516 if (validate_submodule_path(path) < 0) 1517 exit(128); 1518 1519 task->sub = submodule_from_path(spf->r, treeish_name, path); 1520 1521 if (!task->sub) { 1522 /* 1523 * No entry in .gitmodules? Technically not a submodule, 1524 * but historically we supported repositories that happen to be 1525 * in-place where a gitlink is. Keep supporting them. 1526 */ 1527 task->sub = get_non_gitmodules_submodule(path); 1528 if (!task->sub) 1529 goto cleanup; 1530 1531 task->free_sub = 1; 1532 } 1533 1534 if (string_list_lookup(&spf->seen_submodule_names, task->sub->name)) 1535 goto cleanup; 1536 1537 switch (get_fetch_recurse_config(task->sub, spf)) 1538 { 1539 default: 1540 case RECURSE_SUBMODULES_DEFAULT: 1541 case RECURSE_SUBMODULES_ON_DEMAND: 1542 if (!task->sub || 1543 !string_list_lookup( 1544 &spf->changed_submodule_names, 1545 task->sub->name)) 1546 goto cleanup; 1547 task->default_argv = "on-demand"; 1548 break; 1549 case RECURSE_SUBMODULES_ON: 1550 task->default_argv = "yes"; 1551 break; 1552 case RECURSE_SUBMODULES_OFF: 1553 goto cleanup; 1554 } 1555 1556 task->repo = get_submodule_repo_for(spf->r, path, treeish_name); 1557 1558 return task; 1559 1560 cleanup: 1561 fetch_task_free(task); 1562 return NULL; 1563 } 1564 1565 static struct fetch_task * 1566 get_fetch_task_from_index(struct submodule_parallel_fetch *spf, 1567 struct strbuf *err) 1568 { 1569 for (; spf->index_count < spf->r->index->cache_nr; spf->index_count++) { 1570 const struct cache_entry *ce = 1571 spf->r->index->cache[spf->index_count]; 1572 struct fetch_task *task; 1573 1574 if (!S_ISGITLINK(ce->ce_mode)) 1575 continue; 1576 1577 task = fetch_task_create(spf, ce->name, null_oid(the_hash_algo)); 1578 if (!task) 1579 continue; 1580 1581 if (task->repo) { 1582 if (!spf->quiet) 1583 strbuf_addf(err, _("Fetching submodule %s%s\n"), 1584 spf->prefix, ce->name); 1585 1586 spf->index_count++; 1587 return task; 1588 } else { 1589 struct strbuf empty_submodule_path = STRBUF_INIT; 1590 1591 fetch_task_free(task); 1592 1593 /* 1594 * An empty directory is normal, 1595 * the submodule is not initialized 1596 */ 1597 strbuf_addf(&empty_submodule_path, "%s/%s/", 1598 spf->r->worktree, 1599 ce->name); 1600 if (S_ISGITLINK(ce->ce_mode) && 1601 !is_empty_dir(empty_submodule_path.buf)) { 1602 spf->result = 1; 1603 strbuf_addf(err, 1604 _("Could not access submodule '%s'\n"), 1605 ce->name); 1606 } 1607 strbuf_release(&empty_submodule_path); 1608 } 1609 } 1610 return NULL; 1611 } 1612 1613 static struct fetch_task * 1614 get_fetch_task_from_changed(struct submodule_parallel_fetch *spf, 1615 struct strbuf *err) 1616 { 1617 for (; spf->changed_count < spf->changed_submodule_names.nr; 1618 spf->changed_count++) { 1619 struct string_list_item item = 1620 spf->changed_submodule_names.items[spf->changed_count]; 1621 struct changed_submodule_data *cs_data = item.util; 1622 struct fetch_task *task; 1623 1624 if (!is_tree_submodule_active(spf->r, cs_data->super_oid,cs_data->path)) 1625 continue; 1626 1627 task = fetch_task_create(spf, cs_data->path, 1628 cs_data->super_oid); 1629 if (!task) 1630 continue; 1631 1632 if (!task->repo) { 1633 strbuf_addf(err, _("Could not access submodule '%s' at commit %s\n"), 1634 cs_data->path, 1635 repo_find_unique_abbrev(the_repository, cs_data->super_oid, DEFAULT_ABBREV)); 1636 1637 fetch_task_free(task); 1638 continue; 1639 } 1640 1641 if (!spf->quiet) 1642 strbuf_addf(err, 1643 _("Fetching submodule %s%s at commit %s\n"), 1644 spf->prefix, task->sub->path, 1645 repo_find_unique_abbrev(the_repository, cs_data->super_oid, 1646 DEFAULT_ABBREV)); 1647 1648 spf->changed_count++; 1649 /* 1650 * NEEDSWORK: Submodules set/unset a value for 1651 * core.worktree when they are populated/unpopulated by 1652 * "git checkout" (and similar commands, see 1653 * submodule_move_head() and 1654 * connect_work_tree_and_git_dir()), but if the 1655 * submodule is unpopulated in another way (e.g. "git 1656 * rm", "rm -r"), core.worktree will still be set even 1657 * though the directory doesn't exist, and the child 1658 * process will crash while trying to chdir into the 1659 * nonexistent directory. 1660 * 1661 * In this case, we know that the submodule has no 1662 * working tree, so we can work around this by 1663 * setting "--work-tree=." (--bare does not work because 1664 * worktree settings take precedence over bare-ness). 1665 * However, this is not necessarily true in other cases, 1666 * so a generalized solution is still necessary. 1667 * 1668 * Possible solutions: 1669 * - teach "git [add|rm]" to unset core.worktree and 1670 * discourage users from removing submodules without 1671 * using a Git command. 1672 * - teach submodule child processes to ignore stale 1673 * core.worktree values. 1674 */ 1675 strvec_push(&task->git_args, "--work-tree=."); 1676 return task; 1677 } 1678 return NULL; 1679 } 1680 1681 static int get_next_submodule(struct child_process *cp, struct strbuf *err, 1682 void *data, void **task_cb) 1683 { 1684 struct submodule_parallel_fetch *spf = data; 1685 struct fetch_task *task = 1686 get_fetch_task_from_index(spf, err); 1687 if (!task) 1688 task = get_fetch_task_from_changed(spf, err); 1689 1690 if (task) { 1691 child_process_init(cp); 1692 cp->dir = task->repo->gitdir; 1693 prepare_submodule_repo_env_in_gitdir(&cp->env); 1694 cp->git_cmd = 1; 1695 strvec_init(&cp->args); 1696 if (task->git_args.nr) 1697 strvec_pushv(&cp->args, task->git_args.v); 1698 strvec_pushv(&cp->args, spf->args.v); 1699 strvec_push(&cp->args, task->default_argv); 1700 strvec_pushf(&cp->args, "--submodule-prefix=%s%s/", 1701 spf->prefix, task->sub->path); 1702 1703 *task_cb = task; 1704 1705 string_list_insert(&spf->seen_submodule_names, task->sub->name); 1706 return 1; 1707 } 1708 1709 if (spf->oid_fetch_tasks_nr) { 1710 struct fetch_task *task = 1711 spf->oid_fetch_tasks[spf->oid_fetch_tasks_nr - 1]; 1712 struct child_process cp_remote = CHILD_PROCESS_INIT; 1713 struct strbuf remote_name = STRBUF_INIT; 1714 spf->oid_fetch_tasks_nr--; 1715 1716 child_process_init(cp); 1717 prepare_submodule_repo_env_in_gitdir(&cp->env); 1718 cp->git_cmd = 1; 1719 cp->dir = task->repo->gitdir; 1720 1721 strvec_init(&cp->args); 1722 strvec_pushv(&cp->args, spf->args.v); 1723 strvec_push(&cp->args, "on-demand"); 1724 strvec_pushf(&cp->args, "--submodule-prefix=%s%s/", 1725 spf->prefix, task->sub->path); 1726 1727 cp_remote.git_cmd = 1; 1728 strvec_pushl(&cp_remote.args, "submodule--helper", 1729 "get-default-remote", task->sub->path, NULL); 1730 1731 if (!capture_command(&cp_remote, &remote_name, 0)) { 1732 strbuf_trim_trailing_newline(&remote_name); 1733 strvec_push(&cp->args, remote_name.buf); 1734 } else { 1735 /* Fallback to "origin" if the helper fails */ 1736 strvec_push(&cp->args, "origin"); 1737 } 1738 strbuf_release(&remote_name); 1739 1740 oid_array_for_each_unique(task->commits, 1741 append_oid_to_argv, &cp->args); 1742 1743 *task_cb = task; 1744 return 1; 1745 } 1746 1747 return 0; 1748 } 1749 1750 static int fetch_start_failure(struct strbuf *err UNUSED, 1751 void *cb, void *task_cb) 1752 { 1753 struct submodule_parallel_fetch *spf = cb; 1754 struct fetch_task *task = task_cb; 1755 1756 spf->result = 1; 1757 1758 fetch_task_free(task); 1759 return 0; 1760 } 1761 1762 static int commit_missing_in_sub(const struct object_id *oid, void *data) 1763 { 1764 struct repository *subrepo = data; 1765 enum object_type type = odb_read_object_info(subrepo->objects, oid, NULL); 1766 1767 return type != OBJ_COMMIT; 1768 } 1769 1770 static int fetch_finish(int retvalue, struct strbuf *err UNUSED, 1771 void *cb, void *task_cb) 1772 { 1773 struct submodule_parallel_fetch *spf = cb; 1774 struct fetch_task *task = task_cb; 1775 1776 struct string_list_item *it; 1777 struct changed_submodule_data *cs_data; 1778 1779 if (!task || !task->sub) 1780 BUG("callback cookie bogus"); 1781 1782 if (retvalue) { 1783 /* 1784 * NEEDSWORK: This indicates that the overall fetch 1785 * failed, even though there may be a subsequent fetch 1786 * by commit hash that might work. It may be a good 1787 * idea to not indicate failure in this case, and only 1788 * indicate failure if the subsequent fetch fails. 1789 */ 1790 spf->result = 1; 1791 1792 strbuf_addf(&spf->submodules_with_errors, "\t%s\n", 1793 task->sub->name); 1794 } 1795 1796 /* Is this the second time we process this submodule? */ 1797 if (task->commits) 1798 goto out; 1799 1800 it = string_list_lookup(&spf->changed_submodule_names, task->sub->name); 1801 if (!it) 1802 /* Could be an unchanged submodule, not contained in the list */ 1803 goto out; 1804 1805 cs_data = it->util; 1806 oid_array_filter(&cs_data->new_commits, 1807 commit_missing_in_sub, 1808 task->repo); 1809 1810 /* Are there commits we want, but do not exist? */ 1811 if (cs_data->new_commits.nr) { 1812 task->commits = &cs_data->new_commits; 1813 ALLOC_GROW(spf->oid_fetch_tasks, 1814 spf->oid_fetch_tasks_nr + 1, 1815 spf->oid_fetch_tasks_alloc); 1816 spf->oid_fetch_tasks[spf->oid_fetch_tasks_nr] = task; 1817 spf->oid_fetch_tasks_nr++; 1818 return 0; 1819 } 1820 1821 out: 1822 fetch_task_free(task); 1823 return 0; 1824 } 1825 1826 int fetch_submodules(struct repository *r, 1827 const struct strvec *options, 1828 const char *prefix, int command_line_option, 1829 int default_option, 1830 int quiet, int max_parallel_jobs) 1831 { 1832 struct submodule_parallel_fetch spf = SPF_INIT; 1833 const struct run_process_parallel_opts opts = { 1834 .tr2_category = "submodule", 1835 .tr2_label = "parallel/fetch", 1836 1837 .processes = max_parallel_jobs, 1838 1839 .get_next_task = get_next_submodule, 1840 .start_failure = fetch_start_failure, 1841 .task_finished = fetch_finish, 1842 .data = &spf, 1843 }; 1844 1845 spf.r = r; 1846 spf.command_line_option = command_line_option; 1847 spf.default_option = default_option; 1848 spf.quiet = quiet; 1849 spf.prefix = prefix; 1850 1851 if (!r->worktree) 1852 goto out; 1853 1854 if (repo_read_index(r) < 0) 1855 die(_("index file corrupt")); 1856 1857 strvec_push(&spf.args, "fetch"); 1858 strvec_pushv(&spf.args, options->v); 1859 strvec_push(&spf.args, "--recurse-submodules-default"); 1860 /* default value, "--submodule-prefix" and its value are added later */ 1861 1862 calculate_changed_submodule_paths(r, &spf.changed_submodule_names); 1863 string_list_sort(&spf.changed_submodule_names); 1864 run_processes_parallel(&opts); 1865 1866 if (spf.submodules_with_errors.len > 0) 1867 fprintf(stderr, _("Errors during submodule fetch:\n%s"), 1868 spf.submodules_with_errors.buf); 1869 1870 1871 strvec_clear(&spf.args); 1872 out: 1873 free_submodules_data(&spf.changed_submodule_names); 1874 string_list_clear(&spf.seen_submodule_names, 0); 1875 strbuf_release(&spf.submodules_with_errors); 1876 free(spf.oid_fetch_tasks); 1877 return spf.result; 1878 } 1879 1880 unsigned is_submodule_modified(const char *path, int ignore_untracked) 1881 { 1882 struct child_process cp = CHILD_PROCESS_INIT; 1883 struct strbuf buf = STRBUF_INIT; 1884 FILE *fp; 1885 unsigned dirty_submodule = 0; 1886 const char *git_dir; 1887 int ignore_cp_exit_code = 0; 1888 1889 if (validate_submodule_path(path) < 0) 1890 exit(128); 1891 1892 strbuf_addf(&buf, "%s/.git", path); 1893 git_dir = read_gitfile(buf.buf); 1894 if (!git_dir) 1895 git_dir = buf.buf; 1896 if (!is_git_directory(git_dir)) { 1897 if (is_directory(git_dir)) 1898 die(_("'%s' not recognized as a git repository"), git_dir); 1899 strbuf_release(&buf); 1900 /* The submodule is not checked out, so it is not modified */ 1901 return 0; 1902 } 1903 strbuf_reset(&buf); 1904 1905 strvec_pushl(&cp.args, "status", "--porcelain=2", NULL); 1906 if (ignore_untracked) 1907 strvec_push(&cp.args, "-uno"); 1908 1909 prepare_submodule_repo_env(&cp.env); 1910 cp.git_cmd = 1; 1911 cp.no_stdin = 1; 1912 cp.out = -1; 1913 cp.dir = path; 1914 if (start_command(&cp)) 1915 die(_("Could not run 'git status --porcelain=2' in submodule %s"), path); 1916 1917 fp = xfdopen(cp.out, "r"); 1918 while (strbuf_getwholeline(&buf, fp, '\n') != EOF) { 1919 /* regular untracked files */ 1920 if (buf.buf[0] == '?') 1921 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED; 1922 1923 if (buf.buf[0] == 'u' || 1924 buf.buf[0] == '1' || 1925 buf.buf[0] == '2') { 1926 /* T = line type, XY = status, SSSS = submodule state */ 1927 if (buf.len < strlen("T XY SSSS")) 1928 BUG("invalid status --porcelain=2 line %s", 1929 buf.buf); 1930 1931 if (buf.buf[5] == 'S' && buf.buf[8] == 'U') 1932 /* nested untracked file */ 1933 dirty_submodule |= DIRTY_SUBMODULE_UNTRACKED; 1934 1935 if (buf.buf[0] == 'u' || 1936 buf.buf[0] == '2' || 1937 memcmp(buf.buf + 5, "S..U", 4)) 1938 /* other change */ 1939 dirty_submodule |= DIRTY_SUBMODULE_MODIFIED; 1940 } 1941 1942 if ((dirty_submodule & DIRTY_SUBMODULE_MODIFIED) && 1943 ((dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) || 1944 ignore_untracked)) { 1945 /* 1946 * We're not interested in any further information from 1947 * the child any more, neither output nor its exit code. 1948 */ 1949 ignore_cp_exit_code = 1; 1950 break; 1951 } 1952 } 1953 fclose(fp); 1954 1955 if (finish_command(&cp) && !ignore_cp_exit_code) 1956 die(_("'git status --porcelain=2' failed in submodule %s"), path); 1957 1958 strbuf_release(&buf); 1959 return dirty_submodule; 1960 } 1961 1962 int submodule_uses_gitfile(const char *path) 1963 { 1964 struct child_process cp = CHILD_PROCESS_INIT; 1965 struct strbuf buf = STRBUF_INIT; 1966 const char *git_dir; 1967 1968 if (validate_submodule_path(path) < 0) 1969 exit(128); 1970 1971 strbuf_addf(&buf, "%s/.git", path); 1972 git_dir = read_gitfile(buf.buf); 1973 if (!git_dir) { 1974 strbuf_release(&buf); 1975 return 0; 1976 } 1977 strbuf_release(&buf); 1978 1979 /* Now test that all nested submodules use a gitfile too */ 1980 strvec_pushl(&cp.args, 1981 "submodule", "foreach", "--quiet", "--recursive", 1982 "test -f .git", NULL); 1983 1984 prepare_submodule_repo_env(&cp.env); 1985 cp.git_cmd = 1; 1986 cp.no_stdin = 1; 1987 cp.no_stderr = 1; 1988 cp.no_stdout = 1; 1989 cp.dir = path; 1990 if (run_command(&cp)) 1991 return 0; 1992 1993 return 1; 1994 } 1995 1996 /* 1997 * Check if it is a bad idea to remove a submodule, i.e. if we'd lose data 1998 * when doing so. 1999 * 2000 * Return 1 if we'd lose data, return 0 if the removal is fine, 2001 * and negative values for errors. 2002 */ 2003 int bad_to_remove_submodule(const char *path, unsigned flags) 2004 { 2005 ssize_t len; 2006 struct child_process cp = CHILD_PROCESS_INIT; 2007 struct strbuf buf = STRBUF_INIT; 2008 int ret = 0; 2009 2010 if (validate_submodule_path(path) < 0) 2011 exit(128); 2012 2013 if (!file_exists(path) || is_empty_dir(path)) 2014 return 0; 2015 2016 if (!submodule_uses_gitfile(path)) 2017 return 1; 2018 2019 strvec_pushl(&cp.args, "status", "--porcelain", 2020 "--ignore-submodules=none", NULL); 2021 2022 if (flags & SUBMODULE_REMOVAL_IGNORE_UNTRACKED) 2023 strvec_push(&cp.args, "-uno"); 2024 else 2025 strvec_push(&cp.args, "-uall"); 2026 2027 if (!(flags & SUBMODULE_REMOVAL_IGNORE_IGNORED_UNTRACKED)) 2028 strvec_push(&cp.args, "--ignored"); 2029 2030 prepare_submodule_repo_env(&cp.env); 2031 cp.git_cmd = 1; 2032 cp.no_stdin = 1; 2033 cp.out = -1; 2034 cp.dir = path; 2035 if (start_command(&cp)) { 2036 if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR) 2037 die(_("could not start 'git status' in submodule '%s'"), 2038 path); 2039 ret = -1; 2040 goto out; 2041 } 2042 2043 len = strbuf_read(&buf, cp.out, 1024); 2044 if (len > 2) 2045 ret = 1; 2046 close(cp.out); 2047 2048 if (finish_command(&cp)) { 2049 if (flags & SUBMODULE_REMOVAL_DIE_ON_ERROR) 2050 die(_("could not run 'git status' in submodule '%s'"), 2051 path); 2052 ret = -1; 2053 } 2054 out: 2055 strbuf_release(&buf); 2056 return ret; 2057 } 2058 2059 void submodule_unset_core_worktree(const struct submodule *sub) 2060 { 2061 struct strbuf config_path = STRBUF_INIT; 2062 2063 if (validate_submodule_path(sub->path) < 0) 2064 exit(128); 2065 2066 submodule_name_to_gitdir(&config_path, the_repository, sub->name); 2067 strbuf_addstr(&config_path, "/config"); 2068 2069 if (repo_config_set_in_file_gently(the_repository, config_path.buf, "core.worktree", NULL, NULL)) 2070 warning(_("Could not unset core.worktree setting in submodule '%s'"), 2071 sub->path); 2072 2073 strbuf_release(&config_path); 2074 } 2075 2076 static int submodule_has_dirty_index(const struct submodule *sub) 2077 { 2078 struct child_process cp = CHILD_PROCESS_INIT; 2079 2080 if (validate_submodule_path(sub->path) < 0) 2081 exit(128); 2082 2083 prepare_submodule_repo_env(&cp.env); 2084 2085 cp.git_cmd = 1; 2086 strvec_pushl(&cp.args, "diff-index", "--quiet", 2087 "--cached", "HEAD", NULL); 2088 cp.no_stdin = 1; 2089 cp.no_stdout = 1; 2090 cp.dir = sub->path; 2091 if (start_command(&cp)) 2092 die(_("could not recurse into submodule '%s'"), sub->path); 2093 2094 return finish_command(&cp); 2095 } 2096 2097 static void submodule_reset_index(const char *path, const char *super_prefix) 2098 { 2099 struct child_process cp = CHILD_PROCESS_INIT; 2100 2101 if (validate_submodule_path(path) < 0) 2102 exit(128); 2103 2104 prepare_submodule_repo_env(&cp.env); 2105 2106 cp.git_cmd = 1; 2107 cp.no_stdin = 1; 2108 cp.dir = path; 2109 2110 /* TODO: determine if this might overwright untracked files */ 2111 strvec_pushl(&cp.args, "read-tree", "-u", "--reset", NULL); 2112 strvec_pushf(&cp.args, "--super-prefix=%s%s/", 2113 (super_prefix ? super_prefix : ""), path); 2114 2115 strvec_push(&cp.args, empty_tree_oid_hex(the_repository->hash_algo)); 2116 2117 if (run_command(&cp)) 2118 die(_("could not reset submodule index")); 2119 } 2120 2121 /** 2122 * Moves a submodule at a given path from a given head to another new head. 2123 * For edge cases (a submodule coming into existence or removing a submodule) 2124 * pass NULL for old or new respectively. 2125 */ 2126 int submodule_move_head(const char *path, const char *super_prefix, 2127 const char *old_head, const char *new_head, 2128 unsigned flags) 2129 { 2130 int ret = 0; 2131 struct child_process cp = CHILD_PROCESS_INIT; 2132 const struct submodule *sub; 2133 int *error_code_ptr, error_code; 2134 2135 if (!is_submodule_active(the_repository, path)) 2136 return 0; 2137 2138 if (flags & SUBMODULE_MOVE_HEAD_FORCE) 2139 /* 2140 * Pass non NULL pointer to is_submodule_populated_gently 2141 * to prevent die()-ing. We'll use connect_work_tree_and_git_dir 2142 * to fixup the submodule in the force case later. 2143 */ 2144 error_code_ptr = &error_code; 2145 else 2146 error_code_ptr = NULL; 2147 2148 if (old_head && !is_submodule_populated_gently(path, error_code_ptr)) 2149 return 0; 2150 2151 sub = submodule_from_path(the_repository, null_oid(the_hash_algo), path); 2152 2153 if (!sub) 2154 BUG("could not get submodule information for '%s'", path); 2155 2156 if (old_head && !(flags & SUBMODULE_MOVE_HEAD_FORCE)) { 2157 /* Check if the submodule has a dirty index. */ 2158 if (submodule_has_dirty_index(sub)) 2159 return error(_("submodule '%s' has dirty index"), path); 2160 } 2161 2162 if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) { 2163 if (old_head) { 2164 if (!submodule_uses_gitfile(path)) 2165 absorb_git_dir_into_superproject(path, 2166 super_prefix); 2167 else { 2168 char *dotgit = xstrfmt("%s/.git", path); 2169 char *git_dir = xstrdup(read_gitfile(dotgit)); 2170 2171 free(dotgit); 2172 if (validate_submodule_git_dir(git_dir, 2173 sub->name) < 0) 2174 die(_("refusing to create/use '%s' in " 2175 "another submodule's git dir. " 2176 "Enabling extensions.submodulePathConfig " 2177 "should fix this."), git_dir); 2178 free(git_dir); 2179 } 2180 } else { 2181 struct strbuf gitdir = STRBUF_INIT; 2182 submodule_name_to_gitdir(&gitdir, the_repository, 2183 sub->name); 2184 connect_work_tree_and_git_dir(path, gitdir.buf, 0); 2185 strbuf_release(&gitdir); 2186 2187 /* make sure the index is clean as well */ 2188 submodule_reset_index(path, super_prefix); 2189 } 2190 2191 if (old_head && (flags & SUBMODULE_MOVE_HEAD_FORCE)) { 2192 struct strbuf gitdir = STRBUF_INIT; 2193 submodule_name_to_gitdir(&gitdir, the_repository, 2194 sub->name); 2195 connect_work_tree_and_git_dir(path, gitdir.buf, 1); 2196 strbuf_release(&gitdir); 2197 } 2198 } 2199 2200 prepare_submodule_repo_env(&cp.env); 2201 2202 cp.git_cmd = 1; 2203 cp.no_stdin = 1; 2204 cp.dir = path; 2205 2206 strvec_pushl(&cp.args, "read-tree", "--recurse-submodules", NULL); 2207 strvec_pushf(&cp.args, "--super-prefix=%s%s/", 2208 (super_prefix ? super_prefix : ""), path); 2209 2210 if (flags & SUBMODULE_MOVE_HEAD_DRY_RUN) 2211 strvec_push(&cp.args, "-n"); 2212 else 2213 strvec_push(&cp.args, "-u"); 2214 2215 if (flags & SUBMODULE_MOVE_HEAD_FORCE) 2216 strvec_push(&cp.args, "--reset"); 2217 else 2218 strvec_push(&cp.args, "-m"); 2219 2220 if (!(flags & SUBMODULE_MOVE_HEAD_FORCE)) 2221 strvec_push(&cp.args, old_head ? old_head : empty_tree_oid_hex(the_repository->hash_algo)); 2222 2223 strvec_push(&cp.args, new_head ? new_head : empty_tree_oid_hex(the_repository->hash_algo)); 2224 2225 if (run_command(&cp)) { 2226 ret = error(_("Submodule '%s' could not be updated."), path); 2227 goto out; 2228 } 2229 2230 if (!(flags & SUBMODULE_MOVE_HEAD_DRY_RUN)) { 2231 if (new_head) { 2232 child_process_init(&cp); 2233 /* also set the HEAD accordingly */ 2234 cp.git_cmd = 1; 2235 cp.no_stdin = 1; 2236 cp.dir = path; 2237 2238 prepare_submodule_repo_env(&cp.env); 2239 strvec_pushl(&cp.args, "update-ref", "HEAD", 2240 "--no-deref", new_head, NULL); 2241 2242 if (run_command(&cp)) { 2243 ret = -1; 2244 goto out; 2245 } 2246 } else { 2247 struct strbuf sb = STRBUF_INIT; 2248 2249 strbuf_addf(&sb, "%s/.git", path); 2250 unlink_or_warn(sb.buf); 2251 strbuf_release(&sb); 2252 2253 if (is_empty_dir(path)) 2254 rmdir_or_warn(path); 2255 2256 submodule_unset_core_worktree(sub); 2257 } 2258 } 2259 out: 2260 return ret; 2261 } 2262 2263 static int check_casefolding_conflict(const char *git_dir, 2264 const char *submodule_name, 2265 const bool suffixes_match) 2266 { 2267 char *p, *modules_dir = xstrdup(git_dir); 2268 struct dirent *de; 2269 DIR *dir = NULL; 2270 int ret = 0; 2271 2272 if ((p = find_last_dir_sep(modules_dir))) 2273 *p = '\0'; 2274 2275 /* No conflict is possible if modules_dir doesn't exist (first clone) */ 2276 if (!is_directory(modules_dir)) 2277 goto cleanup; 2278 2279 dir = opendir(modules_dir); 2280 if (!dir) { 2281 ret = -1; 2282 goto cleanup; 2283 } 2284 2285 /* Check for another directory under .git/modules that differs only in case. */ 2286 while ((de = readdir(dir))) { 2287 if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) 2288 continue; 2289 2290 if ((suffixes_match || is_git_directory(git_dir)) && 2291 !strcasecmp(de->d_name, submodule_name) && 2292 strcmp(de->d_name, submodule_name)) { 2293 ret = -1; /* collision found */ 2294 break; 2295 } 2296 } 2297 2298 cleanup: 2299 if (dir) 2300 closedir(dir); 2301 free(modules_dir); 2302 return ret; 2303 } 2304 2305 struct submodule_from_gitdir_cb { 2306 const char *gitdir; 2307 const char *submodule_name; 2308 bool conflict_found; 2309 }; 2310 2311 static int find_conflict_by_gitdir_cb(const char *var, const char *value, 2312 const struct config_context *ctx UNUSED, void *data) 2313 { 2314 struct submodule_from_gitdir_cb *cb = data; 2315 const char *submodule_name_start; 2316 size_t submodule_name_len; 2317 const char *suffix = ".gitdir"; 2318 size_t suffix_len = strlen(suffix); 2319 2320 if (!skip_prefix(var, "submodule.", &submodule_name_start)) 2321 return 0; 2322 2323 /* Check if submodule_name_start ends with ".gitdir" */ 2324 submodule_name_len = strlen(submodule_name_start); 2325 if (submodule_name_len < suffix_len || 2326 strcmp(submodule_name_start + submodule_name_len - suffix_len, suffix) != 0) 2327 return 0; /* Does not end with ".gitdir" */ 2328 2329 submodule_name_len -= suffix_len; 2330 2331 /* 2332 * A conflict happens if: 2333 * 1. The submodule names are different and 2334 * 2. The gitdir paths resolve to the same absolute path 2335 */ 2336 if (value && strncmp(cb->submodule_name, submodule_name_start, submodule_name_len)) { 2337 char *abs_path_cb = absolute_pathdup(cb->gitdir); 2338 char *abs_path_value = absolute_pathdup(value); 2339 2340 cb->conflict_found = !strcmp(abs_path_cb, abs_path_value); 2341 2342 free(abs_path_cb); 2343 free(abs_path_value); 2344 } 2345 2346 return cb->conflict_found; 2347 } 2348 2349 static bool submodule_conflicts_with_existing(const char *gitdir, const char *submodule_name) 2350 { 2351 struct submodule_from_gitdir_cb cb = { 0 }; 2352 cb.submodule_name = submodule_name; 2353 cb.gitdir = gitdir; 2354 2355 /* Find conflicts with existing repo gitdir configs */ 2356 repo_config(the_repository, find_conflict_by_gitdir_cb, &cb); 2357 2358 return cb.conflict_found; 2359 } 2360 2361 /* 2362 * Encoded gitdir validation, only used when extensions.submodulePathConfig is enabled. 2363 * This does not print errors like the non-encoded version, because encoding is supposed 2364 * to mitigate / fix all these. 2365 */ 2366 static int validate_submodule_encoded_git_dir(char *git_dir, const char *submodule_name) 2367 { 2368 const char *modules_marker = "/modules/"; 2369 char *p = git_dir, *last_submodule_name = NULL; 2370 int config_ignorecase = 0; 2371 2372 if (!the_repository->repository_format_submodule_path_cfg) 2373 BUG("validate_submodule_encoded_git_dir() must be called with " 2374 "extensions.submodulePathConfig enabled."); 2375 2376 /* Find the last submodule name in the gitdir path (modules can be nested). */ 2377 while ((p = strstr(p, modules_marker))) { 2378 last_submodule_name = p + strlen(modules_marker); 2379 p++; 2380 } 2381 2382 /* Prevent the use of '/' in encoded names */ 2383 if (!last_submodule_name || strchr(last_submodule_name, '/')) 2384 return -1; 2385 2386 /* Prevent conflicts with existing submodule gitdirs */ 2387 if (is_git_directory(git_dir) && 2388 submodule_conflicts_with_existing(git_dir, submodule_name)) 2389 return -1; 2390 2391 /* Prevent conflicts on case-folding filesystems */ 2392 repo_config_get_bool(the_repository, "core.ignorecase", &config_ignorecase); 2393 if (repo_ignore_case(the_repository) || config_ignorecase) { 2394 bool suffixes_match = !strcmp(last_submodule_name, submodule_name); 2395 return check_casefolding_conflict(git_dir, submodule_name, 2396 suffixes_match); 2397 } 2398 2399 return 0; 2400 } 2401 2402 static int validate_submodule_legacy_git_dir(char *git_dir, const char *submodule_name) 2403 { 2404 size_t len = strlen(git_dir), suffix_len = strlen(submodule_name); 2405 char *p; 2406 int ret = 0; 2407 2408 if (the_repository->repository_format_submodule_path_cfg) 2409 BUG("validate_submodule_git_dir() must be called with " 2410 "extensions.submodulePathConfig disabled."); 2411 2412 if (len <= suffix_len || (p = git_dir + len - suffix_len)[-1] != '/' || 2413 strcmp(p, submodule_name)) 2414 BUG("submodule name '%s' not a suffix of git dir '%s'", 2415 submodule_name, git_dir); 2416 2417 /* 2418 * We prevent the contents of sibling submodules' git directories to 2419 * clash. 2420 * 2421 * Example: having a submodule named `hippo` and another one named 2422 * `hippo/hooks` would result in the git directories 2423 * `.git/modules/hippo/` and `.git/modules/hippo/hooks/`, respectively, 2424 * but the latter directory is already designated to contain the hooks 2425 * of the former. 2426 */ 2427 for (; *p; p++) { 2428 if (is_dir_sep(*p)) { 2429 char c = *p; 2430 2431 *p = '\0'; 2432 if (is_git_directory(git_dir)) 2433 ret = -1; 2434 *p = c; 2435 2436 if (ret < 0) 2437 return error(_("submodule git dir '%s' is " 2438 "inside git dir '%.*s'"), 2439 git_dir, 2440 (int)(p - git_dir), git_dir); 2441 } 2442 } 2443 2444 return 0; 2445 } 2446 2447 int validate_submodule_git_dir(char *git_dir, const char *submodule_name) 2448 { 2449 if (!the_repository->repository_format_submodule_path_cfg) 2450 return validate_submodule_legacy_git_dir(git_dir, submodule_name); 2451 2452 return validate_submodule_encoded_git_dir(git_dir, submodule_name); 2453 } 2454 2455 int validate_submodule_path(const char *path) 2456 { 2457 char *p = xstrdup(path); 2458 struct stat st; 2459 int i, ret = 0; 2460 char sep; 2461 2462 for (i = 0; !ret && p[i]; i++) { 2463 if (!is_dir_sep(p[i])) 2464 continue; 2465 2466 sep = p[i]; 2467 p[i] = '\0'; 2468 /* allow missing components, but no symlinks */ 2469 ret = lstat(p, &st) || !S_ISLNK(st.st_mode) ? 0 : -1; 2470 p[i] = sep; 2471 if (ret) 2472 error(_("expected '%.*s' in submodule path '%s' not to " 2473 "be a symbolic link"), i, p, p); 2474 } 2475 if (!lstat(p, &st) && S_ISLNK(st.st_mode)) 2476 ret = error(_("expected submodule path '%s' not to be a " 2477 "symbolic link"), p); 2478 free(p); 2479 return ret; 2480 } 2481 2482 2483 /* 2484 * Embeds a single submodules git directory into the superprojects git dir, 2485 * non recursively. 2486 */ 2487 static void relocate_single_git_dir_into_superproject(const char *path, 2488 const char *super_prefix) 2489 { 2490 char *old_git_dir = NULL, *real_old_git_dir = NULL, *real_new_git_dir = NULL; 2491 struct strbuf new_gitdir = STRBUF_INIT; 2492 const struct submodule *sub; 2493 2494 if (validate_submodule_path(path) < 0) 2495 exit(128); 2496 2497 if (submodule_uses_worktrees(the_repository, path)) 2498 die(_("relocate_gitdir for submodule '%s' with " 2499 "more than one worktree not supported"), path); 2500 2501 old_git_dir = xstrfmt("%s/.git", path); 2502 if (read_gitfile(old_git_dir)) 2503 /* If it is an actual gitfile, it doesn't need migration. */ 2504 return; 2505 2506 real_old_git_dir = real_pathdup(old_git_dir, 1); 2507 2508 sub = submodule_from_path(the_repository, null_oid(the_hash_algo), path); 2509 if (!sub) 2510 die(_("could not lookup name for submodule '%s'"), path); 2511 2512 submodule_name_to_gitdir(&new_gitdir, the_repository, sub->name); 2513 if (safe_create_leading_directories_const(the_repository, new_gitdir.buf) < 0) 2514 die(_("could not create directory '%s'"), new_gitdir.buf); 2515 real_new_git_dir = real_pathdup(new_gitdir.buf, 1); 2516 2517 fprintf(stderr, _("Migrating git directory of '%s%s' from\n'%s' to\n'%s'\n"), 2518 super_prefix ? super_prefix : "", path, 2519 real_old_git_dir, real_new_git_dir); 2520 2521 relocate_gitdir(path, real_old_git_dir, real_new_git_dir); 2522 2523 free(old_git_dir); 2524 free(real_old_git_dir); 2525 free(real_new_git_dir); 2526 strbuf_release(&new_gitdir); 2527 } 2528 2529 static void absorb_git_dir_into_superproject_recurse(const char *path, 2530 const char *super_prefix) 2531 { 2532 2533 struct child_process cp = CHILD_PROCESS_INIT; 2534 2535 if (validate_submodule_path(path) < 0) 2536 exit(128); 2537 2538 cp.dir = path; 2539 cp.git_cmd = 1; 2540 cp.no_stdin = 1; 2541 strvec_pushl(&cp.args, "submodule--helper", 2542 "absorbgitdirs", NULL); 2543 strvec_pushf(&cp.args, "--super-prefix=%s%s/", super_prefix ? 2544 super_prefix : "", path); 2545 2546 prepare_submodule_repo_env(&cp.env); 2547 if (run_command(&cp)) 2548 die(_("could not recurse into submodule '%s'"), path); 2549 } 2550 2551 /* 2552 * Migrate the git directory of the submodule given by path from 2553 * having its git directory within the working tree to the git dir nested 2554 * in its superprojects git dir under modules/. 2555 */ 2556 void absorb_git_dir_into_superproject(const char *path, 2557 const char *super_prefix) 2558 { 2559 int err_code; 2560 const char *sub_git_dir; 2561 struct strbuf gitdir = STRBUF_INIT; 2562 2563 if (validate_submodule_path(path) < 0) 2564 exit(128); 2565 2566 strbuf_addf(&gitdir, "%s/.git", path); 2567 sub_git_dir = resolve_gitdir_gently(gitdir.buf, &err_code); 2568 2569 /* Not populated? */ 2570 if (!sub_git_dir) { 2571 const struct submodule *sub; 2572 struct strbuf sub_gitdir = STRBUF_INIT; 2573 2574 if (err_code == READ_GITFILE_ERR_MISSING) { 2575 /* unpopulated as expected */ 2576 strbuf_release(&gitdir); 2577 return; 2578 } 2579 2580 if (err_code != READ_GITFILE_ERR_NOT_A_REPO) 2581 /* We don't know what broke here. */ 2582 read_gitfile_error_die(err_code, path); 2583 2584 /* 2585 * Maybe populated, but no git directory was found? 2586 * This can happen if the superproject is a submodule 2587 * itself and was just absorbed. The absorption of the 2588 * superproject did not rewrite the git file links yet, 2589 * fix it now. 2590 */ 2591 sub = submodule_from_path(the_repository, null_oid(the_hash_algo), path); 2592 if (!sub) 2593 die(_("could not lookup name for submodule '%s'"), path); 2594 submodule_name_to_gitdir(&sub_gitdir, the_repository, sub->name); 2595 connect_work_tree_and_git_dir(path, sub_gitdir.buf, 0); 2596 strbuf_release(&sub_gitdir); 2597 } else { 2598 /* Is it already absorbed into the superprojects git dir? */ 2599 char *real_sub_git_dir = real_pathdup(sub_git_dir, 1); 2600 char *real_common_git_dir = real_pathdup(repo_get_common_dir(the_repository), 1); 2601 2602 if (!starts_with(real_sub_git_dir, real_common_git_dir)) 2603 relocate_single_git_dir_into_superproject(path, super_prefix); 2604 2605 free(real_sub_git_dir); 2606 free(real_common_git_dir); 2607 } 2608 strbuf_release(&gitdir); 2609 2610 absorb_git_dir_into_superproject_recurse(path, super_prefix); 2611 } 2612 2613 int get_superproject_working_tree(struct strbuf *buf) 2614 { 2615 struct child_process cp = CHILD_PROCESS_INIT; 2616 struct strbuf sb = STRBUF_INIT; 2617 struct strbuf one_up = STRBUF_INIT; 2618 char *cwd = xgetcwd(); 2619 int ret = 0; 2620 const char *subpath; 2621 int code; 2622 ssize_t len; 2623 2624 if (!is_inside_work_tree(the_repository)) 2625 /* 2626 * FIXME: 2627 * We might have a superproject, but it is harder 2628 * to determine. 2629 */ 2630 goto out; 2631 2632 if (!strbuf_realpath(&one_up, "../", 0)) 2633 goto out; 2634 2635 subpath = relative_path(cwd, one_up.buf, &sb); 2636 2637 prepare_submodule_repo_env(&cp.env); 2638 strvec_pop(&cp.env); 2639 2640 strvec_pushl(&cp.args, "--literal-pathspecs", "-C", "..", 2641 "ls-files", "-z", "--stage", "--full-name", "--", 2642 subpath, NULL); 2643 strbuf_reset(&sb); 2644 2645 cp.no_stdin = 1; 2646 cp.no_stderr = 1; 2647 cp.out = -1; 2648 cp.git_cmd = 1; 2649 2650 if (start_command(&cp)) 2651 die(_("could not start ls-files in ..")); 2652 2653 len = strbuf_read(&sb, cp.out, PATH_MAX); 2654 close(cp.out); 2655 2656 if (starts_with(sb.buf, "160000")) { 2657 int super_sub_len; 2658 int cwd_len = strlen(cwd); 2659 char *super_sub, *super_wt; 2660 2661 /* 2662 * There is a superproject having this repo as a submodule. 2663 * The format is SP SP TAB \0, 2664 * We're only interested in the name after the tab. 2665 */ 2666 super_sub = strchr(sb.buf, '\t') + 1; 2667 super_sub_len = strlen(super_sub); 2668 2669 if (super_sub_len > cwd_len || 2670 strcmp(&cwd[cwd_len - super_sub_len], super_sub)) 2671 BUG("returned path string doesn't match cwd?"); 2672 2673 super_wt = xstrdup(cwd); 2674 super_wt[cwd_len - super_sub_len] = '\0'; 2675 2676 strbuf_realpath(buf, super_wt, 1); 2677 ret = 1; 2678 free(super_wt); 2679 } 2680 2681 code = finish_command(&cp); 2682 2683 if (code == 128) 2684 /* '../' is not a git repository */ 2685 ret = 0; 2686 else if (code == 0 && len == 0) 2687 /* There is an unrelated git repository at '../' */ 2688 ret = 0; 2689 else if (code) 2690 die(_("ls-tree returned unexpected return code %d"), code); 2691 2692 out: 2693 strbuf_release(&sb); 2694 strbuf_release(&one_up); 2695 free(cwd); 2696 return ret; 2697 } 2698 2699 /* 2700 * Put the gitdir for a submodule (given relative to the main 2701 * repository worktree) into `buf`, or return -1 on error. 2702 */ 2703 int submodule_to_gitdir(struct repository *repo, 2704 struct strbuf *buf, const char *submodule) 2705 { 2706 const struct submodule *sub; 2707 const char *git_dir; 2708 int ret = 0; 2709 2710 if (validate_submodule_path(submodule) < 0) 2711 exit(128); 2712 2713 strbuf_reset(buf); 2714 strbuf_addstr(buf, submodule); 2715 strbuf_complete(buf, '/'); 2716 strbuf_addstr(buf, ".git"); 2717 2718 git_dir = read_gitfile(buf->buf); 2719 if (git_dir) { 2720 strbuf_reset(buf); 2721 strbuf_addstr(buf, git_dir); 2722 } 2723 if (!is_git_directory(buf->buf)) { 2724 sub = submodule_from_path(repo, null_oid(the_hash_algo), submodule); 2725 if (!sub) { 2726 ret = -1; 2727 goto cleanup; 2728 } 2729 strbuf_reset(buf); 2730 submodule_name_to_gitdir(buf, repo, sub->name); 2731 } 2732 2733 cleanup: 2734 return ret; 2735 } 2736 2737 void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r, 2738 const char *submodule_name) 2739 { 2740 if (!r->repository_format_submodule_path_cfg) { 2741 /* 2742 * If extensions.submodulePathConfig is disabled, 2743 * continue to use the plain path. 2744 */ 2745 repo_git_path_append(r, buf, "modules/%s", submodule_name); 2746 } else { 2747 const char *gitdir; 2748 char *key; 2749 int ret; 2750 2751 /* Otherwise the extension is enabled, so use the gitdir config. */ 2752 key = xstrfmt("submodule.%s.gitdir", submodule_name); 2753 ret = repo_config_get_string_tmp(r, key, &gitdir); 2754 FREE_AND_NULL(key); 2755 2756 if (ret) 2757 die(_("the 'submodule.%s.gitdir' config does not exist for module '%s'. " 2758 "Please ensure it is set, for example by running something like: " 2759 "'git config submodule.%s.gitdir .git/modules/%s'. For details " 2760 "see the extensions.submodulePathConfig documentation."), 2761 submodule_name, submodule_name, submodule_name, submodule_name); 2762 2763 strbuf_addstr(buf, gitdir); 2764 } 2765 2766 /* validate because users might have modified the config */ 2767 if (validate_submodule_git_dir(buf->buf, submodule_name)) { 2768 advise(_("enabling extensions.submodulePathConfig might fix the " 2769 "following error, if it's not already enabled.")); 2770 die(_("refusing to create/use '%s' in another submodule's " 2771 " git dir."), buf->buf); 2772 } 2773 }