]> git.ipfire.org Git - thirdparty/git.git/blame - t/t4020-diff-external.sh
Merge branch 'jk/clone-allow-bare-and-o-together'
[thirdparty/git.git] / t / t4020-diff-external.sh
CommitLineData
f1af60bd
JH
1#!/bin/sh
2
3test_description='external diff interface test'
4
16d4bd4f 5TEST_PASSES_SANITIZE_LEAK=true
f1af60bd
JH
6. ./test-lib.sh
7
f1af60bd
JH
8test_expect_success setup '
9
10 test_tick &&
11 echo initial >file &&
12 git add file &&
13 git commit -m initial &&
14
15 test_tick &&
16 echo second >file &&
f2fffc17 17 before=$(git hash-object file) &&
18 before=$(git rev-parse --short $before) &&
f1af60bd
JH
19 git add file &&
20 git commit -m second &&
21
22 test_tick &&
23 echo third >file
24'
25
26test_expect_success 'GIT_EXTERNAL_DIFF environment' '
5476bdf0
ÆAB
27 cat >expect <<-EOF &&
28 file $(git rev-parse --verify HEAD:file) 100644 file $(test_oid zero) 100644
29 EOF
30 GIT_EXTERNAL_DIFF=echo git diff >out &&
31 cut -d" " -f1,3- <out >actual &&
32 test_cmp expect actual
f1af60bd
JH
33
34'
35
96ecf699 36test_expect_success 'GIT_EXTERNAL_DIFF environment should apply only to diff' '
d239ef1c
ÆAB
37 GIT_EXTERNAL_DIFF=echo git log -p -1 HEAD >out &&
38 grep "^diff --git a/file b/file" out
f1af60bd
JH
39
40'
41
61af494c 42test_expect_success 'GIT_EXTERNAL_DIFF environment and --no-ext-diff' '
d239ef1c
ÆAB
43 GIT_EXTERNAL_DIFF=echo git diff --no-ext-diff >out &&
44 grep "^diff --git a/file b/file" out
61af494c
JH
45
46'
47
bd8c1a9b
JH
48test_expect_success SYMLINKS 'typechange diff' '
49 rm -f file &&
50 ln -s elif file &&
5476bdf0
ÆAB
51
52 cat >expect <<-EOF &&
53 file $(git rev-parse --verify HEAD:file) 100644 $(test_oid zero) 120000
54 EOF
55 GIT_EXTERNAL_DIFF=echo git diff >out &&
56 cut -d" " -f1,3-4,6- <out >actual &&
57 test_cmp expect actual &&
58
bd8c1a9b
JH
59 GIT_EXTERNAL_DIFF=echo git diff --no-ext-diff >actual &&
60 git diff >expect &&
61 test_cmp expect actual
62'
63
c12f82ae
JK
64test_expect_success 'diff.external' '
65 git reset --hard &&
66 echo third >file &&
67 test_config diff.external echo &&
5476bdf0
ÆAB
68
69 cat >expect <<-EOF &&
70 file $(git rev-parse --verify HEAD:file) 100644 $(test_oid zero) 100644
71 EOF
72 git diff >out &&
73 cut -d" " -f1,3-4,6- <out >actual &&
74 test_cmp expect actual
c12f82ae
JK
75'
76
96ecf699 77test_expect_success 'diff.external should apply only to diff' '
c12f82ae 78 test_config diff.external echo &&
d239ef1c
ÆAB
79 git log -p -1 HEAD >out &&
80 grep "^diff --git a/file b/file" out
c12f82ae
JK
81'
82
83test_expect_success 'diff.external and --no-ext-diff' '
84 test_config diff.external echo &&
d239ef1c
ÆAB
85 git diff --no-ext-diff >out &&
86 grep "^diff --git a/file b/file" out
c12f82ae
JK
87'
88
f1af60bd 89test_expect_success 'diff attribute' '
bd8c1a9b
JH
90 git reset --hard &&
91 echo third >file &&
f1af60bd
JH
92
93 git config diff.parrot.command echo &&
94
95 echo >.gitattributes "file diff=parrot" &&
96
5476bdf0
ÆAB
97 cat >expect <<-EOF &&
98 file $(git rev-parse --verify HEAD:file) 100644 $(test_oid zero) 100644
99 EOF
100 git diff >out &&
101 cut -d" " -f1,3-4,6- <out >actual &&
102 test_cmp expect actual
f1af60bd
JH
103'
104
d239ef1c
ÆAB
105test_expect_success !SANITIZE_LEAK 'diff attribute should apply only to diff' '
106 git log -p -1 HEAD >out &&
107 grep "^diff --git a/file b/file" out
f1af60bd
JH
108
109'
110
61af494c 111test_expect_success 'diff attribute and --no-ext-diff' '
d239ef1c
ÆAB
112 git diff --no-ext-diff >out &&
113 grep "^diff --git a/file b/file" out
61af494c
JH
114
115'
116
f1af60bd
JH
117test_expect_success 'diff attribute' '
118
119 git config --unset diff.parrot.command &&
120 git config diff.color.command echo &&
121
122 echo >.gitattributes "file diff=color" &&
123
5476bdf0
ÆAB
124 cat >expect <<-EOF &&
125 file $(git rev-parse --verify HEAD:file) 100644 $(test_oid zero) 100644
126 EOF
127 git diff >out &&
128 cut -d" " -f1,3-4,6- <out >actual &&
129 test_cmp expect actual
f1af60bd
JH
130'
131
d239ef1c
ÆAB
132test_expect_success !SANITIZE_LEAK 'diff attribute should apply only to diff' '
133 git log -p -1 HEAD >out &&
134 grep "^diff --git a/file b/file" out
f1af60bd
JH
135
136'
137
61af494c 138test_expect_success 'diff attribute and --no-ext-diff' '
d239ef1c
ÆAB
139 git diff --no-ext-diff >out &&
140 grep "^diff --git a/file b/file" out
61af494c
JH
141
142'
143
c12f82ae
JK
144test_expect_success 'GIT_EXTERNAL_DIFF trumps diff.external' '
145 >.gitattributes &&
146 test_config diff.external "echo ext-global" &&
5476bdf0
ÆAB
147
148 cat >expect <<-EOF &&
149 ext-env file $(git rev-parse --verify HEAD:file) 100644 file $(test_oid zero) 100644
150 EOF
151 GIT_EXTERNAL_DIFF="echo ext-env" git diff >out &&
152 cut -d" " -f1-2,4- <out >actual &&
153 test_cmp expect actual
c12f82ae
JK
154'
155
156test_expect_success 'attributes trump GIT_EXTERNAL_DIFF and diff.external' '
157 test_config diff.foo.command "echo ext-attribute" &&
158 test_config diff.external "echo ext-global" &&
159 echo "file diff=foo" >.gitattributes &&
5476bdf0
ÆAB
160
161 cat >expect <<-EOF &&
162 ext-attribute file $(git rev-parse --verify HEAD:file) 100644 file $(test_oid zero) 100644
163 EOF
164 GIT_EXTERNAL_DIFF="echo ext-env" git diff >out &&
165 cut -d" " -f1-2,4- <out >actual &&
166 test_cmp expect actual
c12f82ae
JK
167'
168
2c3fa66f
JH
169test_expect_success 'no diff with -diff' '
170 echo >.gitattributes "file -diff" &&
d239ef1c
ÆAB
171 git diff >out &&
172 grep Binary out
2c3fa66f
JH
173'
174
94221d22 175echo NULZbetweenZwords | perl -pe 'y/Z/\000/' > file
2c3fa66f
JH
176
177test_expect_success 'force diff with "diff"' '
f2fffc17 178 after=$(git hash-object file) &&
179 after=$(git rev-parse --short $after) &&
2c3fa66f 180 echo >.gitattributes "file diff" &&
53a5b443 181 git diff >actual &&
f2fffc17 182 sed -e "s/^index .*/index $before..$after 100644/" \
183 "$TEST_DIRECTORY"/t4020/diff.NUL >expected-diff &&
184 test_cmp expected-diff actual
2c3fa66f
JH
185'
186
a8344abe
NR
187test_expect_success 'GIT_EXTERNAL_DIFF with more than one changed files' '
188 echo anotherfile > file2 &&
189 git add file2 &&
190 git commit -m "added 2nd file" &&
191 echo modified >file2 &&
192 GIT_EXTERNAL_DIFF=echo git diff
193'
194
ee7fb0b1
ZK
195test_expect_success 'GIT_EXTERNAL_DIFF path counter/total' '
196 write_script external-diff.sh <<-\EOF &&
197 echo $GIT_DIFF_PATH_COUNTER of $GIT_DIFF_PATH_TOTAL >>counter.txt
198 EOF
199 >counter.txt &&
200 cat >expect <<-\EOF &&
201 1 of 2
202 2 of 2
203 EOF
204 GIT_EXTERNAL_DIFF=./external-diff.sh git diff &&
205 test_cmp expect counter.txt
206'
207
003b33a8 208test_expect_success 'GIT_EXTERNAL_DIFF generates pretty paths' '
8af75937 209 test_when_finished "git rm -f file.ext" &&
003b33a8
DA
210 touch file.ext &&
211 git add file.ext &&
212 echo with extension > file.ext &&
5476bdf0
ÆAB
213
214 cat >expect <<-EOF &&
8af75937 215 file.ext
5476bdf0
ÆAB
216 EOF
217 GIT_EXTERNAL_DIFF=echo git diff file.ext >out &&
8af75937
RS
218 basename $(cut -d" " -f2 <out) >actual &&
219 test_cmp expect actual
003b33a8
DA
220'
221
4e218f54
JS
222echo "#!$SHELL_PATH" >fake-diff.sh
223cat >> fake-diff.sh <<\EOF
224cat $2 >> crlfed.txt
225EOF
226chmod a+x fake-diff.sh
227
228keep_only_cr () {
229 tr -dc '\015'
230}
231
232test_expect_success 'external diff with autocrlf = true' '
aba47272 233 test_config core.autocrlf true &&
4e218f54
JS
234 GIT_EXTERNAL_DIFF=./fake-diff.sh git diff &&
235 test $(wc -l < crlfed.txt) = $(cat crlfed.txt | keep_only_cr | wc -c)
236'
237
150115ad 238test_expect_success 'diff --cached' '
aba47272 239 test_config core.autocrlf true &&
150115ad
JH
240 git add file &&
241 git update-index --assume-unchanged file &&
242 echo second >file &&
243 git diff --cached >actual &&
f2fffc17 244 test_cmp expected-diff actual
150115ad
JH
245'
246
aba47272
TR
247test_expect_success 'clean up crlf leftovers' '
248 git update-index --no-assume-unchanged file &&
249 rm -f file* &&
250 git reset --hard
251'
252
253test_expect_success 'submodule diff' '
254 git init sub &&
255 ( cd sub && test_commit sub1 ) &&
256 git add sub &&
257 test_tick &&
258 git commit -m "add submodule" &&
259 ( cd sub && test_commit sub2 ) &&
260 write_script gather_pre_post.sh <<-\EOF &&
261 echo "$1 $4" # path, mode
262 cat "$2" # old file
263 cat "$5" # new file
264 EOF
265 GIT_EXTERNAL_DIFF=./gather_pre_post.sh git diff >actual &&
266 cat >expected <<-EOF &&
267 sub 160000
268 Subproject commit $(git rev-parse HEAD:sub)
269 Subproject commit $(cd sub && git rev-parse HEAD)
270 EOF
271 test_cmp expected actual
272'
273
f1af60bd 274test_done