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