bisect: peel annotated tags to commits
[git] / t / t2402-worktree-list.sh
1 #!/bin/sh
2
3 test_description='test git worktree list'
4
5 . ./test-lib.sh
6
7 test_expect_success 'setup' '
8         test_commit init
9 '
10
11 test_expect_success 'rev-parse --git-common-dir on main worktree' '
12         git rev-parse --git-common-dir >actual &&
13         echo .git >expected &&
14         test_cmp expected actual &&
15         mkdir sub &&
16         git -C sub rev-parse --git-common-dir >actual2 &&
17         echo ../.git >expected2 &&
18         test_cmp expected2 actual2
19 '
20
21 test_expect_success 'rev-parse --git-path objects linked worktree' '
22         echo "$(git rev-parse --show-toplevel)/.git/objects" >expect &&
23         test_when_finished "rm -rf linked-tree actual expect && git worktree prune" &&
24         git worktree add --detach linked-tree master &&
25         git -C linked-tree rev-parse --git-path objects >actual &&
26         test_cmp expect actual
27 '
28
29 test_expect_success '"list" all worktrees from main' '
30         echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect &&
31         test_when_finished "rm -rf here out actual expect && git worktree prune" &&
32         git worktree add --detach here master &&
33         echo "$(git -C here rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >>expect &&
34         git worktree list >out &&
35         sed "s/  */ /g" <out >actual &&
36         test_cmp expect actual
37 '
38
39 test_expect_success '"list" all worktrees from linked' '
40         echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect &&
41         test_when_finished "rm -rf here out actual expect && git worktree prune" &&
42         git worktree add --detach here master &&
43         echo "$(git -C here rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >>expect &&
44         git -C here worktree list >out &&
45         sed "s/  */ /g" <out >actual &&
46         test_cmp expect actual
47 '
48
49 test_expect_success '"list" all worktrees --porcelain' '
50         echo "worktree $(git rev-parse --show-toplevel)" >expect &&
51         echo "HEAD $(git rev-parse HEAD)" >>expect &&
52         echo "branch $(git symbolic-ref HEAD)" >>expect &&
53         echo >>expect &&
54         test_when_finished "rm -rf here actual expect && git worktree prune" &&
55         git worktree add --detach here master &&
56         echo "worktree $(git -C here rev-parse --show-toplevel)" >>expect &&
57         echo "HEAD $(git rev-parse HEAD)" >>expect &&
58         echo "detached" >>expect &&
59         echo >>expect &&
60         git worktree list --porcelain >actual &&
61         test_cmp expect actual
62 '
63
64 test_expect_success '"list" all worktrees with locked annotation' '
65         test_when_finished "rm -rf locked unlocked out && git worktree prune" &&
66         git worktree add --detach locked master &&
67         git worktree add --detach unlocked master &&
68         git worktree lock locked &&
69         git worktree list >out &&
70         grep "/locked  *[0-9a-f].* locked$" out &&
71         ! grep "/unlocked  *[0-9a-f].* locked$" out
72 '
73
74 test_expect_success 'bare repo setup' '
75         git init --bare bare1 &&
76         echo "data" >file1 &&
77         git add file1 &&
78         git commit -m"File1: add data" &&
79         git push bare1 master &&
80         git reset --hard HEAD^
81 '
82
83 test_expect_success '"list" all worktrees from bare main' '
84         test_when_finished "rm -rf there out actual expect && git -C bare1 worktree prune" &&
85         git -C bare1 worktree add --detach ../there master &&
86         echo "$(pwd)/bare1 (bare)" >expect &&
87         echo "$(git -C there rev-parse --show-toplevel) $(git -C there rev-parse --short HEAD) (detached HEAD)" >>expect &&
88         git -C bare1 worktree list >out &&
89         sed "s/  */ /g" <out >actual &&
90         test_cmp expect actual
91 '
92
93 test_expect_success '"list" all worktrees --porcelain from bare main' '
94         test_when_finished "rm -rf there actual expect && git -C bare1 worktree prune" &&
95         git -C bare1 worktree add --detach ../there master &&
96         echo "worktree $(pwd)/bare1" >expect &&
97         echo "bare" >>expect &&
98         echo >>expect &&
99         echo "worktree $(git -C there rev-parse --show-toplevel)" >>expect &&
100         echo "HEAD $(git -C there rev-parse HEAD)" >>expect &&
101         echo "detached" >>expect &&
102         echo >>expect &&
103         git -C bare1 worktree list --porcelain >actual &&
104         test_cmp expect actual
105 '
106
107 test_expect_success '"list" all worktrees from linked with a bare main' '
108         test_when_finished "rm -rf there out actual expect && git -C bare1 worktree prune" &&
109         git -C bare1 worktree add --detach ../there master &&
110         echo "$(pwd)/bare1 (bare)" >expect &&
111         echo "$(git -C there rev-parse --show-toplevel) $(git -C there rev-parse --short HEAD) (detached HEAD)" >>expect &&
112         git -C there worktree list >out &&
113         sed "s/  */ /g" <out >actual &&
114         test_cmp expect actual
115 '
116
117 test_expect_success 'bare repo cleanup' '
118         rm -rf bare1
119 '
120
121 test_expect_success 'broken main worktree still at the top' '
122         git init broken-main &&
123         (
124                 cd broken-main &&
125                 test_commit new &&
126                 git worktree add linked &&
127                 cat >expected <<-EOF &&
128                 worktree $(pwd)
129                 HEAD $ZERO_OID
130
131                 EOF
132                 cd linked &&
133                 echo "worktree $(pwd)" >expected &&
134                 echo "ref: .broken" >../.git/HEAD &&
135                 git worktree list --porcelain >out &&
136                 head -n 3 out >actual &&
137                 test_cmp ../expected actual &&
138                 git worktree list >out &&
139                 head -n 1 out >actual.2 &&
140                 grep -F "(error)" actual.2
141         )
142 '
143
144 test_expect_success 'linked worktrees are sorted' '
145         mkdir sorted &&
146         git init sorted/main &&
147         (
148                 cd sorted/main &&
149                 test_tick &&
150                 test_commit new &&
151                 git worktree add ../first &&
152                 git worktree add ../second &&
153                 git worktree list --porcelain >out &&
154                 grep ^worktree out >actual
155         ) &&
156         cat >expected <<-EOF &&
157         worktree $(pwd)/sorted/main
158         worktree $(pwd)/sorted/first
159         worktree $(pwd)/sorted/second
160         EOF
161         test_cmp expected sorted/main/actual
162 '
163
164 test_expect_success 'worktree path when called in .git directory' '
165         git worktree list >list1 &&
166         git -C .git worktree list >list2 &&
167         test_cmp list1 list2
168 '
169
170 test_done