Merge branch 'jk/interpret-branch-name'
[git] / t / t2027-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 && 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 && 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 | sed "s/  */ /g" >actual &&
35         test_cmp expect actual
36 '
37
38 test_expect_success '"list" all worktrees from linked' '
39         echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect &&
40         test_when_finished "rm -rf here && git worktree prune" &&
41         git worktree add --detach here master &&
42         echo "$(git -C here rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >>expect &&
43         git -C here worktree list | sed "s/  */ /g" >actual &&
44         test_cmp expect actual
45 '
46
47 test_expect_success '"list" all worktrees --porcelain' '
48         echo "worktree $(git rev-parse --show-toplevel)" >expect &&
49         echo "HEAD $(git rev-parse HEAD)" >>expect &&
50         echo "branch $(git symbolic-ref HEAD)" >>expect &&
51         echo >>expect &&
52         test_when_finished "rm -rf here && git worktree prune" &&
53         git worktree add --detach here master &&
54         echo "worktree $(git -C here rev-parse --show-toplevel)" >>expect &&
55         echo "HEAD $(git rev-parse HEAD)" >>expect &&
56         echo "detached" >>expect &&
57         echo >>expect &&
58         git worktree list --porcelain >actual &&
59         test_cmp expect actual
60 '
61
62 test_expect_success 'bare repo setup' '
63         git init --bare bare1 &&
64         echo "data" >file1 &&
65         git add file1 &&
66         git commit -m"File1: add data" &&
67         git push bare1 master &&
68         git reset --hard HEAD^
69 '
70
71 test_expect_success '"list" all worktrees from bare main' '
72         test_when_finished "rm -rf there && git -C bare1 worktree prune" &&
73         git -C bare1 worktree add --detach ../there master &&
74         echo "$(pwd)/bare1 (bare)" >expect &&
75         echo "$(git -C there rev-parse --show-toplevel) $(git -C there rev-parse --short HEAD) (detached HEAD)" >>expect &&
76         git -C bare1 worktree list | sed "s/  */ /g" >actual &&
77         test_cmp expect actual
78 '
79
80 test_expect_success '"list" all worktrees --porcelain from bare main' '
81         test_when_finished "rm -rf there && git -C bare1 worktree prune" &&
82         git -C bare1 worktree add --detach ../there master &&
83         echo "worktree $(pwd)/bare1" >expect &&
84         echo "bare" >>expect &&
85         echo >>expect &&
86         echo "worktree $(git -C there rev-parse --show-toplevel)" >>expect &&
87         echo "HEAD $(git -C there rev-parse HEAD)" >>expect &&
88         echo "detached" >>expect &&
89         echo >>expect &&
90         git -C bare1 worktree list --porcelain >actual &&
91         test_cmp expect actual
92 '
93
94 test_expect_success '"list" all worktrees from linked with a bare main' '
95         test_when_finished "rm -rf there && git -C bare1 worktree prune" &&
96         git -C bare1 worktree add --detach ../there master &&
97         echo "$(pwd)/bare1 (bare)" >expect &&
98         echo "$(git -C there rev-parse --show-toplevel) $(git -C there rev-parse --short HEAD) (detached HEAD)" >>expect &&
99         git -C there worktree list | sed "s/  */ /g" >actual &&
100         test_cmp expect actual
101 '
102
103 test_expect_success 'bare repo cleanup' '
104         rm -rf bare1
105 '
106
107 test_expect_success 'broken main worktree still at the top' '
108         git init broken-main &&
109         (
110                 cd broken-main &&
111                 test_commit new &&
112                 git worktree add linked &&
113                 cat >expected <<-EOF &&
114                 worktree $(pwd)
115                 HEAD $_z40
116
117                 EOF
118                 cd linked &&
119                 echo "worktree $(pwd)" >expected &&
120                 echo "ref: .broken" >../.git/HEAD &&
121                 git worktree list --porcelain | head -n 3 >actual &&
122                 test_cmp ../expected actual &&
123                 git worktree list | head -n 1 >actual.2 &&
124                 grep -F "(error)" actual.2
125         )
126 '
127
128 test_expect_success 'linked worktrees are sorted' '
129         mkdir sorted &&
130         git init sorted/main &&
131         (
132                 cd sorted/main &&
133                 test_tick &&
134                 test_commit new &&
135                 git worktree add ../first &&
136                 git worktree add ../second &&
137                 git worktree list --porcelain | grep ^worktree >actual
138         ) &&
139         cat >expected <<-EOF &&
140         worktree $(pwd)/sorted/main
141         worktree $(pwd)/sorted/first
142         worktree $(pwd)/sorted/second
143         EOF
144         test_cmp expected sorted/main/actual
145 '
146
147 test_done