|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +test_description='translate WSL/Cygwin /mnt/<x>/ paths in worktree gitfiles |
| 4 | +
|
| 5 | +Verify that `git worktree add` artefacts written from inside WSL2 or |
| 6 | +Cygwin/MSYS - which use POSIX-mounted paths like `/mnt/c/...` or |
| 7 | +`/cygdrive/c/...` - are still resolvable when read back from native |
| 8 | +Windows git. |
| 9 | +' |
| 10 | + |
| 11 | +GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
| 12 | +export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
| 13 | + |
| 14 | +. ./test-lib.sh |
| 15 | + |
| 16 | +# Convert any drive-prefixed path Windows git might emit to the named |
| 17 | +# POSIX-mount form. Handles MSYS form (/c/foo) and Windows forms |
| 18 | +# (C:/foo and C:\foo). MINGW-only. |
| 19 | +mount_form () { |
| 20 | + prefix=$1 ;# /mnt or /cygdrive |
| 21 | + path=$2 |
| 22 | + case "$path" in |
| 23 | + /[A-Za-z]/*) |
| 24 | + echo "$path" | sed -E "s|^/([A-Za-z])/|$prefix/\\L\\1/|" |
| 25 | + ;; |
| 26 | + [A-Za-z]:/*) |
| 27 | + echo "$path" | sed -E "s|^([A-Za-z]):/|$prefix/\\L\\1/|" |
| 28 | + ;; |
| 29 | + [A-Za-z]:'\'*) |
| 30 | + echo "$path" | sed -E "s|^([A-Za-z]):.|$prefix/\\L\\1/|; s|\\\\|/|g" |
| 31 | + ;; |
| 32 | + *) |
| 33 | + echo "$path" |
| 34 | + ;; |
| 35 | + esac |
| 36 | +} |
| 37 | + |
| 38 | +to_mnt () { |
| 39 | + mount_form /mnt "$1" |
| 40 | +} |
| 41 | + |
| 42 | +to_cygdrive () { |
| 43 | + mount_form /cygdrive "$1" |
| 44 | +} |
| 45 | + |
| 46 | +# Convert any drive-prefixed path to the MSYS2 form (/c/foo). MINGW-only. |
| 47 | +to_msys () { |
| 48 | + case "$1" in |
| 49 | + /[A-Za-z]/*) |
| 50 | + # Already MSYS form. |
| 51 | + echo "$1" |
| 52 | + ;; |
| 53 | + [A-Za-z]:/*) |
| 54 | + echo "$1" | sed -E "s|^([A-Za-z]):/|/\\L\\1/|" |
| 55 | + ;; |
| 56 | + [A-Za-z]:'\'*) |
| 57 | + echo "$1" | sed -E "s|^([A-Za-z]):.|/\\L\\1/|; s|\\\\|/|g" |
| 58 | + ;; |
| 59 | + *) |
| 60 | + echo "$1" |
| 61 | + ;; |
| 62 | + esac |
| 63 | +} |
| 64 | + |
| 65 | +test_expect_success MINGW 'setup main repo' ' |
| 66 | + git init repo && |
| 67 | + test_commit -C repo init |
| 68 | +' |
| 69 | + |
| 70 | +test_expect_success MINGW 'read_gitfile_gently translates /mnt/<x>/ gitdir' ' |
| 71 | + test_when_finished "rm -rf wtlink actual" && |
| 72 | + REAL=$(cd repo/.git && pwd) && |
| 73 | + MNT=$(to_mnt "$REAL") && |
| 74 | +
|
| 75 | + # Sanity: the path must actually start with /mnt/ - if it does not, |
| 76 | + # the host shell did not give us a path with a drive prefix and the |
| 77 | + # rest of the test would be silently meaningless. |
| 78 | + case "$MNT" in |
| 79 | + /mnt/*) : ok ;; |
| 80 | + *) BUG "to_mnt produced $MNT from $REAL" ;; |
| 81 | + esac && |
| 82 | +
|
| 83 | + mkdir wtlink && |
| 84 | + printf "gitdir: %s\n" "$MNT" >wtlink/.git && |
| 85 | +
|
| 86 | + (cd wtlink && git rev-parse --git-dir) >actual && |
| 87 | + test_path_is_dir "$(cat actual)" |
| 88 | +' |
| 89 | + |
| 90 | +test_expect_success MINGW 'read_gitfile_gently translates /cygdrive/<x>/ gitdir' ' |
| 91 | + test_when_finished "rm -rf wtlink actual" && |
| 92 | + REAL=$(cd repo/.git && pwd) && |
| 93 | + CYG=$(to_cygdrive "$REAL") && |
| 94 | +
|
| 95 | + mkdir wtlink && |
| 96 | + printf "gitdir: %s\n" "$CYG" >wtlink/.git && |
| 97 | +
|
| 98 | + (cd wtlink && git rev-parse --git-dir) >actual && |
| 99 | + test_path_is_dir "$(cat actual)" |
| 100 | +' |
| 101 | + |
| 102 | +test_expect_success MINGW 'read_gitfile_gently translates /<x>/ MSYS2 gitdir' ' |
| 103 | + test_when_finished "rm -rf wtlink actual" && |
| 104 | + REAL=$(cd repo/.git && pwd) && |
| 105 | + MSYS_PATH=$(to_msys "$REAL") && |
| 106 | +
|
| 107 | + case "$MSYS_PATH" in |
| 108 | + /[A-Za-z]/*) : ok ;; |
| 109 | + *) BUG "to_msys produced $MSYS_PATH from $REAL" ;; |
| 110 | + esac && |
| 111 | +
|
| 112 | + mkdir wtlink && |
| 113 | + printf "gitdir: %s\n" "$MSYS_PATH" >wtlink/.git && |
| 114 | +
|
| 115 | + (cd wtlink && git rev-parse --git-dir) >actual && |
| 116 | + test_path_is_dir "$(cat actual)" |
| 117 | +' |
| 118 | + |
| 119 | +test_expect_success MINGW 'read_gitfile_gently leaves /mnt/<multichar>/ alone' ' |
| 120 | + test_when_finished "rm -rf wtlink" && |
| 121 | + mkdir wtlink && |
| 122 | + # "storage" is not a single drive letter, so this must not be |
| 123 | + # translated. The path does not exist on Windows, so the open fails. |
| 124 | + echo "gitdir: /mnt/storage/no/such/repo" >wtlink/.git && |
| 125 | +
|
| 126 | + test_must_fail git -C wtlink rev-parse --git-dir 2>err && |
| 127 | + test_grep "not a git repository" err |
| 128 | +' |
| 129 | + |
| 130 | +test_expect_success MINGW 'get_linked_worktree finds worktree recorded with /mnt/<x>/ path' ' |
| 131 | + test_when_finished "rm -rf repo/wt repo/.git/worktrees/wt" && |
| 132 | +
|
| 133 | + git -C repo worktree add --detach wt && |
| 134 | + WT_REAL=$(cd repo/wt && pwd) && |
| 135 | + WT_MNT=$(to_mnt "$WT_REAL") && |
| 136 | +
|
| 137 | + # Overwrite the recorded worktree path with the WSL form, mimicking |
| 138 | + # what `git worktree add` writes when run from inside WSL. |
| 139 | + printf "%s/.git\n" "$WT_MNT" >repo/.git/worktrees/wt/gitdir && |
| 140 | +
|
| 141 | + # `git worktree list` reads that file via get_linked_worktree. |
| 142 | + # After translation the worktree must still be reachable: it must |
| 143 | + # NOT be flagged prunable, and a git operation inside the worktree |
| 144 | + # directory must succeed. |
| 145 | + git -C repo worktree list --porcelain >list && |
| 146 | + ! grep -q "^prunable" list && |
| 147 | + (cd "$WT_REAL" && git rev-parse --is-inside-work-tree) |
| 148 | +' |
| 149 | + |
| 150 | +test_expect_success MINGW 'get_common_dir_noenv translates /mnt/<x>/ commondir' ' |
| 151 | + test_when_finished "rm -rf wtdir wt actual" && |
| 152 | +
|
| 153 | + REAL=$(cd repo/.git && pwd) && |
| 154 | + MNT=$(to_mnt "$REAL") && |
| 155 | +
|
| 156 | + # Build a synthetic linked-worktree gitdir that points at the main |
| 157 | + # repo via a /mnt/<x>/ commondir record. |
| 158 | + mkdir wtdir && |
| 159 | + echo "$(cd repo && git rev-parse HEAD)" >wtdir/HEAD && |
| 160 | + echo "$MNT" >wtdir/commondir && |
| 161 | + printf "%s/.git\n" "$(pwd)" >wtdir/gitdir && |
| 162 | +
|
| 163 | + # rev-parse --git-common-dir on a checkout that points here should |
| 164 | + # resolve through the translated commondir. |
| 165 | + mkdir wt && |
| 166 | + printf "gitdir: %s\n" "$(pwd)/wtdir" >wt/.git && |
| 167 | + (cd wt && git rev-parse --git-common-dir) >actual && |
| 168 | + test_path_is_dir "$(cat actual)" |
| 169 | +' |
| 170 | + |
| 171 | +test_done |
0 commit comments