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/worktree.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 / worktree.c` 1139 lines ยท 31092 bytes Raw Blame History 1 #define DISABLE_SIGN_COMPARE_WARNINGS 2 3 #include "git-compat-util.h" 4 #include "abspath.h" 5 #include "environment.h" 6 #include "gettext.h" 7 #include "path.h" 8 #include "repository.h" 9 #include "refs.h" 10 #include "setup.h" 11 #include "strbuf.h" 12 #include "worktree.h" 13 #include "dir.h" 14 #include "wt-status.h" 15 #include "config.h" 16 17 void free_worktree(struct worktree *worktree) 18 { 19 if (!worktree) 20 return; 21 free(worktree->path); 22 free(worktree->id); 23 free(worktree->head_ref); 24 free(worktree->lock_reason); 25 free(worktree->prune_reason); 26 free(worktree); 27 } 28 29 void free_worktrees(struct worktree **worktrees) 30 { 31 int i = 0; 32 for (i = 0; worktrees[i]; i++) 33 free_worktree(worktrees[i]); 34 free (worktrees); 35 } 36 37 /** 38 * Update head_oid, head_ref and is_detached of the given worktree 39 */ 40 static void add_head_info(struct worktree *wt) 41 { 42 int flags; 43 const char *target; 44 45 target = refs_resolve_ref_unsafe(get_worktree_ref_store(wt), 46 "HEAD", 47 0, 48 &wt->head_oid, &flags); 49 if (!target) 50 return; 51 52 if (flags & REF_ISSYMREF) 53 wt->head_ref = xstrdup(target); 54 else 55 wt->is_detached = 1; 56 } 57 58 static int is_current_worktree(struct worktree *wt) 59 { 60 char *git_dir = absolute_pathdup(repo_get_git_dir(wt->repo)); 61 char *wt_git_dir = get_worktree_git_dir(wt); 62 int is_current = !fspathcmp(git_dir, absolute_path(wt_git_dir)); 63 free(wt_git_dir); 64 free(git_dir); 65 return is_current; 66 } 67 68 struct worktree *get_current_worktree(struct repository *repo) 69 { 70 struct worktree *wt = xcalloc(1, sizeof(*wt)); 71 char *gitdir = absolute_pathdup(repo->gitdir); 72 char *commondir = absolute_pathdup(repo->commondir); 73 74 wt->repo = repo; 75 wt->path = absolute_pathdup(repo->worktree ? repo->worktree 76 : repo->gitdir); 77 wt->is_bare = !repo->worktree; 78 if (fspathcmp(gitdir, commondir)) 79 wt->id = xstrdup(find_last_dir_sep(gitdir) + 1); 80 wt->is_current = true; 81 add_head_info(wt); 82 83 free(gitdir); 84 free(commondir); 85 return wt; 86 } 87 88 /* 89 * When in a secondary worktree, and when extensions.worktreeConfig 90 * is true, only $commondir/config and $commondir/worktrees// 91 * config.worktree are consulted, hence any core.bare=true setting in 92 * $commondir/config.worktree gets overlooked. Thus, check it manually 93 * to determine if the repository is bare. 94 */ 95 static int is_main_worktree_bare(struct repository *repo) 96 { 97 int bare = 0; 98 struct config_set cs = {0}; 99 char *worktree_config = xstrfmt("%s/config.worktree", repo_get_common_dir(repo)); 100 101 git_configset_init(&cs); 102 git_configset_add_file(&cs, worktree_config); 103 git_configset_get_bool(&cs, "core.bare", &bare); 104 105 git_configset_clear(&cs); 106 free(worktree_config); 107 return bare; 108 } 109 110 /** 111 * get the main worktree 112 */ 113 static struct worktree *get_main_worktree(struct repository *repo, 114 int skip_reading_head) 115 { 116 struct worktree *worktree = NULL; 117 struct strbuf worktree_path = STRBUF_INIT; 118 119 strbuf_add_real_path(&worktree_path, repo_get_common_dir(repo)); 120 strbuf_strip_suffix(&worktree_path, "/.git"); 121 122 CALLOC_ARRAY(worktree, 1); 123 worktree->repo = repo; 124 worktree->path = strbuf_detach(&worktree_path, NULL); 125 worktree->is_current = is_current_worktree(worktree); 126 worktree->is_bare = (repo->bare_cfg == 1) || 127 is_bare_repository(repo) || 128 /* 129 * When in a secondary worktree we have to also verify if the main 130 * worktree is bare in $commondir/config.worktree. 131 * This check is unnecessary if we're currently in the main worktree, 132 * as prior checks already consulted all configs of the current worktree. 133 */ 134 (!worktree->is_current && is_main_worktree_bare(repo)); 135 136 if (!skip_reading_head) 137 add_head_info(worktree); 138 return worktree; 139 } 140 141 struct worktree *get_linked_worktree(struct repository *repo, 142 const char *id, 143 int skip_reading_head) 144 { 145 struct worktree *worktree = NULL; 146 struct strbuf path = STRBUF_INIT; 147 struct strbuf worktree_path = STRBUF_INIT; 148 149 if (!id) 150 die("Missing linked worktree name"); 151 152 repo_common_path_append(repo, &path, "worktrees/%s/gitdir", id); 153 if (strbuf_read_file(&worktree_path, path.buf, 0) <= 0) 154 /* invalid gitdir file */ 155 goto done; 156 strbuf_rtrim(&worktree_path); 157 strbuf_strip_suffix(&worktree_path, "/.git"); 158 159 if (!is_absolute_path(worktree_path.buf)) { 160 strbuf_strip_suffix(&path, "gitdir"); 161 strbuf_addbuf(&path, &worktree_path); 162 strbuf_realpath_forgiving(&worktree_path, path.buf, 0); 163 } 164 165 CALLOC_ARRAY(worktree, 1); 166 worktree->repo = repo; 167 worktree->path = strbuf_detach(&worktree_path, NULL); 168 worktree->id = xstrdup(id); 169 worktree->is_current = is_current_worktree(worktree); 170 if (!skip_reading_head) 171 add_head_info(worktree); 172 173 done: 174 strbuf_release(&path); 175 strbuf_release(&worktree_path); 176 return worktree; 177 } 178 179 /* 180 * NEEDSWORK: This function exists so that we can look up metadata of a 181 * worktree without trying to access any of its internals like the refdb. It 182 * would be preferable to instead have a corruption-tolerant function for 183 * retrieving worktree metadata that could be used when the worktree is known 184 * to not be in a healthy state, e.g. when creating or repairing it. 185 */ 186 static struct worktree **get_worktrees_internal(struct repository *repo, 187 int skip_reading_head) 188 { 189 struct worktree **list = NULL; 190 struct strbuf path = STRBUF_INIT; 191 DIR *dir; 192 struct dirent *d; 193 int counter = 0, alloc = 2; 194 195 ALLOC_ARRAY(list, alloc); 196 197 list[counter++] = get_main_worktree(repo, skip_reading_head); 198 199 strbuf_addf(&path, "%s/worktrees", repo_get_common_dir(repo)); 200 dir = opendir(path.buf); 201 strbuf_release(&path); 202 if (dir) { 203 while ((d = readdir_skip_dot_and_dotdot(dir)) != NULL) { 204 struct worktree *linked = NULL; 205 206 if ((linked = get_linked_worktree(repo, d->d_name, skip_reading_head))) { 207 ALLOC_GROW(list, counter + 1, alloc); 208 list[counter++] = linked; 209 } 210 } 211 closedir(dir); 212 } 213 ALLOC_GROW(list, counter + 1, alloc); 214 list[counter] = NULL; 215 216 return list; 217 } 218 219 struct worktree **get_worktrees(struct repository *repo) 220 { 221 return get_worktrees_internal(repo, 0); 222 } 223 224 struct worktree **get_worktrees_without_reading_head(struct repository *repo) 225 { 226 return get_worktrees_internal(repo, 1); 227 } 228 229 char *get_worktree_git_dir(const struct worktree *wt) 230 { 231 if (!wt) 232 BUG("%s() called with NULL worktree", __func__); 233 else if (!wt->id) 234 return xstrdup(repo_get_common_dir(wt->repo)); 235 else 236 return repo_common_path(wt->repo, "worktrees/%s", wt->id); 237 } 238 239 static struct worktree *find_worktree_by_suffix(struct worktree **list, 240 const char *suffix) 241 { 242 struct worktree *found = NULL; 243 int nr_found = 0, suffixlen; 244 245 suffixlen = strlen(suffix); 246 if (!suffixlen) 247 return NULL; 248 249 for (; *list && nr_found < 2; list++) { 250 const char *path = (*list)->path; 251 int pathlen = strlen(path); 252 int start = pathlen - suffixlen; 253 254 /* suffix must start at directory boundary */ 255 if ((!start || (start > 0 && is_dir_sep(path[start - 1]))) && 256 !fspathcmp(suffix, path + start)) { 257 found = *list; 258 nr_found++; 259 } 260 } 261 return nr_found == 1 ? found : NULL; 262 } 263 264 struct worktree *find_worktree(struct worktree **list, 265 const char *prefix, 266 const char *arg) 267 { 268 struct worktree *wt; 269 char *to_free = NULL; 270 271 if ((wt = find_worktree_by_suffix(list, arg))) 272 return wt; 273 274 if (prefix) 275 arg = to_free = prefix_filename(prefix, arg); 276 wt = find_worktree_by_path(list, arg); 277 free(to_free); 278 return wt; 279 } 280 281 struct worktree *find_worktree_by_path(struct worktree **list, const char *p) 282 { 283 struct strbuf wt_path = STRBUF_INIT; 284 char *path = real_pathdup(p, 0); 285 286 if (!path) 287 return NULL; 288 for (; *list; list++) { 289 if (!strbuf_realpath(&wt_path, (*list)->path, 0)) 290 continue; 291 292 if (!fspathcmp(path, wt_path.buf)) 293 break; 294 } 295 free(path); 296 strbuf_release(&wt_path); 297 return *list; 298 } 299 300 int is_main_worktree(const struct worktree *wt) 301 { 302 return !wt->id; 303 } 304 305 const char *worktree_lock_reason(struct worktree *wt) 306 { 307 if (is_main_worktree(wt)) 308 return NULL; 309 310 if (!wt->lock_reason_valid) { 311 struct strbuf path = STRBUF_INIT; 312 313 strbuf_addstr(&path, worktree_git_path(wt, "locked")); 314 if (file_exists(path.buf)) { 315 struct strbuf lock_reason = STRBUF_INIT; 316 if (strbuf_read_file(&lock_reason, path.buf, 0) < 0) 317 die_errno(_("failed to read '%s'"), path.buf); 318 strbuf_trim(&lock_reason); 319 wt->lock_reason = strbuf_detach(&lock_reason, NULL); 320 } else 321 wt->lock_reason = NULL; 322 wt->lock_reason_valid = 1; 323 strbuf_release(&path); 324 } 325 326 return wt->lock_reason; 327 } 328 329 const char *worktree_prune_reason(struct worktree *wt, timestamp_t expire) 330 { 331 struct strbuf reason = STRBUF_INIT; 332 char *path = NULL; 333 334 if (is_main_worktree(wt)) 335 return NULL; 336 if (wt->prune_reason_valid) 337 return wt->prune_reason; 338 339 if (should_prune_worktree(wt->repo, wt->id, &reason, &path, expire)) 340 wt->prune_reason = strbuf_detach(&reason, NULL); 341 wt->prune_reason_valid = 1; 342 343 strbuf_release(&reason); 344 free(path); 345 return wt->prune_reason; 346 } 347 348 /* convenient wrapper to deal with NULL strbuf */ 349 __attribute__((format (printf, 2, 3))) 350 static void strbuf_addf_gently(struct strbuf *buf, const char *fmt, ...) 351 { 352 va_list params; 353 354 if (!buf) 355 return; 356 357 va_start(params, fmt); 358 strbuf_vaddf(buf, fmt, params); 359 va_end(params); 360 } 361 362 int validate_worktree(const struct worktree *wt, struct strbuf *errmsg, 363 unsigned flags) 364 { 365 struct strbuf wt_path = STRBUF_INIT; 366 struct strbuf realpath = STRBUF_INIT; 367 struct strbuf buf = STRBUF_INIT; 368 char *path = NULL; 369 int err, ret = -1; 370 371 strbuf_addf(&wt_path, "%s/.git", wt->path); 372 373 if (is_main_worktree(wt)) { 374 if (is_directory(wt_path.buf)) { 375 ret = 0; 376 goto done; 377 } 378 /* 379 * Main worktree using .git file to point to the 380 * repository would make it impossible to know where 381 * the actual worktree is if this function is executed 382 * from another worktree. No .git file support for now. 383 */ 384 strbuf_addf_gently(errmsg, 385 _("'%s' at main working tree is not the repository directory"), 386 wt_path.buf); 387 goto done; 388 } 389 390 /* 391 * Make sure "gitdir" file points to a real .git file and that 392 * file points back here. 393 */ 394 if (!is_absolute_path(wt->path)) { 395 strbuf_addf_gently(errmsg, 396 _("'%s' file does not contain absolute path to the working tree location"), 397 repo_common_path_replace(wt->repo, &buf, "worktrees/%s/gitdir", wt->id)); 398 goto done; 399 } 400 401 if (flags & WT_VALIDATE_WORKTREE_MISSING_OK && 402 !file_exists(wt->path)) { 403 ret = 0; 404 goto done; 405 } 406 407 if (!file_exists(wt_path.buf)) { 408 strbuf_addf_gently(errmsg, _("'%s' does not exist"), wt_path.buf); 409 goto done; 410 } 411 412 path = xstrdup_or_null(read_gitfile_gently(wt_path.buf, &err)); 413 if (!path) { 414 strbuf_addf_gently(errmsg, _("'%s' is not a .git file, error code %d"), 415 wt_path.buf, err); 416 goto done; 417 } 418 419 strbuf_realpath(&realpath, repo_common_path_replace(wt->repo, &buf, "worktrees/%s", wt->id), 1); 420 ret = fspathcmp(path, realpath.buf); 421 422 if (ret) 423 strbuf_addf_gently(errmsg, _("'%s' does not point back to '%s'"), 424 wt->path, repo_common_path_replace(wt->repo, &buf, 425 "worktrees/%s", wt->id)); 426 done: 427 free(path); 428 strbuf_release(&buf); 429 strbuf_release(&wt_path); 430 strbuf_release(&realpath); 431 return ret; 432 } 433 434 void update_worktree_location(struct worktree *wt, const char *path_, 435 int use_relative_paths) 436 { 437 struct strbuf path = STRBUF_INIT; 438 struct strbuf dotgit = STRBUF_INIT; 439 struct strbuf gitdir = STRBUF_INIT; 440 char *wt_gitdir; 441 442 if (is_main_worktree(wt)) 443 BUG("can't relocate main worktree"); 444 445 wt_gitdir = repo_common_path(wt->repo, "worktrees/%s/gitdir", wt->id); 446 strbuf_realpath(&gitdir, wt_gitdir, 1); 447 strbuf_realpath(&path, path_, 1); 448 strbuf_addf(&dotgit, "%s/.git", path.buf); 449 if (fspathcmp(wt->path, path.buf)) { 450 write_worktree_linking_files(wt->repo, dotgit.buf, 451 gitdir.buf, use_relative_paths); 452 453 free(wt->path); 454 wt->path = strbuf_detach(&path, NULL); 455 } 456 strbuf_release(&path); 457 strbuf_release(&dotgit); 458 strbuf_release(&gitdir); 459 free(wt_gitdir); 460 } 461 462 int is_worktree_being_rebased(const struct worktree *wt, 463 const char *target) 464 { 465 struct wt_status_state state; 466 int found_rebase; 467 468 memset(&state, 0, sizeof(state)); 469 found_rebase = wt_status_check_rebase(wt, &state) && 470 (state.rebase_in_progress || 471 state.rebase_interactive_in_progress) && 472 state.branch && 473 skip_prefix(target, "refs/heads/", &target) && 474 !strcmp(state.branch, target); 475 wt_status_state_free_buffers(&state); 476 return found_rebase; 477 } 478 479 int is_worktree_being_bisected(const struct worktree *wt, 480 const char *target) 481 { 482 struct wt_status_state state; 483 int found_bisect; 484 485 memset(&state, 0, sizeof(state)); 486 found_bisect = wt_status_check_bisect(wt, &state) && 487 state.bisecting_from && 488 skip_prefix(target, "refs/heads/", &target) && 489 !strcmp(state.bisecting_from, target); 490 wt_status_state_free_buffers(&state); 491 return found_bisect; 492 } 493 494 /* 495 * note: this function should be able to detect shared symref even if 496 * HEAD is temporarily detached (e.g. in the middle of rebase or 497 * bisect). New commands that do similar things should update this 498 * function as well. 499 */ 500 int is_shared_symref(const struct worktree *wt, const char *symref, 501 const char *target) 502 { 503 const char *symref_target; 504 struct ref_store *refs; 505 int flags; 506 507 if (wt->is_bare) 508 return 0; 509 510 if (wt->is_detached && !strcmp(symref, "HEAD")) { 511 if (is_worktree_being_rebased(wt, target)) 512 return 1; 513 if (is_worktree_being_bisected(wt, target)) 514 return 1; 515 } 516 517 refs = get_worktree_ref_store(wt); 518 symref_target = refs_resolve_ref_unsafe(refs, symref, 0, 519 NULL, &flags); 520 if ((flags & REF_ISSYMREF) && 521 symref_target && !strcmp(symref_target, target)) 522 return 1; 523 524 return 0; 525 } 526 527 const struct worktree *find_shared_symref(struct worktree **worktrees, 528 const char *symref, 529 const char *target) 530 { 531 532 for (int i = 0; worktrees[i]; i++) 533 if (is_shared_symref(worktrees[i], symref, target)) 534 return worktrees[i]; 535 536 return NULL; 537 } 538 539 int submodule_uses_worktrees(struct repository *repo, 540 const char *path) 541 { 542 char *submodule_gitdir; 543 struct strbuf sb = STRBUF_INIT, err = STRBUF_INIT; 544 DIR *dir; 545 struct dirent *d; 546 int ret = 0; 547 struct repository_format format = REPOSITORY_FORMAT_INIT; 548 549 submodule_gitdir = repo_submodule_path(repo, 550 path, "%s", ""); 551 if (!submodule_gitdir) 552 return 0; 553 554 /* The env would be set for the superproject. */ 555 get_common_dir_noenv(&sb, submodule_gitdir); 556 free(submodule_gitdir); 557 558 strbuf_addstr(&sb, "/config"); 559 read_repository_format(&format, sb.buf); 560 if (verify_repository_format(&format, &err)) { 561 strbuf_release(&err); 562 strbuf_release(&sb); 563 clear_repository_format(&format); 564 return 1; 565 } 566 clear_repository_format(&format); 567 strbuf_release(&err); 568 569 /* Replace config by worktrees. */ 570 strbuf_setlen(&sb, sb.len - strlen("config")); 571 strbuf_addstr(&sb, "worktrees"); 572 573 /* See if there is any file inside the worktrees directory. */ 574 dir = opendir(sb.buf); 575 strbuf_release(&sb); 576 577 if (!dir) 578 return 0; 579 580 d = readdir_skip_dot_and_dotdot(dir); 581 if (d) 582 ret = 1; 583 closedir(dir); 584 return ret; 585 } 586 587 void strbuf_worktree_ref(const struct worktree *wt, 588 struct strbuf *sb, 589 const char *refname) 590 { 591 if (parse_worktree_ref(refname, NULL, NULL, NULL) == 592 REF_WORKTREE_CURRENT && 593 wt && !wt->is_current) { 594 if (is_main_worktree(wt)) 595 strbuf_addstr(sb, "main-worktree/"); 596 else 597 strbuf_addf(sb, "worktrees/%s/", wt->id); 598 } 599 strbuf_addstr(sb, refname); 600 } 601 602 int other_head_refs(struct repository *repo, 603 refs_for_each_cb fn, void *cb_data) 604 { 605 struct worktree **worktrees, **p; 606 struct strbuf refname = STRBUF_INIT; 607 int ret = 0; 608 609 worktrees = get_worktrees(repo); 610 for (p = worktrees; *p; p++) { 611 struct worktree *wt = *p; 612 struct object_id oid; 613 int flag; 614 615 if (wt->is_current) 616 continue; 617 618 strbuf_reset(&refname); 619 strbuf_worktree_ref(wt, &refname, "HEAD"); 620 if (refs_resolve_ref_unsafe(get_main_ref_store(repo), 621 refname.buf, 622 RESOLVE_REF_READING, 623 &oid, &flag)) { 624 struct reference ref = { 625 .name = refname.buf, 626 .oid = &oid, 627 .flags = flag, 628 }; 629 630 ret = fn(&ref, cb_data); 631 } 632 if (ret) 633 break; 634 } 635 free_worktrees(worktrees); 636 strbuf_release(&refname); 637 return ret; 638 } 639 640 /* 641 * Repair worktree's /path/to/worktree/.git file if missing, corrupt, or not 642 * pointing at /worktrees/. 643 */ 644 static void repair_gitfile(struct worktree *wt, 645 worktree_repair_fn fn, void *cb_data, 646 int use_relative_paths) 647 { 648 struct strbuf dotgit = STRBUF_INIT; 649 struct strbuf gitdir = STRBUF_INIT; 650 struct strbuf repo = STRBUF_INIT; 651 struct strbuf backlink = STRBUF_INIT; 652 struct strbuf contents = STRBUF_INIT; 653 const char *dotgit_contents = NULL; 654 const char *repair = NULL; 655 char *path = NULL; 656 int err; 657 658 /* missing worktree can't be repaired */ 659 if (!file_exists(wt->path)) 660 goto done; 661 662 if (!is_directory(wt->path)) { 663 fn(1, wt->path, _("not a directory"), cb_data); 664 goto done; 665 } 666 667 path = repo_common_path(wt->repo, "worktrees/%s", wt->id); 668 strbuf_realpath(&repo, path, 1); 669 strbuf_addf(&dotgit, "%s/.git", wt->path); 670 strbuf_addf(&gitdir, "%s/gitdir", repo.buf); 671 err = read_gitfile_raw(&contents, dotgit.buf); 672 if (!err) 673 dotgit_contents = contents.buf; 674 675 if (dotgit_contents) { 676 if (is_absolute_path(dotgit_contents)) { 677 strbuf_addstr(&backlink, dotgit_contents); 678 } else { 679 strbuf_addf(&backlink, "%s/%s", wt->path, dotgit_contents); 680 strbuf_realpath_forgiving(&backlink, backlink.buf, 0); 681 } 682 } 683 684 if (err == READ_GITFILE_ERR_NOT_A_FILE || 685 err == READ_GITFILE_ERR_IS_A_DIR) 686 fn(1, wt->path, _(".git is not a file"), cb_data); 687 else if (err || !is_git_directory(backlink.buf)) 688 repair = _(".git file broken"); 689 else if (fspathcmp(backlink.buf, repo.buf)) 690 repair = _(".git file incorrect"); 691 else if (use_relative_paths == is_absolute_path(dotgit_contents)) 692 repair = _(".git file absolute/relative path mismatch"); 693 694 if (repair) { 695 fn(0, wt->path, repair, cb_data); 696 write_worktree_linking_files(wt->repo, dotgit.buf, 697 gitdir.buf, use_relative_paths); 698 } 699 700 done: 701 free(path); 702 strbuf_release(&repo); 703 strbuf_release(&dotgit); 704 strbuf_release(&gitdir); 705 strbuf_release(&backlink); 706 strbuf_release(&contents); 707 } 708 709 static void repair_noop(int iserr UNUSED, 710 const char *path UNUSED, 711 const char *msg UNUSED, 712 void *cb_data UNUSED) 713 { 714 /* nothing */ 715 } 716 717 void repair_worktrees(struct repository *repo, worktree_repair_fn fn, 718 void *cb_data, int use_relative_paths) 719 { 720 struct worktree **worktrees = get_worktrees_internal(repo, 1); 721 struct worktree **wt = worktrees + 1; /* +1 skips main worktree */ 722 723 if (!fn) 724 fn = repair_noop; 725 for (; *wt; wt++) 726 repair_gitfile(*wt, fn, cb_data, use_relative_paths); 727 free_worktrees(worktrees); 728 } 729 730 void repair_worktree_after_gitdir_move(struct worktree *wt, const char *old_path) 731 { 732 struct strbuf gitdir = STRBUF_INIT; 733 struct strbuf dotgit = STRBUF_INIT; 734 int is_relative_path; 735 char *path = NULL; 736 737 if (is_main_worktree(wt)) 738 goto done; 739 740 path = repo_common_path(wt->repo, "worktrees/%s/gitdir", wt->id); 741 strbuf_realpath(&gitdir, path, 1); 742 743 if (strbuf_read_file(&dotgit, gitdir.buf, 0) < 0) 744 goto done; 745 746 strbuf_rtrim(&dotgit); 747 is_relative_path = ! is_absolute_path(dotgit.buf); 748 if (is_relative_path) { 749 strbuf_insertf(&dotgit, 0, "%s/worktrees/%s/", old_path, wt->id); 750 strbuf_realpath_forgiving(&dotgit, dotgit.buf, 0); 751 } 752 753 if (!file_exists(dotgit.buf)) 754 goto done; 755 756 write_worktree_linking_files(wt->repo, dotgit.buf, 757 gitdir.buf, is_relative_path); 758 done: 759 strbuf_release(&gitdir); 760 strbuf_release(&dotgit); 761 free(path); 762 } 763 764 void repair_worktrees_after_gitdir_move(struct repository *repo, const char *old_path) 765 { 766 struct worktree **worktrees = get_worktrees_internal(repo, 1); 767 struct worktree **wt = worktrees + 1; /* +1 skips main worktree */ 768 769 for (; *wt; wt++) 770 repair_worktree_after_gitdir_move(*wt, old_path); 771 free_worktrees(worktrees); 772 } 773 774 static int is_main_worktree_path(struct repository *repo, const char *path) 775 { 776 struct strbuf target = STRBUF_INIT; 777 struct strbuf maindir = STRBUF_INIT; 778 int cmp; 779 780 strbuf_add_real_path(&target, path); 781 strbuf_strip_suffix(&target, "/.git"); 782 strbuf_add_real_path(&maindir, repo_get_common_dir(repo)); 783 strbuf_strip_suffix(&maindir, "/.git"); 784 cmp = fspathcmp(maindir.buf, target.buf); 785 786 strbuf_release(&maindir); 787 strbuf_release(&target); 788 return !cmp; 789 } 790 791 /* 792 * If both the main worktree and linked worktree have been moved, then the 793 * gitfile /path/to/worktree/.git won't point into the repository, thus we 794 * won't know which /worktrees//gitdir to repair. However, we may 795 * be able to infer the gitdir by manually reading /path/to/worktree/.git, 796 * extracting the , and checking if /worktrees/ exists. 797 * 798 * Returns -1 on failure and strbuf.len on success. 799 */ 800 static ssize_t infer_backlink(struct repository *repo, 801 const char *gitfile, 802 struct strbuf *inferred) 803 { 804 struct strbuf actual = STRBUF_INIT; 805 const char *id; 806 807 if (strbuf_read_file(&actual, gitfile, 0) < 0) 808 goto error; 809 if (!starts_with(actual.buf, "gitdir:")) 810 goto error; 811 if (!(id = find_last_dir_sep(actual.buf))) 812 goto error; 813 strbuf_trim(&actual); 814 id++; /* advance past '/' to point at */ 815 if (!*id) 816 goto error; 817 repo_common_path_replace(repo, inferred, "worktrees/%s", id); 818 if (!is_directory(inferred->buf)) 819 goto error; 820 821 strbuf_release(&actual); 822 return inferred->len; 823 error: 824 strbuf_release(&actual); 825 strbuf_reset(inferred); /* clear invalid path */ 826 return -1; 827 } 828 829 /* 830 * Repair /worktrees//gitdir if missing, corrupt, or not pointing at 831 * the worktree's path. 832 */ 833 void repair_worktree_at_path(struct repository *repo, 834 const char *path, 835 worktree_repair_fn fn, void *cb_data, 836 int use_relative_paths) 837 { 838 struct strbuf dotgit = STRBUF_INIT; 839 struct strbuf backlink = STRBUF_INIT; 840 struct strbuf inferred_backlink = STRBUF_INIT; 841 struct strbuf gitdir = STRBUF_INIT; 842 struct strbuf olddotgit = STRBUF_INIT; 843 char *dotgit_contents = NULL; 844 const char *repair = NULL; 845 int err; 846 847 if (!fn) 848 fn = repair_noop; 849 850 if (is_main_worktree_path(repo, path)) 851 goto done; 852 853 strbuf_addf(&dotgit, "%s/.git", path); 854 if (!strbuf_realpath(&dotgit, dotgit.buf, 0)) { 855 fn(1, path, _("not a valid path"), cb_data); 856 goto done; 857 } 858 859 infer_backlink(repo, dotgit.buf, &inferred_backlink); 860 strbuf_realpath_forgiving(&inferred_backlink, inferred_backlink.buf, 0); 861 dotgit_contents = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err)); 862 if (dotgit_contents) { 863 strbuf_addstr(&backlink, dotgit_contents); 864 } else if (err == READ_GITFILE_ERR_NOT_A_FILE || 865 err == READ_GITFILE_ERR_IS_A_DIR) { 866 fn(1, dotgit.buf, _("unable to locate repository; .git is not a file"), cb_data); 867 goto done; 868 } else if (err == READ_GITFILE_ERR_NOT_A_REPO) { 869 if (inferred_backlink.len) { 870 /* 871 * Worktree's .git file does not point at a repository 872 * but we found a .git/worktrees/ in this 873 * repository with the same as recorded in the 874 * worktree's .git file so make the worktree point at 875 * the discovered .git/worktrees/. 876 */ 877 strbuf_swap(&backlink, &inferred_backlink); 878 } else { 879 fn(1, dotgit.buf, _("unable to locate repository; .git file does not reference a repository"), cb_data); 880 goto done; 881 } 882 } else { 883 fn(1, dotgit.buf, _("unable to locate repository; .git file broken"), cb_data); 884 goto done; 885 } 886 887 /* 888 * If we got this far, either the worktree's .git file pointed at a 889 * valid repository (i.e. read_gitfile_gently() returned success) or 890 * the .git file did not point at a repository but we were able to 891 * infer a suitable new value for the .git file by locating a 892 * .git/worktrees/ in *this* repository corresponding to the 893 * recorded in the worktree's .git file. 894 * 895 * However, if, at this point, inferred_backlink is non-NULL (i.e. we 896 * found a suitable .git/worktrees/ in *this* repository) *and* the 897 * worktree's .git file points at a valid repository *and* those two 898 * paths differ, then that indicates that the user probably *copied* 899 * the main and linked worktrees to a new location as a unit rather 900 * than *moving* them. Thus, the copied worktree's .git file actually 901 * points at the .git/worktrees/ in the *original* repository, not 902 * in the "copy" repository. In this case, point the "copy" worktree's 903 * .git file at the "copy" repository. 904 */ 905 if (inferred_backlink.len && fspathcmp(backlink.buf, inferred_backlink.buf)) 906 strbuf_swap(&backlink, &inferred_backlink); 907 908 strbuf_addf(&gitdir, "%s/gitdir", backlink.buf); 909 if (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0) 910 repair = _("gitdir unreadable"); 911 else if (use_relative_paths == is_absolute_path(olddotgit.buf)) 912 repair = _("gitdir absolute/relative path mismatch"); 913 else { 914 strbuf_rtrim(&olddotgit); 915 if (!is_absolute_path(olddotgit.buf)) { 916 strbuf_insertf(&olddotgit, 0, "%s/", backlink.buf); 917 strbuf_realpath_forgiving(&olddotgit, olddotgit.buf, 0); 918 } 919 if (fspathcmp(olddotgit.buf, dotgit.buf)) 920 repair = _("gitdir incorrect"); 921 } 922 923 if (repair) { 924 fn(0, gitdir.buf, repair, cb_data); 925 write_worktree_linking_files(repo, dotgit.buf, 926 gitdir.buf, use_relative_paths); 927 } 928 done: 929 free(dotgit_contents); 930 strbuf_release(&olddotgit); 931 strbuf_release(&backlink); 932 strbuf_release(&inferred_backlink); 933 strbuf_release(&gitdir); 934 strbuf_release(&dotgit); 935 } 936 937 int should_prune_worktree(struct repository *repo, 938 const char *id, 939 struct strbuf *reason, 940 char **wtpath, 941 timestamp_t expire) 942 { 943 struct stat st; 944 struct strbuf dotgit = STRBUF_INIT; 945 struct strbuf gitdir = STRBUF_INIT; 946 struct strbuf repo_path = STRBUF_INIT; 947 struct strbuf file = STRBUF_INIT; 948 char *path = NULL; 949 int rc = 0; 950 int fd; 951 size_t len; 952 ssize_t read_result; 953 954 *wtpath = NULL; 955 956 path = repo_common_path(repo, "worktrees/%s", id); 957 strbuf_realpath(&repo_path, path, 1); 958 FREE_AND_NULL(path); 959 960 strbuf_addf(&gitdir, "%s/gitdir", repo_path.buf); 961 if (!is_directory(repo_path.buf)) { 962 strbuf_addstr(reason, _("not a valid directory")); 963 rc = 1; 964 goto done; 965 } 966 strbuf_addf(&file, "%s/locked", repo_path.buf); 967 if (file_exists(file.buf)) { 968 goto done; 969 } 970 if (stat(gitdir.buf, &st)) { 971 strbuf_addstr(reason, _("gitdir file does not exist")); 972 rc = 1; 973 goto done; 974 } 975 fd = open(gitdir.buf, O_RDONLY); 976 if (fd < 0) { 977 strbuf_addf(reason, _("unable to read gitdir file (%s)"), 978 strerror(errno)); 979 rc = 1; 980 goto done; 981 } 982 len = xsize_t(st.st_size); 983 path = xmallocz(len); 984 985 read_result = read_in_full(fd, path, len); 986 close(fd); 987 if (read_result < 0) { 988 strbuf_addf(reason, _("unable to read gitdir file (%s)"), 989 strerror(errno)); 990 rc = 1; 991 goto done; 992 } else if (read_result != len) { 993 strbuf_addf(reason, 994 _("short read (expected %"PRIuMAX" bytes, read %"PRIuMAX")"), 995 (uintmax_t)len, (uintmax_t)read_result); 996 rc = 1; 997 goto done; 998 } 999 while (len && (path[len - 1] == '\n' || path[len - 1] == '\r')) 1000 len--; 1001 if (!len) { 1002 strbuf_addstr(reason, _("invalid gitdir file")); 1003 rc = 1; 1004 goto done; 1005 } 1006 path[len] = '\0'; 1007 if (is_absolute_path(path)) { 1008 strbuf_addstr(&dotgit, path); 1009 } else { 1010 strbuf_addf(&dotgit, "%s/%s", repo_path.buf, path); 1011 strbuf_realpath_forgiving(&dotgit, dotgit.buf, 0); 1012 } 1013 if (!file_exists(dotgit.buf)) { 1014 strbuf_reset(&file); 1015 strbuf_addf(&file, "%s/index", repo_path.buf); 1016 if (stat(file.buf, &st) || st.st_mtime <= expire) { 1017 strbuf_addstr(reason, _("gitdir file points to non-existent location")); 1018 rc = 1; 1019 goto done; 1020 } 1021 } 1022 *wtpath = strbuf_detach(&dotgit, NULL); 1023 done: 1024 free(path); 1025 strbuf_release(&dotgit); 1026 strbuf_release(&gitdir); 1027 strbuf_release(&repo_path); 1028 strbuf_release(&file); 1029 return rc; 1030 } 1031 1032 static int move_config_setting(struct repository *repo, 1033 const char *key, const char *value, 1034 const char *from_file, const char *to_file) 1035 { 1036 if (repo_config_set_in_file_gently(repo, to_file, key, NULL, value)) 1037 return error(_("unable to set %s in '%s'"), key, to_file); 1038 if (repo_config_set_in_file_gently(repo, from_file, key, NULL, NULL)) 1039 return error(_("unable to unset %s in '%s'"), key, from_file); 1040 return 0; 1041 } 1042 1043 int init_worktree_config(struct repository *r) 1044 { 1045 int res = 0; 1046 int bare = 0; 1047 struct config_set cs = { { 0 } }; 1048 const char *core_worktree; 1049 char *common_config_file; 1050 char *main_worktree_file; 1051 1052 /* 1053 * If the extension is already enabled, then we can skip the 1054 * upgrade process. 1055 */ 1056 if (r->repository_format_worktree_config) 1057 return 0; 1058 if ((res = repo_config_set_gently(r, "extensions.worktreeConfig", "true"))) 1059 return error(_("failed to set extensions.worktreeConfig setting")); 1060 1061 common_config_file = xstrfmt("%s/config", r->commondir); 1062 main_worktree_file = xstrfmt("%s/config.worktree", r->commondir); 1063 1064 git_configset_init(&cs); 1065 git_configset_add_file(&cs, common_config_file); 1066 1067 /* 1068 * If core.bare is true in the common config file, then we need to 1069 * move it to the main worktree's config file or it will break all 1070 * worktrees. If it is false, then leave it in place because it 1071 * _could_ be negating a global core.bare=true. 1072 */ 1073 if (!git_configset_get_bool(&cs, "core.bare", &bare) && bare) { 1074 if ((res = move_config_setting(r, "core.bare", "true", 1075 common_config_file, 1076 main_worktree_file))) 1077 goto cleanup; 1078 } 1079 /* 1080 * If core.worktree is set, then the main worktree is located 1081 * somewhere different than the parent of the common Git dir. 1082 * Relocate that value to avoid breaking all worktrees with this 1083 * upgrade to worktree config. 1084 */ 1085 if (!git_configset_get_value(&cs, "core.worktree", &core_worktree, NULL)) { 1086 if ((res = move_config_setting(r, "core.worktree", core_worktree, 1087 common_config_file, 1088 main_worktree_file))) 1089 goto cleanup; 1090 } 1091 1092 /* 1093 * Ensure that we use worktree config for the remaining lifetime 1094 * of the current process. 1095 */ 1096 r->repository_format_worktree_config = 1; 1097 1098 cleanup: 1099 git_configset_clear(&cs); 1100 free(common_config_file); 1101 free(main_worktree_file); 1102 return res; 1103 } 1104 1105 void write_worktree_linking_files(struct repository *repo, 1106 const char *dotgit, const char *gitdir, 1107 int use_relative_paths) 1108 { 1109 struct strbuf path = STRBUF_INIT; 1110 struct strbuf repo_path = STRBUF_INIT; 1111 struct strbuf tmp = STRBUF_INIT; 1112 1113 strbuf_addstr(&path, dotgit); 1114 strbuf_strip_suffix(&path, "/.git"); 1115 strbuf_realpath(&path, path.buf, 1); 1116 strbuf_addstr(&repo_path, gitdir); 1117 strbuf_strip_suffix(&repo_path, "/gitdir"); 1118 strbuf_realpath(&repo_path, repo_path.buf, 1); 1119 1120 if (use_relative_paths && !repo->repository_format_relative_worktrees) { 1121 if (upgrade_repository_format(repo, 1) < 0) 1122 die(_("unable to upgrade repository format to support relative worktrees")); 1123 if (repo_config_set_gently(repo, "extensions.relativeWorktrees", "true")) 1124 die(_("unable to set extensions.relativeWorktrees setting")); 1125 repo->repository_format_relative_worktrees = 1; 1126 } 1127 1128 if (use_relative_paths) { 1129 write_file(gitdir, "%s/.git", relative_path(path.buf, repo_path.buf, &tmp)); 1130 write_file(dotgit, "gitdir: %s", relative_path(repo_path.buf, path.buf, &tmp)); 1131 } else { 1132 write_file(gitdir, "%s/.git", path.buf); 1133 write_file(dotgit, "gitdir: %s", repo_path.buf); 1134 } 1135 1136 strbuf_release(&path); 1137 strbuf_release(&repo_path); 1138 strbuf_release(&tmp); 1139 }