Compare
8043d64be4ab216efcb6de2d61c668f8142ed4d4..52e324c8d60c30b8ea3bac654f183bfb3467dfae
Merge branch 'rs/worktree-add-basename-fixes'
The string extraction logic for the branch name and worktree name from the given path in 'git worktree add' has been corrected and simplified to avoid out-of-bounds reads and improper handling of trailing slashes. * rs/worktree-add-basename-fixes: worktree add: let worktree_basename() return string copy worktree add: trim slashes when deriving branch name from path worktree add: reject separator-only path worktree add: don't read out of bounds in worktree_basename()
Junio C Hamano <gitster@pobox.com> · 2026-09-10 12:36 UTC
11 files changed,
+209
−82
- .github/workflows/main.yml
- Documentation/RelNotes/2.56.0.adoc
- builtin/worktree.c
- contrib/completion/git-completion.zsh
- setup.c
- setup.h
- t/t1401-symbolic-ref.sh
- t/t1402-check-ref-format.sh
- t/t2400-worktree-add.sh
- t/t2406-worktree-repair.sh
- worktree.c
modified .github/workflows/main.yml
| @@ -5,18 +5,20 @@ on: [push, pull_request] | |||
| 5 | 5 | env: | |
| 6 | 6 | DEVELOPER: 1 | |
| 7 | 7 | ||
| 8 | - | # If more than one workflow run is triggered for the very same commit hash | |
| 9 | - | # (which happens when multiple branches pointing to the same commit), only | |
| 10 | - | # the first one is allowed to run, the second will be kept in the "queued" | |
| 11 | - | # state. This allows a successful completion of the first run to be reused | |
| 12 | - | # in the second run via the `skip-if-redundant` logic in the `config` job. | |
| 8 | + | # For pull requests, only the latest workflow run is allowed to proceed. | |
| 9 | + | # Older runs are canceled when a new revision is pushed. | |
| 13 | 10 | # | |
| 14 | - | # The only caveat is that if a workflow run is triggered for the same commit | |
| 15 | - | # hash that another run is already being held, that latter run will be | |
| 16 | - | # canceled. For more details about the `concurrency` attribute, see: | |
| 11 | + | # For pushes, if more than one workflow run is triggered for the very same | |
| 12 | + | # commit hash (which happens when multiple branches point to the same commit), | |
| 13 | + | # only the first one is allowed to run. This allows a successful completion of | |
| 14 | + | # the first run to be reused in the second run via the `skip-if-redundant` | |
| 15 | + | # logic in the `config` job. | |
| 16 | + | # | |
| 17 | + | # For more details about the `concurrency` attribute, see: | |
| 17 | 18 | # https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#concurrency | |
| 18 | 19 | concurrency: | |
| 19 | - | group: ${{ github.sha }} | |
| 20 | + | group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }} | |
| 21 | + | cancel-in-progress: ${{ github.event_name == 'pull_request' }} | |
| 20 | 22 | ||
| 21 | 23 | jobs: | |
| 22 | 24 | ci-config: | |
modified Documentation/RelNotes/2.56.0.adoc
| @@ -139,6 +139,11 @@ UI, Workflows & Features | |||
| 139 | 139 | * The command line completion (in contrib/) has been taught to handle | |
| 140 | 140 | the experimental 'git history' command. | |
| 141 | 141 | ||
| 142 | + | * 'git checkout' and 'git worktree add' makes guesses based on a name | |
| 143 | + | of a remote-tracking branch, but does not give an error when such a | |
| 144 | + | remote-tracking branch cannot be uniquely identified, which has | |
| 145 | + | been corrected. | |
| 146 | + | ||
| 142 | 147 | ||
| 143 | 148 | Performance, Internal Implementation, Development Support etc. | |
| 144 | 149 | -------------------------------------------------------------- | |
| @@ -484,6 +489,15 @@ Performance, Internal Implementation, Development Support etc. | |||
| 484 | 489 | 'struct repository', continuing the libification process and | |
| 485 | 490 | allowing per-repository control (such as for submodules). | |
| 486 | 491 | ||
| 492 | + | * The reftable code has been optimized to avoid an unnecessary | |
| 493 | + | stat/reload of the stack when an addition already holds the | |
| 494 | + | list_file lock, reducing the number of newfstatat syscalls from | |
| 495 | + | linear to constant when writing refs. | |
| 496 | + | ||
| 497 | + | * The application of the edited patch in 'git add -e' has been | |
| 498 | + | refactored to use the internal apply API directly, avoiding the need | |
| 499 | + | to spawn a 'git apply' subprocess. | |
| 500 | + | ||
| 487 | 501 | ||
| 488 | 502 | Fixes since v2.55 | |
| 489 | 503 | ----------------- | |
| @@ -748,6 +762,23 @@ Fixes since v2.55 | |||
| 748 | 762 | This prevents intended textual URLs from being mangled or mistakenly | |
| 749 | 763 | treated as metadata keys. | |
| 750 | 764 | ||
| 765 | + | * The instructions for deprecated commands emitted by | |
| 766 | + | you_still_use_that() have been reworded to clarify that the removal | |
| 767 | + | decision is final and to provide more assertive guidance on finding | |
| 768 | + | a replacement. | |
| 769 | + | (merge 8ace32221c jc/you-still-use-that later to maint). | |
| 770 | + | ||
| 771 | + | * The zsh completion script (in 'contrib/') has been updated to | |
| 772 | + | correctly locate the Git command after global options like '-C' by | |
| 773 | + | properly skipping them, similar to how the bash completion does. | |
| 774 | + | ||
| 775 | + | * The git worktree repair command failed to rewrite the .git file of | |
| 776 | + | a working tree from a relative path to an absolute path when the | |
| 777 | + | command was run in the working tree itself. The | |
| 778 | + | read_gitfile_gently() function was modified to also return whether | |
| 779 | + | the path originally recorded in the file was absolute, and this new | |
| 780 | + | capability is used to correctly detect such mismatches. | |
| 781 | + | ||
| 751 | 782 | * Other code cleanup, docfix, build fix, etc. | |
| 752 | 783 | (merge 026636128f ss/submittingpatches-typofix later to maint). | |
| 753 | 784 | (merge d2af22cc21 jc/rerere-doc-typofix later to maint). | |
modified builtin/worktree.c
| @@ -294,7 +294,7 @@ static void remove_junk_on_signal(int signo) | |||
| 294 | 294 | raise(signo); | |
| 295 | 295 | } | |
| 296 | 296 | ||
| 297 | - | static const char *worktree_basename(const char *path, int *olen) | |
| 297 | + | static char *worktree_basename_dup(const char *path) | |
| 298 | 298 | { | |
| 299 | 299 | const char *name; | |
| 300 | 300 | int len; | |
| @@ -303,14 +303,11 @@ static const char *worktree_basename(const char *path, int *olen) | |||
| 303 | 303 | while (len && is_dir_sep(path[len - 1])) | |
| 304 | 304 | len--; | |
| 305 | 305 | ||
| 306 | - | for (name = path + len - 1; name > path; name--) | |
| 307 | - | if (is_dir_sep(*name)) { | |
| 308 | - | name++; | |
| 309 | - | break; | |
| 310 | - | } | |
| 306 | + | name = path + len; | |
| 307 | + | while (name > path && !is_dir_sep(name[-1])) | |
| 308 | + | name--; | |
| 311 | 309 | ||
| 312 | - | *olen = len; | |
| 313 | - | return name; | |
| 310 | + | return xmemdupz(name, path + len - name); | |
| 314 | 311 | } | |
| 315 | 312 | ||
| 316 | 313 | /* check that path is viable location for worktree */ | |
| @@ -464,6 +461,7 @@ static int add_worktree(const char *path, const char *refname, | |||
| 464 | 461 | struct strbuf sb_git = STRBUF_INIT, sb_repo = STRBUF_INIT; | |
| 465 | 462 | struct strbuf sb = STRBUF_INIT; | |
| 466 | 463 | const char *name; | |
| 464 | + | char *name_to_free = NULL; | |
| 467 | 465 | struct strvec child_env = STRVEC_INIT; | |
| 468 | 466 | unsigned int counter = 0; | |
| 469 | 467 | int len, ret; | |
| @@ -491,12 +489,12 @@ static int add_worktree(const char *path, const char *refname, | |||
| 491 | 489 | if (!commit && !opts->orphan) | |
| 492 | 490 | die(_("invalid reference: %s"), refname); | |
| 493 | 491 | ||
| 494 | - | name = worktree_basename(path, &len); | |
| 495 | - | strbuf_add(&sb, name, path + len - name); | |
| 496 | - | sanitize_refname_component(sb.buf, &sb_name); | |
| 492 | + | name = name_to_free = worktree_basename_dup(path); | |
| 493 | + | if (!*name) | |
| 494 | + | die(_("invalid path '%s'"), path); | |
| 495 | + | sanitize_refname_component(name, &sb_name); | |
| 497 | 496 | if (!sb_name.len) | |
| 498 | - | BUG("How come '%s' becomes empty after sanitization?", sb.buf); | |
| 499 | - | strbuf_reset(&sb); | |
| 497 | + | BUG("How come '%s' becomes empty after sanitization?", name); | |
| 500 | 498 | name = sb_name.buf; | |
| 501 | 499 | repo_git_path_replace(the_repository, &sb_repo, "worktrees/%s", name); | |
| 502 | 500 | len = sb_repo.len; | |
| @@ -630,6 +628,7 @@ done: | |||
| 630 | 628 | strbuf_release(&sb_git); | |
| 631 | 629 | strbuf_release(&sb_name); | |
| 632 | 630 | free_worktree(wt); | |
| 631 | + | free(name_to_free); | |
| 633 | 632 | return ret; | |
| 634 | 633 | } | |
| 635 | 634 | ||
| @@ -785,10 +784,8 @@ static void advise_disambiguating_remotes(const char *path, const char *branch, | |||
| 785 | 784 | ||
| 786 | 785 | static char *dwim_branch(const struct add_opts *opts, const char *path, char **new_branch) | |
| 787 | 786 | { | |
| 788 | - | int n; | |
| 789 | 787 | int branch_exists; | |
| 790 | - | const char *s = worktree_basename(path, &n); | |
| 791 | - | char *branchname = xstrndup(s, n); | |
| 788 | + | char *branchname = worktree_basename_dup(path); | |
| 792 | 789 | struct strbuf ref = STRBUF_INIT; | |
| 793 | 790 | ||
| 794 | 791 | branch_exists = !check_branch_ref(the_repository, &ref, branchname) && | |
| @@ -909,9 +906,7 @@ static int add(int ac, const char **av, const char *prefix, | |||
| 909 | 906 | } | |
| 910 | 907 | ||
| 911 | 908 | if (opts.orphan && !new_branch) { | |
| 912 | - | int n; | |
| 913 | - | const char *s = worktree_basename(path, &n); | |
| 914 | - | new_branch = new_branch_to_free = xstrndup(s, n); | |
| 909 | + | new_branch = new_branch_to_free = worktree_basename_dup(path); | |
| 915 | 910 | } else if (opts.orphan) { | |
| 916 | 911 | ; /* no-op */ | |
| 917 | 912 | } else if (opts.detach) { | |
modified contrib/completion/git-completion.zsh
| @@ -227,6 +227,7 @@ __git_zsh_main () | |||
| 227 | 227 | '(-p --paginate --no-pager)'{-p,--paginate}'[pipe all output into ''less'']' \ | |
| 228 | 228 | '(-p --paginate)--no-pager[do not pipe git output into a pager]' \ | |
| 229 | 229 | '--git-dir=-[set the path to the repository]: :_directories' \ | |
| 230 | + | '*-C[run as if git was started in <path>]: :_directories' \ | |
| 230 | 231 | '--bare[treat the repository as a bare repository]' \ | |
| 231 | 232 | '(- :)--version[prints the git suite version]' \ | |
| 232 | 233 | '--exec-path=-[path to where your core git programs are installed]:: :_directories' \ | |
| @@ -251,7 +252,29 @@ __git_zsh_main () | |||
| 251 | 252 | done | |
| 252 | 253 | ;; | |
| 253 | 254 | (arg) | |
| 254 | - | local command="${words[1]}" __git_dir __git_cmd_idx=1 | |
| 255 | + | local command="${words[1]}" __git_dir __git_cmd_idx | |
| 256 | + | local -a __git_C_args | |
| 257 | + | local -i i=2 | |
| 258 | + | ||
| 259 | + | while (( i <= $#orig_words )); do | |
| 260 | + | case ${orig_words[i]} in | |
| 261 | + | -C) | |
| 262 | + | __git_C_args+=(-C ${orig_words[i+1]}) | |
| 263 | + | (( i++ )) | |
| 264 | + | ;; | |
| 265 | + | -c|--git-dir|--work-tree|--namespace) | |
| 266 | + | (( i++ )) | |
| 267 | + | ;; | |
| 268 | + | -*) | |
| 269 | + | ;; | |
| 270 | + | *) | |
| 271 | + | break | |
| 272 | + | ;; | |
| 273 | + | esac | |
| 274 | + | (( i++ )) | |
| 275 | + | done | |
| 276 | + | ||
| 277 | + | __git_cmd_idx=$(( i - 1 )) | |
| 255 | 278 | ||
| 256 | 279 | if (( $+opt_args[--bare] )); then | |
| 257 | 280 | __git_dir='.' | |
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/t1401-symbolic-ref.sh
| @@ -38,6 +38,18 @@ test_expect_success 'symbolic-ref refuses bare sha1' ' | |||
| 38 | 38 | ||
| 39 | 39 | reset_to_sane | |
| 40 | 40 | ||
| 41 | + | test_expect_success 'symbolic-ref reports a non-symbolic ref' ' | |
| 42 | + | test_must_fail git symbolic-ref refs/heads/foo >out 2>err && | |
| 43 | + | test_must_be_empty out && | |
| 44 | + | test_grep "is not a symbolic ref" err | |
| 45 | + | ' | |
| 46 | + | ||
| 47 | + | test_expect_success 'symbolic-ref -q is silent on a non-symbolic ref' ' | |
| 48 | + | test_must_fail git symbolic-ref -q refs/heads/foo >out 2>err && | |
| 49 | + | test_must_be_empty out && | |
| 50 | + | test_must_be_empty err | |
| 51 | + | ' | |
| 52 | + | ||
| 41 | 53 | test_expect_success 'HEAD cannot be removed' ' | |
| 42 | 54 | test_must_fail git symbolic-ref -d HEAD | |
| 43 | 55 | ' | |
modified t/t1402-check-ref-format.sh
| @@ -49,16 +49,19 @@ invalid_ref 'foo/./bar' | |||
| 49 | 49 | invalid_ref 'foo/bar/.' | |
| 50 | 50 | invalid_ref '.refs/foo' | |
| 51 | 51 | invalid_ref 'refs/heads/foo.' | |
| 52 | - | invalid_ref 'heads/foo..bar' | |
| 53 | - | invalid_ref 'heads/foo?bar' | |
| 52 | + | for c in '?' '~' '^' ':' '*' '[' ' ' '\' '..' | |
| 53 | + | do | |
| 54 | + | invalid_ref "heads/foo${c}bar" | |
| 55 | + | done | |
| 54 | 56 | valid_ref 'foo./bar' | |
| 55 | 57 | invalid_ref 'heads/foo.lock' | |
| 56 | 58 | invalid_ref 'heads///foo.lock' | |
| 57 | 59 | invalid_ref 'foo.lock/bar' | |
| 58 | 60 | invalid_ref 'foo.lock///bar' | |
| 59 | 61 | valid_ref 'heads/foo@bar' | |
| 62 | + | valid_ref 'refs/@' | |
| 63 | + | invalid_ref '@' --allow-onelevel | |
| 60 | 64 | invalid_ref 'heads/v@{ation' | |
| 61 | - | invalid_ref 'heads/foo\bar' | |
| 62 | 65 | invalid_ref "$(printf 'heads/foo\t')" | |
| 63 | 66 | invalid_ref "$(printf 'heads/foo\177')" | |
| 64 | 67 | valid_ref "$(printf 'heads/fu\303\237')" | |
modified t/t2400-worktree-add.sh
| @@ -46,6 +46,10 @@ test_expect_success '"add" refuses to checkout locked branch' ' | |||
| 46 | 46 | test_path_is_missing .git/worktrees/zere | |
| 47 | 47 | ' | |
| 48 | 48 | ||
| 49 | + | test_expect_success '"add" rejects an empty path' ' | |
| 50 | + | test_must_fail git worktree add "" HEAD | |
| 51 | + | ' | |
| 52 | + | ||
| 49 | 53 | test_expect_success 'checking out paths not complaining about linked checkouts' ' | |
| 50 | 54 | ( | |
| 51 | 55 | cd existing_empty && | |
| @@ -294,6 +298,11 @@ test_expect_success '"add" with <branch> omitted' ' | |||
| 294 | 298 | test_cmp_rev HEAD bat | |
| 295 | 299 | ' | |
| 296 | 300 | ||
| 301 | + | test_expect_success '"add" with trailing slash and <branch> omitted' ' | |
| 302 | + | git worktree add waffle/bit/ && | |
| 303 | + | test_cmp_rev HEAD bit | |
| 304 | + | ' | |
| 305 | + | ||
| 297 | 306 | test_expect_success '"add" checks out existing branch of dwimd name' ' | |
| 298 | 307 | git branch dwim HEAD~1 && | |
| 299 | 308 | git worktree add dwim && | |
| @@ -384,6 +393,14 @@ test_expect_success '"add --orphan (no -b)"' ' | |||
| 384 | 393 | test_cmp expected actual | |
| 385 | 394 | ' | |
| 386 | 395 | ||
| 396 | + | test_expect_success '"add --orphan with trailing slash (no -b)"' ' | |
| 397 | + | test_when_finished "git worktree remove -f -f neworphan" && | |
| 398 | + | git worktree add --orphan ./neworphan/ && | |
| 399 | + | echo refs/heads/neworphan >expected && | |
| 400 | + | git -C neworphan symbolic-ref HEAD >actual && | |
| 401 | + | test_cmp expected actual | |
| 402 | + | ' | |
| 403 | + | ||
| 387 | 404 | test_expect_success '"add --orphan --quiet"' ' | |
| 388 | 405 | test_when_finished "git worktree remove -f -f orphandir" && | |
| 389 | 406 | git worktree add --quiet --orphan -b neworphan orphandir 2>log.actual && | |
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); | |