t / t2025-worktree-add.shon commit Merge branch 'mm/send-email-cc-cruft-after-address' (bdcaebb)
   1#!/bin/sh
   2
   3test_description='test git worktree add'
   4
   5. ./test-lib.sh
   6
   7. "$TEST_DIRECTORY"/lib-rebase.sh
   8
   9test_expect_success 'setup' '
  10        test_commit init
  11'
  12
  13test_expect_success '"add" an existing worktree' '
  14        mkdir -p existing/subtree &&
  15        test_must_fail git worktree add --detach existing master
  16'
  17
  18test_expect_success '"add" an existing empty worktree' '
  19        mkdir existing_empty &&
  20        git worktree add --detach existing_empty master
  21'
  22
  23test_expect_success '"add" using shorthand - fails when no previous branch' '
  24        test_must_fail git worktree add existing_short -
  25'
  26
  27test_expect_success '"add" using - shorthand' '
  28        git checkout -b newbranch &&
  29        echo hello >myworld &&
  30        git add myworld &&
  31        git commit -m myworld &&
  32        git checkout master &&
  33        git worktree add short-hand - &&
  34        echo refs/heads/newbranch >expect &&
  35        git -C short-hand rev-parse --symbolic-full-name HEAD >actual &&
  36        test_cmp expect actual
  37'
  38
  39test_expect_success '"add" refuses to checkout locked branch' '
  40        test_must_fail git worktree add zere master &&
  41        ! test -d zere &&
  42        ! test -d .git/worktrees/zere
  43'
  44
  45test_expect_success 'checking out paths not complaining about linked checkouts' '
  46        (
  47        cd existing_empty &&
  48        echo dirty >>init.t &&
  49        git checkout master -- init.t
  50        )
  51'
  52
  53test_expect_success '"add" worktree' '
  54        git rev-parse HEAD >expect &&
  55        git worktree add --detach here master &&
  56        (
  57                cd here &&
  58                test_cmp ../init.t init.t &&
  59                test_must_fail git symbolic-ref HEAD &&
  60                git rev-parse HEAD >actual &&
  61                test_cmp ../expect actual &&
  62                git fsck
  63        )
  64'
  65
  66test_expect_success '"add" worktree from a subdir' '
  67        (
  68                mkdir sub &&
  69                cd sub &&
  70                git worktree add --detach here master &&
  71                cd here &&
  72                test_cmp ../../init.t init.t
  73        )
  74'
  75
  76test_expect_success '"add" from a linked checkout' '
  77        (
  78                cd here &&
  79                git worktree add --detach nested-here master &&
  80                cd nested-here &&
  81                git fsck
  82        )
  83'
  84
  85test_expect_success '"add" worktree creating new branch' '
  86        git worktree add -b newmaster there master &&
  87        (
  88                cd there &&
  89                test_cmp ../init.t init.t &&
  90                git symbolic-ref HEAD >actual &&
  91                echo refs/heads/newmaster >expect &&
  92                test_cmp expect actual &&
  93                git fsck
  94        )
  95'
  96
  97test_expect_success 'die the same branch is already checked out' '
  98        (
  99                cd here &&
 100                test_must_fail git checkout newmaster
 101        )
 102'
 103
 104test_expect_success SYMLINKS 'die the same branch is already checked out (symlink)' '
 105        head=$(git -C there rev-parse --git-path HEAD) &&
 106        ref=$(git -C there symbolic-ref HEAD) &&
 107        rm "$head" &&
 108        ln -s "$ref" "$head" &&
 109        test_must_fail git -C here checkout newmaster
 110'
 111
 112test_expect_success 'not die the same branch is already checked out' '
 113        (
 114                cd here &&
 115                git worktree add --force anothernewmaster newmaster
 116        )
 117'
 118
 119test_expect_success 'not die on re-checking out current branch' '
 120        (
 121                cd there &&
 122                git checkout newmaster
 123        )
 124'
 125
 126test_expect_success '"add" from a bare repo' '
 127        (
 128                git clone --bare . bare &&
 129                cd bare &&
 130                git worktree add -b bare-master ../there2 master
 131        )
 132'
 133
 134test_expect_success 'checkout from a bare repo without "add"' '
 135        (
 136                cd bare &&
 137                test_must_fail git checkout master
 138        )
 139'
 140
 141test_expect_success '"add" default branch of a bare repo' '
 142        (
 143                git clone --bare . bare2 &&
 144                cd bare2 &&
 145                git worktree add ../there3 master
 146        )
 147'
 148
 149test_expect_success 'checkout with grafts' '
 150        test_when_finished rm .git/info/grafts &&
 151        test_commit abc &&
 152        SHA1=$(git rev-parse HEAD) &&
 153        test_commit def &&
 154        test_commit xyz &&
 155        echo "$(git rev-parse HEAD) $SHA1" >.git/info/grafts &&
 156        cat >expected <<-\EOF &&
 157        xyz
 158        abc
 159        EOF
 160        git log --format=%s -2 >actual &&
 161        test_cmp expected actual &&
 162        git worktree add --detach grafted master &&
 163        git --git-dir=grafted/.git log --format=%s -2 >actual &&
 164        test_cmp expected actual
 165'
 166
 167test_expect_success '"add" from relative HEAD' '
 168        test_commit a &&
 169        test_commit b &&
 170        test_commit c &&
 171        git rev-parse HEAD~1 >expected &&
 172        git worktree add relhead HEAD~1 &&
 173        git -C relhead rev-parse HEAD >actual &&
 174        test_cmp expected actual
 175'
 176
 177test_expect_success '"add -b" with <branch> omitted' '
 178        git worktree add -b burble flornk &&
 179        test_cmp_rev HEAD burble
 180'
 181
 182test_expect_success '"add --detach" with <branch> omitted' '
 183        git worktree add --detach fishhook &&
 184        git rev-parse HEAD >expected &&
 185        git -C fishhook rev-parse HEAD >actual &&
 186        test_cmp expected actual &&
 187        test_must_fail git -C fishhook symbolic-ref HEAD
 188'
 189
 190test_expect_success '"add" with <branch> omitted' '
 191        git worktree add wiffle/bat &&
 192        test_cmp_rev HEAD bat
 193'
 194
 195test_expect_success '"add" auto-vivify does not clobber existing branch' '
 196        test_commit c1 &&
 197        test_commit c2 &&
 198        git branch precious HEAD~1 &&
 199        test_must_fail git worktree add precious &&
 200        test_cmp_rev HEAD~1 precious &&
 201        test_path_is_missing precious
 202'
 203
 204test_expect_success '"add" no auto-vivify with --detach and <branch> omitted' '
 205        git worktree add --detach mish/mash &&
 206        test_must_fail git rev-parse mash -- &&
 207        test_must_fail git -C mish/mash symbolic-ref HEAD
 208'
 209
 210test_expect_success '"add" -b/-B mutually exclusive' '
 211        test_must_fail git worktree add -b poodle -B poodle bamboo master
 212'
 213
 214test_expect_success '"add" -b/--detach mutually exclusive' '
 215        test_must_fail git worktree add -b poodle --detach bamboo master
 216'
 217
 218test_expect_success '"add" -B/--detach mutually exclusive' '
 219        test_must_fail git worktree add -B poodle --detach bamboo master
 220'
 221
 222test_expect_success '"add -B" fails if the branch is checked out' '
 223        git rev-parse newmaster >before &&
 224        test_must_fail git worktree add -B newmaster bamboo master &&
 225        git rev-parse newmaster >after &&
 226        test_cmp before after
 227'
 228
 229test_expect_success 'add -B' '
 230        git worktree add -B poodle bamboo2 master^ &&
 231        git -C bamboo2 symbolic-ref HEAD >actual &&
 232        echo refs/heads/poodle >expected &&
 233        test_cmp expected actual &&
 234        test_cmp_rev master^ poodle
 235'
 236
 237test_expect_success 'local clone from linked checkout' '
 238        git clone --local here here-clone &&
 239        ( cd here-clone && git fsck )
 240'
 241
 242test_expect_success '"add" worktree with --no-checkout' '
 243        git worktree add --no-checkout -b swamp swamp &&
 244        ! test -e swamp/init.t &&
 245        git -C swamp reset --hard &&
 246        test_cmp init.t swamp/init.t
 247'
 248
 249test_expect_success '"add" worktree with --checkout' '
 250        git worktree add --checkout -b swmap2 swamp2 &&
 251        test_cmp init.t swamp2/init.t
 252'
 253
 254test_expect_success 'put a worktree under rebase' '
 255        git worktree add under-rebase &&
 256        (
 257                cd under-rebase &&
 258                set_fake_editor &&
 259                FAKE_LINES="edit 1" git rebase -i HEAD^ &&
 260                git worktree list | grep "under-rebase.*detached HEAD"
 261        )
 262'
 263
 264test_expect_success 'add a worktree, checking out a rebased branch' '
 265        test_must_fail git worktree add new-rebase under-rebase &&
 266        ! test -d new-rebase
 267'
 268
 269test_expect_success 'checking out a rebased branch from another worktree' '
 270        git worktree add new-place &&
 271        test_must_fail git -C new-place checkout under-rebase
 272'
 273
 274test_expect_success 'not allow to delete a branch under rebase' '
 275        (
 276                cd under-rebase &&
 277                test_must_fail git branch -D under-rebase
 278        )
 279'
 280
 281test_expect_success 'rename a branch under rebase not allowed' '
 282        test_must_fail git branch -M under-rebase rebase-with-new-name
 283'
 284
 285test_expect_success 'check out from current worktree branch ok' '
 286        (
 287                cd under-rebase &&
 288                git checkout under-rebase &&
 289                git checkout - &&
 290                git rebase --abort
 291        )
 292'
 293
 294test_expect_success 'checkout a branch under bisect' '
 295        git worktree add under-bisect &&
 296        (
 297                cd under-bisect &&
 298                git bisect start &&
 299                git bisect bad &&
 300                git bisect good HEAD~2 &&
 301                git worktree list | grep "under-bisect.*detached HEAD" &&
 302                test_must_fail git worktree add new-bisect under-bisect &&
 303                ! test -d new-bisect
 304        )
 305'
 306
 307test_expect_success 'rename a branch under bisect not allowed' '
 308        test_must_fail git branch -M under-bisect bisect-with-new-name
 309'
 310
 311test_done