|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +test_description='test git worktree repair' |
| 4 | + |
| 5 | +. ./test-lib.sh |
| 6 | + |
| 7 | +test_expect_success setup ' |
| 8 | + test_commit init |
| 9 | +' |
| 10 | + |
| 11 | +test_expect_success 'skip missing worktree' ' |
| 12 | + test_when_finished "git worktree prune" && |
| 13 | + git worktree add --detach missing && |
| 14 | + rm -rf missing && |
| 15 | + git worktree repair >out 2>err && |
| 16 | + test_must_be_empty out && |
| 17 | + test_must_be_empty err |
| 18 | +' |
| 19 | + |
| 20 | +test_expect_success 'worktree path not directory' ' |
| 21 | + test_when_finished "git worktree prune" && |
| 22 | + git worktree add --detach notdir && |
| 23 | + rm -rf notdir && |
| 24 | + >notdir && |
| 25 | + test_must_fail git worktree repair >out 2>err && |
| 26 | + test_must_be_empty out && |
| 27 | + test_i18ngrep "not a directory" err |
| 28 | +' |
| 29 | + |
| 30 | +test_expect_success "don't clobber .git repo" ' |
| 31 | + test_when_finished "rm -rf repo && git worktree prune" && |
| 32 | + git worktree add --detach repo && |
| 33 | + rm -rf repo && |
| 34 | + test_create_repo repo && |
| 35 | + test_must_fail git worktree repair >out 2>err && |
| 36 | + test_must_be_empty out && |
| 37 | + test_i18ngrep ".git is not a file" err |
| 38 | +' |
| 39 | + |
| 40 | +test_corrupt_gitfile () { |
| 41 | + butcher=$1 && |
| 42 | + problem=$2 && |
| 43 | + repairdir=${3:-.} && |
| 44 | + test_when_finished 'rm -rf corrupt && git worktree prune' && |
| 45 | + git worktree add --detach corrupt && |
| 46 | + git -C corrupt rev-parse --absolute-git-dir >expect && |
| 47 | + eval "$butcher" && |
| 48 | + git -C "$repairdir" worktree repair >out 2>err && |
| 49 | + test_i18ngrep "$problem" out && |
| 50 | + test_must_be_empty err && |
| 51 | + git -C corrupt rev-parse --absolute-git-dir >actual && |
| 52 | + test_cmp expect actual |
| 53 | +} |
| 54 | + |
| 55 | +test_expect_success 'repair missing .git file' ' |
| 56 | + test_corrupt_gitfile "rm -f corrupt/.git" ".git file broken" |
| 57 | +' |
| 58 | + |
| 59 | +test_expect_success 'repair bogus .git file' ' |
| 60 | + test_corrupt_gitfile "echo \"gitdir: /nowhere\" >corrupt/.git" \ |
| 61 | + ".git file broken" |
| 62 | +' |
| 63 | + |
| 64 | +test_expect_success 'repair incorrect .git file' ' |
| 65 | + test_when_finished "rm -rf other && git worktree prune" && |
| 66 | + test_create_repo other && |
| 67 | + other=$(git -C other rev-parse --absolute-git-dir) && |
| 68 | + test_corrupt_gitfile "echo \"gitdir: $other\" >corrupt/.git" \ |
| 69 | + ".git file incorrect" |
| 70 | +' |
| 71 | + |
| 72 | +test_expect_success 'repair .git file from main/.git' ' |
| 73 | + test_corrupt_gitfile "rm -f corrupt/.git" ".git file broken" .git |
| 74 | +' |
| 75 | + |
| 76 | +test_expect_success 'repair .git file from linked worktree' ' |
| 77 | + test_when_finished "rm -rf other && git worktree prune" && |
| 78 | + git worktree add --detach other && |
| 79 | + test_corrupt_gitfile "rm -f corrupt/.git" ".git file broken" other |
| 80 | +' |
| 81 | + |
| 82 | +test_expect_success 'repair .git file from bare.git' ' |
| 83 | + test_when_finished "rm -rf bare.git corrupt && git worktree prune" && |
| 84 | + git clone --bare . bare.git && |
| 85 | + git -C bare.git worktree add --detach ../corrupt && |
| 86 | + git -C corrupt rev-parse --absolute-git-dir >expect && |
| 87 | + rm -f corrupt/.git && |
| 88 | + git -C bare.git worktree repair && |
| 89 | + git -C corrupt rev-parse --absolute-git-dir >actual && |
| 90 | + test_cmp expect actual |
| 91 | +' |
| 92 | + |
| 93 | +test_expect_success 'invalid worktree path' ' |
| 94 | + test_must_fail git worktree repair /notvalid >out 2>err && |
| 95 | + test_must_be_empty out && |
| 96 | + test_i18ngrep "not a valid path" err |
| 97 | +' |
| 98 | + |
| 99 | +test_expect_success 'repo not found; .git not file' ' |
| 100 | + test_when_finished "rm -rf not-a-worktree" && |
| 101 | + test_create_repo not-a-worktree && |
| 102 | + test_must_fail git worktree repair not-a-worktree >out 2>err && |
| 103 | + test_must_be_empty out && |
| 104 | + test_i18ngrep ".git is not a file" err |
| 105 | +' |
| 106 | + |
| 107 | +test_expect_success 'repo not found; .git file broken' ' |
| 108 | + test_when_finished "rm -rf orig moved && git worktree prune" && |
| 109 | + git worktree add --detach orig && |
| 110 | + echo /invalid >orig/.git && |
| 111 | + mv orig moved && |
| 112 | + test_must_fail git worktree repair moved >out 2>err && |
| 113 | + test_must_be_empty out && |
| 114 | + test_i18ngrep ".git file broken" err |
| 115 | +' |
| 116 | + |
| 117 | +test_expect_success 'repair broken gitdir' ' |
| 118 | + test_when_finished "rm -rf orig moved && git worktree prune" && |
| 119 | + git worktree add --detach orig && |
| 120 | + sed s,orig/\.git$,moved/.git, .git/worktrees/orig/gitdir >expect && |
| 121 | + rm .git/worktrees/orig/gitdir && |
| 122 | + mv orig moved && |
| 123 | + git worktree repair moved >out 2>err && |
| 124 | + test_cmp expect .git/worktrees/orig/gitdir && |
| 125 | + test_i18ngrep "gitdir unreadable" out && |
| 126 | + test_must_be_empty err |
| 127 | +' |
| 128 | + |
| 129 | +test_expect_success 'repair incorrect gitdir' ' |
| 130 | + test_when_finished "rm -rf orig moved && git worktree prune" && |
| 131 | + git worktree add --detach orig && |
| 132 | + sed s,orig/\.git$,moved/.git, .git/worktrees/orig/gitdir >expect && |
| 133 | + mv orig moved && |
| 134 | + git worktree repair moved >out 2>err && |
| 135 | + test_cmp expect .git/worktrees/orig/gitdir && |
| 136 | + test_i18ngrep "gitdir incorrect" out && |
| 137 | + test_must_be_empty err |
| 138 | +' |
| 139 | + |
| 140 | +test_expect_success 'repair gitdir (implicit) from linked worktree' ' |
| 141 | + test_when_finished "rm -rf orig moved && git worktree prune" && |
| 142 | + git worktree add --detach orig && |
| 143 | + sed s,orig/\.git$,moved/.git, .git/worktrees/orig/gitdir >expect && |
| 144 | + mv orig moved && |
| 145 | + git -C moved worktree repair >out 2>err && |
| 146 | + test_cmp expect .git/worktrees/orig/gitdir && |
| 147 | + test_i18ngrep "gitdir incorrect" out && |
| 148 | + test_must_be_empty err |
| 149 | +' |
| 150 | + |
| 151 | +test_expect_success 'unable to repair gitdir (implicit) from main worktree' ' |
| 152 | + test_when_finished "rm -rf orig moved && git worktree prune" && |
| 153 | + git worktree add --detach orig && |
| 154 | + cat .git/worktrees/orig/gitdir >expect && |
| 155 | + mv orig moved && |
| 156 | + git worktree repair >out 2>err && |
| 157 | + test_cmp expect .git/worktrees/orig/gitdir && |
| 158 | + test_must_be_empty out && |
| 159 | + test_must_be_empty err |
| 160 | +' |
| 161 | + |
| 162 | +test_expect_success 'repair multiple gitdir files' ' |
| 163 | + test_when_finished "rm -rf orig1 orig2 moved1 moved2 && |
| 164 | + git worktree prune" && |
| 165 | + git worktree add --detach orig1 && |
| 166 | + git worktree add --detach orig2 && |
| 167 | + sed s,orig1/\.git$,moved1/.git, .git/worktrees/orig1/gitdir >expect1 && |
| 168 | + sed s,orig2/\.git$,moved2/.git, .git/worktrees/orig2/gitdir >expect2 && |
| 169 | + mv orig1 moved1 && |
| 170 | + mv orig2 moved2 && |
| 171 | + git worktree repair moved1 moved2 >out 2>err && |
| 172 | + test_cmp expect1 .git/worktrees/orig1/gitdir && |
| 173 | + test_cmp expect2 .git/worktrees/orig2/gitdir && |
| 174 | + test_i18ngrep "gitdir incorrect:.*orig1/gitdir$" out && |
| 175 | + test_i18ngrep "gitdir incorrect:.*orig2/gitdir$" out && |
| 176 | + test_must_be_empty err |
| 177 | +' |
| 178 | + |
| 179 | +test_done |
0 commit comments