t / t2025-worktree-add.shon commit Merge branch 'va/git-gui-i18n' (cb3debd)
   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 'checkout with grafts' '
 142        test_when_finished rm .git/info/grafts &&
 143        test_commit abc &&
 144        SHA1=$(git rev-parse HEAD) &&
 145        test_commit def &&
 146        test_commit xyz &&
 147        echo "$(git rev-parse HEAD) $SHA1" >.git/info/grafts &&
 148        cat >expected <<-\EOF &&
 149        xyz
 150        abc
 151        EOF
 152        git log --format=%s -2 >actual &&
 153        test_cmp expected actual &&
 154        git worktree add --detach grafted master &&
 155        git --git-dir=grafted/.git log --format=%s -2 >actual &&
 156        test_cmp expected actual
 157'
 158
 159test_expect_success '"add" from relative HEAD' '
 160        test_commit a &&
 161        test_commit b &&
 162        test_commit c &&
 163        git rev-parse HEAD~1 >expected &&
 164        git worktree add relhead HEAD~1 &&
 165        git -C relhead rev-parse HEAD >actual &&
 166        test_cmp expected actual
 167'
 168
 169test_expect_success '"add -b" with <branch> omitted' '
 170        git worktree add -b burble flornk &&
 171        test_cmp_rev HEAD burble
 172'
 173
 174test_expect_success '"add --detach" with <branch> omitted' '
 175        git worktree add --detach fishhook &&
 176        git rev-parse HEAD >expected &&
 177        git -C fishhook rev-parse HEAD >actual &&
 178        test_cmp expected actual &&
 179        test_must_fail git -C fishhook symbolic-ref HEAD
 180'
 181
 182test_expect_success '"add" with <branch> omitted' '
 183        git worktree add wiffle/bat &&
 184        test_cmp_rev HEAD bat
 185'
 186
 187test_expect_success '"add" auto-vivify does not clobber existing branch' '
 188        test_commit c1 &&
 189        test_commit c2 &&
 190        git branch precious HEAD~1 &&
 191        test_must_fail git worktree add precious &&
 192        test_cmp_rev HEAD~1 precious &&
 193        test_path_is_missing precious
 194'
 195
 196test_expect_success '"add" no auto-vivify with --detach and <branch> omitted' '
 197        git worktree add --detach mish/mash &&
 198        test_must_fail git rev-parse mash -- &&
 199        test_must_fail git -C mish/mash symbolic-ref HEAD
 200'
 201
 202test_expect_success '"add" -b/-B mutually exclusive' '
 203        test_must_fail git worktree add -b poodle -B poodle bamboo master
 204'
 205
 206test_expect_success '"add" -b/--detach mutually exclusive' '
 207        test_must_fail git worktree add -b poodle --detach bamboo master
 208'
 209
 210test_expect_success '"add" -B/--detach mutually exclusive' '
 211        test_must_fail git worktree add -B poodle --detach bamboo master
 212'
 213
 214test_expect_success '"add -B" fails if the branch is checked out' '
 215        git rev-parse newmaster >before &&
 216        test_must_fail git worktree add -B newmaster bamboo master &&
 217        git rev-parse newmaster >after &&
 218        test_cmp before after
 219'
 220
 221test_expect_success 'add -B' '
 222        git worktree add -B poodle bamboo2 master^ &&
 223        git -C bamboo2 symbolic-ref HEAD >actual &&
 224        echo refs/heads/poodle >expected &&
 225        test_cmp expected actual &&
 226        test_cmp_rev master^ poodle
 227'
 228
 229test_expect_success 'local clone from linked checkout' '
 230        git clone --local here here-clone &&
 231        ( cd here-clone && git fsck )
 232'
 233
 234test_expect_success '"add" worktree with --no-checkout' '
 235        git worktree add --no-checkout -b swamp swamp &&
 236        ! test -e swamp/init.t &&
 237        git -C swamp reset --hard &&
 238        test_cmp init.t swamp/init.t
 239'
 240
 241test_expect_success '"add" worktree with --checkout' '
 242        git worktree add --checkout -b swmap2 swamp2 &&
 243        test_cmp init.t swamp2/init.t
 244'
 245
 246test_expect_success 'put a worktree under rebase' '
 247        git worktree add under-rebase &&
 248        (
 249                cd under-rebase &&
 250                set_fake_editor &&
 251                FAKE_LINES="edit 1" git rebase -i HEAD^ &&
 252                git worktree list | grep "under-rebase.*detached HEAD"
 253        )
 254'
 255
 256test_expect_success 'add a worktree, checking out a rebased branch' '
 257        test_must_fail git worktree add new-rebase under-rebase &&
 258        ! test -d new-rebase
 259'
 260
 261test_expect_success 'checking out a rebased branch from another worktree' '
 262        git worktree add new-place &&
 263        test_must_fail git -C new-place checkout under-rebase
 264'
 265
 266test_expect_success 'not allow to delete a branch under rebase' '
 267        (
 268                cd under-rebase &&
 269                test_must_fail git branch -D under-rebase
 270        )
 271'
 272
 273test_expect_success 'rename a branch under rebase not allowed' '
 274        test_must_fail git branch -M under-rebase rebase-with-new-name
 275'
 276
 277test_expect_success 'check out from current worktree branch ok' '
 278        (
 279                cd under-rebase &&
 280                git checkout under-rebase &&
 281                git checkout - &&
 282                git rebase --abort
 283        )
 284'
 285
 286test_expect_success 'checkout a branch under bisect' '
 287        git worktree add under-bisect &&
 288        (
 289                cd under-bisect &&
 290                git bisect start &&
 291                git bisect bad &&
 292                git bisect good HEAD~2 &&
 293                git worktree list | grep "under-bisect.*detached HEAD" &&
 294                test_must_fail git worktree add new-bisect under-bisect &&
 295                ! test -d new-bisect
 296        )
 297'
 298
 299test_expect_success 'rename a branch under bisect not allowed' '
 300        test_must_fail git branch -M under-bisect bisect-with-new-name
 301'
 302
 303test_done