]> git.ipfire.org Git - thirdparty/git.git/blame - t/t1501-worktree.sh
t: use test_expect_code instead of hand-rolled comparison
[thirdparty/git.git] / t / t1501-worktree.sh
CommitLineData
3ae4a867
ML
1#!/bin/sh
2
3test_description='test separate work tree'
4. ./test-lib.sh
5
fc688759
JN
6test_expect_success 'setup' '
7 EMPTY_TREE=$(git write-tree) &&
8 EMPTY_BLOB=$(git hash-object -t blob --stdin </dev/null) &&
9 CHANGED_BLOB=$(echo changed | git hash-object -t blob --stdin) &&
fc688759
JN
10 EMPTY_BLOB7=$(echo $EMPTY_BLOB | sed "s/\(.......\).*/\1/") &&
11 CHANGED_BLOB7=$(echo $CHANGED_BLOB | sed "s/\(.......\).*/\1/") &&
12
13 mkdir -p work/sub/dir &&
14 mkdir -p work2 &&
15 mv .git repo.git
16'
17
18test_expect_success 'setup: helper for testing rev-parse' '
19 test_rev_parse() {
20 echo $1 >expected.bare &&
21 echo $2 >expected.inside-git &&
22 echo $3 >expected.inside-worktree &&
23 if test $# -ge 4
24 then
25 echo $4 >expected.prefix
26 fi &&
27
28 git rev-parse --is-bare-repository >actual.bare &&
29 git rev-parse --is-inside-git-dir >actual.inside-git &&
30 git rev-parse --is-inside-work-tree >actual.inside-worktree &&
31 if test $# -ge 4
32 then
33 git rev-parse --show-prefix >actual.prefix
34 fi &&
35
36 test_cmp expected.bare actual.bare &&
37 test_cmp expected.inside-git actual.inside-git &&
38 test_cmp expected.inside-worktree actual.inside-worktree &&
39 if test $# -ge 4
40 then
41 # rev-parse --show-prefix should output
42 # a single newline when at the top of the work tree,
43 # but we test for that separately.
44 test -z "$4" && ! test -s actual.prefix ||
45 test_cmp expected.prefix actual.prefix
46 fi
47 }
48'
49
50test_expect_success 'setup: core.worktree = relative path' '
7f9a5fc6 51 sane_unset GIT_WORK_TREE &&
fc688759
JN
52 GIT_DIR=repo.git &&
53 GIT_CONFIG="$(pwd)"/$GIT_DIR/config &&
54 export GIT_DIR GIT_CONFIG &&
55 git config core.worktree ../work
56'
57
58test_expect_success 'outside' '
59 test_rev_parse false false false
60'
61
62test_expect_success 'inside work tree' '
63 (
64 cd work &&
65 GIT_DIR=../repo.git &&
66 GIT_CONFIG="$(pwd)"/$GIT_DIR/config &&
67 test_rev_parse false false true ""
68 )
69'
70
658219f1 71test_expect_success 'empty prefix is actually written out' '
fc688759
JN
72 echo >expected &&
73 (
74 cd work &&
75 GIT_DIR=../repo.git &&
76 GIT_CONFIG="$(pwd)"/$GIT_DIR/config &&
77 git rev-parse --show-prefix >../actual
78 ) &&
79 test_cmp expected actual
80'
81
82test_expect_success 'subdir of work tree' '
83 (
84 cd work/sub/dir &&
85 GIT_DIR=../../../repo.git &&
86 GIT_CONFIG="$(pwd)"/$GIT_DIR/config &&
87 test_rev_parse false false true sub/dir/
88 )
89'
90
91test_expect_success 'setup: core.worktree = absolute path' '
7f9a5fc6 92 sane_unset GIT_WORK_TREE &&
fc688759
JN
93 GIT_DIR=$(pwd)/repo.git &&
94 GIT_CONFIG=$GIT_DIR/config &&
95 export GIT_DIR GIT_CONFIG &&
96 git config core.worktree "$(pwd)/work"
97'
98
99test_expect_success 'outside' '
100 test_rev_parse false false false &&
101 (
102 cd work2 &&
103 test_rev_parse false false false
104 )
105'
106
107test_expect_success 'inside work tree' '
108 (
109 cd work &&
110 test_rev_parse false false true ""
111 )
112'
113
114test_expect_success 'subdir of work tree' '
115 (
116 cd work/sub/dir &&
117 test_rev_parse false false true sub/dir/
118 )
119'
120
121test_expect_success 'setup: GIT_WORK_TREE=relative (override core.worktree)' '
122 GIT_DIR=$(pwd)/repo.git &&
123 GIT_CONFIG=$GIT_DIR/config &&
124 git config core.worktree non-existent &&
125 GIT_WORK_TREE=work &&
126 export GIT_DIR GIT_CONFIG GIT_WORK_TREE
127'
128
129test_expect_success 'outside' '
130 test_rev_parse false false false &&
131 (
132 cd work2 &&
133 test_rev_parse false false false
134 )
135'
136
137test_expect_success 'inside work tree' '
138 (
139 cd work &&
140 GIT_WORK_TREE=. &&
141 test_rev_parse false false true ""
142 )
143'
144
145test_expect_success 'subdir of work tree' '
146 (
147 cd work/sub/dir &&
148 GIT_WORK_TREE=../.. &&
149 test_rev_parse false false true sub/dir/
150 )
151'
152
153test_expect_success 'setup: GIT_WORK_TREE=absolute, below git dir' '
154 mv work repo.git/work &&
155 mv work2 repo.git/work2 &&
156 GIT_DIR=$(pwd)/repo.git &&
157 GIT_CONFIG=$GIT_DIR/config &&
158 GIT_WORK_TREE=$(pwd)/repo.git/work &&
159 export GIT_DIR GIT_CONFIG GIT_WORK_TREE
160'
161
162test_expect_success 'outside' '
163 echo outside &&
164 test_rev_parse false false false
165'
166
167test_expect_success 'in repo.git' '
168 (
169 cd repo.git &&
170 test_rev_parse false true false
171 ) &&
172 (
173 cd repo.git/objects &&
174 test_rev_parse false true false
175 ) &&
176 (
177 cd repo.git/work2 &&
178 test_rev_parse false true false
179 )
180'
181
182test_expect_success 'inside work tree' '
183 (
184 cd repo.git/work &&
185 test_rev_parse false true true ""
186 )
187'
188
189test_expect_success 'subdir of work tree' '
190 (
191 cd repo.git/work/sub/dir &&
192 test_rev_parse false true true sub/dir/
193 )
194'
195
196test_expect_success 'find work tree from repo' '
197 echo sub/dir/untracked >expected &&
198 cat <<-\EOF >repo.git/work/.gitignore &&
199 expected.*
200 actual.*
201 .gitignore
202 EOF
203 >repo.git/work/sub/dir/untracked &&
204 (
205 cd repo.git &&
206 git ls-files --others --exclude-standard >../actual
207 ) &&
208 test_cmp expected actual
209'
210
211test_expect_success 'find work tree from work tree' '
212 echo sub/dir/tracked >expected &&
213 >repo.git/work/sub/dir/tracked &&
214 (
215 cd repo.git/work/sub/dir &&
216 git --git-dir=../../.. add tracked
217 ) &&
218 (
219 cd repo.git &&
220 git ls-files >../actual
221 ) &&
222 test_cmp expected actual
e90fdc39
JS
223'
224
dd5c8af1 225test_expect_success '_gently() groks relative GIT_DIR & GIT_WORK_TREE' '
fc688759
JN
226 (
227 cd repo.git/work/sub/dir &&
228 GIT_DIR=../../.. &&
229 GIT_WORK_TREE=../.. &&
230 GIT_PAGER= &&
231 export GIT_DIR GIT_WORK_TREE GIT_PAGER &&
232
dd5c8af1 233 git diff --exit-code tracked &&
fc688759
JN
234 echo changed >tracked &&
235 test_must_fail git diff --exit-code tracked
236 )
237'
238
239test_expect_success 'diff-index respects work tree under .git dir' '
240 cat >diff-index-cached.expected <<-EOF &&
3749fde5 241 :000000 100644 $_z40 $EMPTY_BLOB A sub/dir/tracked
fc688759
JN
242 EOF
243 cat >diff-index.expected <<-EOF &&
3749fde5 244 :000000 100644 $_z40 $_z40 A sub/dir/tracked
fc688759
JN
245 EOF
246
247 (
248 GIT_DIR=repo.git &&
249 GIT_WORK_TREE=repo.git/work &&
250 export GIT_DIR GIT_WORK_TREE &&
251 git diff-index $EMPTY_TREE >diff-index.actual &&
252 git diff-index --cached $EMPTY_TREE >diff-index-cached.actual
253 ) &&
254 test_cmp diff-index.expected diff-index.actual &&
255 test_cmp diff-index-cached.expected diff-index-cached.actual
256'
257
258test_expect_success 'diff-files respects work tree under .git dir' '
259 cat >diff-files.expected <<-EOF &&
3749fde5 260 :100644 100644 $EMPTY_BLOB $_z40 M sub/dir/tracked
fc688759
JN
261 EOF
262
263 (
264 GIT_DIR=repo.git &&
265 GIT_WORK_TREE=repo.git/work &&
266 export GIT_DIR GIT_WORK_TREE &&
267 git diff-files >diff-files.actual
268 ) &&
269 test_cmp diff-files.expected diff-files.actual
270'
271
272test_expect_success 'git diff respects work tree under .git dir' '
273 cat >diff-TREE.expected <<-EOF &&
274 diff --git a/sub/dir/tracked b/sub/dir/tracked
275 new file mode 100644
276 index 0000000..$CHANGED_BLOB7
277 --- /dev/null
278 +++ b/sub/dir/tracked
279 @@ -0,0 +1 @@
280 +changed
281 EOF
282 cat >diff-TREE-cached.expected <<-EOF &&
283 diff --git a/sub/dir/tracked b/sub/dir/tracked
284 new file mode 100644
285 index 0000000..$EMPTY_BLOB7
286 EOF
287 cat >diff-FILES.expected <<-EOF &&
288 diff --git a/sub/dir/tracked b/sub/dir/tracked
289 index $EMPTY_BLOB7..$CHANGED_BLOB7 100644
290 --- a/sub/dir/tracked
291 +++ b/sub/dir/tracked
292 @@ -0,0 +1 @@
293 +changed
294 EOF
295
296 (
297 GIT_DIR=repo.git &&
298 GIT_WORK_TREE=repo.git/work &&
299 export GIT_DIR GIT_WORK_TREE &&
300 git diff $EMPTY_TREE >diff-TREE.actual &&
301 git diff --cached $EMPTY_TREE >diff-TREE-cached.actual &&
302 git diff >diff-FILES.actual
303 ) &&
304 test_cmp diff-TREE.expected diff-TREE.actual &&
305 test_cmp diff-TREE-cached.expected diff-TREE-cached.actual &&
306 test_cmp diff-FILES.expected diff-FILES.actual
dd5c8af1
JS
307'
308
6577f542 309test_expect_success 'git grep' '
fc688759
JN
310 echo dir/tracked >expected.grep &&
311 (
312 cd repo.git/work/sub &&
313 GIT_DIR=../.. &&
314 GIT_WORK_TREE=.. &&
315 export GIT_DIR GIT_WORK_TREE &&
316 git grep -l changed >../../../actual.grep
317 ) &&
318 test_cmp expected.grep actual.grep
6577f542
NTND
319'
320
b3100fd5
JH
321test_expect_success 'git commit' '
322 (
323 cd repo.git &&
324 GIT_DIR=. GIT_WORK_TREE=work git commit -a -m done
325 )
326'
327
328test_expect_success 'absolute pathspec should fail gracefully' '
329 (
fc688759
JN
330 cd repo.git &&
331 test_might_fail git config --unset core.worktree &&
b3100fd5
JH
332 test_must_fail git log HEAD -- /home
333 )
334'
335
288123f0 336test_expect_success 'make_relative_path handles double slashes in GIT_DIR' '
7f9a5fc6 337 >dummy_file &&
288123f0
JH
338 echo git --git-dir="$(pwd)//repo.git" --work-tree="$(pwd)" add dummy_file &&
339 git --git-dir="$(pwd)//repo.git" --work-tree="$(pwd)" add dummy_file
340'
341
0ed74813
NTND
342test_expect_success 'relative $GIT_WORK_TREE and git subprocesses' '
343 GIT_DIR=repo.git GIT_WORK_TREE=repo.git/work \
344 test-subprocess --setup-work-tree rev-parse --show-toplevel >actual &&
e6ec2b6a 345 echo "$(pwd)/repo.git/work" >expected &&
0ed74813
NTND
346 test_cmp expected actual
347'
348
3ae4a867 349test_done