Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/builtin/checkout.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / builtin / checkout.c` 2290 lines ยท 70772 bytes Raw Blame History 1 #define USE_THE_REPOSITORY_VARIABLE 2 #define DISABLE_SIGN_COMPARE_WARNINGS 3 4 #include "builtin.h" 5 #include "advice.h" 6 #include "branch.h" 7 #include "cache-tree.h" 8 #include "checkout.h" 9 #include "commit.h" 10 #include "config.h" 11 #include "diff.h" 12 #include "dir.h" 13 #include "environment.h" 14 #include "gettext.h" 15 #include "hex.h" 16 #include "hook.h" 17 #include "merge-ll.h" 18 #include "merge.h" 19 #include "lockfile.h" 20 #include "mem-pool.h" 21 #include "object-file.h" 22 #include "object-name.h" 23 #include "odb.h" 24 #include "parse-options.h" 25 #include "path.h" 26 #include "preload-index.h" 27 #include "read-cache.h" 28 #include "refs.h" 29 #include "remote.h" 30 #include "repo-settings.h" 31 #include "resolve-undo.h" 32 #include "revision.h" 33 #include "sequencer.h" 34 #include "setup.h" 35 #include "sparse-index.h" 36 #include "strvec.h" 37 #include "submodule.h" 38 #include "symlinks.h" 39 #include "trace2.h" 40 #include "tree.h" 41 #include "tree-walk.h" 42 #include "unpack-trees.h" 43 #include "wt-status.h" 44 #include "xdiff-interface.h" 45 #include "entry.h" 46 #include "parallel-checkout.h" 47 #include "add-interactive.h" 48 49 struct checkout_opts { 50 int patch_mode; 51 int patch_context; 52 int patch_interhunk_context; 53 int auto_advance; 54 int quiet; 55 int merge; 56 int force; 57 int force_detach; 58 int implicit_detach; 59 int writeout_stage; 60 int overwrite_ignore; 61 int ignore_skipworktree; 62 int ignore_other_worktrees; 63 int show_progress; 64 int count_checkout_paths; 65 int overlay_mode; 66 int dwim_new_local_branch; 67 int discard_changes; 68 int accept_ref; 69 int accept_pathspec; 70 int switch_branch_doing_nothing_is_ok; 71 int only_merge_on_switching_branches; 72 int can_switch_when_in_progress; 73 int orphan_from_empty_tree; 74 int empty_pathspec_ok; 75 int checkout_index; 76 int checkout_worktree; 77 const char *ignore_unmerged_opt; 78 int ignore_unmerged; 79 int pathspec_file_nul; 80 char *pathspec_from_file; 81 82 const char *new_branch; 83 const char *new_branch_force; 84 const char *new_orphan_branch; 85 int new_branch_log; 86 enum branch_track track; 87 struct diff_options diff_options; 88 int conflict_style; 89 90 int branch_exists; 91 const char *prefix; 92 struct pathspec pathspec; 93 const char *from_treeish; 94 struct tree *source_tree; 95 }; 96 97 #define CHECKOUT_OPTS_INIT { \ 98 .conflict_style = -1, \ 99 .merge = -1, \ 100 .patch_context = -1, \ 101 .patch_interhunk_context = -1, \ 102 .auto_advance = 1, \ 103 } 104 105 #define MERGE_WORKING_TREE_UNPACK_FAILED (-2) 106 107 struct branch_info { 108 char *name; /* The short name used */ 109 char *path; /* The full name of a real branch */ 110 struct commit *commit; /* The named commit */ 111 char *refname; /* The full name of the ref being checked out. */ 112 struct object_id oid; /* The object ID of the commit being checked out. */ 113 /* 114 * if not null the branch is detached because it's already 115 * checked out in this checkout 116 */ 117 char *checkout; 118 }; 119 120 static void branch_info_release(struct branch_info *info) 121 { 122 free(info->name); 123 free(info->path); 124 free(info->refname); 125 free(info->checkout); 126 } 127 128 static int post_checkout_hook(struct commit *old_commit, struct commit *new_commit, 129 int changed) 130 { 131 struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT_FORCE_SERIAL; 132 133 /* 134 * "new_commit" can be NULL when checking out from the index before 135 * a commit exists. 136 */ 137 strvec_pushl(&opt.args, 138 oid_to_hex(old_commit ? &old_commit->object.oid : null_oid(the_hash_algo)), 139 oid_to_hex(new_commit ? &new_commit->object.oid : null_oid(the_hash_algo)), 140 changed ? "1" : "0", 141 NULL); 142 143 return run_hooks_opt(the_repository, "post-checkout", &opt); 144 } 145 146 /* 147 * Handle a tree object and determine if we need to recurse into the 148 * tree (READ_TREE_RECURSIVE) or skip it (0). 149 */ 150 static int try_update_sparse_directory(const struct object_id *oid, 151 struct strbuf *base, 152 const char *pathname, 153 int overlay_mode) 154 { 155 struct strbuf dirpath = STRBUF_INIT; 156 struct cache_entry *old; 157 int pos, result = READ_TREE_RECURSIVE; 158 159 if (!the_repository->index->sparse_index) 160 return result; 161 162 strbuf_addbuf(&dirpath, base); 163 strbuf_addstr(&dirpath, pathname); 164 strbuf_addch(&dirpath, '/'); 165 166 pos = index_name_pos_sparse(the_repository->index, 167 dirpath.buf, dirpath.len); 168 if (pos < 0) 169 goto cleanup; 170 171 old = the_repository->index->cache[pos]; 172 if (!S_ISSPARSEDIR(old->ce_mode)) 173 goto cleanup; 174 175 if (oideq(oid, &old->oid)) { 176 /* Tree content already matches; no need to descend. */ 177 result = 0; 178 } else if (!overlay_mode) { 179 /* 180 * In non-overlay mode (e.g., restore --staged), replace the 181 * sparse directory OID directly since files not present in 182 * the source tree should be removed anyway. 183 */ 184 oidcpy(&old->oid, oid); 185 old->ce_flags |= CE_UPDATE; 186 result = 0; 187 } 188 189 cleanup: 190 strbuf_release(&dirpath); 191 return result; 192 } 193 194 static int update_some(const struct object_id *oid, struct strbuf *base, 195 const char *pathname, unsigned mode, void *context) 196 { 197 size_t len; 198 struct cache_entry *ce; 199 int pos; 200 int overlay_mode = context ? *((int *)context) : 1; 201 202 if (S_ISDIR(mode)) 203 return try_update_sparse_directory(oid, base, pathname, 204 overlay_mode); 205 206 len = st_add(base->len, strlen(pathname)); 207 ce = make_empty_cache_entry(the_repository->index, len); 208 oidcpy(&ce->oid, oid); 209 memcpy(ce->name, base->buf, base->len); 210 memcpy(ce->name + base->len, pathname, len - base->len); 211 ce->ce_flags = create_ce_flags(0) | CE_UPDATE; 212 ce->ce_namelen = len; 213 ce->ce_mode = create_ce_mode(mode); 214 215 /* 216 * If the entry is the same as the current index, we can leave the old 217 * entry in place. Whether it is UPTODATE or not, checkout_entry will 218 * do the right thing. 219 */ 220 pos = index_name_pos_sparse(the_repository->index, ce->name, ce->ce_namelen); 221 if (pos >= 0) { 222 struct cache_entry *old = the_repository->index->cache[pos]; 223 if (ce->ce_mode == old->ce_mode && 224 !ce_intent_to_add(old) && 225 oideq(&ce->oid, &old->oid)) { 226 old->ce_flags |= CE_UPDATE; 227 discard_cache_entry(ce); 228 return 0; 229 } 230 } 231 232 add_index_entry(the_repository->index, ce, 233 ADD_CACHE_OK_TO_ADD | ADD_CACHE_OK_TO_REPLACE); 234 return 0; 235 } 236 237 static int read_tree_some(struct tree *tree, const struct pathspec *pathspec, 238 int overlay_mode) 239 { 240 read_tree(the_repository, tree, 241 pathspec, update_some, &overlay_mode); 242 243 /* update the index with the given tree's info 244 * for all args, expanding wildcards, and exit 245 * with any non-zero return code. 246 */ 247 return 0; 248 } 249 250 static int skip_same_name(const struct cache_entry *ce, int pos) 251 { 252 while (++pos < the_repository->index->cache_nr && 253 !strcmp(the_repository->index->cache[pos]->name, ce->name)) 254 ; /* skip */ 255 return pos; 256 } 257 258 static int check_stage(int stage, const struct cache_entry *ce, int pos, 259 int overlay_mode) 260 { 261 while (pos < the_repository->index->cache_nr && 262 !strcmp(the_repository->index->cache[pos]->name, ce->name)) { 263 if (ce_stage(the_repository->index->cache[pos]) == stage) 264 return 0; 265 pos++; 266 } 267 if (!overlay_mode) 268 return 0; 269 if (stage == 2) 270 return error(_("path '%s' does not have our version"), ce->name); 271 else 272 return error(_("path '%s' does not have their version"), ce->name); 273 } 274 275 static int check_stages(unsigned stages, const struct cache_entry *ce, int pos) 276 { 277 unsigned seen = 0; 278 const char *name = ce->name; 279 280 while (pos < the_repository->index->cache_nr) { 281 ce = the_repository->index->cache[pos]; 282 if (strcmp(name, ce->name)) 283 break; 284 seen |= (1 << ce_stage(ce)); 285 pos++; 286 } 287 if ((stages & seen) != stages) 288 return error(_("path '%s' does not have all necessary versions"), 289 name); 290 return 0; 291 } 292 293 static int checkout_stage(int stage, const struct cache_entry *ce, int pos, 294 const struct checkout *state, int *nr_checkouts, 295 int overlay_mode) 296 { 297 while (pos < the_repository->index->cache_nr && 298 !strcmp(the_repository->index->cache[pos]->name, ce->name)) { 299 if (ce_stage(the_repository->index->cache[pos]) == stage) 300 return checkout_entry(the_repository->index->cache[pos], state, 301 NULL, nr_checkouts); 302 pos++; 303 } 304 if (!overlay_mode) { 305 unlink_entry(ce, NULL); 306 return 0; 307 } 308 if (stage == 2) 309 return error(_("path '%s' does not have our version"), ce->name); 310 else 311 return error(_("path '%s' does not have their version"), ce->name); 312 } 313 314 struct checkout_merged_data { 315 int conflict_style; 316 int renormalize; 317 char *base_label, *ours_label, *theirs_label; 318 }; 319 320 static void checkout_merged_release(struct checkout_merged_data *data) 321 { 322 free(data->base_label); 323 free(data->ours_label); 324 free(data->theirs_label); 325 } 326 327 static void checkout_merged_prepare(struct checkout_merged_data *data) 328 { 329 int renormalize = 0; 330 331 repo_config_get_bool(the_repository, "merge.renormalize", &renormalize); 332 data->renormalize = renormalize; 333 if (read_merge_labels(the_repository, &data->base_label, &data->ours_label, 334 &data->theirs_label)) { 335 data->base_label = xstrdup("base"); 336 data->ours_label = xstrdup("ours"); 337 data->theirs_label = xstrdup("theirs"); 338 } 339 } 340 341 static int checkout_merged(int pos, const struct checkout *state, 342 int *nr_checkouts, struct mem_pool *ce_mem_pool, 343 struct checkout_merged_data *data) 344 { 345 struct cache_entry *ce = the_repository->index->cache[pos]; 346 const char *path = ce->name; 347 mmfile_t ancestor, ours, theirs; 348 enum ll_merge_result merge_status; 349 int status; 350 struct object_id oid; 351 mmfile_t result_buf; 352 struct object_id threeway[3]; 353 unsigned mode = 0; 354 struct ll_merge_options ll_opts = LL_MERGE_OPTIONS_INIT; 355 356 memset(threeway, 0, sizeof(threeway)); 357 while (pos < the_repository->index->cache_nr) { 358 int stage; 359 stage = ce_stage(ce); 360 if (!stage || strcmp(path, ce->name)) 361 break; 362 oidcpy(&threeway[stage - 1], &ce->oid); 363 if (stage == 2) 364 mode = create_ce_mode(ce->ce_mode); 365 pos++; 366 ce = the_repository->index->cache[pos]; 367 } 368 if (is_null_oid(&threeway[1]) || is_null_oid(&threeway[2])) 369 return error(_("path '%s' does not have necessary versions"), path); 370 371 read_mmblob(&ancestor, the_repository->objects, &threeway[0]); 372 read_mmblob(&ours, the_repository->objects, &threeway[1]); 373 read_mmblob(&theirs, the_repository->objects, &threeway[2]); 374 375 ll_opts.renormalize = data->renormalize; 376 ll_opts.conflict_style = data->conflict_style; 377 merge_status = ll_merge(&result_buf, path, &ancestor, data->base_label, 378 &ours, data->ours_label, &theirs, data->theirs_label, 379 state->istate, &ll_opts); 380 free(ancestor.ptr); 381 free(ours.ptr); 382 free(theirs.ptr); 383 if (merge_status == LL_MERGE_BINARY_CONFLICT) 384 warning("Cannot merge binary files: %s (%s vs. %s)", 385 path, "ours", "theirs"); 386 if (merge_status < 0 || !result_buf.ptr) { 387 free(result_buf.ptr); 388 return error(_("path '%s': cannot merge"), path); 389 } 390 391 /* 392 * NEEDSWORK: 393 * There is absolutely no reason to write this as a blob object 394 * and create a phony cache entry. This hack is primarily to get 395 * to the write_entry() machinery that massages the contents to 396 * work-tree format and writes out which only allows it for a 397 * cache entry. The code in write_entry() needs to be refactored 398 * to allow us to feed a instead of a cache 399 * entry. Such a refactoring would help merge_recursive as well 400 * (it also writes the merge result to the object database even 401 * when it may contain conflicts). 402 */ 403 if (odb_write_object(the_repository->objects, result_buf.ptr, result_buf.size, OBJ_BLOB, &oid)) 404 die(_("Unable to add merge result for '%s'"), path); 405 free(result_buf.ptr); 406 ce = make_transient_cache_entry(mode, &oid, path, 2, ce_mem_pool); 407 if (!ce) 408 die(_("make_cache_entry failed for path '%s'"), path); 409 status = checkout_entry(ce, state, NULL, nr_checkouts); 410 return status; 411 } 412 413 static void mark_ce_for_checkout_overlay(struct cache_entry *ce, 414 char *ps_matched, 415 const struct checkout_opts *opts) 416 { 417 ce->ce_flags &= ~CE_MATCHED; 418 if (!opts->ignore_skipworktree && ce_skip_worktree(ce)) 419 return; 420 if (opts->source_tree && !(ce->ce_flags & CE_UPDATE)) 421 /* 422 * "git checkout tree-ish -- path", but this entry 423 * is in the original index but is not in tree-ish 424 * or does not match the pathspec; it will not be 425 * checked out to the working tree. We will not do 426 * anything to this entry at all. 427 */ 428 return; 429 /* 430 * Either this entry came from the tree-ish we are 431 * checking the paths out of, or we are checking out 432 * of the index. 433 * 434 * If it comes from the tree-ish, we already know it 435 * matches the pathspec and could just stamp 436 * CE_MATCHED to it from update_some(). But we still 437 * need ps_matched and read_tree (and 438 * eventually tree_entry_interesting) cannot fill 439 * ps_matched yet. Once it can, we can avoid calling 440 * match_pathspec() for _all_ entries when 441 * opts->source_tree != NULL. 442 */ 443 if (ce_path_match(the_repository->index, ce, &opts->pathspec, ps_matched)) 444 ce->ce_flags |= CE_MATCHED; 445 } 446 447 static void mark_ce_for_checkout_no_overlay(struct cache_entry *ce, 448 char *ps_matched, 449 const struct checkout_opts *opts) 450 { 451 ce->ce_flags &= ~CE_MATCHED; 452 if (!opts->ignore_skipworktree && ce_skip_worktree(ce)) 453 return; 454 if (ce_path_match(the_repository->index, ce, &opts->pathspec, ps_matched)) { 455 ce->ce_flags |= CE_MATCHED; 456 if (opts->source_tree && !(ce->ce_flags & CE_UPDATE)) 457 /* 458 * In overlay mode, but the path is not in 459 * tree-ish, which means we should remove it 460 * from the index and the working tree. 461 */ 462 ce->ce_flags |= CE_REMOVE | CE_WT_REMOVE; 463 } 464 } 465 466 static int checkout_worktree(const struct checkout_opts *opts, 467 const struct branch_info *info) 468 { 469 struct checkout state = CHECKOUT_INIT; 470 int nr_checkouts = 0, nr_unmerged = 0; 471 int errs = 0; 472 int pos; 473 int pc_workers, pc_threshold; 474 struct mem_pool ce_mem_pool; 475 struct checkout_merged_data checkout_merged_data = {0}; 476 477 state.force = 1; 478 state.refresh_cache = 1; 479 state.istate = the_repository->index; 480 481 mem_pool_init(&ce_mem_pool, 0); 482 get_parallel_checkout_configs(&pc_workers, &pc_threshold); 483 init_checkout_metadata(&state.meta, info->refname, 484 info->commit ? &info->commit->object.oid : &info->oid, 485 NULL); 486 487 enable_delayed_checkout(&state); 488 489 if (pc_workers > 1) 490 init_parallel_checkout(); 491 492 if (opts->merge) { 493 checkout_merged_prepare(&checkout_merged_data); 494 checkout_merged_data.conflict_style = opts->conflict_style; 495 } 496 for (pos = 0; pos < the_repository->index->cache_nr; pos++) { 497 struct cache_entry *ce = the_repository->index->cache[pos]; 498 if (ce->ce_flags & CE_MATCHED) { 499 if (!ce_stage(ce)) { 500 errs |= checkout_entry(ce, &state, 501 NULL, &nr_checkouts); 502 continue; 503 } 504 if (opts->writeout_stage) 505 errs |= checkout_stage(opts->writeout_stage, 506 ce, pos, 507 &state, 508 &nr_checkouts, opts->overlay_mode); 509 else if (opts->merge) 510 errs |= checkout_merged(pos, &state, 511 &nr_unmerged, 512 &ce_mem_pool, 513 &checkout_merged_data); 514 pos = skip_same_name(ce, pos) - 1; 515 } 516 } 517 518 if (opts->merge) 519 checkout_merged_release(&checkout_merged_data); 520 521 if (pc_workers > 1) 522 errs |= run_parallel_checkout(&state, pc_workers, pc_threshold, 523 NULL, NULL); 524 mem_pool_discard(&ce_mem_pool, should_validate_cache_entries()); 525 remove_marked_cache_entries(the_repository->index, 1); 526 remove_scheduled_dirs(); 527 errs |= finish_delayed_checkout(&state, opts->show_progress); 528 529 if (opts->count_checkout_paths) { 530 if (nr_unmerged) 531 fprintf_ln(stderr, Q_("Recreated %d merge conflict", 532 "Recreated %d merge conflicts", 533 nr_unmerged), 534 nr_unmerged); 535 if (opts->source_tree) 536 fprintf_ln(stderr, Q_("Updated %d path from %s", 537 "Updated %d paths from %s", 538 nr_checkouts), 539 nr_checkouts, 540 repo_find_unique_abbrev(the_repository, &opts->source_tree->object.oid, 541 DEFAULT_ABBREV)); 542 else if (!nr_unmerged || nr_checkouts) 543 fprintf_ln(stderr, Q_("Updated %d path from the index", 544 "Updated %d paths from the index", 545 nr_checkouts), 546 nr_checkouts); 547 } 548 549 return errs; 550 } 551 552 static int checkout_paths(const struct checkout_opts *opts, 553 const struct branch_info *new_branch_info) 554 { 555 int pos; 556 static char *ps_matched; 557 struct object_id rev; 558 struct commit *head; 559 int errs = 0; 560 struct lock_file lock_file = LOCK_INIT; 561 int checkout_index; 562 563 trace2_cmd_mode(opts->patch_mode ? "patch" : "path"); 564 565 if (opts->track != BRANCH_TRACK_UNSPECIFIED) 566 die(_("'%s' cannot be used with updating paths"), "--track"); 567 568 if (opts->new_branch_log) 569 die(_("'%s' cannot be used with updating paths"), "-l"); 570 571 if (opts->ignore_unmerged && opts->patch_mode) 572 die(_("'%s' cannot be used with updating paths"), 573 opts->ignore_unmerged_opt); 574 575 if (opts->force_detach) 576 die(_("'%s' cannot be used with updating paths"), "--detach"); 577 578 if (opts->merge && opts->patch_mode) 579 die(_("options '%s' and '%s' cannot be used together"), "--merge", "--patch"); 580 581 if (opts->ignore_unmerged && opts->merge) 582 die(_("options '%s' and '%s' cannot be used together"), 583 opts->ignore_unmerged_opt, "-m"); 584 585 if (opts->new_branch) 586 die(_("Cannot update paths and switch to branch '%s' at the same time."), 587 opts->new_branch); 588 589 if (!opts->checkout_worktree && !opts->checkout_index) 590 die(_("neither '%s' or '%s' is specified"), 591 "--staged", "--worktree"); 592 593 if (!opts->checkout_worktree && !opts->from_treeish) 594 die(_("'%s' must be used when '%s' is not specified"), 595 "--worktree", "--source"); 596 597 /* 598 * Reject --staged option to the restore command when combined with 599 * merge-related options. Use the accept_ref flag to distinguish it 600 * from the checkout command, which does not accept --staged anyway. 601 * 602 * `restore --ours|--theirs --worktree --staged` could mean resolving 603 * conflicted paths to one side in both the worktree and the index, 604 * but does not currently. 605 * 606 * `restore --merge|--conflict=