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