3 test_description='test git worktree list'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10 test_expect_success 'setup' '
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 &&
19 git -C sub rev-parse --git-common-dir >actual2 &&
20 echo ../.git >expected2 &&
21 test_cmp expected2 actual2
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
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
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
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 &&
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 &&
63 git worktree list --porcelain >actual &&
64 test_cmp expect actual
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
77 test_expect_success 'bare repo setup' '
78 git init --bare bare1 &&
81 git commit -m"File1: add data" &&
82 git push bare1 main &&
83 git reset --hard HEAD^
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
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 &&
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 &&
106 git -C bare1 worktree list --porcelain >actual &&
107 test_cmp expect actual
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
120 test_expect_success 'bare repo cleanup' '
124 test_expect_success 'broken main worktree still at the top' '
125 git init broken-main &&
129 git worktree add linked &&
130 cat >expected <<-EOF &&
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
147 test_expect_success 'linked worktrees are sorted' '
149 git init sorted/main &&
154 git worktree add ../first &&
155 git worktree add ../second &&
156 git worktree list --porcelain >out &&
157 grep ^worktree out >actual
159 cat >expected <<-EOF &&
160 worktree $(pwd)/sorted/main
161 worktree $(pwd)/sorted/first
162 worktree $(pwd)/sorted/second
164 test_cmp expected sorted/main/actual
167 test_expect_success 'worktree path when called in .git directory' '
168 git worktree list >list1 &&
169 git -C .git worktree list >list2 &&