]> git.ipfire.org Git - thirdparty/git.git/blame - t/t2403-worktree-move.sh
The sixth batch
[thirdparty/git.git] / t / t2403-worktree-move.sh
CommitLineData
58142c09
NTND
1#!/bin/sh
2
3test_description='test git worktree move, remove, lock and unlock'
4
d40c42e0 5TEST_PASSES_SANITIZE_LEAK=true
58142c09
NTND
6. ./test-lib.sh
7
8test_expect_success 'setup' '
9 test_commit init &&
10 git worktree add source &&
7f19def0
ES
11 git worktree list --porcelain >out &&
12 grep "^worktree" out >actual &&
58142c09
NTND
13 cat <<-EOF >expected &&
14 worktree $(pwd)
15 worktree $(pwd)/source
16 EOF
17 test_cmp expected actual
18'
19
20test_expect_success 'lock main worktree' '
21 test_must_fail git worktree lock .
22'
23
24test_expect_success 'lock linked worktree' '
25 git worktree lock --reason hahaha source &&
26 echo hahaha >expected &&
27 test_cmp expected .git/worktrees/source/locked
28'
29
30test_expect_success 'lock linked worktree from another worktree' '
31 rm .git/worktrees/source/locked &&
32 git worktree add elsewhere &&
33 git -C elsewhere worktree lock --reason hahaha ../source &&
34 echo hahaha >expected &&
35 test_cmp expected .git/worktrees/source/locked
36'
37
38test_expect_success 'lock worktree twice' '
39 test_must_fail git worktree lock source &&
40 echo hahaha >expected &&
41 test_cmp expected .git/worktrees/source/locked
42'
43
44test_expect_success 'lock worktree twice (from the locked worktree)' '
45 test_must_fail git -C source worktree lock . &&
46 echo hahaha >expected &&
47 test_cmp expected .git/worktrees/source/locked
48'
49
6d308627
NTND
50test_expect_success 'unlock main worktree' '
51 test_must_fail git worktree unlock .
52'
53
54test_expect_success 'unlock linked worktree' '
55 git worktree unlock source &&
56 test_path_is_missing .git/worktrees/source/locked
57'
58
59test_expect_success 'unlock worktree twice' '
60 test_must_fail git worktree unlock source &&
61 test_path_is_missing .git/worktrees/source/locked
62'
63
9f792bb4
NTND
64test_expect_success 'move non-worktree' '
65 mkdir abc &&
66 test_must_fail git worktree move abc def
67'
68
69test_expect_success 'move locked worktree' '
70 git worktree lock source &&
71 test_when_finished "git worktree unlock source" &&
72 test_must_fail git worktree move source destination
73'
74
75test_expect_success 'move worktree' '
9f792bb4
NTND
76 git worktree move source destination &&
77 test_path_is_missing source &&
7f19def0 78 git worktree list --porcelain >out &&
b1801b85
ES
79 grep "^worktree.*/destination$" out &&
80 ! grep "^worktree.*/source$" out &&
9f792bb4
NTND
81 git -C destination log --format=%s >actual2 &&
82 echo init >expected2 &&
83 test_cmp expected2 actual2
84'
85
86test_expect_success 'move main worktree' '
87 test_must_fail git worktree move . def
88'
89
c64a8d20 90test_expect_success 'move worktree to another dir' '
c64a8d20
NTND
91 mkdir some-dir &&
92 git worktree move destination some-dir &&
7f19def0
ES
93 test_when_finished "git worktree move some-dir/destination destination" &&
94 test_path_is_missing destination &&
95 git worktree list --porcelain >out &&
b1801b85 96 grep "^worktree.*/some-dir/destination$" out &&
c64a8d20
NTND
97 git -C some-dir/destination log --format=%s >actual2 &&
98 echo init >expected2 &&
99 test_cmp expected2 actual2
100'
101
68a6b3a1
ES
102test_expect_success 'move locked worktree (force)' '
103 test_when_finished "
104 git worktree unlock flump || :
105 git worktree remove flump || :
106 git worktree unlock ploof || :
107 git worktree remove ploof || :
108 " &&
109 git worktree add --detach flump &&
110 git worktree lock flump &&
111 test_must_fail git worktree move flump ploof" &&
112 test_must_fail git worktree move --force flump ploof" &&
113 git worktree move --force --force flump ploof
114'
115
810382ed
ES
116test_expect_success 'refuse to move worktree atop existing path' '
117 >bobble &&
118 git worktree add --detach beeble &&
119 test_must_fail git worktree move beeble bobble
120'
121
122test_expect_success 'move atop existing but missing worktree' '
123 git worktree add --detach gnoo &&
124 git worktree add --detach pneu &&
125 rm -fr pneu &&
126 test_must_fail git worktree move gnoo pneu &&
127 git worktree move --force gnoo pneu &&
128
129 git worktree add --detach nu &&
130 git worktree lock nu &&
131 rm -fr nu &&
132 test_must_fail git worktree move pneu nu &&
133 test_must_fail git worktree --force move pneu nu &&
134 git worktree move --force --force pneu nu
135'
136
00a6d4d1
NTND
137test_expect_success 'move a repo with uninitialized submodule' '
138 git init withsub &&
139 (
140 cd withsub &&
141 test_commit initial &&
99f4abb8
TB
142 git -c protocol.file.allow=always \
143 submodule add "$PWD"/.git sub &&
00a6d4d1
NTND
144 git commit -m withsub &&
145 git worktree add second HEAD &&
146 git worktree move second third
147 )
148'
149
150test_expect_success 'not move a repo with initialized submodule' '
151 (
152 cd withsub &&
99f4abb8 153 git -c protocol.file.allow=always -C third submodule update &&
00a6d4d1
NTND
154 test_must_fail git worktree move third forth
155 )
156'
157
cc73385c
NTND
158test_expect_success 'remove main worktree' '
159 test_must_fail git worktree remove .
160'
161
cc73385c
NTND
162test_expect_success 'remove locked worktree' '
163 git worktree lock destination &&
164 test_when_finished "git worktree unlock destination" &&
165 test_must_fail git worktree remove destination
166'
167
168test_expect_success 'remove worktree with dirty tracked file' '
169 echo dirty >>destination/init.t &&
170 test_when_finished "git -C destination checkout init.t" &&
171 test_must_fail git worktree remove destination
172'
173
174test_expect_success 'remove worktree with untracked file' '
175 : >destination/untracked &&
176 test_must_fail git worktree remove destination
177'
178
179test_expect_success 'force remove worktree with untracked file' '
180 git worktree remove --force destination &&
181 test_path_is_missing destination
182'
183
ee6763af
NTND
184test_expect_success 'remove missing worktree' '
185 git worktree add to-be-gone &&
186 test -d .git/worktrees/to-be-gone &&
187 mv to-be-gone gone &&
188 git worktree remove to-be-gone &&
189 test_path_is_missing .git/worktrees/to-be-gone
190'
191
192test_expect_success 'NOT remove missing-but-locked worktree' '
193 git worktree add gone-but-locked &&
194 git worktree lock gone-but-locked &&
195 test -d .git/worktrees/gone-but-locked &&
196 mv gone-but-locked really-gone-now &&
197 test_must_fail git worktree remove gone-but-locked &&
198 test_path_is_dir .git/worktrees/gone-but-locked
199'
200
4c5fa9e6
ES
201test_expect_success 'proper error when worktree not found' '
202 for i in noodle noodle/bork
203 do
204 test_must_fail git worktree lock $i 2>err &&
6789275d 205 test_grep "not a working tree" err || return 1
4c5fa9e6
ES
206 done
207'
208
f4143101
ES
209test_expect_success 'remove locked worktree (force)' '
210 git worktree add --detach gumby &&
211 test_when_finished "git worktree remove gumby || :" &&
212 git worktree lock gumby &&
213 test_when_finished "git worktree unlock gumby || :" &&
214 test_must_fail git worktree remove gumby &&
215 test_must_fail git worktree remove --force gumby &&
216 git worktree remove --force --force gumby
217'
218
3a540433
ES
219test_expect_success 'remove cleans up .git/worktrees when empty' '
220 git init moog &&
221 (
222 cd moog &&
223 test_commit bim &&
224 git worktree add --detach goom &&
225 test_path_exists .git/worktrees &&
226 git worktree remove goom &&
227 test_path_is_missing .git/worktrees
228 )
229'
230
00a6d4d1 231test_expect_success 'remove a repo with uninitialized submodule' '
99f4abb8 232 test_config_global protocol.file.allow always &&
00a6d4d1
NTND
233 (
234 cd withsub &&
235 git worktree add to-remove HEAD &&
236 git worktree remove to-remove
237 )
238'
239
240test_expect_success 'not remove a repo with initialized submodule' '
99f4abb8 241 test_config_global protocol.file.allow always &&
00a6d4d1
NTND
242 (
243 cd withsub &&
244 git worktree add to-remove HEAD &&
245 git -C to-remove submodule update &&
246 test_must_fail git worktree remove to-remove
247 )
248'
249
58142c09 250test_done