| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='test git worktree move, remove, lock and unlock' |
| 4 | |
| 5 | . ./test-lib.sh |
| 6 | |
| 7 | test_expect_success 'setup' ' |
| 8 | test_commit init && |
| 9 | git worktree add source && |
| 10 | git worktree list --porcelain >out && |
| 11 | grep "^worktree" out >actual && |
| 12 | cat <<-EOF >expected && |
| 13 | worktree $(pwd) |
| 14 | worktree $(pwd)/source |
| 15 | EOF |
| 16 | test_cmp expected actual |
| 17 | ' |
| 18 | |
| 19 | test_expect_success 'lock main worktree' ' |
| 20 | test_must_fail git worktree lock . |
| 21 | ' |
| 22 | |
| 23 | test_expect_success 'lock linked worktree' ' |
| 24 | git worktree lock --reason hahaha source && |
| 25 | echo hahaha >expected && |
| 26 | test_cmp expected .git/worktrees/source/locked |
| 27 | ' |
| 28 | |
| 29 | test_expect_success 'lock linked worktree from another worktree' ' |
| 30 | rm .git/worktrees/source/locked && |
| 31 | git worktree add elsewhere && |
| 32 | git -C elsewhere worktree lock --reason hahaha ../source && |
| 33 | echo hahaha >expected && |
| 34 | test_cmp expected .git/worktrees/source/locked |
| 35 | ' |
| 36 | |
| 37 | test_expect_success 'lock worktree twice' ' |
| 38 | test_must_fail git worktree lock source && |
| 39 | echo hahaha >expected && |
| 40 | test_cmp expected .git/worktrees/source/locked |
| 41 | ' |
| 42 | |
| 43 | test_expect_success 'lock worktree twice (from the locked worktree)' ' |
| 44 | test_must_fail git -C source worktree lock . && |
| 45 | echo hahaha >expected && |
| 46 | test_cmp expected .git/worktrees/source/locked |
| 47 | ' |
| 48 | |
| 49 | test_expect_success 'unlock main worktree' ' |
| 50 | test_must_fail git worktree unlock . |
| 51 | ' |
| 52 | |
| 53 | test_expect_success 'unlock linked worktree' ' |
| 54 | git worktree unlock source && |
| 55 | test_path_is_missing .git/worktrees/source/locked |
| 56 | ' |
| 57 | |
| 58 | test_expect_success 'unlock worktree twice' ' |
| 59 | test_must_fail git worktree unlock source && |
| 60 | test_path_is_missing .git/worktrees/source/locked |
| 61 | ' |
| 62 | |
| 63 | test_expect_success 'move non-worktree' ' |
| 64 | mkdir abc && |
| 65 | test_must_fail git worktree move abc def |
| 66 | ' |
| 67 | |
| 68 | test_expect_success 'move locked worktree' ' |
| 69 | git worktree lock source && |
| 70 | test_when_finished "git worktree unlock source" && |
| 71 | test_must_fail git worktree move source destination |
| 72 | ' |
| 73 | |
| 74 | test_expect_success 'move worktree' ' |
| 75 | git worktree move source destination && |
| 76 | test_path_is_missing source && |
| 77 | git worktree list --porcelain >out && |
| 78 | grep "^worktree.*/destination$" out && |
| 79 | ! grep "^worktree.*/source$" out && |
| 80 | git -C destination log --format=%s >actual2 && |
| 81 | echo init >expected2 && |
| 82 | test_cmp expected2 actual2 |
| 83 | ' |
| 84 | |
| 85 | test_expect_success 'move main worktree' ' |
| 86 | test_must_fail git worktree move . def |
| 87 | ' |
| 88 | |
| 89 | test_expect_success 'move worktree to another dir' ' |
| 90 | mkdir some-dir && |
| 91 | git worktree move destination some-dir && |
| 92 | test_when_finished "git worktree move some-dir/destination destination" && |
| 93 | test_path_is_missing destination && |
| 94 | git worktree list --porcelain >out && |
| 95 | grep "^worktree.*/some-dir/destination$" out && |
| 96 | git -C some-dir/destination log --format=%s >actual2 && |
| 97 | echo init >expected2 && |
| 98 | test_cmp expected2 actual2 |
| 99 | ' |
| 100 | |
| 101 | test_expect_success 'move locked worktree (force)' ' |
| 102 | test_when_finished " |
| 103 | git worktree unlock flump || : |
| 104 | git worktree remove flump || : |
| 105 | git worktree unlock ploof || : |
| 106 | git worktree remove ploof || : |
| 107 | " && |
| 108 | git worktree add --detach flump && |
| 109 | git worktree lock flump && |
| 110 | test_must_fail git worktree move flump ploof" && |
| 111 | test_must_fail git worktree move --force flump ploof" && |
| 112 | git worktree move --force --force flump ploof |
| 113 | ' |
| 114 | |
| 115 | test_expect_success 'refuse to move worktree atop existing path' ' |
| 116 | >bobble && |
| 117 | git worktree add --detach beeble && |
| 118 | test_must_fail git worktree move beeble bobble |
| 119 | ' |
| 120 | |
| 121 | test_expect_success 'move atop existing but missing worktree' ' |
| 122 | git worktree add --detach gnoo && |
| 123 | git worktree add --detach pneu && |
| 124 | rm -fr pneu && |
| 125 | test_must_fail git worktree move gnoo pneu && |
| 126 | git worktree move --force gnoo pneu && |
| 127 | |
| 128 | git worktree add --detach nu && |
| 129 | git worktree lock nu && |
| 130 | rm -fr nu && |
| 131 | test_must_fail git worktree move pneu nu && |
| 132 | test_must_fail git worktree --force move pneu nu && |
| 133 | git worktree move --force --force pneu nu |
| 134 | ' |
| 135 | |
| 136 | test_expect_success 'move a repo with uninitialized submodule' ' |
| 137 | git init withsub && |
| 138 | ( |
| 139 | cd withsub && |
| 140 | test_commit initial && |
| 141 | git -c protocol.file.allow=always \ |
| 142 | submodule add "$PWD"/.git sub && |
| 143 | git commit -m withsub && |
| 144 | git worktree add second HEAD && |
| 145 | git worktree move second third |
| 146 | ) |
| 147 | ' |
| 148 | |
| 149 | test_expect_success 'not move a repo with initialized submodule' ' |
| 150 | ( |
| 151 | cd withsub && |
| 152 | git -c protocol.file.allow=always -C third submodule update && |
| 153 | test_must_fail git worktree move third forth |
| 154 | ) |
| 155 | ' |
| 156 | |
| 157 | test_expect_success 'remove main worktree' ' |
| 158 | test_must_fail git worktree remove . |
| 159 | ' |
| 160 | |
| 161 | test_expect_success 'remove locked worktree' ' |
| 162 | git worktree lock destination && |
| 163 | test_when_finished "git worktree unlock destination" && |
| 164 | test_must_fail git worktree remove destination |
| 165 | ' |
| 166 | |
| 167 | test_expect_success 'remove worktree with dirty tracked file' ' |
| 168 | echo dirty >>destination/init.t && |
| 169 | test_when_finished "git -C destination checkout init.t" && |
| 170 | test_must_fail git worktree remove destination |
| 171 | ' |
| 172 | |
| 173 | test_expect_success 'remove worktree with untracked file' ' |
| 174 | : >destination/untracked && |
| 175 | test_must_fail git worktree remove destination |
| 176 | ' |
| 177 | |
| 178 | test_expect_success 'force remove worktree with untracked file' ' |
| 179 | git worktree remove --force destination && |
| 180 | test_path_is_missing destination |
| 181 | ' |
| 182 | |
| 183 | test_expect_success 'remove missing worktree' ' |
| 184 | git worktree add to-be-gone && |
| 185 | test -d .git/worktrees/to-be-gone && |
| 186 | mv to-be-gone gone && |
| 187 | git worktree remove to-be-gone && |
| 188 | test_path_is_missing .git/worktrees/to-be-gone |
| 189 | ' |
| 190 | |
| 191 | test_expect_success 'NOT remove missing-but-locked worktree' ' |
| 192 | git worktree add gone-but-locked && |
| 193 | git worktree lock gone-but-locked && |
| 194 | test -d .git/worktrees/gone-but-locked && |
| 195 | mv gone-but-locked really-gone-now && |
| 196 | test_must_fail git worktree remove gone-but-locked && |
| 197 | test_path_is_dir .git/worktrees/gone-but-locked |
| 198 | ' |
| 199 | |
| 200 | test_expect_success 'proper error when worktree not found' ' |
| 201 | for i in noodle noodle/bork |
| 202 | do |
| 203 | test_must_fail git worktree lock $i 2>err && |
| 204 | test_grep "not a working tree" err || return 1 |
| 205 | done |
| 206 | ' |
| 207 | |
| 208 | test_expect_success 'remove locked worktree (force)' ' |
| 209 | git worktree add --detach gumby && |
| 210 | test_when_finished "git worktree remove gumby || :" && |
| 211 | git worktree lock gumby && |
| 212 | test_when_finished "git worktree unlock gumby || :" && |
| 213 | test_must_fail git worktree remove gumby && |
| 214 | test_must_fail git worktree remove --force gumby && |
| 215 | git worktree remove --force --force gumby |
| 216 | ' |
| 217 | |
| 218 | test_expect_success 'remove cleans up .git/worktrees when empty' ' |
| 219 | git init moog && |
| 220 | ( |
| 221 | cd moog && |
| 222 | test_commit bim && |
| 223 | git worktree add --detach goom && |
| 224 | test_path_exists .git/worktrees && |
| 225 | git worktree remove goom && |
| 226 | test_path_is_missing .git/worktrees |
| 227 | ) |
| 228 | ' |
| 229 | |
| 230 | test_expect_success 'remove a repo with uninitialized submodule' ' |
| 231 | test_config_global protocol.file.allow always && |
| 232 | ( |
| 233 | cd withsub && |
| 234 | git worktree add to-remove HEAD && |
| 235 | git worktree remove to-remove |
| 236 | ) |
| 237 | ' |
| 238 | |
| 239 | test_expect_success 'not remove a repo with initialized submodule' ' |
| 240 | test_config_global protocol.file.allow always && |
| 241 | ( |
| 242 | cd withsub && |
| 243 | git worktree add to-remove HEAD && |
| 244 | git -C to-remove submodule update && |
| 245 | test_must_fail git worktree remove to-remove |
| 246 | ) |
| 247 | ' |
| 248 | |
| 249 | test_expect_success 'move worktree with absolute path to relative path' ' |
| 250 | test_config worktree.useRelativePaths false && |
| 251 | git worktree add ./absolute && |
| 252 | git worktree move --relative-paths absolute relative && |
| 253 | echo "gitdir: ../.git/worktrees/absolute" >expect && |
| 254 | test_cmp expect relative/.git && |
| 255 | echo "../../../relative/.git" >expect && |
| 256 | test_cmp expect .git/worktrees/absolute/gitdir && |
| 257 | test_config worktree.useRelativePaths true && |
| 258 | git worktree move relative relative2 && |
| 259 | echo "gitdir: ../.git/worktrees/absolute" >expect && |
| 260 | test_cmp expect relative2/.git && |
| 261 | echo "../../../relative2/.git" >expect && |
| 262 | test_cmp expect .git/worktrees/absolute/gitdir |
| 263 | ' |
| 264 | |
| 265 | test_expect_success 'move worktree with relative path to absolute path' ' |
| 266 | test_config worktree.useRelativePaths true && |
| 267 | git worktree move --no-relative-paths relative2 absolute && |
| 268 | echo "gitdir: $(pwd)/.git/worktrees/absolute" >expect && |
| 269 | test_cmp expect absolute/.git && |
| 270 | echo "$(pwd)/absolute/.git" >expect && |
| 271 | test_cmp expect .git/worktrees/absolute/gitdir |
| 272 | ' |
| 273 | |
| 274 | test_done |