worktree: new place for "git prune --worktrees"
[git] / t / t2025-checkout-to.sh
1 #!/bin/sh
2
3 test_description='test git checkout --to'
4
5 . ./test-lib.sh
6
7 test_expect_success 'setup' '
8         test_commit init
9 '
10
11 test_expect_success 'checkout --to not updating paths' '
12         test_must_fail git checkout --to -- init.t
13 '
14
15 test_expect_success 'checkout --to an existing worktree' '
16         mkdir -p existing/subtree &&
17         test_must_fail git checkout --detach --to existing master
18 '
19
20 test_expect_success 'checkout --to an existing empty worktree' '
21         mkdir existing_empty &&
22         git checkout --detach --to existing_empty master
23 '
24
25 test_expect_success 'checkout --to refuses to checkout locked branch' '
26         test_must_fail git checkout --to zere master &&
27         ! test -d zere &&
28         ! test -d .git/worktrees/zere
29 '
30
31 test_expect_success 'checking out paths not complaining about linked checkouts' '
32         (
33         cd existing_empty &&
34         echo dirty >>init.t &&
35         git checkout master -- init.t
36         )
37 '
38
39 test_expect_success 'checkout --to a new worktree' '
40         git rev-parse HEAD >expect &&
41         git checkout --detach --to here master &&
42         (
43                 cd here &&
44                 test_cmp ../init.t init.t &&
45                 test_must_fail git symbolic-ref HEAD &&
46                 git rev-parse HEAD >actual &&
47                 test_cmp ../expect actual &&
48                 git fsck
49         )
50 '
51
52 test_expect_success 'checkout --to a new worktree from a subdir' '
53         (
54                 mkdir sub &&
55                 cd sub &&
56                 git checkout --detach --to here master &&
57                 cd here &&
58                 test_cmp ../../init.t init.t
59         )
60 '
61
62 test_expect_success 'checkout --to from a linked checkout' '
63         (
64                 cd here &&
65                 git checkout --detach --to nested-here master &&
66                 cd nested-here &&
67                 git fsck
68         )
69 '
70
71 test_expect_success 'checkout --to a new worktree creating new branch' '
72         git checkout --to there -b newmaster master &&
73         (
74                 cd there &&
75                 test_cmp ../init.t init.t &&
76                 git symbolic-ref HEAD >actual &&
77                 echo refs/heads/newmaster >expect &&
78                 test_cmp expect actual &&
79                 git fsck
80         )
81 '
82
83 test_expect_success 'die the same branch is already checked out' '
84         (
85                 cd here &&
86                 test_must_fail git checkout newmaster
87         )
88 '
89
90 test_expect_success 'not die the same branch is already checked out' '
91         (
92                 cd here &&
93                 git checkout --ignore-other-worktrees --to anothernewmaster newmaster
94         )
95 '
96
97 test_expect_success 'not die on re-checking out current branch' '
98         (
99                 cd there &&
100                 git checkout newmaster
101         )
102 '
103
104 test_expect_success 'checkout --to from a bare repo' '
105         (
106                 git clone --bare . bare &&
107                 cd bare &&
108                 git checkout --to ../there2 -b bare-master master
109         )
110 '
111
112 test_expect_success 'checkout from a bare repo without --to' '
113         (
114                 cd bare &&
115                 test_must_fail git checkout master
116         )
117 '
118
119 test_expect_success 'checkout with grafts' '
120         test_when_finished rm .git/info/grafts &&
121         test_commit abc &&
122         SHA1=`git rev-parse HEAD` &&
123         test_commit def &&
124         test_commit xyz &&
125         echo "`git rev-parse HEAD` $SHA1" >.git/info/grafts &&
126         cat >expected <<-\EOF &&
127         xyz
128         abc
129         EOF
130         git log --format=%s -2 >actual &&
131         test_cmp expected actual &&
132         git checkout --detach --to grafted master &&
133         git --git-dir=grafted/.git log --format=%s -2 >actual &&
134         test_cmp expected actual
135 '
136
137 test_done