interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / t / t4053-diff-no-index.sh

434 lines · 11298 bytes Raw Blame History
1#!/bin/sh
2
3test_description='diff --no-index'
4
5. ./test-lib.sh
6
7test_expect_success 'setup' '
8 mkdir a &&
9 mkdir b &&
10 echo 1 >a/1 &&
11 echo 2 >a/2 &&
12 git init repo &&
13 echo 1 >repo/a &&
14 mkdir -p non/git &&
15 echo 1 >non/git/a &&
16 echo 1 >non/git/b
17'
18
19test_expect_success 'git diff --no-index --exit-code' '
20 git diff --no-index --exit-code a/1 non/git/a &&
21 test_expect_code 1 git diff --no-index --exit-code a/1 a/2
22'
23
24test_expect_success 'git diff --no-index directories' '
25 test_expect_code 1 git diff --no-index a b >cnt &&
26 test_line_count = 14 cnt
27'
28
29test_expect_success 'git diff --no-index with -' '
30 cat >expect <<-\EOF &&
31 diff --git a/- b/-
32 new file mode 100644
33 --- /dev/null
34 +++ b/-
35 @@ -0,0 +1 @@
36 +frotz
37 EOF
38 (
39 cd a &&
40 echo frotz |
41 test_expect_code 1 git diff --no-index /dev/null - >../actual
42 ) &&
43 test_cmp expect actual
44'
45
46test_expect_success 'git diff --no-index relative path outside repo' '
47 (
48 cd repo &&
49 test_expect_code 0 git diff --no-index a ../non/git/a &&
50 test_expect_code 0 git diff --no-index ../non/git/a ../non/git/b
51 )
52'
53
54test_expect_success 'git diff --no-index with broken index' '
55 (
56 cd repo &&
57 echo broken >.git/index &&
58 git diff --no-index a ../non/git/a
59 )
60'
61
62test_expect_success 'git diff outside repo with broken index' '
63 (
64 cd repo &&
65 git diff ../non/git/a ../non/git/b
66 )
67'
68
69test_expect_success 'git diff --no-index executed outside repo gives correct error message' '
70 (
71 GIT_CEILING_DIRECTORIES=$TRASH_DIRECTORY/non &&
72 export GIT_CEILING_DIRECTORIES &&
73 cd non/git &&
74 test_must_fail git diff --no-index a 2>actual.err &&
75 test_grep "usage: git diff --no-index" actual.err
76 )
77'
78
79test_expect_success 'git diff --find-object outside repo fails gracefully' '
80 (
81 GIT_CEILING_DIRECTORIES=$TRASH_DIRECTORY/non &&
82 export GIT_CEILING_DIRECTORIES &&
83 cd non/git &&
84 test_must_fail git diff --find-object=abc123 2>err &&
85 test_grep "find-object requires a git repository" err
86 )
87'
88
89test_expect_success 'diff D F and diff F D' '
90 (
91 cd repo &&
92 echo in-repo >a &&
93 echo non-repo >../non/git/a &&
94 mkdir sub &&
95 echo sub-repo >sub/a &&
96
97 test_must_fail git diff --no-index sub/a ../non/git/a >expect &&
98 test_must_fail git diff --no-index sub/a ../non/git/ >actual &&
99 test_cmp expect actual &&
100
101 test_must_fail git diff --no-index a ../non/git/a >expect &&
102 test_must_fail git diff --no-index a ../non/git/ >actual &&
103 test_cmp expect actual &&
104
105 test_must_fail git diff --no-index ../non/git/a a >expect &&
106 test_must_fail git diff --no-index ../non/git a >actual &&
107 test_cmp expect actual
108 )
109'
110
111test_expect_success 'turning a file into a directory' '
112 (
113 cd non/git &&
114 mkdir d e e/sub &&
115 echo 1 >d/sub &&
116 echo 2 >e/sub/file &&
117 printf "D\td/sub\nA\te/sub/file\n" >expect &&
118 test_must_fail git diff --no-index --name-status d e >actual &&
119 test_cmp expect actual
120 )
121'
122
123test_expect_success 'reverse diff when turning a file into a directory' '
124 (
125 cd non/git &&
126 printf "A\td/sub\nD\te/sub/file\n" >expect &&
127 test_expect_code 1 git diff --no-index -R --name-status d e >actual &&
128 test_cmp expect actual
129 )
130'
131
132test_expect_success 'reverse diff when turning a directory into a file' '
133 (
134 cd non/git &&
135 printf "D\td/sub\nA\te/sub/file\n" >expect &&
136 test_expect_code 1 git diff --no-index -R --name-status e d >actual &&
137 test_cmp expect actual
138 )
139'
140
141test_expect_success 'diff from repo subdir shows real paths (explicit)' '
142 echo "diff --git a/../../non/git/a b/../../non/git/b" >expect &&
143 test_expect_code 1 \
144 git -C repo/sub \
145 diff --no-index ../../non/git/a ../../non/git/b >actual &&
146 head -n 1 <actual >actual.head &&
147 test_cmp expect actual.head
148'
149
150test_expect_success 'diff from repo subdir shows real paths (implicit)' '
151 echo "diff --git a/../../non/git/a b/../../non/git/b" >expect &&
152 test_expect_code 1 \
153 git -C repo/sub \
154 diff ../../non/git/a ../../non/git/b >actual &&
155 head -n 1 <actual >actual.head &&
156 test_cmp expect actual.head
157'
158
159test_expect_success 'diff --no-index from repo subdir respects config (explicit)' '
160 echo "diff --git ../../non/git/a ../../non/git/b" >expect &&
161 test_config -C repo diff.noprefix true &&
162 test_expect_code 1 \
163 git -C repo/sub \
164 diff --no-index ../../non/git/a ../../non/git/b >actual &&
165 head -n 1 <actual >actual.head &&
166 test_cmp expect actual.head
167'
168
169test_expect_success 'diff --no-index from repo subdir respects config (implicit)' '
170 echo "diff --git ../../non/git/a ../../non/git/b" >expect &&
171 test_config -C repo diff.noprefix true &&
172 test_expect_code 1 \
173 git -C repo/sub \
174 diff ../../non/git/a ../../non/git/b >actual &&
175 head -n 1 <actual >actual.head &&
176 test_cmp expect actual.head
177'
178
179test_expect_success 'diff --no-index from repo subdir with absolute paths' '
180 cat <<-EOF >expect &&
181 1 1 $(pwd)/non/git/{a => b}
182 EOF
183 test_expect_code 1 \
184 git -C repo/sub diff --numstat \
185 "$(pwd)/non/git/a" "$(pwd)/non/git/b" >actual &&
186 test_cmp expect actual
187'
188
189test_expect_success 'diff --no-index allows external diff' '
190 test_expect_code 1 \
191 env GIT_EXTERNAL_DIFF="echo external ;:" \
192 git diff --no-index non/git/a non/git/b >actual &&
193 echo external >expect &&
194 test_cmp expect actual
195'
196
197test_expect_success 'diff --no-index normalizes mode: no changes' '
198 echo foo >x &&
199 cp x y &&
200 git diff --no-index x y >out &&
201 test_must_be_empty out
202'
203
204test_expect_success POSIXPERM 'diff --no-index normalizes mode: chmod +x' '
205 chmod +x y &&
206 cat >expected <<-\EOF &&
207 diff --git a/x b/y
208 old mode 100644
209 new mode 100755
210 EOF
211 test_expect_code 1 git diff --no-index x y >actual &&
212 test_cmp expected actual
213'
214
215test_expect_success POSIXPERM 'diff --no-index normalizes: mode not like git mode' '
216 chmod 666 x &&
217 chmod 777 y &&
218 cat >expected <<-\EOF &&
219 diff --git a/x b/y
220 old mode 100644
221 new mode 100755
222 EOF
223 test_expect_code 1 git diff --no-index x y >actual &&
224 test_cmp expected actual
225'
226
227test_expect_success POSIXPERM,SYMLINKS 'diff --no-index normalizes: mode not like git mode (symlink)' '
228 ln -s y z &&
229 X_OID=$(git hash-object --stdin <x) &&
230 Z_OID=$(printf y | git hash-object --stdin) &&
231 cat >expected <<-EOF &&
232 diff --git a/x b/x
233 deleted file mode 100644
234 index $X_OID..$ZERO_OID
235 --- a/x
236 +++ /dev/null
237 @@ -1 +0,0 @@
238 -foo
239 diff --git a/z b/z
240 new file mode 120000
241 index $ZERO_OID..$Z_OID
242 --- /dev/null
243 +++ b/z
244 @@ -0,0 +1 @@
245 +y
246 \ No newline at end of file
247 EOF
248 test_expect_code 1 git -c core.abbrev=no diff --no-index x z >actual &&
249 test_cmp expected actual
250'
251
252test_expect_success POSIXPERM 'external diff with mode-only change' '
253 echo content >not-executable &&
254 echo content >executable &&
255 chmod +x executable &&
256 echo executable executable $(test_oid zero) 100755 \
257 not-executable $(test_oid zero) 100644 not-executable \
258 >expect &&
259 test_expect_code 1 git -c diff.external=echo diff \
260 --no-index executable not-executable >actual &&
261 test_cmp expect actual
262'
263
264test_expect_success "diff --no-index treats '-' as stdin" '
265 cat >expect <<-EOF &&
266 diff --git a/- b/a/1
267 index $ZERO_OID..$(git hash-object --stdin <a/1) 100644
268 --- a/-
269 +++ b/a/1
270 @@ -1 +1 @@
271 -x
272 +1
273 EOF
274
275 test_write_lines x | test_expect_code 1 \
276 git -c core.abbrev=no diff --no-index -- - a/1 >actual &&
277 test_cmp expect actual &&
278
279 test_write_lines 1 | git diff --no-index -- a/1 - >actual &&
280 test_must_be_empty actual
281'
282
283test_expect_success "diff --no-index -R treats '-' as stdin" '
284 cat >expect <<-EOF &&
285 diff --git b/a/1 a/-
286 index $(git hash-object --stdin <a/1)..$ZERO_OID 100644
287 --- b/a/1
288 +++ a/-
289 @@ -1 +1 @@
290 -1
291 +x
292 EOF
293
294 test_write_lines x | test_expect_code 1 \
295 git -c core.abbrev=no diff --no-index -R -- - a/1 >actual &&
296 test_cmp expect actual &&
297
298 test_write_lines 1 | git diff --no-index -R -- a/1 - >actual &&
299 test_must_be_empty actual
300'
301
302test_expect_success 'diff --no-index refuses to diff stdin and a directory' '
303 test_must_fail git diff --no-index -- - a </dev/null 2>err &&
304 test_grep "fatal: cannot compare stdin to a directory" err
305'
306
307test_expect_success PIPE 'diff --no-index refuses to diff a named pipe and a directory' '
308 test_when_finished "rm -f pipe" &&
309 mkfifo pipe &&
310 test_must_fail git diff --no-index -- pipe a 2>err &&
311 test_grep "fatal: cannot compare a named pipe to a directory" err
312'
313
314test_expect_success PIPE,SYMLINKS 'diff --no-index reads from pipes' '
315 test_when_finished "rm -f old new new-link" &&
316 mkfifo old &&
317 mkfifo new &&
318 ln -s new new-link &&
319 {
320 (test_write_lines a b c >old) &
321 } &&
322 test_when_finished "kill $! || :" &&
323 {
324 (test_write_lines a x c >new) &
325 } &&
326 test_when_finished "kill $! || :" &&
327
328 cat >expect <<-EOF &&
329 diff --git a/old b/new-link
330 --- a/old
331 +++ b/new-link
332 @@ -1,3 +1,3 @@
333 a
334 -b
335 +x
336 c
337 EOF
338
339 test_expect_code 1 git diff --no-index old new-link >actual &&
340 test_cmp expect actual
341'
342
343test_expect_success 'diff --no-index F F rejects pathspecs' '
344 test_must_fail git diff --no-index -- a/1 a/2 a 2>actual.err &&
345 test_grep "usage: git diff --no-index" actual.err
346'
347
348test_expect_success 'diff --no-index D F rejects pathspecs' '
349 test_must_fail git diff --no-index -- a a/2 a 2>actual.err &&
350 test_grep "usage: git diff --no-index" actual.err
351'
352
353test_expect_success 'diff --no-index F D rejects pathspecs' '
354 test_must_fail git diff --no-index -- a/1 b b 2>actual.err &&
355 test_grep "usage: git diff --no-index" actual.err
356'
357
358test_expect_success 'diff --no-index rejects absolute pathspec' '
359 test_must_fail git diff --no-index -- a b $(pwd)/a/1
360'
361
362test_expect_success 'diff --no-index with pathspec' '
363 test_expect_code 1 git diff --name-status --no-index a b 1 >actual &&
364 cat >expect <<-EOF &&
365 D a/1
366 EOF
367 test_cmp expect actual
368'
369
370test_expect_success 'diff --no-index first path ending in slash with pathspec' '
371 test_expect_code 1 git diff --name-status --no-index a/ b 1 >actual &&
372 cat >expect <<-EOF &&
373 D a/1
374 EOF
375 test_cmp expect actual
376'
377
378test_expect_success 'diff --no-index second path ending in slash with pathspec' '
379 test_expect_code 1 git diff --name-status --no-index a b/ 1 >actual &&
380 cat >expect <<-EOF &&
381 D a/1
382 EOF
383 test_cmp expect actual
384'
385
386test_expect_success 'diff --no-index with pathspec no matches' '
387 test_expect_code 0 git diff --name-status --no-index a b missing
388'
389
390test_expect_success 'diff --no-index with negative pathspec' '
391 test_expect_code 1 git diff --name-status --no-index a b ":!2" >actual &&
392 cat >expect <<-EOF &&
393 D a/1
394 EOF
395 test_cmp expect actual
396'
397
398test_expect_success 'setup nested' '
399 mkdir -p c/1/2 &&
400 mkdir -p d/1/2 &&
401 echo 1 >c/1/2/a &&
402 echo 2 >c/1/2/b
403'
404
405test_expect_success 'diff --no-index with pathspec nested negative pathspec' '
406 test_expect_code 0 git diff --no-index c d ":!1"
407'
408
409test_expect_success 'diff --no-index with pathspec nested pathspec' '
410 test_expect_code 1 git diff --name-status --no-index c d 1/2 >actual &&
411 cat >expect <<-EOF &&
412 D c/1/2/a
413 D c/1/2/b
414 EOF
415 test_cmp expect actual
416'
417
418test_expect_success 'diff --no-index with pathspec glob' '
419 test_expect_code 1 git diff --name-status --no-index c d ":(glob)**/a" >actual &&
420 cat >expect <<-EOF &&
421 D c/1/2/a
422 EOF
423 test_cmp expect actual
424'
425
426test_expect_success 'diff --no-index with pathspec glob and exclude' '
427 test_expect_code 1 git diff --name-status --no-index c d ":(glob,exclude)**/a" >actual &&
428 cat >expect <<-EOF &&
429 D c/1/2/b
430 EOF
431 test_cmp expect actual
432'
433
434test_done