Eric Sunshine | f194b1e | 2015-07-06 13:30:54 -0400 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='test git worktree add' |
| 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 | |
Ævar Arnfjörð Bjarmason | 93e02b6 | 2022-06-03 13:15:05 +0200 | [diff] [blame] | 8 | TEST_CREATE_REPO_NO_TEMPLATE=1 |
Eric Sunshine | f194b1e | 2015-07-06 13:30:54 -0400 | [diff] [blame] | 9 | . ./test-lib.sh |
| 10 | |
Nguyễn Thái Ngọc Duy | 8d9fdd7 | 2016-04-22 20:01:33 +0700 | [diff] [blame] | 11 | . "$TEST_DIRECTORY"/lib-rebase.sh |
| 12 | |
Eric Sunshine | f194b1e | 2015-07-06 13:30:54 -0400 | [diff] [blame] | 13 | test_expect_success 'setup' ' |
| 14 | test_commit init |
| 15 | ' |
| 16 | |
| 17 | test_expect_success '"add" an existing worktree' ' |
| 18 | mkdir -p existing/subtree && |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 19 | test_must_fail git worktree add --detach existing main |
Eric Sunshine | f194b1e | 2015-07-06 13:30:54 -0400 | [diff] [blame] | 20 | ' |
| 21 | |
| 22 | test_expect_success '"add" an existing empty worktree' ' |
| 23 | mkdir existing_empty && |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 24 | git worktree add --detach existing_empty main |
Eric Sunshine | f194b1e | 2015-07-06 13:30:54 -0400 | [diff] [blame] | 25 | ' |
| 26 | |
Jordan DE GEA | 1a450e2 | 2016-05-27 15:17:08 +0200 | [diff] [blame] | 27 | test_expect_success '"add" using shorthand - fails when no previous branch' ' |
| 28 | test_must_fail git worktree add existing_short - |
| 29 | ' |
| 30 | |
| 31 | test_expect_success '"add" using - shorthand' ' |
| 32 | git checkout -b newbranch && |
| 33 | echo hello >myworld && |
| 34 | git add myworld && |
| 35 | git commit -m myworld && |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 36 | git checkout main && |
Jordan DE GEA | 1a450e2 | 2016-05-27 15:17:08 +0200 | [diff] [blame] | 37 | git worktree add short-hand - && |
| 38 | echo refs/heads/newbranch >expect && |
| 39 | git -C short-hand rev-parse --symbolic-full-name HEAD >actual && |
| 40 | test_cmp expect actual |
| 41 | ' |
| 42 | |
Eric Sunshine | f194b1e | 2015-07-06 13:30:54 -0400 | [diff] [blame] | 43 | test_expect_success '"add" refuses to checkout locked branch' ' |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 44 | test_must_fail git worktree add zere main && |
Eric Sunshine | f194b1e | 2015-07-06 13:30:54 -0400 | [diff] [blame] | 45 | ! test -d zere && |
| 46 | ! test -d .git/worktrees/zere |
| 47 | ' |
| 48 | |
| 49 | test_expect_success 'checking out paths not complaining about linked checkouts' ' |
| 50 | ( |
| 51 | cd existing_empty && |
| 52 | echo dirty >>init.t && |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 53 | git checkout main -- init.t |
Eric Sunshine | f194b1e | 2015-07-06 13:30:54 -0400 | [diff] [blame] | 54 | ) |
| 55 | ' |
| 56 | |
| 57 | test_expect_success '"add" worktree' ' |
| 58 | git rev-parse HEAD >expect && |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 59 | git worktree add --detach here main && |
Eric Sunshine | f194b1e | 2015-07-06 13:30:54 -0400 | [diff] [blame] | 60 | ( |
| 61 | cd here && |
| 62 | test_cmp ../init.t init.t && |
| 63 | test_must_fail git symbolic-ref HEAD && |
| 64 | git rev-parse HEAD >actual && |
| 65 | test_cmp ../expect actual && |
| 66 | git fsck |
| 67 | ) |
| 68 | ' |
| 69 | |
Nguyễn Thái Ngọc Duy | 507e6e9 | 2017-04-12 20:58:05 +0700 | [diff] [blame] | 70 | test_expect_success '"add" worktree with lock' ' |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 71 | git worktree add --detach --lock here-with-lock main && |
Stephen Manz | f9365c0 | 2021-07-11 00:27:18 +0000 | [diff] [blame] | 72 | test_when_finished "git worktree unlock here-with-lock || :" && |
Nguyễn Thái Ngọc Duy | 507e6e9 | 2017-04-12 20:58:05 +0700 | [diff] [blame] | 73 | test -f .git/worktrees/here-with-lock/locked |
| 74 | ' |
| 75 | |
Stephen Manz | 0db4961 | 2021-07-15 02:32:30 +0000 | [diff] [blame] | 76 | test_expect_success '"add" worktree with lock and reason' ' |
| 77 | lock_reason="why not" && |
| 78 | git worktree add --detach --lock --reason "$lock_reason" here-with-lock-reason main && |
| 79 | test_when_finished "git worktree unlock here-with-lock-reason || :" && |
| 80 | test -f .git/worktrees/here-with-lock-reason/locked && |
| 81 | echo "$lock_reason" >expect && |
| 82 | test_cmp expect .git/worktrees/here-with-lock-reason/locked |
| 83 | ' |
| 84 | |
| 85 | test_expect_success '"add" worktree with reason but no lock' ' |
| 86 | test_must_fail git worktree add --detach --reason "why not" here-with-reason-only main && |
| 87 | test_path_is_missing .git/worktrees/here-with-reason-only/locked |
| 88 | ' |
| 89 | |
Eric Sunshine | f194b1e | 2015-07-06 13:30:54 -0400 | [diff] [blame] | 90 | test_expect_success '"add" worktree from a subdir' ' |
| 91 | ( |
| 92 | mkdir sub && |
| 93 | cd sub && |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 94 | git worktree add --detach here main && |
Eric Sunshine | f194b1e | 2015-07-06 13:30:54 -0400 | [diff] [blame] | 95 | cd here && |
| 96 | test_cmp ../../init.t init.t |
| 97 | ) |
| 98 | ' |
| 99 | |
| 100 | test_expect_success '"add" from a linked checkout' ' |
| 101 | ( |
| 102 | cd here && |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 103 | git worktree add --detach nested-here main && |
Eric Sunshine | f194b1e | 2015-07-06 13:30:54 -0400 | [diff] [blame] | 104 | cd nested-here && |
| 105 | git fsck |
| 106 | ) |
| 107 | ' |
| 108 | |
| 109 | test_expect_success '"add" worktree creating new branch' ' |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 110 | git worktree add -b newmain there main && |
Eric Sunshine | f194b1e | 2015-07-06 13:30:54 -0400 | [diff] [blame] | 111 | ( |
| 112 | cd there && |
| 113 | test_cmp ../init.t init.t && |
| 114 | git symbolic-ref HEAD >actual && |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 115 | echo refs/heads/newmain >expect && |
Eric Sunshine | f194b1e | 2015-07-06 13:30:54 -0400 | [diff] [blame] | 116 | test_cmp expect actual && |
| 117 | git fsck |
| 118 | ) |
| 119 | ' |
| 120 | |
| 121 | test_expect_success 'die the same branch is already checked out' ' |
| 122 | ( |
| 123 | cd here && |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 124 | test_must_fail git checkout newmain |
Eric Sunshine | f194b1e | 2015-07-06 13:30:54 -0400 | [diff] [blame] | 125 | ) |
| 126 | ' |
| 127 | |
Eric Sunshine | 746bbdc | 2015-07-17 19:00:03 -0400 | [diff] [blame] | 128 | test_expect_success SYMLINKS 'die the same branch is already checked out (symlink)' ' |
| 129 | head=$(git -C there rev-parse --git-path HEAD) && |
| 130 | ref=$(git -C there symbolic-ref HEAD) && |
| 131 | rm "$head" && |
| 132 | ln -s "$ref" "$head" && |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 133 | test_must_fail git -C here checkout newmain |
Eric Sunshine | 746bbdc | 2015-07-17 19:00:03 -0400 | [diff] [blame] | 134 | ' |
| 135 | |
Eric Sunshine | f194b1e | 2015-07-06 13:30:54 -0400 | [diff] [blame] | 136 | test_expect_success 'not die the same branch is already checked out' ' |
| 137 | ( |
| 138 | cd here && |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 139 | git worktree add --force anothernewmain newmain |
Eric Sunshine | f194b1e | 2015-07-06 13:30:54 -0400 | [diff] [blame] | 140 | ) |
| 141 | ' |
| 142 | |
| 143 | test_expect_success 'not die on re-checking out current branch' ' |
| 144 | ( |
| 145 | cd there && |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 146 | git checkout newmain |
Eric Sunshine | f194b1e | 2015-07-06 13:30:54 -0400 | [diff] [blame] | 147 | ) |
| 148 | ' |
| 149 | |
| 150 | test_expect_success '"add" from a bare repo' ' |
| 151 | ( |
| 152 | git clone --bare . bare && |
| 153 | cd bare && |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 154 | git worktree add -b bare-main ../there2 main |
Eric Sunshine | f194b1e | 2015-07-06 13:30:54 -0400 | [diff] [blame] | 155 | ) |
| 156 | ' |
| 157 | |
| 158 | test_expect_success 'checkout from a bare repo without "add"' ' |
| 159 | ( |
| 160 | cd bare && |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 161 | test_must_fail git checkout main |
Eric Sunshine | f194b1e | 2015-07-06 13:30:54 -0400 | [diff] [blame] | 162 | ) |
| 163 | ' |
| 164 | |
Dennis Kaarsemaker | 171c646 | 2016-10-12 18:41:07 +0200 | [diff] [blame] | 165 | test_expect_success '"add" default branch of a bare repo' ' |
| 166 | ( |
| 167 | git clone --bare . bare2 && |
| 168 | cd bare2 && |
Derrick Stolee | 5325591 | 2022-02-07 21:33:02 +0000 | [diff] [blame] | 169 | git worktree add ../there3 main && |
| 170 | cd ../there3 && |
| 171 | # Simple check that a Git command does not |
| 172 | # immediately fail with the current setup |
| 173 | git status |
| 174 | ) && |
| 175 | cat >expect <<-EOF && |
| 176 | init.t |
| 177 | EOF |
| 178 | ls there3 >actual && |
| 179 | test_cmp expect actual |
| 180 | ' |
| 181 | |
| 182 | test_expect_success '"add" to bare repo with worktree config' ' |
| 183 | ( |
| 184 | git clone --bare . bare3 && |
| 185 | cd bare3 && |
| 186 | git config extensions.worktreeconfig true && |
| 187 | |
| 188 | # Add config values that are erroneous to have in |
| 189 | # a config.worktree file outside of the main |
| 190 | # working tree, to check that Git filters them out |
| 191 | # when copying config during "git worktree add". |
| 192 | git config --worktree core.bare true && |
| 193 | git config --worktree core.worktree "$(pwd)" && |
| 194 | |
| 195 | # We want to check that bogus.key is copied |
| 196 | git config --worktree bogus.key value && |
| 197 | git config --unset core.bare && |
| 198 | git worktree add ../there4 main && |
| 199 | cd ../there4 && |
| 200 | |
| 201 | # Simple check that a Git command does not |
| 202 | # immediately fail with the current setup |
| 203 | git status && |
| 204 | git worktree add --detach ../there5 && |
| 205 | cd ../there5 && |
| 206 | git status |
| 207 | ) && |
| 208 | |
| 209 | # the worktree has the arbitrary value copied. |
| 210 | test_cmp_config -C there4 value bogus.key && |
| 211 | test_cmp_config -C there5 value bogus.key && |
| 212 | |
| 213 | # however, core.bare and core.worktree were removed. |
| 214 | test_must_fail git -C there4 config core.bare && |
| 215 | test_must_fail git -C there4 config core.worktree && |
| 216 | |
| 217 | cat >expect <<-EOF && |
| 218 | init.t |
| 219 | EOF |
| 220 | |
| 221 | ls there4 >actual && |
| 222 | test_cmp expect actual && |
| 223 | ls there5 >actual && |
| 224 | test_cmp expect actual |
Dennis Kaarsemaker | 171c646 | 2016-10-12 18:41:07 +0200 | [diff] [blame] | 225 | ' |
| 226 | |
Eric Sunshine | f194b1e | 2015-07-06 13:30:54 -0400 | [diff] [blame] | 227 | test_expect_success 'checkout with grafts' ' |
| 228 | test_when_finished rm .git/info/grafts && |
| 229 | test_commit abc && |
Elia Pinto | 697b90d | 2015-12-22 16:05:52 +0100 | [diff] [blame] | 230 | SHA1=$(git rev-parse HEAD) && |
Eric Sunshine | f194b1e | 2015-07-06 13:30:54 -0400 | [diff] [blame] | 231 | test_commit def && |
| 232 | test_commit xyz && |
Ævar Arnfjörð Bjarmason | 93e02b6 | 2022-06-03 13:15:05 +0200 | [diff] [blame] | 233 | mkdir .git/info && |
Elia Pinto | 697b90d | 2015-12-22 16:05:52 +0100 | [diff] [blame] | 234 | echo "$(git rev-parse HEAD) $SHA1" >.git/info/grafts && |
Eric Sunshine | f194b1e | 2015-07-06 13:30:54 -0400 | [diff] [blame] | 235 | cat >expected <<-\EOF && |
| 236 | xyz |
| 237 | abc |
| 238 | EOF |
| 239 | git log --format=%s -2 >actual && |
| 240 | test_cmp expected actual && |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 241 | git worktree add --detach grafted main && |
Eric Sunshine | f194b1e | 2015-07-06 13:30:54 -0400 | [diff] [blame] | 242 | git --git-dir=grafted/.git log --format=%s -2 >actual && |
| 243 | test_cmp expected actual |
| 244 | ' |
| 245 | |
| 246 | test_expect_success '"add" from relative HEAD' ' |
| 247 | test_commit a && |
| 248 | test_commit b && |
| 249 | test_commit c && |
| 250 | git rev-parse HEAD~1 >expected && |
| 251 | git worktree add relhead HEAD~1 && |
| 252 | git -C relhead rev-parse HEAD >actual && |
| 253 | test_cmp expected actual |
| 254 | ' |
| 255 | |
Eric Sunshine | 0f4af3b | 2015-07-06 13:30:58 -0400 | [diff] [blame] | 256 | test_expect_success '"add -b" with <branch> omitted' ' |
| 257 | git worktree add -b burble flornk && |
| 258 | test_cmp_rev HEAD burble |
| 259 | ' |
| 260 | |
Eric Sunshine | 5c94257 | 2015-07-17 19:00:09 -0400 | [diff] [blame] | 261 | test_expect_success '"add --detach" with <branch> omitted' ' |
| 262 | git worktree add --detach fishhook && |
| 263 | git rev-parse HEAD >expected && |
| 264 | git -C fishhook rev-parse HEAD >actual && |
| 265 | test_cmp expected actual && |
| 266 | test_must_fail git -C fishhook symbolic-ref HEAD |
| 267 | ' |
| 268 | |
Eric Sunshine | 1eb07d8 | 2015-07-06 13:30:59 -0400 | [diff] [blame] | 269 | test_expect_success '"add" with <branch> omitted' ' |
| 270 | git worktree add wiffle/bat && |
| 271 | test_cmp_rev HEAD bat |
| 272 | ' |
| 273 | |
Thomas Gummerer | f60a7b7 | 2018-04-24 22:56:35 +0100 | [diff] [blame] | 274 | test_expect_success '"add" checks out existing branch of dwimd name' ' |
| 275 | git branch dwim HEAD~1 && |
| 276 | git worktree add dwim && |
| 277 | test_cmp_rev HEAD~1 dwim && |
| 278 | ( |
| 279 | cd dwim && |
| 280 | test_cmp_rev HEAD dwim |
| 281 | ) |
| 282 | ' |
| 283 | |
| 284 | test_expect_success '"add <path>" dwim fails with checked out branch' ' |
| 285 | git checkout -b test-branch && |
| 286 | test_must_fail git worktree add test-branch && |
| 287 | test_path_is_missing test-branch |
| 288 | ' |
| 289 | |
| 290 | test_expect_success '"add --force" with existing dwimd name doesnt die' ' |
| 291 | git checkout test-branch && |
| 292 | git worktree add --force test-branch |
Eric Sunshine | 1eb07d8 | 2015-07-06 13:30:59 -0400 | [diff] [blame] | 293 | ' |
| 294 | |
Eric Sunshine | 5c94257 | 2015-07-17 19:00:09 -0400 | [diff] [blame] | 295 | test_expect_success '"add" no auto-vivify with --detach and <branch> omitted' ' |
| 296 | git worktree add --detach mish/mash && |
| 297 | test_must_fail git rev-parse mash -- && |
| 298 | test_must_fail git -C mish/mash symbolic-ref HEAD |
| 299 | ' |
| 300 | |
Eric Sunshine | ab0b2c5 | 2015-07-17 19:00:08 -0400 | [diff] [blame] | 301 | test_expect_success '"add" -b/-B mutually exclusive' ' |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 302 | test_must_fail git worktree add -b poodle -B poodle bamboo main |
Eric Sunshine | ab0b2c5 | 2015-07-17 19:00:08 -0400 | [diff] [blame] | 303 | ' |
| 304 | |
| 305 | test_expect_success '"add" -b/--detach mutually exclusive' ' |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 306 | test_must_fail git worktree add -b poodle --detach bamboo main |
Eric Sunshine | ab0b2c5 | 2015-07-17 19:00:08 -0400 | [diff] [blame] | 307 | ' |
| 308 | |
| 309 | test_expect_success '"add" -B/--detach mutually exclusive' ' |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 310 | test_must_fail git worktree add -B poodle --detach bamboo main |
Eric Sunshine | ab0b2c5 | 2015-07-17 19:00:08 -0400 | [diff] [blame] | 311 | ' |
| 312 | |
Nguyễn Thái Ngọc Duy | beb6f24 | 2016-02-15 20:35:33 +0700 | [diff] [blame] | 313 | test_expect_success '"add -B" fails if the branch is checked out' ' |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 314 | git rev-parse newmain >before && |
| 315 | test_must_fail git worktree add -B newmain bamboo main && |
| 316 | git rev-parse newmain >after && |
Nguyễn Thái Ngọc Duy | beb6f24 | 2016-02-15 20:35:33 +0700 | [diff] [blame] | 317 | test_cmp before after |
| 318 | ' |
| 319 | |
Nguyễn Thái Ngọc Duy | 0ebf4a2 | 2016-02-15 20:35:32 +0700 | [diff] [blame] | 320 | test_expect_success 'add -B' ' |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 321 | git worktree add -B poodle bamboo2 main^ && |
Nguyễn Thái Ngọc Duy | 0ebf4a2 | 2016-02-15 20:35:32 +0700 | [diff] [blame] | 322 | git -C bamboo2 symbolic-ref HEAD >actual && |
| 323 | echo refs/heads/poodle >expected && |
| 324 | test_cmp expected actual && |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 325 | test_cmp_rev main^ poodle |
Nguyễn Thái Ngọc Duy | 0ebf4a2 | 2016-02-15 20:35:32 +0700 | [diff] [blame] | 326 | ' |
| 327 | |
Elia Pinto | 371979c | 2018-08-15 20:56:30 +0000 | [diff] [blame] | 328 | test_expect_success 'add --quiet' ' |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 329 | git worktree add --quiet another-worktree main 2>actual && |
Elia Pinto | 371979c | 2018-08-15 20:56:30 +0000 | [diff] [blame] | 330 | test_must_be_empty actual |
| 331 | ' |
| 332 | |
Nguyễn Thái Ngọc Duy | 744e469 | 2015-09-28 20:06:15 +0700 | [diff] [blame] | 333 | test_expect_success 'local clone from linked checkout' ' |
| 334 | git clone --local here here-clone && |
| 335 | ( cd here-clone && git fsck ) |
| 336 | ' |
| 337 | |
Eric Sunshine | b3b0597 | 2017-12-11 18:16:12 -0500 | [diff] [blame] | 338 | test_expect_success 'local clone --shared from linked checkout' ' |
| 339 | git -C bare worktree add --detach ../baretree && |
| 340 | git clone --local --shared baretree bare-clone && |
| 341 | grep /bare/ bare-clone/.git/objects/info/alternates |
| 342 | ' |
| 343 | |
Ray Zhang | ef2a0ac | 2016-03-29 10:11:01 +0000 | [diff] [blame] | 344 | test_expect_success '"add" worktree with --no-checkout' ' |
| 345 | git worktree add --no-checkout -b swamp swamp && |
| 346 | ! test -e swamp/init.t && |
| 347 | git -C swamp reset --hard && |
| 348 | test_cmp init.t swamp/init.t |
| 349 | ' |
| 350 | |
| 351 | test_expect_success '"add" worktree with --checkout' ' |
| 352 | git worktree add --checkout -b swmap2 swamp2 && |
| 353 | test_cmp init.t swamp2/init.t |
| 354 | ' |
| 355 | |
Nguyễn Thái Ngọc Duy | 8d9fdd7 | 2016-04-22 20:01:33 +0700 | [diff] [blame] | 356 | test_expect_success 'put a worktree under rebase' ' |
| 357 | git worktree add under-rebase && |
| 358 | ( |
| 359 | cd under-rebase && |
| 360 | set_fake_editor && |
| 361 | FAKE_LINES="edit 1" git rebase -i HEAD^ && |
| 362 | git worktree list | grep "under-rebase.*detached HEAD" |
| 363 | ) |
| 364 | ' |
| 365 | |
| 366 | test_expect_success 'add a worktree, checking out a rebased branch' ' |
| 367 | test_must_fail git worktree add new-rebase under-rebase && |
| 368 | ! test -d new-rebase |
| 369 | ' |
| 370 | |
| 371 | test_expect_success 'checking out a rebased branch from another worktree' ' |
| 372 | git worktree add new-place && |
| 373 | test_must_fail git -C new-place checkout under-rebase |
| 374 | ' |
| 375 | |
| 376 | test_expect_success 'not allow to delete a branch under rebase' ' |
| 377 | ( |
| 378 | cd under-rebase && |
| 379 | test_must_fail git branch -D under-rebase |
| 380 | ) |
| 381 | ' |
| 382 | |
Nguyễn Thái Ngọc Duy | 14ace5b | 2016-04-22 20:01:36 +0700 | [diff] [blame] | 383 | test_expect_success 'rename a branch under rebase not allowed' ' |
| 384 | test_must_fail git branch -M under-rebase rebase-with-new-name |
| 385 | ' |
| 386 | |
Nguyễn Thái Ngọc Duy | 8d9fdd7 | 2016-04-22 20:01:33 +0700 | [diff] [blame] | 387 | test_expect_success 'check out from current worktree branch ok' ' |
| 388 | ( |
| 389 | cd under-rebase && |
| 390 | git checkout under-rebase && |
| 391 | git checkout - && |
| 392 | git rebase --abort |
| 393 | ) |
| 394 | ' |
| 395 | |
Nguyễn Thái Ngọc Duy | 04a3dfb | 2016-04-22 20:01:35 +0700 | [diff] [blame] | 396 | test_expect_success 'checkout a branch under bisect' ' |
| 397 | git worktree add under-bisect && |
| 398 | ( |
| 399 | cd under-bisect && |
| 400 | git bisect start && |
| 401 | git bisect bad && |
| 402 | git bisect good HEAD~2 && |
| 403 | git worktree list | grep "under-bisect.*detached HEAD" && |
| 404 | test_must_fail git worktree add new-bisect under-bisect && |
| 405 | ! test -d new-bisect |
| 406 | ) |
| 407 | ' |
| 408 | |
Nguyễn Thái Ngọc Duy | 14ace5b | 2016-04-22 20:01:36 +0700 | [diff] [blame] | 409 | test_expect_success 'rename a branch under bisect not allowed' ' |
| 410 | test_must_fail git branch -M under-bisect bisect-with-new-name |
| 411 | ' |
Thomas Gummerer | e284e89 | 2017-11-26 19:43:53 +0000 | [diff] [blame] | 412 | # Is branch "refs/heads/$1" set to pull from "$2/$3"? |
| 413 | test_branch_upstream () { |
| 414 | printf "%s\n" "$2" "refs/heads/$3" >expect.upstream && |
| 415 | { |
| 416 | git config "branch.$1.remote" && |
| 417 | git config "branch.$1.merge" |
| 418 | } >actual.upstream && |
| 419 | test_cmp expect.upstream actual.upstream |
| 420 | } |
| 421 | |
| 422 | test_expect_success '--track sets up tracking' ' |
| 423 | test_when_finished rm -rf track && |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 424 | git worktree add --track -b track track main && |
| 425 | test_branch_upstream track . main |
Thomas Gummerer | e284e89 | 2017-11-26 19:43:53 +0000 | [diff] [blame] | 426 | ' |
| 427 | |
| 428 | # setup remote repository $1 and repository $2 with $1 set up as |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 429 | # remote. The remote has two branches, main and foo. |
Thomas Gummerer | e284e89 | 2017-11-26 19:43:53 +0000 | [diff] [blame] | 430 | setup_remote_repo () { |
| 431 | git init $1 && |
| 432 | ( |
| 433 | cd $1 && |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 434 | test_commit $1_main && |
Thomas Gummerer | e284e89 | 2017-11-26 19:43:53 +0000 | [diff] [blame] | 435 | git checkout -b foo && |
| 436 | test_commit upstream_foo |
| 437 | ) && |
| 438 | git init $2 && |
| 439 | ( |
| 440 | cd $2 && |
Johannes Schindelin | 883b98e | 2020-11-18 23:44:22 +0000 | [diff] [blame] | 441 | test_commit $2_main && |
Thomas Gummerer | e284e89 | 2017-11-26 19:43:53 +0000 | [diff] [blame] | 442 | git remote add $1 ../$1 && |
| 443 | git config remote.$1.fetch \ |
| 444 | "refs/heads/*:refs/remotes/$1/*" && |
| 445 | git fetch --all |
| 446 | ) |
| 447 | } |
| 448 | |
| 449 | test_expect_success '--no-track avoids setting up tracking' ' |
| 450 | test_when_finished rm -rf repo_upstream repo_local foo && |
| 451 | setup_remote_repo repo_upstream repo_local && |
| 452 | ( |
| 453 | cd repo_local && |
| 454 | git worktree add --no-track -b foo ../foo repo_upstream/foo |
| 455 | ) && |
| 456 | ( |
| 457 | cd foo && |
| 458 | test_must_fail git config "branch.foo.remote" && |
| 459 | test_must_fail git config "branch.foo.merge" && |
| 460 | test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo |
| 461 | ) |
| 462 | ' |
Nguyễn Thái Ngọc Duy | 14ace5b | 2016-04-22 20:01:36 +0700 | [diff] [blame] | 463 | |
Thomas Gummerer | 4e85333 | 2017-11-26 19:43:54 +0000 | [diff] [blame] | 464 | test_expect_success '"add" <path> <non-existent-branch> fails' ' |
| 465 | test_must_fail git worktree add foo non-existent |
| 466 | ' |
| 467 | |
| 468 | test_expect_success '"add" <path> <branch> dwims' ' |
| 469 | test_when_finished rm -rf repo_upstream repo_dwim foo && |
| 470 | setup_remote_repo repo_upstream repo_dwim && |
| 471 | git init repo_dwim && |
| 472 | ( |
| 473 | cd repo_dwim && |
| 474 | git worktree add ../foo foo |
| 475 | ) && |
| 476 | ( |
| 477 | cd foo && |
| 478 | test_branch_upstream foo repo_upstream foo && |
| 479 | test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo |
| 480 | ) |
| 481 | ' |
| 482 | |
Ævar Arnfjörð Bjarmason | 8d7b558 | 2018-06-05 14:40:49 +0000 | [diff] [blame] | 483 | test_expect_success '"add" <path> <branch> dwims with checkout.defaultRemote' ' |
| 484 | test_when_finished rm -rf repo_upstream repo_dwim foo && |
| 485 | setup_remote_repo repo_upstream repo_dwim && |
| 486 | git init repo_dwim && |
| 487 | ( |
| 488 | cd repo_dwim && |
| 489 | git remote add repo_upstream2 ../repo_upstream && |
| 490 | git fetch repo_upstream2 && |
| 491 | test_must_fail git worktree add ../foo foo && |
| 492 | git -c checkout.defaultRemote=repo_upstream worktree add ../foo foo && |
Ævar Arnfjörð Bjarmason | 8d7b558 | 2018-06-05 14:40:49 +0000 | [diff] [blame] | 493 | git status -uno --porcelain >status.actual && |
Ævar Arnfjörð Bjarmason | 9f4bcf8 | 2018-07-27 17:48:11 +0000 | [diff] [blame] | 494 | test_must_be_empty status.actual |
Ævar Arnfjörð Bjarmason | 8d7b558 | 2018-06-05 14:40:49 +0000 | [diff] [blame] | 495 | ) && |
| 496 | ( |
| 497 | cd foo && |
| 498 | test_branch_upstream foo repo_upstream foo && |
| 499 | test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo |
| 500 | ) |
| 501 | ' |
| 502 | |
Thomas Gummerer | 71d6682 | 2017-11-29 20:04:50 +0000 | [diff] [blame] | 503 | test_expect_success 'git worktree add does not match remote' ' |
| 504 | test_when_finished rm -rf repo_a repo_b foo && |
| 505 | setup_remote_repo repo_a repo_b && |
| 506 | ( |
| 507 | cd repo_b && |
| 508 | git worktree add ../foo |
| 509 | ) && |
| 510 | ( |
| 511 | cd foo && |
| 512 | test_must_fail git config "branch.foo.remote" && |
| 513 | test_must_fail git config "branch.foo.merge" && |
Denton Liu | 2c9e125 | 2019-11-12 15:07:45 -0800 | [diff] [blame] | 514 | test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo |
Thomas Gummerer | 71d6682 | 2017-11-29 20:04:50 +0000 | [diff] [blame] | 515 | ) |
| 516 | ' |
| 517 | |
| 518 | test_expect_success 'git worktree add --guess-remote sets up tracking' ' |
| 519 | test_when_finished rm -rf repo_a repo_b foo && |
| 520 | setup_remote_repo repo_a repo_b && |
| 521 | ( |
| 522 | cd repo_b && |
| 523 | git worktree add --guess-remote ../foo |
| 524 | ) && |
| 525 | ( |
| 526 | cd foo && |
| 527 | test_branch_upstream foo repo_a foo && |
| 528 | test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo |
| 529 | ) |
| 530 | ' |
| 531 | |
Thomas Gummerer | e92445a | 2017-11-29 20:04:51 +0000 | [diff] [blame] | 532 | test_expect_success 'git worktree add with worktree.guessRemote sets up tracking' ' |
| 533 | test_when_finished rm -rf repo_a repo_b foo && |
| 534 | setup_remote_repo repo_a repo_b && |
| 535 | ( |
| 536 | cd repo_b && |
| 537 | git config worktree.guessRemote true && |
| 538 | git worktree add ../foo |
| 539 | ) && |
| 540 | ( |
| 541 | cd foo && |
| 542 | test_branch_upstream foo repo_a foo && |
| 543 | test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo |
| 544 | ) |
| 545 | ' |
| 546 | |
| 547 | test_expect_success 'git worktree --no-guess-remote option overrides config' ' |
| 548 | test_when_finished rm -rf repo_a repo_b foo && |
| 549 | setup_remote_repo repo_a repo_b && |
| 550 | ( |
| 551 | cd repo_b && |
| 552 | git config worktree.guessRemote true && |
| 553 | git worktree add --no-guess-remote ../foo |
| 554 | ) && |
| 555 | ( |
| 556 | cd foo && |
| 557 | test_must_fail git config "branch.foo.remote" && |
| 558 | test_must_fail git config "branch.foo.merge" && |
Denton Liu | 2c9e125 | 2019-11-12 15:07:45 -0800 | [diff] [blame] | 559 | test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo |
Thomas Gummerer | e92445a | 2017-11-29 20:04:51 +0000 | [diff] [blame] | 560 | ) |
| 561 | ' |
| 562 | |
Eric Sunshine | ade546b | 2017-12-07 16:20:17 -0500 | [diff] [blame] | 563 | post_checkout_hook () { |
Ævar Arnfjörð Bjarmason | 93e02b6 | 2022-06-03 13:15:05 +0200 | [diff] [blame] | 564 | test_when_finished "rm -rf .git/hooks" && |
| 565 | mkdir .git/hooks && |
Ævar Arnfjörð Bjarmason | 66865d1 | 2022-03-17 11:13:16 +0100 | [diff] [blame] | 566 | test_hook -C "$1" post-checkout <<-\EOF |
Eric Sunshine | a4bf1e3 | 2018-02-15 14:18:41 -0500 | [diff] [blame] | 567 | { |
| 568 | echo $* |
| 569 | git rev-parse --git-dir --show-toplevel |
| 570 | } >hook.actual |
Eric Sunshine | ade546b | 2017-12-07 16:20:17 -0500 | [diff] [blame] | 571 | EOF |
| 572 | } |
| 573 | |
| 574 | test_expect_success '"add" invokes post-checkout hook (branch)' ' |
| 575 | post_checkout_hook && |
Eric Sunshine | a4bf1e3 | 2018-02-15 14:18:41 -0500 | [diff] [blame] | 576 | { |
brian m. carlson | 8125a58 | 2018-05-13 02:24:13 +0000 | [diff] [blame] | 577 | echo $ZERO_OID $(git rev-parse HEAD) 1 && |
Eric Sunshine | a4bf1e3 | 2018-02-15 14:18:41 -0500 | [diff] [blame] | 578 | echo $(pwd)/.git/worktrees/gumby && |
| 579 | echo $(pwd)/gumby |
| 580 | } >hook.expect && |
Eric Sunshine | ade546b | 2017-12-07 16:20:17 -0500 | [diff] [blame] | 581 | git worktree add gumby && |
Eric Sunshine | a4bf1e3 | 2018-02-15 14:18:41 -0500 | [diff] [blame] | 582 | test_cmp hook.expect gumby/hook.actual |
Eric Sunshine | ade546b | 2017-12-07 16:20:17 -0500 | [diff] [blame] | 583 | ' |
| 584 | |
| 585 | test_expect_success '"add" invokes post-checkout hook (detached)' ' |
| 586 | post_checkout_hook && |
Eric Sunshine | a4bf1e3 | 2018-02-15 14:18:41 -0500 | [diff] [blame] | 587 | { |
brian m. carlson | 8125a58 | 2018-05-13 02:24:13 +0000 | [diff] [blame] | 588 | echo $ZERO_OID $(git rev-parse HEAD) 1 && |
Eric Sunshine | a4bf1e3 | 2018-02-15 14:18:41 -0500 | [diff] [blame] | 589 | echo $(pwd)/.git/worktrees/grumpy && |
| 590 | echo $(pwd)/grumpy |
| 591 | } >hook.expect && |
Eric Sunshine | ade546b | 2017-12-07 16:20:17 -0500 | [diff] [blame] | 592 | git worktree add --detach grumpy && |
Eric Sunshine | a4bf1e3 | 2018-02-15 14:18:41 -0500 | [diff] [blame] | 593 | test_cmp hook.expect grumpy/hook.actual |
Eric Sunshine | ade546b | 2017-12-07 16:20:17 -0500 | [diff] [blame] | 594 | ' |
| 595 | |
| 596 | test_expect_success '"add --no-checkout" suppresses post-checkout hook' ' |
| 597 | post_checkout_hook && |
| 598 | rm -f hook.actual && |
| 599 | git worktree add --no-checkout gloopy && |
Eric Sunshine | a4bf1e3 | 2018-02-15 14:18:41 -0500 | [diff] [blame] | 600 | test_path_is_missing gloopy/hook.actual |
| 601 | ' |
| 602 | |
| 603 | test_expect_success '"add" in other worktree invokes post-checkout hook' ' |
| 604 | post_checkout_hook && |
| 605 | { |
brian m. carlson | 8125a58 | 2018-05-13 02:24:13 +0000 | [diff] [blame] | 606 | echo $ZERO_OID $(git rev-parse HEAD) 1 && |
Eric Sunshine | a4bf1e3 | 2018-02-15 14:18:41 -0500 | [diff] [blame] | 607 | echo $(pwd)/.git/worktrees/guppy && |
| 608 | echo $(pwd)/guppy |
| 609 | } >hook.expect && |
| 610 | git -C gloopy worktree add --detach ../guppy && |
| 611 | test_cmp hook.expect guppy/hook.actual |
| 612 | ' |
| 613 | |
| 614 | test_expect_success '"add" in bare repo invokes post-checkout hook' ' |
| 615 | rm -rf bare && |
| 616 | git clone --bare . bare && |
| 617 | { |
brian m. carlson | 8125a58 | 2018-05-13 02:24:13 +0000 | [diff] [blame] | 618 | echo $ZERO_OID $(git --git-dir=bare rev-parse HEAD) 1 && |
Eric Sunshine | a4bf1e3 | 2018-02-15 14:18:41 -0500 | [diff] [blame] | 619 | echo $(pwd)/bare/worktrees/goozy && |
| 620 | echo $(pwd)/goozy |
| 621 | } >hook.expect && |
| 622 | post_checkout_hook bare && |
| 623 | git -C bare worktree add --detach ../goozy && |
| 624 | test_cmp hook.expect goozy/hook.actual |
Eric Sunshine | ade546b | 2017-12-07 16:20:17 -0500 | [diff] [blame] | 625 | ' |
| 626 | |
Eric Sunshine | cb56f55 | 2018-08-28 17:20:22 -0400 | [diff] [blame] | 627 | test_expect_success '"add" an existing but missing worktree' ' |
| 628 | git worktree add --detach pneu && |
| 629 | test_must_fail git worktree add --detach pneu && |
| 630 | rm -fr pneu && |
Eric Sunshine | e19831c | 2018-08-28 17:20:23 -0400 | [diff] [blame] | 631 | test_must_fail git worktree add --detach pneu && |
| 632 | git worktree add --force --detach pneu |
| 633 | ' |
| 634 | |
| 635 | test_expect_success '"add" an existing locked but missing worktree' ' |
| 636 | git worktree add --detach gnoo && |
| 637 | git worktree lock gnoo && |
| 638 | test_when_finished "git worktree unlock gnoo || :" && |
| 639 | rm -fr gnoo && |
| 640 | test_must_fail git worktree add --detach gnoo && |
| 641 | test_must_fail git worktree add --force --detach gnoo && |
| 642 | git worktree add --force --force --detach gnoo |
Eric Sunshine | cb56f55 | 2018-08-28 17:20:22 -0400 | [diff] [blame] | 643 | ' |
| 644 | |
Eric Sunshine | bb69b3b | 2020-02-24 04:08:48 -0500 | [diff] [blame] | 645 | test_expect_success '"add" not tripped up by magic worktree matching"' ' |
| 646 | # if worktree "sub1/bar" exists, "git worktree add bar" in distinct |
| 647 | # directory `sub2` should not mistakenly complain that `bar` is an |
| 648 | # already-registered worktree |
| 649 | mkdir sub1 sub2 && |
| 650 | git -C sub1 --git-dir=../.git worktree add --detach bozo && |
| 651 | git -C sub2 --git-dir=../.git worktree add --detach bozo |
| 652 | ' |
| 653 | |
Nguyễn Thái Ngọc Duy | 1de16ae | 2019-03-08 16:28:34 +0700 | [diff] [blame] | 654 | test_expect_success FUNNYNAMES 'sanitize generated worktree name' ' |
| 655 | git worktree add --detach ". weird*..?.lock.lock" && |
| 656 | test -d .git/worktrees/---weird-.- |
| 657 | ' |
| 658 | |
Nguyễn Thái Ngọc Duy | 105df73 | 2019-05-13 17:49:44 +0700 | [diff] [blame] | 659 | test_expect_success '"add" should not fail because of another bad worktree' ' |
| 660 | git init add-fail && |
| 661 | ( |
| 662 | cd add-fail && |
| 663 | test_commit first && |
| 664 | mkdir sub && |
| 665 | git worktree add sub/to-be-deleted && |
| 666 | rm -rf sub && |
| 667 | git worktree add second |
| 668 | ) |
| 669 | ' |
| 670 | |
Philippe Blain | 4782cf2 | 2019-10-27 17:16:25 +0000 | [diff] [blame] | 671 | test_expect_success '"add" with uninitialized submodule, with submodule.recurse unset' ' |
Taylor Blau | 99f4abb | 2022-07-29 15:20:03 -0400 | [diff] [blame] | 672 | test_config_global protocol.file.allow always && |
Philippe Blain | 4782cf2 | 2019-10-27 17:16:25 +0000 | [diff] [blame] | 673 | test_create_repo submodule && |
| 674 | test_commit -C submodule first && |
| 675 | test_create_repo project && |
| 676 | git -C project submodule add ../submodule && |
| 677 | git -C project add submodule && |
| 678 | test_tick && |
| 679 | git -C project commit -m add_sub && |
| 680 | git clone project project-clone && |
| 681 | git -C project-clone worktree add ../project-2 |
| 682 | ' |
| 683 | test_expect_success '"add" with uninitialized submodule, with submodule.recurse set' ' |
| 684 | git -C project-clone -c submodule.recurse worktree add ../project-3 |
| 685 | ' |
| 686 | |
| 687 | test_expect_success '"add" with initialized submodule, with submodule.recurse unset' ' |
Taylor Blau | 99f4abb | 2022-07-29 15:20:03 -0400 | [diff] [blame] | 688 | test_config_global protocol.file.allow always && |
Philippe Blain | 4782cf2 | 2019-10-27 17:16:25 +0000 | [diff] [blame] | 689 | git -C project-clone submodule update --init && |
| 690 | git -C project-clone worktree add ../project-4 |
| 691 | ' |
| 692 | |
| 693 | test_expect_success '"add" with initialized submodule, with submodule.recurse set' ' |
| 694 | git -C project-clone -c submodule.recurse worktree add ../project-5 |
| 695 | ' |
| 696 | |
Eric Sunshine | f194b1e | 2015-07-06 13:30:54 -0400 | [diff] [blame] | 697 | test_done |