| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='test git worktree list' |
| 4 | |
| 5 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
| 6 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
| 7 | |
| 8 | . ./test-lib.sh |
| 9 | |
| 10 | test_expect_success 'setup' ' |
| 11 | test_commit init |
| 12 | ' |
| 13 | |
| 14 | test_expect_success 'rev-parse --git-common-dir on main worktree' ' |
| 15 | git rev-parse --git-common-dir >actual && |
| 16 | echo .git >expected && |
| 17 | test_cmp expected actual && |
| 18 | mkdir sub && |
| 19 | git -C sub rev-parse --git-common-dir >actual2 && |
| 20 | echo ../.git >expected2 && |
| 21 | test_cmp expected2 actual2 |
| 22 | ' |
| 23 | |
| 24 | test_expect_success 'rev-parse --git-path objects linked worktree' ' |
| 25 | echo "$(git rev-parse --show-toplevel)/.git/objects" >expect && |
| 26 | test_when_finished "rm -rf linked-tree actual expect && git worktree prune" && |
| 27 | git worktree add --detach linked-tree main && |
| 28 | git -C linked-tree rev-parse --git-path objects >actual && |
| 29 | test_cmp expect actual |
| 30 | ' |
| 31 | |
| 32 | test_expect_success '"list" all worktrees from main core.quotepath=false' ' |
| 33 | test_config core.quotepath false && |
| 34 | echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect && |
| 35 | test_when_finished "rm -rf áááá out actual expect && git worktree prune" && |
| 36 | git worktree add --detach áááá main && |
| 37 | echo "$(git -C áááá rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >>expect && |
| 38 | git worktree list >actual && |
| 39 | test_cmp expect actual |
| 40 | ' |
| 41 | |
| 42 | test_expect_success '"list" all worktrees from main core.quotepath=true' ' |
| 43 | test_config core.quotepath true && |
| 44 | echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect && |
| 45 | test_when_finished "rm -rf á out actual expect && git worktree prune" && |
| 46 | git worktree add --detach á main && |
| 47 | echo "\"$(git -C á rev-parse --show-toplevel)\" $(git rev-parse --short HEAD) (detached HEAD)" | |
| 48 | sed s/á/\\\\303\\\\241/g >>expect && |
| 49 | git worktree list >actual && |
| 50 | test_cmp expect actual |
| 51 | ' |
| 52 | |
| 53 | test_expect_success '"list" all worktrees from linked' ' |
| 54 | test_config core.quotepath false && |
| 55 | echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect && |
| 56 | test_when_finished "rm -rf áááá out actual expect && git worktree prune" && |
| 57 | git worktree add --detach áááá main && |
| 58 | echo "$(git -C áááá rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >>expect && |
| 59 | git -C áááá worktree list >actual && |
| 60 | test_cmp expect actual |
| 61 | ' |
| 62 | |
| 63 | test_expect_success '"list" all worktrees --porcelain' ' |
| 64 | echo "worktree $(git rev-parse --show-toplevel)" >expect && |
| 65 | echo "HEAD $(git rev-parse HEAD)" >>expect && |
| 66 | echo "branch $(git symbolic-ref HEAD)" >>expect && |
| 67 | echo >>expect && |
| 68 | test_when_finished "rm -rf here actual expect && git worktree prune" && |
| 69 | git worktree add --detach here main && |
| 70 | echo "worktree $(git -C here rev-parse --show-toplevel)" >>expect && |
| 71 | echo "HEAD $(git rev-parse HEAD)" >>expect && |
| 72 | echo "detached" >>expect && |
| 73 | echo >>expect && |
| 74 | git worktree list --porcelain >actual && |
| 75 | test_cmp expect actual |
| 76 | ' |
| 77 | |
| 78 | test_expect_success '"list" all worktrees --porcelain -z' ' |
| 79 | test_when_finished "rm -rf here _actual actual expect && |
| 80 | git worktree prune" && |
| 81 | printf "worktree %sQHEAD %sQbranch %sQQ" \ |
| 82 | "$(git rev-parse --show-toplevel)" \ |
| 83 | $(git rev-parse HEAD --symbolic-full-name HEAD) >expect && |
| 84 | git worktree add --detach here main && |
| 85 | printf "worktree %sQHEAD %sQdetachedQQ" \ |
| 86 | "$(git -C here rev-parse --show-toplevel)" \ |
| 87 | "$(git rev-parse HEAD)" >>expect && |
| 88 | git worktree list --porcelain -z >_actual && |
| 89 | nul_to_q <_actual >actual && |
| 90 | test_cmp expect actual |
| 91 | ' |
| 92 | |
| 93 | test_expect_success '"list" -z fails without --porcelain' ' |
| 94 | test_must_fail git worktree list -z |
| 95 | ' |
| 96 | |
| 97 | test_expect_success '"list" all worktrees with locked annotation' ' |
| 98 | test_when_finished "rm -rf locked unlocked out && git worktree prune" && |
| 99 | git worktree add --detach locked main && |
| 100 | git worktree add --detach unlocked main && |
| 101 | git worktree lock locked && |
| 102 | test_when_finished "git worktree unlock locked" && |
| 103 | git worktree list >out && |
| 104 | grep "/locked *[0-9a-f].* locked$" out && |
| 105 | ! grep "/unlocked *[0-9a-f].* locked$" out |
| 106 | ' |
| 107 | |
| 108 | test_expect_success '"list" all worktrees --porcelain with locked' ' |
| 109 | test_when_finished "rm -rf locked1 locked2 unlocked out actual expect && git worktree prune" && |
| 110 | echo "locked" >expect && |
| 111 | echo "locked with reason" >>expect && |
| 112 | git worktree add --detach locked1 && |
| 113 | git worktree add --detach locked2 && |
| 114 | # unlocked worktree should not be annotated with "locked" |
| 115 | git worktree add --detach unlocked && |
| 116 | git worktree lock locked1 && |
| 117 | test_when_finished "git worktree unlock locked1" && |
| 118 | git worktree lock locked2 --reason "with reason" && |
| 119 | test_when_finished "git worktree unlock locked2" && |
| 120 | git worktree list --porcelain >out && |
| 121 | grep "^locked" out >actual && |
| 122 | test_cmp expect actual |
| 123 | ' |
| 124 | |
| 125 | test_expect_success '"list" all worktrees --porcelain with locked reason newline escaped' ' |
| 126 | test_when_finished "rm -rf locked_lf locked_crlf out actual expect && git worktree prune" && |
| 127 | printf "locked \"locked\\\\r\\\\nreason\"\n" >expect && |
| 128 | printf "locked \"locked\\\\nreason\"\n" >>expect && |
| 129 | git worktree add --detach locked_lf && |
| 130 | git worktree add --detach locked_crlf && |
| 131 | git worktree lock locked_lf --reason "$(printf "locked\nreason")" && |
| 132 | test_when_finished "git worktree unlock locked_lf" && |
| 133 | git worktree lock locked_crlf --reason "$(printf "locked\r\nreason")" && |
| 134 | test_when_finished "git worktree unlock locked_crlf" && |
| 135 | git worktree list --porcelain >out && |
| 136 | grep "^locked" out >actual && |
| 137 | test_cmp expect actual |
| 138 | ' |
| 139 | |
| 140 | test_expect_success '"list" all worktrees with prunable annotation' ' |
| 141 | test_when_finished "rm -rf prunable unprunable out && git worktree prune" && |
| 142 | git worktree add --detach prunable && |
| 143 | git worktree add --detach unprunable && |
| 144 | rm -rf prunable && |
| 145 | git worktree list >out && |
| 146 | grep "/prunable *[0-9a-f].* prunable$" out && |
| 147 | ! grep "/unprunable *[0-9a-f].* prunable$" |
| 148 | ' |
| 149 | |
| 150 | test_expect_success '"list" all worktrees --porcelain with prunable' ' |
| 151 | test_when_finished "rm -rf prunable out && git worktree prune" && |
| 152 | git worktree add --detach prunable && |
| 153 | rm -rf prunable && |
| 154 | git worktree list --porcelain >out && |
| 155 | sed -n "/^worktree .*\/prunable$/,/^$/p" <out >only_prunable && |
| 156 | test_grep "^prunable gitdir file points to non-existent location$" only_prunable |
| 157 | ' |
| 158 | |
| 159 | test_expect_success '"list" all worktrees with prunable consistent with "prune"' ' |
| 160 | test_when_finished "rm -rf prunable unprunable out && git worktree prune" && |
| 161 | git worktree add --detach prunable && |
| 162 | git worktree add --detach unprunable && |
| 163 | rm -rf prunable && |
| 164 | git worktree list >out && |
| 165 | grep "/prunable *[0-9a-f].* prunable$" out && |
| 166 | ! grep "/unprunable *[0-9a-f].* unprunable$" out && |
| 167 | git worktree prune --verbose 2>out && |
| 168 | test_grep "^Removing worktrees/prunable" out && |
| 169 | test_grep ! "^Removing worktrees/unprunable" out |
| 170 | ' |
| 171 | |
| 172 | test_expect_success '"list" --verbose and --porcelain mutually exclusive' ' |
| 173 | test_must_fail git worktree list --verbose --porcelain |
| 174 | ' |
| 175 | |
| 176 | test_expect_success '"list" all worktrees --verbose with locked' ' |
| 177 | test_when_finished "rm -rf locked1 locked2 out actual expect && git worktree prune" && |
| 178 | git worktree add locked1 --detach && |
| 179 | git worktree add locked2 --detach && |
| 180 | git worktree lock locked1 && |
| 181 | test_when_finished "git worktree unlock locked1" && |
| 182 | git worktree lock locked2 --reason "with reason" && |
| 183 | test_when_finished "git worktree unlock locked2" && |
| 184 | echo "$(git -C locked2 rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >expect && |
| 185 | printf "\tlocked: with reason\n" >>expect && |
| 186 | git worktree list --verbose >out && |
| 187 | grep "/locked1 *[0-9a-f].* locked$" out && |
| 188 | sed -n "s/ */ /g;/\/locked2 *[0-9a-f].*$/,/locked: .*$/p" <out >actual && |
| 189 | test_cmp actual expect |
| 190 | ' |
| 191 | |
| 192 | test_expect_success '"list" all worktrees --verbose with prunable' ' |
| 193 | test_when_finished "rm -rf prunable out actual expect && git worktree prune" && |
| 194 | git worktree add prunable --detach && |
| 195 | echo "$(git -C prunable rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >expect && |
| 196 | printf "\tprunable: gitdir file points to non-existent location\n" >>expect && |
| 197 | rm -rf prunable && |
| 198 | git worktree list --verbose >out && |
| 199 | sed -n "s/ */ /g;/\/prunable *[0-9a-f].*$/,/prunable: .*$/p" <out >actual && |
| 200 | test_cmp actual expect |
| 201 | ' |
| 202 | |
| 203 | test_expect_success 'bare repo setup' ' |
| 204 | git init --bare bare1 && |
| 205 | echo "data" >file1 && |
| 206 | git add file1 && |
| 207 | git commit -m"File1: add data" && |
| 208 | git push bare1 main && |
| 209 | git reset --hard HEAD^ |
| 210 | ' |
| 211 | |
| 212 | test_expect_success '"list" all worktrees from bare main' ' |
| 213 | test_when_finished "rm -rf there out actual expect && git -C bare1 worktree prune" && |
| 214 | git -C bare1 worktree add --detach ../there main && |
| 215 | echo "$(pwd)/bare1 (bare)" >expect && |
| 216 | echo "$(git -C there rev-parse --show-toplevel) $(git -C there rev-parse --short HEAD) (detached HEAD)" >>expect && |
| 217 | git -C bare1 worktree list >out && |
| 218 | sed "s/ */ /g" <out >actual && |
| 219 | test_cmp expect actual |
| 220 | ' |
| 221 | |
| 222 | test_expect_success '"list" all worktrees --porcelain from bare main' ' |
| 223 | test_when_finished "rm -rf there actual expect && git -C bare1 worktree prune" && |
| 224 | git -C bare1 worktree add --detach ../there main && |
| 225 | echo "worktree $(pwd)/bare1" >expect && |
| 226 | echo "bare" >>expect && |
| 227 | echo >>expect && |
| 228 | echo "worktree $(git -C there rev-parse --show-toplevel)" >>expect && |
| 229 | echo "HEAD $(git -C there rev-parse HEAD)" >>expect && |
| 230 | echo "detached" >>expect && |
| 231 | echo >>expect && |
| 232 | git -C bare1 worktree list --porcelain >actual && |
| 233 | test_cmp expect actual |
| 234 | ' |
| 235 | |
| 236 | test_expect_success '"list" all worktrees from linked with a bare main' ' |
| 237 | test_when_finished "rm -rf there out actual expect && git -C bare1 worktree prune" && |
| 238 | git -C bare1 worktree add --detach ../there main && |
| 239 | echo "$(pwd)/bare1 (bare)" >expect && |
| 240 | echo "$(git -C there rev-parse --show-toplevel) $(git -C there rev-parse --short HEAD) (detached HEAD)" >>expect && |
| 241 | git -C there worktree list >out && |
| 242 | sed "s/ */ /g" <out >actual && |
| 243 | test_cmp expect actual |
| 244 | ' |
| 245 | |
| 246 | test_expect_success 'bare repo cleanup' ' |
| 247 | rm -rf bare1 |
| 248 | ' |
| 249 | |
| 250 | test_expect_success 'broken main worktree still at the top' ' |
| 251 | git init broken-main && |
| 252 | ( |
| 253 | cd broken-main && |
| 254 | test_commit new && |
| 255 | git worktree add linked && |
| 256 | cat >expected <<-EOF && |
| 257 | worktree $(pwd) |
| 258 | HEAD $ZERO_OID |
| 259 | |
| 260 | EOF |
| 261 | cd linked && |
| 262 | echo "worktree $(pwd)" >expected && |
| 263 | (cd ../ && test-tool ref-store main create-symref HEAD .broken ) && |
| 264 | git worktree list --porcelain >out && |
| 265 | head -n 3 out >actual && |
| 266 | test_cmp ../expected actual && |
| 267 | git worktree list >out && |
| 268 | head -n 1 out >actual.2 && |
| 269 | grep -F "(error)" actual.2 |
| 270 | ) |
| 271 | ' |
| 272 | |
| 273 | test_expect_success 'linked worktrees are sorted' ' |
| 274 | test_when_finished "rm -rf sorted" && |
| 275 | mkdir sorted && |
| 276 | git init sorted/main && |
| 277 | ( |
| 278 | cd sorted/main && |
| 279 | test_tick && |
| 280 | test_commit new && |
| 281 | git worktree add ../first && |
| 282 | git worktree add ../second && |
| 283 | git worktree list --porcelain >out && |
| 284 | grep ^worktree out >actual |
| 285 | ) && |
| 286 | cat >expected <<-EOF && |
| 287 | worktree $(pwd)/sorted/main |
| 288 | worktree $(pwd)/sorted/first |
| 289 | worktree $(pwd)/sorted/second |
| 290 | EOF |
| 291 | test_cmp expected sorted/main/actual |
| 292 | ' |
| 293 | |
| 294 | test_expect_success 'linked worktrees with relative paths are shown with absolute paths' ' |
| 295 | test_when_finished "rm -rf sorted" && |
| 296 | mkdir sorted && |
| 297 | git init sorted/main && |
| 298 | ( |
| 299 | cd sorted/main && |
| 300 | test_tick && |
| 301 | test_commit new && |
| 302 | git worktree add --relative-paths ../first && |
| 303 | git worktree add ../second && |
| 304 | git worktree list --porcelain >out && |
| 305 | grep ^worktree out >actual |
| 306 | ) && |
| 307 | cat >expected <<-EOF && |
| 308 | worktree $(pwd)/sorted/main |
| 309 | worktree $(pwd)/sorted/first |
| 310 | worktree $(pwd)/sorted/second |
| 311 | EOF |
| 312 | test_cmp expected sorted/main/actual |
| 313 | ' |
| 314 | |
| 315 | test_expect_success 'worktree path when called in .git directory' ' |
| 316 | git worktree list >list1 && |
| 317 | git -C .git worktree list >list2 && |
| 318 | test_cmp list1 list2 |
| 319 | ' |
| 320 | |
| 321 | test_done |