Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='test git worktree list' |
| 4 | |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 5 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
Johannes Schindelin | 334afbc | 2020-11-18 23:44:19 +0000 | [diff] [blame] | 6 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
| 7 | |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 8 | . ./test-lib.sh |
| 9 | |
| 10 | test_expect_success 'setup' ' |
| 11 | test_commit init |
| 12 | ' |
| 13 | |
Johannes Schindelin | 098aa86 | 2017-02-17 17:59:06 +0100 | [diff] [blame] | 14 | test_expect_success 'rev-parse --git-common-dir on main worktree' ' |
Nguyễn Thái Ngọc Duy | 17f1365 | 2016-02-12 11:31:45 +0700 | [diff] [blame] | 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 && |
Michael Rappazzo | 5de8a54 | 2017-02-17 17:59:02 +0100 | [diff] [blame] | 20 | echo ../.git >expected2 && |
Nguyễn Thái Ngọc Duy | 17f1365 | 2016-02-12 11:31:45 +0700 | [diff] [blame] | 21 | test_cmp expected2 actual2 |
| 22 | ' |
| 23 | |
Johannes Schindelin | 098aa86 | 2017-02-17 17:59:06 +0100 | [diff] [blame] | 24 | test_expect_success 'rev-parse --git-path objects linked worktree' ' |
Michael Rappazzo | 5de8a54 | 2017-02-17 17:59:02 +0100 | [diff] [blame] | 25 | echo "$(git rev-parse --show-toplevel)/.git/objects" >expect && |
Prathamesh Chavan | 210e5db | 2017-04-04 03:05:57 +0530 | [diff] [blame] | 26 | test_when_finished "rm -rf linked-tree actual expect && git worktree prune" && |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 27 | git worktree add --detach linked-tree main && |
Michael Rappazzo | 5de8a54 | 2017-02-17 17:59:02 +0100 | [diff] [blame] | 28 | git -C linked-tree rev-parse --git-path objects >actual && |
| 29 | test_cmp expect actual |
| 30 | ' |
| 31 | |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 32 | test_expect_success '"list" all worktrees from main' ' |
| 33 | echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect && |
Prathamesh Chavan | 210e5db | 2017-04-04 03:05:57 +0530 | [diff] [blame] | 34 | test_when_finished "rm -rf here out actual expect && git worktree prune" && |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 35 | git worktree add --detach here main && |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 36 | echo "$(git -C here rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >>expect && |
Prathamesh Chavan | 210e5db | 2017-04-04 03:05:57 +0530 | [diff] [blame] | 37 | git worktree list >out && |
| 38 | sed "s/ */ /g" <out >actual && |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 39 | test_cmp expect actual |
| 40 | ' |
| 41 | |
| 42 | test_expect_success '"list" all worktrees from linked' ' |
| 43 | echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect && |
Prathamesh Chavan | 210e5db | 2017-04-04 03:05:57 +0530 | [diff] [blame] | 44 | test_when_finished "rm -rf here out actual expect && git worktree prune" && |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 45 | git worktree add --detach here main && |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 46 | echo "$(git -C here rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >>expect && |
Prathamesh Chavan | 210e5db | 2017-04-04 03:05:57 +0530 | [diff] [blame] | 47 | git -C here worktree list >out && |
| 48 | sed "s/ */ /g" <out >actual && |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 49 | test_cmp expect actual |
| 50 | ' |
| 51 | |
| 52 | test_expect_success '"list" all worktrees --porcelain' ' |
| 53 | echo "worktree $(git rev-parse --show-toplevel)" >expect && |
| 54 | echo "HEAD $(git rev-parse HEAD)" >>expect && |
| 55 | echo "branch $(git symbolic-ref HEAD)" >>expect && |
| 56 | echo >>expect && |
Prathamesh Chavan | 210e5db | 2017-04-04 03:05:57 +0530 | [diff] [blame] | 57 | test_when_finished "rm -rf here actual expect && git worktree prune" && |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 58 | git worktree add --detach here main && |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 59 | echo "worktree $(git -C here rev-parse --show-toplevel)" >>expect && |
| 60 | echo "HEAD $(git rev-parse HEAD)" >>expect && |
| 61 | echo "detached" >>expect && |
| 62 | echo >>expect && |
| 63 | git worktree list --porcelain >actual && |
| 64 | test_cmp expect actual |
| 65 | ' |
| 66 | |
Phillip Wood | d97eb30 | 2022-03-31 16:21:28 +0000 | [diff] [blame] | 67 | test_expect_success '"list" all worktrees --porcelain -z' ' |
| 68 | test_when_finished "rm -rf here _actual actual expect && |
| 69 | git worktree prune" && |
| 70 | printf "worktree %sQHEAD %sQbranch %sQQ" \ |
| 71 | "$(git rev-parse --show-toplevel)" \ |
| 72 | $(git rev-parse HEAD --symbolic-full-name HEAD) >expect && |
| 73 | git worktree add --detach here main && |
| 74 | printf "worktree %sQHEAD %sQdetachedQQ" \ |
| 75 | "$(git -C here rev-parse --show-toplevel)" \ |
| 76 | "$(git rev-parse HEAD)" >>expect && |
| 77 | git worktree list --porcelain -z >_actual && |
| 78 | nul_to_q <_actual >actual && |
| 79 | test_cmp expect actual |
| 80 | ' |
| 81 | |
| 82 | test_expect_success '"list" -z fails without --porcelain' ' |
| 83 | test_must_fail git worktree list -z |
| 84 | ' |
| 85 | |
Johannes Schindelin | 8d88931 | 2020-11-03 11:48:07 +0000 | [diff] [blame] | 86 | test_expect_success '"list" all worktrees with locked annotation' ' |
Rafael Silva | c57b336 | 2020-10-11 10:11:52 +0000 | [diff] [blame] | 87 | test_when_finished "rm -rf locked unlocked out && git worktree prune" && |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 88 | git worktree add --detach locked main && |
| 89 | git worktree add --detach unlocked main && |
Rafael Silva | c57b336 | 2020-10-11 10:11:52 +0000 | [diff] [blame] | 90 | git worktree lock locked && |
Rafael Silva | 47409e7 | 2021-01-27 09:03:07 +0100 | [diff] [blame] | 91 | test_when_finished "git worktree unlock locked" && |
Rafael Silva | c57b336 | 2020-10-11 10:11:52 +0000 | [diff] [blame] | 92 | git worktree list >out && |
| 93 | grep "/locked *[0-9a-f].* locked$" out && |
| 94 | ! grep "/unlocked *[0-9a-f].* locked$" out |
| 95 | ' |
| 96 | |
Rafael Silva | 862c723 | 2021-01-27 09:03:08 +0100 | [diff] [blame] | 97 | test_expect_success '"list" all worktrees --porcelain with locked' ' |
| 98 | test_when_finished "rm -rf locked1 locked2 unlocked out actual expect && git worktree prune" && |
| 99 | echo "locked" >expect && |
| 100 | echo "locked with reason" >>expect && |
| 101 | git worktree add --detach locked1 && |
| 102 | git worktree add --detach locked2 && |
| 103 | # unlocked worktree should not be annotated with "locked" |
| 104 | git worktree add --detach unlocked && |
| 105 | git worktree lock locked1 && |
| 106 | test_when_finished "git worktree unlock locked1" && |
| 107 | git worktree lock locked2 --reason "with reason" && |
| 108 | test_when_finished "git worktree unlock locked2" && |
| 109 | git worktree list --porcelain >out && |
| 110 | grep "^locked" out >actual && |
| 111 | test_cmp expect actual |
| 112 | ' |
| 113 | |
| 114 | test_expect_success '"list" all worktrees --porcelain with locked reason newline escaped' ' |
| 115 | test_when_finished "rm -rf locked_lf locked_crlf out actual expect && git worktree prune" && |
| 116 | printf "locked \"locked\\\\r\\\\nreason\"\n" >expect && |
| 117 | printf "locked \"locked\\\\nreason\"\n" >>expect && |
| 118 | git worktree add --detach locked_lf && |
| 119 | git worktree add --detach locked_crlf && |
| 120 | git worktree lock locked_lf --reason "$(printf "locked\nreason")" && |
| 121 | test_when_finished "git worktree unlock locked_lf" && |
| 122 | git worktree lock locked_crlf --reason "$(printf "locked\r\nreason")" && |
| 123 | test_when_finished "git worktree unlock locked_crlf" && |
| 124 | git worktree list --porcelain >out && |
| 125 | grep "^locked" out >actual && |
| 126 | test_cmp expect actual |
| 127 | ' |
| 128 | |
Rafael Silva | 9b19a58 | 2021-01-27 09:03:09 +0100 | [diff] [blame] | 129 | test_expect_success '"list" all worktrees with prunable annotation' ' |
| 130 | test_when_finished "rm -rf prunable unprunable out && git worktree prune" && |
| 131 | git worktree add --detach prunable && |
| 132 | git worktree add --detach unprunable && |
| 133 | rm -rf prunable && |
| 134 | git worktree list >out && |
| 135 | grep "/prunable *[0-9a-f].* prunable$" out && |
| 136 | ! grep "/unprunable *[0-9a-f].* prunable$" |
| 137 | ' |
| 138 | |
| 139 | test_expect_success '"list" all worktrees --porcelain with prunable' ' |
| 140 | test_when_finished "rm -rf prunable out && git worktree prune" && |
| 141 | git worktree add --detach prunable && |
| 142 | rm -rf prunable && |
| 143 | git worktree list --porcelain >out && |
| 144 | sed -n "/^worktree .*\/prunable$/,/^$/p" <out >only_prunable && |
| 145 | test_i18ngrep "^prunable gitdir file points to non-existent location$" only_prunable |
| 146 | ' |
| 147 | |
| 148 | test_expect_success '"list" all worktrees with prunable consistent with "prune"' ' |
| 149 | test_when_finished "rm -rf prunable unprunable out && git worktree prune" && |
| 150 | git worktree add --detach prunable && |
| 151 | git worktree add --detach unprunable && |
| 152 | rm -rf prunable && |
| 153 | git worktree list >out && |
| 154 | grep "/prunable *[0-9a-f].* prunable$" out && |
| 155 | ! grep "/unprunable *[0-9a-f].* unprunable$" out && |
Eric Sunshine | da8fb6b | 2021-12-02 22:44:19 -0500 | [diff] [blame] | 156 | git worktree prune --verbose 2>out && |
Rafael Silva | 9b19a58 | 2021-01-27 09:03:09 +0100 | [diff] [blame] | 157 | test_i18ngrep "^Removing worktrees/prunable" out && |
| 158 | test_i18ngrep ! "^Removing worktrees/unprunable" out |
| 159 | ' |
| 160 | |
Rafael Silva | 076b444 | 2021-01-27 09:03:10 +0100 | [diff] [blame] | 161 | test_expect_success '"list" --verbose and --porcelain mutually exclusive' ' |
| 162 | test_must_fail git worktree list --verbose --porcelain |
| 163 | ' |
| 164 | |
| 165 | test_expect_success '"list" all worktrees --verbose with locked' ' |
| 166 | test_when_finished "rm -rf locked1 locked2 out actual expect && git worktree prune" && |
| 167 | git worktree add locked1 --detach && |
| 168 | git worktree add locked2 --detach && |
| 169 | git worktree lock locked1 && |
| 170 | test_when_finished "git worktree unlock locked1" && |
| 171 | git worktree lock locked2 --reason "with reason" && |
| 172 | test_when_finished "git worktree unlock locked2" && |
| 173 | echo "$(git -C locked2 rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >expect && |
| 174 | printf "\tlocked: with reason\n" >>expect && |
| 175 | git worktree list --verbose >out && |
| 176 | grep "/locked1 *[0-9a-f].* locked$" out && |
| 177 | sed -n "s/ */ /g;/\/locked2 *[0-9a-f].*$/,/locked: .*$/p" <out >actual && |
| 178 | test_cmp actual expect |
| 179 | ' |
| 180 | |
| 181 | test_expect_success '"list" all worktrees --verbose with prunable' ' |
| 182 | test_when_finished "rm -rf prunable out actual expect && git worktree prune" && |
| 183 | git worktree add prunable --detach && |
| 184 | echo "$(git -C prunable rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >expect && |
| 185 | printf "\tprunable: gitdir file points to non-existent location\n" >>expect && |
| 186 | rm -rf prunable && |
| 187 | git worktree list --verbose >out && |
| 188 | sed -n "s/ */ /g;/\/prunable *[0-9a-f].*$/,/prunable: .*$/p" <out >actual && |
Ævar Arnfjörð Bjarmason | 1108cea | 2021-02-11 02:53:53 +0100 | [diff] [blame] | 189 | test_cmp actual expect |
Rafael Silva | 076b444 | 2021-01-27 09:03:10 +0100 | [diff] [blame] | 190 | ' |
| 191 | |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 192 | test_expect_success 'bare repo setup' ' |
| 193 | git init --bare bare1 && |
| 194 | echo "data" >file1 && |
| 195 | git add file1 && |
| 196 | git commit -m"File1: add data" && |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 197 | git push bare1 main && |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 198 | git reset --hard HEAD^ |
| 199 | ' |
| 200 | |
| 201 | test_expect_success '"list" all worktrees from bare main' ' |
Prathamesh Chavan | 210e5db | 2017-04-04 03:05:57 +0530 | [diff] [blame] | 202 | test_when_finished "rm -rf there out actual expect && git -C bare1 worktree prune" && |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 203 | git -C bare1 worktree add --detach ../there main && |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 204 | echo "$(pwd)/bare1 (bare)" >expect && |
| 205 | echo "$(git -C there rev-parse --show-toplevel) $(git -C there rev-parse --short HEAD) (detached HEAD)" >>expect && |
Prathamesh Chavan | 210e5db | 2017-04-04 03:05:57 +0530 | [diff] [blame] | 206 | git -C bare1 worktree list >out && |
| 207 | sed "s/ */ /g" <out >actual && |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 208 | test_cmp expect actual |
| 209 | ' |
| 210 | |
| 211 | test_expect_success '"list" all worktrees --porcelain from bare main' ' |
Prathamesh Chavan | 210e5db | 2017-04-04 03:05:57 +0530 | [diff] [blame] | 212 | test_when_finished "rm -rf there actual expect && git -C bare1 worktree prune" && |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 213 | git -C bare1 worktree add --detach ../there main && |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 214 | echo "worktree $(pwd)/bare1" >expect && |
| 215 | echo "bare" >>expect && |
| 216 | echo >>expect && |
| 217 | echo "worktree $(git -C there rev-parse --show-toplevel)" >>expect && |
| 218 | echo "HEAD $(git -C there rev-parse HEAD)" >>expect && |
| 219 | echo "detached" >>expect && |
| 220 | echo >>expect && |
| 221 | git -C bare1 worktree list --porcelain >actual && |
| 222 | test_cmp expect actual |
| 223 | ' |
| 224 | |
| 225 | test_expect_success '"list" all worktrees from linked with a bare main' ' |
Prathamesh Chavan | 210e5db | 2017-04-04 03:05:57 +0530 | [diff] [blame] | 226 | test_when_finished "rm -rf there out actual expect && git -C bare1 worktree prune" && |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 227 | git -C bare1 worktree add --detach ../there main && |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 228 | echo "$(pwd)/bare1 (bare)" >expect && |
| 229 | echo "$(git -C there rev-parse --show-toplevel) $(git -C there rev-parse --short HEAD) (detached HEAD)" >>expect && |
Prathamesh Chavan | 210e5db | 2017-04-04 03:05:57 +0530 | [diff] [blame] | 230 | git -C there worktree list >out && |
| 231 | sed "s/ */ /g" <out >actual && |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 232 | test_cmp expect actual |
| 233 | ' |
| 234 | |
| 235 | test_expect_success 'bare repo cleanup' ' |
| 236 | rm -rf bare1 |
| 237 | ' |
| 238 | |
Nguyễn Thái Ngọc Duy | a234563 | 2016-11-28 16:36:54 +0700 | [diff] [blame] | 239 | test_expect_success 'broken main worktree still at the top' ' |
| 240 | git init broken-main && |
| 241 | ( |
| 242 | cd broken-main && |
| 243 | test_commit new && |
| 244 | git worktree add linked && |
| 245 | cat >expected <<-EOF && |
| 246 | worktree $(pwd) |
brian m. carlson | 8125a58 | 2018-05-13 02:24:13 +0000 | [diff] [blame] | 247 | HEAD $ZERO_OID |
Nguyễn Thái Ngọc Duy | a234563 | 2016-11-28 16:36:54 +0700 | [diff] [blame] | 248 | |
| 249 | EOF |
| 250 | cd linked && |
| 251 | echo "worktree $(pwd)" >expected && |
Han-Wen Nienhuys | 100ac47 | 2021-08-02 16:53:30 +0000 | [diff] [blame] | 252 | (cd ../ && test-tool ref-store main create-symref HEAD .broken ) && |
Prathamesh Chavan | 210e5db | 2017-04-04 03:05:57 +0530 | [diff] [blame] | 253 | git worktree list --porcelain >out && |
| 254 | head -n 3 out >actual && |
Nguyễn Thái Ngọc Duy | a234563 | 2016-11-28 16:36:54 +0700 | [diff] [blame] | 255 | test_cmp ../expected actual && |
Prathamesh Chavan | 210e5db | 2017-04-04 03:05:57 +0530 | [diff] [blame] | 256 | git worktree list >out && |
| 257 | head -n 1 out >actual.2 && |
Nguyễn Thái Ngọc Duy | a234563 | 2016-11-28 16:36:54 +0700 | [diff] [blame] | 258 | grep -F "(error)" actual.2 |
| 259 | ) |
| 260 | ' |
| 261 | |
Nguyễn Thái Ngọc Duy | 4df1d4d | 2016-11-28 16:36:56 +0700 | [diff] [blame] | 262 | test_expect_success 'linked worktrees are sorted' ' |
| 263 | mkdir sorted && |
| 264 | git init sorted/main && |
| 265 | ( |
| 266 | cd sorted/main && |
| 267 | test_tick && |
| 268 | test_commit new && |
| 269 | git worktree add ../first && |
| 270 | git worktree add ../second && |
Prathamesh Chavan | 210e5db | 2017-04-04 03:05:57 +0530 | [diff] [blame] | 271 | git worktree list --porcelain >out && |
| 272 | grep ^worktree out >actual |
Nguyễn Thái Ngọc Duy | 4df1d4d | 2016-11-28 16:36:56 +0700 | [diff] [blame] | 273 | ) && |
| 274 | cat >expected <<-EOF && |
| 275 | worktree $(pwd)/sorted/main |
| 276 | worktree $(pwd)/sorted/first |
| 277 | worktree $(pwd)/sorted/second |
| 278 | EOF |
| 279 | test_cmp expected sorted/main/actual |
| 280 | ' |
| 281 | |
Hariom Verma | 4d86489 | 2020-03-04 07:00:00 +0000 | [diff] [blame] | 282 | test_expect_success 'worktree path when called in .git directory' ' |
Andrei Rybak | 64d1022 | 2020-03-22 22:14:22 +0100 | [diff] [blame] | 283 | git worktree list >list1 && |
Hariom Verma | 4d86489 | 2020-03-04 07:00:00 +0000 | [diff] [blame] | 284 | git -C .git worktree list >list2 && |
| 285 | test_cmp list1 list2 |
| 286 | ' |
| 287 | |
Michael Rappazzo | bb9c03b | 2015-10-08 13:01:05 -0400 | [diff] [blame] | 288 | test_done |