CodingGuidelines: do not ==/!= compare with 0 or '\0' or NULL
[git.git] / t / t2403-worktree-move.sh
blob939d18d7286c1be1e58a698e9164fda8e24c654a
1 #!/bin/sh
3 test_description='test git worktree move, remove, lock and unlock'
5 . ./test-lib.sh
7 test_expect_success 'setup' '
8 test_commit init &&
9 git worktree add source &&
10 git worktree list --porcelain >out &&
11 grep "^worktree" out >actual &&
12 cat <<-EOF >expected &&
13 worktree $(pwd)
14 worktree $(pwd)/source
15 EOF
16 test_cmp expected actual
19 test_expect_success 'lock main worktree' '
20 test_must_fail git worktree lock .
23 test_expect_success 'lock linked worktree' '
24 git worktree lock --reason hahaha source &&
25 echo hahaha >expected &&
26 test_cmp expected .git/worktrees/source/locked
29 test_expect_success 'lock linked worktree from another worktree' '
30 rm .git/worktrees/source/locked &&
31 git worktree add elsewhere &&
32 git -C elsewhere worktree lock --reason hahaha ../source &&
33 echo hahaha >expected &&
34 test_cmp expected .git/worktrees/source/locked
37 test_expect_success 'lock worktree twice' '
38 test_must_fail git worktree lock source &&
39 echo hahaha >expected &&
40 test_cmp expected .git/worktrees/source/locked
43 test_expect_success 'lock worktree twice (from the locked worktree)' '
44 test_must_fail git -C source worktree lock . &&
45 echo hahaha >expected &&
46 test_cmp expected .git/worktrees/source/locked
49 test_expect_success 'unlock main worktree' '
50 test_must_fail git worktree unlock .
53 test_expect_success 'unlock linked worktree' '
54 git worktree unlock source &&
55 test_path_is_missing .git/worktrees/source/locked
58 test_expect_success 'unlock worktree twice' '
59 test_must_fail git worktree unlock source &&
60 test_path_is_missing .git/worktrees/source/locked
63 test_expect_success 'move non-worktree' '
64 mkdir abc &&
65 test_must_fail git worktree move abc def
68 test_expect_success 'move locked worktree' '
69 git worktree lock source &&
70 test_when_finished "git worktree unlock source" &&
71 test_must_fail git worktree move source destination
74 test_expect_success 'move worktree' '
75 git worktree move source destination &&
76 test_path_is_missing source &&
77 git worktree list --porcelain >out &&
78 grep "^worktree.*/destination$" out &&
79 ! grep "^worktree.*/source$" out &&
80 git -C destination log --format=%s >actual2 &&
81 echo init >expected2 &&
82 test_cmp expected2 actual2
85 test_expect_success 'move main worktree' '
86 test_must_fail git worktree move . def
89 test_expect_success 'move worktree to another dir' '
90 mkdir some-dir &&
91 git worktree move destination some-dir &&
92 test_when_finished "git worktree move some-dir/destination destination" &&
93 test_path_is_missing destination &&
94 git worktree list --porcelain >out &&
95 grep "^worktree.*/some-dir/destination$" out &&
96 git -C some-dir/destination log --format=%s >actual2 &&
97 echo init >expected2 &&
98 test_cmp expected2 actual2
101 test_expect_success 'move locked worktree (force)' '
102 test_when_finished "
103 git worktree unlock flump || :
104 git worktree remove flump || :
105 git worktree unlock ploof || :
106 git worktree remove ploof || :
107 " &&
108 git worktree add --detach flump &&
109 git worktree lock flump &&
110 test_must_fail git worktree move flump ploof" &&
111 test_must_fail git worktree move --force flump ploof" &&
112 git worktree move --force --force flump ploof
115 test_expect_success 'move a repo with uninitialized submodule' '
116 git init withsub &&
118 cd withsub &&
119 test_commit initial &&
120 git submodule add "$PWD"/.git sub &&
121 git commit -m withsub &&
122 git worktree add second HEAD &&
123 git worktree move second third
127 test_expect_success 'not move a repo with initialized submodule' '
129 cd withsub &&
130 git -C third submodule update &&
131 test_must_fail git worktree move third forth
135 test_expect_success 'remove main worktree' '
136 test_must_fail git worktree remove .
139 test_expect_success 'remove locked worktree' '
140 git worktree lock destination &&
141 test_when_finished "git worktree unlock destination" &&
142 test_must_fail git worktree remove destination
145 test_expect_success 'remove worktree with dirty tracked file' '
146 echo dirty >>destination/init.t &&
147 test_when_finished "git -C destination checkout init.t" &&
148 test_must_fail git worktree remove destination
151 test_expect_success 'remove worktree with untracked file' '
152 : >destination/untracked &&
153 test_must_fail git worktree remove destination
156 test_expect_success 'force remove worktree with untracked file' '
157 git worktree remove --force destination &&
158 test_path_is_missing destination
161 test_expect_success 'remove missing worktree' '
162 git worktree add to-be-gone &&
163 test -d .git/worktrees/to-be-gone &&
164 mv to-be-gone gone &&
165 git worktree remove to-be-gone &&
166 test_path_is_missing .git/worktrees/to-be-gone
169 test_expect_success 'NOT remove missing-but-locked worktree' '
170 git worktree add gone-but-locked &&
171 git worktree lock gone-but-locked &&
172 test -d .git/worktrees/gone-but-locked &&
173 mv gone-but-locked really-gone-now &&
174 test_must_fail git worktree remove gone-but-locked &&
175 test_path_is_dir .git/worktrees/gone-but-locked
178 test_expect_success 'proper error when worktree not found' '
179 for i in noodle noodle/bork
181 test_must_fail git worktree lock $i 2>err &&
182 test_i18ngrep "not a working tree" err || return 1
183 done
186 test_expect_success 'remove locked worktree (force)' '
187 git worktree add --detach gumby &&
188 test_when_finished "git worktree remove gumby || :" &&
189 git worktree lock gumby &&
190 test_when_finished "git worktree unlock gumby || :" &&
191 test_must_fail git worktree remove gumby &&
192 test_must_fail git worktree remove --force gumby &&
193 git worktree remove --force --force gumby
196 test_expect_success 'remove cleans up .git/worktrees when empty' '
197 git init moog &&
199 cd moog &&
200 test_commit bim &&
201 git worktree add --detach goom &&
202 test_path_exists .git/worktrees &&
203 git worktree remove goom &&
204 test_path_is_missing .git/worktrees
208 test_expect_success 'remove a repo with uninitialized submodule' '
210 cd withsub &&
211 git worktree add to-remove HEAD &&
212 git worktree remove to-remove
216 test_expect_success 'not remove a repo with initialized submodule' '
218 cd withsub &&
219 git worktree add to-remove HEAD &&
220 git -C to-remove submodule update &&
221 test_must_fail git worktree remove to-remove
225 test_done