]> git.ipfire.org Git - thirdparty/git.git/blame - t/t4053-diff-no-index.sh
Start the 2.46 cycle
[thirdparty/git.git] / t / t4053-diff-no-index.sh
CommitLineData
f3999e03
BP
1#!/bin/sh
2
3test_description='diff --no-index'
4
e5e37517 5TEST_PASSES_SANITIZE_LEAK=true
f3999e03
BP
6. ./test-lib.sh
7
8test_expect_success 'setup' '
9 mkdir a &&
10 mkdir b &&
11 echo 1 >a/1 &&
546e0fd9
JK
12 echo 2 >a/2 &&
13 git init repo &&
14 echo 1 >repo/a &&
15 mkdir -p non/git &&
16 echo 1 >non/git/a &&
17 echo 1 >non/git/b
f3999e03
BP
18'
19
271cb303
ÆAB
20test_expect_success 'git diff --no-index --exit-code' '
21 git diff --no-index --exit-code a/1 non/git/a &&
22 test_expect_code 1 git diff --no-index --exit-code a/1 a/2
23'
24
f3999e03 25test_expect_success 'git diff --no-index directories' '
c21fc9d0
JK
26 test_expect_code 1 git diff --no-index a b >cnt &&
27 test_line_count = 14 cnt
f3999e03
BP
28'
29
546e0fd9
JK
30test_expect_success 'git diff --no-index relative path outside repo' '
31 (
32 cd repo &&
33 test_expect_code 0 git diff --no-index a ../non/git/a &&
34 test_expect_code 0 git diff --no-index ../non/git/a ../non/git/b
35 )
36'
37
6df5762d
TG
38test_expect_success 'git diff --no-index with broken index' '
39 (
40 cd repo &&
41 echo broken >.git/index &&
42 git diff --no-index a ../non/git/a
43 )
44'
45
46test_expect_success 'git diff outside repo with broken index' '
47 (
48 cd repo &&
49 git diff ../non/git/a ../non/git/b
50 )
51'
52
8a19dfa1
TG
53test_expect_success 'git diff --no-index executed outside repo gives correct error message' '
54 (
55 GIT_CEILING_DIRECTORIES=$TRASH_DIRECTORY/non &&
56 export GIT_CEILING_DIRECTORIES &&
57 cd non/git &&
58 test_must_fail git diff --no-index a 2>actual.err &&
6789275d 59 test_grep "usage: git diff --no-index" actual.err
8a19dfa1
TG
60 )
61'
62
c9e1f2c7
JH
63test_expect_success 'diff D F and diff F D' '
64 (
65 cd repo &&
66 echo in-repo >a &&
67 echo non-repo >../non/git/a &&
68 mkdir sub &&
69 echo sub-repo >sub/a &&
70
71 test_must_fail git diff --no-index sub/a ../non/git/a >expect &&
72 test_must_fail git diff --no-index sub/a ../non/git/ >actual &&
73 test_cmp expect actual &&
74
75 test_must_fail git diff --no-index a ../non/git/a >expect &&
76 test_must_fail git diff --no-index a ../non/git/ >actual &&
77 test_cmp expect actual &&
78
79 test_must_fail git diff --no-index ../non/git/a a >expect &&
80 test_must_fail git diff --no-index ../non/git a >actual &&
81 test_cmp expect actual
82 )
83'
84
06151739
JH
85test_expect_success 'turning a file into a directory' '
86 (
87 cd non/git &&
88 mkdir d e e/sub &&
89 echo 1 >d/sub &&
90 echo 2 >e/sub/file &&
91 printf "D\td/sub\nA\te/sub/file\n" >expect &&
92 test_must_fail git diff --no-index --name-status d e >actual &&
93 test_cmp expect actual
94 )
95'
96
7d8930d9
JK
97test_expect_success 'diff from repo subdir shows real paths (explicit)' '
98 echo "diff --git a/../../non/git/a b/../../non/git/b" >expect &&
99 test_expect_code 1 \
100 git -C repo/sub \
101 diff --no-index ../../non/git/a ../../non/git/b >actual &&
102 head -n 1 <actual >actual.head &&
103 test_cmp expect actual.head
104'
105
106test_expect_success 'diff from repo subdir shows real paths (implicit)' '
107 echo "diff --git a/../../non/git/a b/../../non/git/b" >expect &&
108 test_expect_code 1 \
109 git -C repo/sub \
110 diff ../../non/git/a ../../non/git/b >actual &&
111 head -n 1 <actual >actual.head &&
112 test_cmp expect actual.head
113'
114
28a4e580
JK
115test_expect_success 'diff --no-index from repo subdir respects config (explicit)' '
116 echo "diff --git ../../non/git/a ../../non/git/b" >expect &&
117 test_config -C repo diff.noprefix true &&
118 test_expect_code 1 \
119 git -C repo/sub \
120 diff --no-index ../../non/git/a ../../non/git/b >actual &&
121 head -n 1 <actual >actual.head &&
122 test_cmp expect actual.head
123'
124
125test_expect_success 'diff --no-index from repo subdir respects config (implicit)' '
126 echo "diff --git ../../non/git/a ../../non/git/b" >expect &&
127 test_config -C repo diff.noprefix true &&
128 test_expect_code 1 \
129 git -C repo/sub \
130 diff ../../non/git/a ../../non/git/b >actual &&
131 head -n 1 <actual >actual.head &&
132 test_cmp expect actual.head
133'
134
ffd04e92
JS
135test_expect_success 'diff --no-index from repo subdir with absolute paths' '
136 cat <<-EOF >expect &&
137 1 1 $(pwd)/non/git/{a => b}
138 EOF
139 test_expect_code 1 \
140 git -C repo/sub diff --numstat \
141 "$(pwd)/non/git/a" "$(pwd)/non/git/b" >actual &&
142 test_cmp expect actual
143'
144
287ab28b
JK
145test_expect_success 'diff --no-index allows external diff' '
146 test_expect_code 1 \
147 env GIT_EXTERNAL_DIFF="echo external ;:" \
148 git diff --no-index non/git/a non/git/b >actual &&
149 echo external >expect &&
150 test_cmp expect actual
151'
152
2be927f3
ÆAB
153test_expect_success 'diff --no-index normalizes mode: no changes' '
154 echo foo >x &&
155 cp x y &&
156 git diff --no-index x y >out &&
157 test_must_be_empty out
158'
159
160test_expect_success POSIXPERM 'diff --no-index normalizes mode: chmod +x' '
161 chmod +x y &&
162 cat >expected <<-\EOF &&
163 diff --git a/x b/y
164 old mode 100644
165 new mode 100755
166 EOF
167 test_expect_code 1 git diff --no-index x y >actual &&
168 test_cmp expected actual
169'
170
171test_expect_success POSIXPERM 'diff --no-index normalizes: mode not like git mode' '
172 chmod 666 x &&
173 chmod 777 y &&
174 cat >expected <<-\EOF &&
175 diff --git a/x b/y
176 old mode 100644
177 new mode 100755
178 EOF
179 test_expect_code 1 git diff --no-index x y >actual &&
180 test_cmp expected actual
181'
182
183test_expect_success POSIXPERM,SYMLINKS 'diff --no-index normalizes: mode not like git mode (symlink)' '
184 ln -s y z &&
185 X_OID=$(git hash-object --stdin <x) &&
186 Z_OID=$(printf y | git hash-object --stdin) &&
187 cat >expected <<-EOF &&
188 diff --git a/x b/x
189 deleted file mode 100644
190 index $X_OID..$ZERO_OID
191 --- a/x
192 +++ /dev/null
193 @@ -1 +0,0 @@
194 -foo
195 diff --git a/z b/z
196 new file mode 120000
197 index $ZERO_OID..$Z_OID
198 --- /dev/null
199 +++ b/z
200 @@ -0,0 +1 @@
201 +y
202 \ No newline at end of file
203 EOF
204 test_expect_code 1 git -c core.abbrev=no diff --no-index x z >actual &&
205 test_cmp expected actual
206'
207
85a9a63c
JK
208test_expect_success POSIXPERM 'external diff with mode-only change' '
209 echo content >not-executable &&
210 echo content >executable &&
211 chmod +x executable &&
212 echo executable executable $(test_oid zero) 100755 \
213 not-executable $(test_oid zero) 100644 not-executable \
214 >expect &&
215 test_expect_code 1 git -c diff.external=echo diff \
216 --no-index executable not-executable >actual &&
217 test_cmp expect actual
218'
219
df521462
PW
220test_expect_success "diff --no-index treats '-' as stdin" '
221 cat >expect <<-EOF &&
222 diff --git a/- b/a/1
223 index $ZERO_OID..$(git hash-object --stdin <a/1) 100644
224 --- a/-
225 +++ b/a/1
226 @@ -1 +1 @@
227 -x
228 +1
229 EOF
230
231 test_write_lines x | test_expect_code 1 \
232 git -c core.abbrev=no diff --no-index -- - a/1 >actual &&
233 test_cmp expect actual &&
234
235 test_write_lines 1 | git diff --no-index -- a/1 - >actual &&
236 test_must_be_empty actual
48944f21
RS
237'
238
239test_expect_success "diff --no-index -R treats '-' as stdin" '
240 cat >expect <<-EOF &&
241 diff --git b/a/1 a/-
242 index $(git hash-object --stdin <a/1)..$ZERO_OID 100644
243 --- b/a/1
244 +++ a/-
245 @@ -1 +1 @@
246 -1
247 +x
248 EOF
249
250 test_write_lines x | test_expect_code 1 \
251 git -c core.abbrev=no diff --no-index -R -- - a/1 >actual &&
252 test_cmp expect actual &&
253
254 test_write_lines 1 | git diff --no-index -R -- a/1 - >actual &&
255 test_must_be_empty actual
df521462
PW
256'
257
49819845
PW
258test_expect_success 'diff --no-index refuses to diff stdin and a directory' '
259 test_must_fail git diff --no-index -- - a </dev/null 2>err &&
260 grep "fatal: cannot compare stdin to a directory" err
261'
262
1e3f2654
PW
263test_expect_success PIPE 'diff --no-index refuses to diff a named pipe and a directory' '
264 test_when_finished "rm -f pipe" &&
265 mkfifo pipe &&
1e3f2654
PW
266 test_must_fail git diff --no-index -- pipe a 2>err &&
267 grep "fatal: cannot compare a named pipe to a directory" err
268'
269
270test_expect_success PIPE,SYMLINKS 'diff --no-index reads from pipes' '
271 test_when_finished "rm -f old new new-link" &&
272 mkfifo old &&
273 mkfifo new &&
274 ln -s new new-link &&
275 {
276 (test_write_lines a b c >old) &
277 } &&
231e86c1 278 test_when_finished "kill $! || :" &&
1e3f2654
PW
279 {
280 (test_write_lines a x c >new) &
281 } &&
231e86c1 282 test_when_finished "kill $! || :" &&
1e3f2654
PW
283
284 cat >expect <<-EOF &&
285 diff --git a/old b/new-link
286 --- a/old
287 +++ b/new-link
288 @@ -1,3 +1,3 @@
289 a
290 -b
291 +x
292 c
293 EOF
294
295 test_expect_code 1 git diff --no-index old new-link >actual &&
296 test_cmp expect actual
297'
298
f3999e03 299test_done