]> git.ipfire.org Git - thirdparty/git.git/blame - t/t4053-diff-no-index.sh
Merge branch 'jc/mv-d-to-d-error-message-fix' into maint-2.42
[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 &&
16bb3d71 59 test_i18ngrep "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
df521462
PW
208test_expect_success "diff --no-index treats '-' as stdin" '
209 cat >expect <<-EOF &&
210 diff --git a/- b/a/1
211 index $ZERO_OID..$(git hash-object --stdin <a/1) 100644
212 --- a/-
213 +++ b/a/1
214 @@ -1 +1 @@
215 -x
216 +1
217 EOF
218
219 test_write_lines x | test_expect_code 1 \
220 git -c core.abbrev=no diff --no-index -- - a/1 >actual &&
221 test_cmp expect actual &&
222
223 test_write_lines 1 | git diff --no-index -- a/1 - >actual &&
224 test_must_be_empty actual
48944f21
RS
225'
226
227test_expect_success "diff --no-index -R treats '-' as stdin" '
228 cat >expect <<-EOF &&
229 diff --git b/a/1 a/-
230 index $(git hash-object --stdin <a/1)..$ZERO_OID 100644
231 --- b/a/1
232 +++ a/-
233 @@ -1 +1 @@
234 -1
235 +x
236 EOF
237
238 test_write_lines x | test_expect_code 1 \
239 git -c core.abbrev=no diff --no-index -R -- - a/1 >actual &&
240 test_cmp expect actual &&
241
242 test_write_lines 1 | git diff --no-index -R -- a/1 - >actual &&
243 test_must_be_empty actual
df521462
PW
244'
245
49819845
PW
246test_expect_success 'diff --no-index refuses to diff stdin and a directory' '
247 test_must_fail git diff --no-index -- - a </dev/null 2>err &&
248 grep "fatal: cannot compare stdin to a directory" err
249'
250
1e3f2654
PW
251test_expect_success PIPE 'diff --no-index refuses to diff a named pipe and a directory' '
252 test_when_finished "rm -f pipe" &&
253 mkfifo pipe &&
1e3f2654
PW
254 test_must_fail git diff --no-index -- pipe a 2>err &&
255 grep "fatal: cannot compare a named pipe to a directory" err
256'
257
258test_expect_success PIPE,SYMLINKS 'diff --no-index reads from pipes' '
259 test_when_finished "rm -f old new new-link" &&
260 mkfifo old &&
261 mkfifo new &&
262 ln -s new new-link &&
263 {
264 (test_write_lines a b c >old) &
265 } &&
231e86c1 266 test_when_finished "kill $! || :" &&
1e3f2654
PW
267 {
268 (test_write_lines a x c >new) &
269 } &&
231e86c1 270 test_when_finished "kill $! || :" &&
1e3f2654
PW
271
272 cat >expect <<-EOF &&
273 diff --git a/old b/new-link
274 --- a/old
275 +++ b/new-link
276 @@ -1,3 +1,3 @@
277 a
278 -b
279 +x
280 c
281 EOF
282
283 test_expect_code 1 git diff --no-index old new-link >actual &&
284 test_cmp expect actual
285'
286
f3999e03 287test_done