|
| 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 an MSYS path (/c/foo) to a WSL path (/mnt/c/foo). |
| 17 | +# MINGW-only; on other platforms /c/foo is not a drive prefix. |
| 18 | +to_mnt () { |
| 19 | + echo "$1" | sed -E 's|^/([a-zA-Z])/|/mnt/\L\1/|' |
| 20 | +} |
| 21 | + |
| 22 | +# Same, for Cygwin form (/cygdrive/c/foo). |
| 23 | +to_cygdrive () { |
| 24 | + echo "$1" | sed -E 's|^/([a-zA-Z])/|/cygdrive/\L\1/|' |
| 25 | +} |
| 26 | + |
| 27 | +test_expect_success MINGW 'setup main repo' ' |
| 28 | + git init repo && |
| 29 | + test_commit -C repo init |
| 30 | +' |
| 31 | + |
| 32 | +test_expect_success MINGW 'read_gitfile_gently translates /mnt/<x>/ gitdir' ' |
| 33 | + test_when_finished "rm -rf wtlink actual" && |
| 34 | + REAL=$(cd repo/.git && pwd) && |
| 35 | + MNT=$(to_mnt "$REAL") && |
| 36 | +
|
| 37 | + # Sanity: the path must actually start with /mnt/ - if it does not, |
| 38 | + # the host shell did not give us a path with a drive prefix and the |
| 39 | + # rest of the test would be silently meaningless. |
| 40 | + case "$MNT" in |
| 41 | + /mnt/*) : ok ;; |
| 42 | + *) BUG "to_mnt produced $MNT from $REAL" ;; |
| 43 | + esac && |
| 44 | +
|
| 45 | + mkdir wtlink && |
| 46 | + printf "gitdir: %s\n" "$MNT" >wtlink/.git && |
| 47 | +
|
| 48 | + (cd wtlink && git rev-parse --git-dir) >actual && |
| 49 | + test_path_is_dir "$(cat actual)" |
| 50 | +' |
| 51 | + |
| 52 | +test_expect_success MINGW 'read_gitfile_gently translates /cygdrive/<x>/ gitdir' ' |
| 53 | + test_when_finished "rm -rf wtlink actual" && |
| 54 | + REAL=$(cd repo/.git && pwd) && |
| 55 | + CYG=$(to_cygdrive "$REAL") && |
| 56 | +
|
| 57 | + mkdir wtlink && |
| 58 | + printf "gitdir: %s\n" "$CYG" >wtlink/.git && |
| 59 | +
|
| 60 | + (cd wtlink && git rev-parse --git-dir) >actual && |
| 61 | + test_path_is_dir "$(cat actual)" |
| 62 | +' |
| 63 | + |
| 64 | +test_expect_success MINGW 'read_gitfile_gently leaves /mnt/<multichar>/ alone' ' |
| 65 | + test_when_finished "rm -rf wtlink" && |
| 66 | + mkdir wtlink && |
| 67 | + # "storage" is not a single drive letter, so this must not be |
| 68 | + # translated. The path does not exist on Windows, so the open fails. |
| 69 | + echo "gitdir: /mnt/storage/no/such/repo" >wtlink/.git && |
| 70 | +
|
| 71 | + test_must_fail git -C wtlink rev-parse --git-dir 2>err && |
| 72 | + test_grep "not a git repository" err |
| 73 | +' |
| 74 | + |
| 75 | +test_expect_success MINGW 'get_linked_worktree finds worktree recorded with /mnt/<x>/ path' ' |
| 76 | + test_when_finished "rm -rf repo/wt repo/.git/worktrees/wt" && |
| 77 | +
|
| 78 | + git -C repo worktree add --detach wt && |
| 79 | + WT_REAL=$(cd repo/wt && pwd) && |
| 80 | + WT_MNT=$(to_mnt "$WT_REAL") && |
| 81 | +
|
| 82 | + # Overwrite the recorded worktree path with the WSL form, mimicking |
| 83 | + # what `git worktree add` writes when run from inside WSL. |
| 84 | + printf "%s/.git\n" "$WT_MNT" >repo/.git/worktrees/wt/gitdir && |
| 85 | +
|
| 86 | + # `git worktree list` reads that file via get_linked_worktree. |
| 87 | + # After translation the worktree should still be enumerated. |
| 88 | + git -C repo worktree list --porcelain >list && |
| 89 | + grep -F "worktree $WT_REAL" list |
| 90 | +' |
| 91 | + |
| 92 | +test_expect_success MINGW 'get_common_dir_noenv translates /mnt/<x>/ commondir' ' |
| 93 | + test_when_finished "rm -rf wtdir wt actual" && |
| 94 | +
|
| 95 | + REAL=$(cd repo/.git && pwd) && |
| 96 | + MNT=$(to_mnt "$REAL") && |
| 97 | +
|
| 98 | + # Build a synthetic linked-worktree gitdir that points at the main |
| 99 | + # repo via a /mnt/<x>/ commondir record. |
| 100 | + mkdir wtdir && |
| 101 | + echo "$(cd repo && git rev-parse HEAD)" >wtdir/HEAD && |
| 102 | + echo "$MNT" >wtdir/commondir && |
| 103 | + printf "%s/.git\n" "$(pwd)" >wtdir/gitdir && |
| 104 | +
|
| 105 | + # rev-parse --git-common-dir on a checkout that points here should |
| 106 | + # resolve through the translated commondir. |
| 107 | + mkdir wt && |
| 108 | + printf "gitdir: %s\n" "$(pwd)/wtdir" >wt/.git && |
| 109 | + (cd wt && git rev-parse --git-common-dir) >actual && |
| 110 | + test_path_is_dir "$(cat actual)" |
| 111 | +' |
| 112 | + |
| 113 | +test_done |
0 commit comments