Commit
53d330c360227e66298e0f4dba2ce6b6a3a36c37
worktree repair: detect relative path in .git file correctly
Given a state in which the cross-references between the worktree and the repository (specifically worktree/id/gitdir in the main repository and the .git file in the worktree) are recorded using absolute paths, setting 'worktree.useRelativePaths=true' and running 'git worktree repair' within the main worktree converts them to relative paths. Conversely, given a state in which the cross-references are recorded using relative paths, one would expect that setting 'worktree.useRelativePaths=false' and running 'git worktree repair' would convert them to absolute paths. However, they remain as relative paths. This is because we incorrectly use read_gitfile_gently(), which always returns an absolute path. To fix this, introduce read_gitfile_raw(), which reads the path from the .git file without resolving it to an absolute path. Because read_gitfile_raw() does not validate the path with is_git_directory(), repair_gitfile() performs this validation to preserve the existing behavior. Signed-off-by: Yoichi NAKAYAMA <yoichi.nakayama@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Yoichi NAKAYAMA <yoichi.nakayama@gmail.com> · 2026-08-28 15:19 UTC · committed by Junio C Hamano
4 files changed,
+94
−50
modified setup.c
| @@ -962,16 +962,54 @@ void read_gitfile_error_die(int error_code, const char *path) | |||
| 962 | 962 | * cases). | |
| 963 | 963 | */ | |
| 964 | 964 | const char *read_gitfile_gently(const char *path, int *return_error_code) | |
| 965 | + | { | |
| 966 | + | int error_code = 0; | |
| 967 | + | const char *slash; | |
| 968 | + | struct strbuf contents = STRBUF_INIT; | |
| 969 | + | static struct strbuf realpath = STRBUF_INIT; | |
| 970 | + | ||
| 971 | + | error_code = read_gitfile_raw(&contents, path); | |
| 972 | + | if (error_code) | |
| 973 | + | goto cleanup_return; | |
| 974 | + | ||
| 975 | + | if (!is_absolute_path(contents.buf) && (slash = strrchr(path, '/'))) { | |
| 976 | + | size_t pathlen = slash+1 - path; | |
| 977 | + | char *dir = xstrfmt("%.*s%s", (int)pathlen, path, contents.buf); | |
| 978 | + | strbuf_reset(&contents); | |
| 979 | + | strbuf_addstr(&contents, dir); | |
| 980 | + | free(dir); | |
| 981 | + | } | |
| 982 | + | if (!is_git_directory(contents.buf)) { | |
| 983 | + | error_code = READ_GITFILE_ERR_NOT_A_REPO; | |
| 984 | + | goto cleanup_return; | |
| 985 | + | } | |
| 986 | + | ||
| 987 | + | strbuf_realpath(&realpath, contents.buf, 1); | |
| 988 | + | ||
| 989 | + | cleanup_return: | |
| 990 | + | if (return_error_code) | |
| 991 | + | *return_error_code = error_code; | |
| 992 | + | else if (error_code) | |
| 993 | + | read_gitfile_error_die(error_code, path); | |
| 994 | + | ||
| 995 | + | strbuf_release(&contents); | |
| 996 | + | return error_code ? NULL : realpath.buf; | |
| 997 | + | } | |
| 998 | + | ||
| 999 | + | /* | |
| 1000 | + | * Read the path following "gitdir: " from the .git file into strbuf. | |
| 1001 | + | * | |
| 1002 | + | * Unlike read_gitfile_gently(), this function does not resolve a | |
| 1003 | + | * relative path or validate it using is_git_directory(). | |
| 1004 | + | */ | |
| 1005 | + | int read_gitfile_raw(struct strbuf *contents, const char *path) | |
| 965 | 1006 | { | |
| 966 | 1007 | const int max_file_size = 1 << 20; /* 1MB */ | |
| 967 | 1008 | int error_code = 0; | |
| 968 | 1009 | char *buf = NULL; | |
| 969 | - | char *dir = NULL; | |
| 970 | - | const char *slash; | |
| 971 | 1010 | struct stat st; | |
| 972 | 1011 | int fd; | |
| 973 | 1012 | ssize_t len; | |
| 974 | - | static struct strbuf realpath = STRBUF_INIT; | |
| 975 | 1013 | ||
| 976 | 1014 | if (stat(path, &st)) { | |
| 977 | 1015 | if (errno == ENOENT || errno == ENOTDIR) | |
| @@ -1014,32 +1052,11 @@ const char *read_gitfile_gently(const char *path, int *return_error_code) | |||
| 1014 | 1052 | error_code = READ_GITFILE_ERR_NO_PATH; | |
| 1015 | 1053 | goto cleanup_return; | |
| 1016 | 1054 | } | |
| 1017 | - | buf[len] = '\0'; | |
| 1018 | - | dir = buf + 8; | |
| 1019 | - | ||
| 1020 | - | if (!is_absolute_path(dir) && (slash = strrchr(path, '/'))) { | |
| 1021 | - | size_t pathlen = slash+1 - path; | |
| 1022 | - | dir = xstrfmt("%.*s%.*s", (int)pathlen, path, | |
| 1023 | - | (int)(len - 8), buf + 8); | |
| 1024 | - | free(buf); | |
| 1025 | - | buf = dir; | |
| 1026 | - | } | |
| 1027 | - | if (!is_git_directory(dir)) { | |
| 1028 | - | error_code = READ_GITFILE_ERR_NOT_A_REPO; | |
| 1029 | - | goto cleanup_return; | |
| 1030 | - | } | |
| 1031 | - | ||
| 1032 | - | strbuf_realpath(&realpath, dir, 1); | |
| 1033 | - | path = realpath.buf; | |
| 1055 | + | strbuf_add(contents, buf+8, len-8); | |
| 1034 | 1056 | ||
| 1035 | 1057 | cleanup_return: | |
| 1036 | - | if (return_error_code) | |
| 1037 | - | *return_error_code = error_code; | |
| 1038 | - | else if (error_code) | |
| 1039 | - | read_gitfile_error_die(error_code, path); | |
| 1040 | - | ||
| 1041 | 1058 | free(buf); | |
| 1042 | - | return error_code ? NULL : path; | |
| 1059 | + | return error_code; | |
| 1043 | 1060 | } | |
| 1044 | 1061 | ||
| 1045 | 1062 | static void apply_gitdir_and_environment(struct repository *repo, const char *path) | |
modified setup.h
| @@ -40,6 +40,7 @@ int is_nonbare_repository_dir(struct strbuf *path); | |||
| 40 | 40 | #define READ_GITFILE_ERR_IS_A_DIR 10 | |
| 41 | 41 | void read_gitfile_error_die(int error_code, const char *path); | |
| 42 | 42 | const char *read_gitfile_gently(const char *path, int *return_error_code); | |
| 43 | + | int read_gitfile_raw(struct strbuf *contents, const char *path); | |
| 43 | 44 | #define read_gitfile(path) read_gitfile_gently((path), NULL) | |
| 44 | 45 | const char *resolve_gitdir_gently(const char *suspect, int *return_error_code); | |
| 45 | 46 | #define resolve_gitdir(path) resolve_gitdir_gently((path), NULL) | |
modified t/t2406-worktree-repair.sh
| @@ -228,30 +228,60 @@ test_expect_success 'repair worktree with relative path with missing gitfile' ' | |||
| 228 | 228 | test_cmp expect wt/.git | |
| 229 | 229 | ' | |
| 230 | 230 | ||
| 231 | - | test_expect_success 'repair absolute worktree to use relative paths' ' | |
| 232 | - | test_when_finished "rm -rf main side sidemoved" && | |
| 231 | + | test_expect_success 'repair absolute to relative from side worktree' ' | |
| 232 | + | test_when_finished "rm -rf main side" && | |
| 233 | 233 | test_create_repo main && | |
| 234 | 234 | test_commit -C main init && | |
| 235 | 235 | git -C main worktree add --detach ../side && | |
| 236 | - | echo "../../../../sidemoved/.git" >expect-gitdir && | |
| 236 | + | echo "../../../../side/.git" >expect-gitdir && | |
| 237 | 237 | echo "gitdir: ../main/.git/worktrees/side" >expect-gitfile && | |
| 238 | - | mv side sidemoved && | |
| 239 | - | git -C main worktree repair --relative-paths ../sidemoved && | |
| 238 | + | git -C main worktree repair --relative-paths ../side 2>main/err && | |
| 239 | + | test_grep "gitdir absolute/relative path mismatch" main/err && | |
| 240 | 240 | test_cmp expect-gitdir main/.git/worktrees/side/gitdir && | |
| 241 | - | test_cmp expect-gitfile sidemoved/.git | |
| 241 | + | test_cmp expect-gitfile side/.git | |
| 242 | 242 | ' | |
| 243 | 243 | ||
| 244 | - | test_expect_success 'repair relative worktree to use absolute paths' ' | |
| 245 | - | test_when_finished "rm -rf main side sidemoved" && | |
| 244 | + | test_expect_success 'repair relative to absolute from side worktree' ' | |
| 245 | + | test_when_finished "rm -rf main side" && | |
| 246 | 246 | test_create_repo main && | |
| 247 | 247 | test_commit -C main init && | |
| 248 | 248 | git -C main worktree add --relative-paths --detach ../side && | |
| 249 | - | echo "$(pwd)/sidemoved/.git" >expect-gitdir && | |
| 249 | + | echo "$(pwd)/side/.git" >expect-gitdir && | |
| 250 | 250 | echo "gitdir: $(pwd)/main/.git/worktrees/side" >expect-gitfile && | |
| 251 | - | mv side sidemoved && | |
| 252 | - | git -C main worktree repair ../sidemoved && | |
| 251 | + | git -C main worktree repair ../side 2>main/err && | |
| 252 | + | test_grep "gitdir absolute/relative path mismatch" main/err && | |
| 253 | 253 | test_cmp expect-gitdir main/.git/worktrees/side/gitdir && | |
| 254 | - | test_cmp expect-gitfile sidemoved/.git | |
| 254 | + | test_cmp expect-gitfile side/.git | |
| 255 | + | ' | |
| 256 | + | ||
| 257 | + | test_expect_success 'repair absolute to relative from main worktree' ' | |
| 258 | + | test_when_finished "rm -rf main side" && | |
| 259 | + | test_create_repo main && | |
| 260 | + | git -C main config worktree.useRelativePaths false && | |
| 261 | + | test_commit -C main init && | |
| 262 | + | git -C main worktree add --detach ../side && | |
| 263 | + | echo "../../../../side/.git" >expect-gitdir && | |
| 264 | + | echo "gitdir: ../main/.git/worktrees/side" >expect-gitfile && | |
| 265 | + | git -C main config worktree.useRelativePaths true && | |
| 266 | + | git -C main worktree repair 2>main/err && | |
| 267 | + | test_grep ".git file absolute/relative path mismatch" main/err && | |
| 268 | + | test_cmp expect-gitdir main/.git/worktrees/side/gitdir && | |
| 269 | + | test_cmp expect-gitfile side/.git | |
| 270 | + | ' | |
| 271 | + | ||
| 272 | + | test_expect_success 'repair relative to absolute from main worktree' ' | |
| 273 | + | test_when_finished "rm -rf main side" && | |
| 274 | + | test_create_repo main && | |
| 275 | + | git -C main config worktree.useRelativePaths true && | |
| 276 | + | test_commit -C main init && | |
| 277 | + | git -C main worktree add --detach ../side && | |
| 278 | + | echo "$(pwd)/side/.git" >expect-gitdir && | |
| 279 | + | echo "gitdir: $(pwd)/main/.git/worktrees/side" >expect-gitfile && | |
| 280 | + | git -C main config worktree.useRelativePaths false && | |
| 281 | + | git -C main worktree repair 2>main/err && | |
| 282 | + | test_grep ".git file absolute/relative path mismatch" main/err && | |
| 283 | + | test_cmp expect-gitdir main/.git/worktrees/side/gitdir && | |
| 284 | + | test_cmp expect-gitfile side/.git | |
| 255 | 285 | ' | |
| 256 | 286 | ||
| 257 | 287 | test_done | |
modified worktree.c
| @@ -649,7 +649,8 @@ static void repair_gitfile(struct worktree *wt, | |||
| 649 | 649 | struct strbuf gitdir = STRBUF_INIT; | |
| 650 | 650 | struct strbuf repo = STRBUF_INIT; | |
| 651 | 651 | struct strbuf backlink = STRBUF_INIT; | |
| 652 | - | char *dotgit_contents = NULL; | |
| 652 | + | struct strbuf contents = STRBUF_INIT; | |
| 653 | + | const char *dotgit_contents = NULL; | |
| 653 | 654 | const char *repair = NULL; | |
| 654 | 655 | char *path = NULL; | |
| 655 | 656 | int err; | |
| @@ -667,7 +668,9 @@ static void repair_gitfile(struct worktree *wt, | |||
| 667 | 668 | strbuf_realpath(&repo, path, 1); | |
| 668 | 669 | strbuf_addf(&dotgit, "%s/.git", wt->path); | |
| 669 | 670 | strbuf_addf(&gitdir, "%s/gitdir", repo.buf); | |
| 670 | - | dotgit_contents = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err)); | |
| 671 | + | err = read_gitfile_raw(&contents, dotgit.buf); | |
| 672 | + | if (!err) | |
| 673 | + | dotgit_contents = contents.buf; | |
| 671 | 674 | ||
| 672 | 675 | if (dotgit_contents) { | |
| 673 | 676 | if (is_absolute_path(dotgit_contents)) { | |
| @@ -681,7 +684,7 @@ static void repair_gitfile(struct worktree *wt, | |||
| 681 | 684 | if (err == READ_GITFILE_ERR_NOT_A_FILE || | |
| 682 | 685 | err == READ_GITFILE_ERR_IS_A_DIR) | |
| 683 | 686 | fn(1, wt->path, _(".git is not a file"), cb_data); | |
| 684 | - | else if (err) | |
| 687 | + | else if (err || !is_git_directory(backlink.buf)) | |
| 685 | 688 | repair = _(".git file broken"); | |
| 686 | 689 | else if (fspathcmp(backlink.buf, repo.buf)) | |
| 687 | 690 | repair = _(".git file incorrect"); | |
| @@ -695,12 +698,12 @@ static void repair_gitfile(struct worktree *wt, | |||
| 695 | 698 | } | |
| 696 | 699 | ||
| 697 | 700 | done: | |
| 698 | - | free(dotgit_contents); | |
| 699 | 701 | free(path); | |
| 700 | 702 | strbuf_release(&repo); | |
| 701 | 703 | strbuf_release(&dotgit); | |
| 702 | 704 | strbuf_release(&gitdir); | |
| 703 | 705 | strbuf_release(&backlink); | |
| 706 | + | strbuf_release(&contents); | |
| 704 | 707 | } | |
| 705 | 708 | ||
| 706 | 709 | static void repair_noop(int iserr UNUSED, | |
| @@ -857,14 +860,7 @@ void repair_worktree_at_path(struct repository *repo, | |||
| 857 | 860 | strbuf_realpath_forgiving(&inferred_backlink, inferred_backlink.buf, 0); | |
| 858 | 861 | dotgit_contents = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err)); | |
| 859 | 862 | if (dotgit_contents) { | |
| 860 | - | if (is_absolute_path(dotgit_contents)) { | |
| 861 | - | strbuf_addstr(&backlink, dotgit_contents); | |
| 862 | - | } else { | |
| 863 | - | strbuf_addbuf(&backlink, &dotgit); | |
| 864 | - | strbuf_strip_suffix(&backlink, ".git"); | |
| 865 | - | strbuf_addstr(&backlink, dotgit_contents); | |
| 866 | - | strbuf_realpath_forgiving(&backlink, backlink.buf, 0); | |
| 867 | - | } | |
| 863 | + | strbuf_addstr(&backlink, dotgit_contents); | |
| 868 | 864 | } else if (err == READ_GITFILE_ERR_NOT_A_FILE || | |
| 869 | 865 | err == READ_GITFILE_ERR_IS_A_DIR) { | |
| 870 | 866 | fn(1, dotgit.buf, _("unable to locate repository; .git is not a file"), cb_data); | |