Merge branch 'ab/test-lib'
[git/debian.git] / t / t4020-diff-external.sh
blobe009826fcbe5a893df748a47d162e010b95d758b
1 #!/bin/sh
3 test_description='external diff interface test'
5 . ./test-lib.sh
7 test_expect_success setup '
9 test_tick &&
10 echo initial >file &&
11 git add file &&
12 git commit -m initial &&
14 test_tick &&
15 echo second >file &&
16 before=$(git hash-object file) &&
17 before=$(git rev-parse --short $before) &&
18 git add file &&
19 git commit -m second &&
21 test_tick &&
22 echo third >file
25 test_expect_success 'GIT_EXTERNAL_DIFF environment' '
27 GIT_EXTERNAL_DIFF=echo git diff | {
28 read path oldfile oldhex oldmode newfile newhex newmode &&
29 test "z$path" = zfile &&
30 test "z$oldmode" = z100644 &&
31 test "z$newhex" = "z$ZERO_OID" &&
32 test "z$newmode" = z100644 &&
33 oh=$(git rev-parse --verify HEAD:file) &&
34 test "z$oh" = "z$oldhex"
39 test_expect_success 'GIT_EXTERNAL_DIFF environment should apply only to diff' '
41 GIT_EXTERNAL_DIFF=echo git log -p -1 HEAD |
42 grep "^diff --git a/file b/file"
46 test_expect_success 'GIT_EXTERNAL_DIFF environment and --no-ext-diff' '
48 GIT_EXTERNAL_DIFF=echo git diff --no-ext-diff |
49 grep "^diff --git a/file b/file"
53 test_expect_success SYMLINKS 'typechange diff' '
54 rm -f file &&
55 ln -s elif file &&
56 GIT_EXTERNAL_DIFF=echo git diff | {
57 read path oldfile oldhex oldmode newfile newhex newmode &&
58 test "z$path" = zfile &&
59 test "z$oldmode" = z100644 &&
60 test "z$newhex" = "z$ZERO_OID" &&
61 test "z$newmode" = z120000 &&
62 oh=$(git rev-parse --verify HEAD:file) &&
63 test "z$oh" = "z$oldhex"
64 } &&
65 GIT_EXTERNAL_DIFF=echo git diff --no-ext-diff >actual &&
66 git diff >expect &&
67 test_cmp expect actual
70 test_expect_success 'diff.external' '
71 git reset --hard &&
72 echo third >file &&
73 test_config diff.external echo &&
74 git diff | {
75 read path oldfile oldhex oldmode newfile newhex newmode &&
76 test "z$path" = zfile &&
77 test "z$oldmode" = z100644 &&
78 test "z$newhex" = "z$ZERO_OID" &&
79 test "z$newmode" = z100644 &&
80 oh=$(git rev-parse --verify HEAD:file) &&
81 test "z$oh" = "z$oldhex"
85 test_expect_success 'diff.external should apply only to diff' '
86 test_config diff.external echo &&
87 git log -p -1 HEAD |
88 grep "^diff --git a/file b/file"
91 test_expect_success 'diff.external and --no-ext-diff' '
92 test_config diff.external echo &&
93 git diff --no-ext-diff |
94 grep "^diff --git a/file b/file"
97 test_expect_success 'diff attribute' '
98 git reset --hard &&
99 echo third >file &&
101 git config diff.parrot.command echo &&
103 echo >.gitattributes "file diff=parrot" &&
105 git diff | {
106 read path oldfile oldhex oldmode newfile newhex newmode &&
107 test "z$path" = zfile &&
108 test "z$oldmode" = z100644 &&
109 test "z$newhex" = "z$ZERO_OID" &&
110 test "z$newmode" = z100644 &&
111 oh=$(git rev-parse --verify HEAD:file) &&
112 test "z$oh" = "z$oldhex"
117 test_expect_success 'diff attribute should apply only to diff' '
119 git log -p -1 HEAD |
120 grep "^diff --git a/file b/file"
124 test_expect_success 'diff attribute and --no-ext-diff' '
126 git diff --no-ext-diff |
127 grep "^diff --git a/file b/file"
131 test_expect_success 'diff attribute' '
133 git config --unset diff.parrot.command &&
134 git config diff.color.command echo &&
136 echo >.gitattributes "file diff=color" &&
138 git diff | {
139 read path oldfile oldhex oldmode newfile newhex newmode &&
140 test "z$path" = zfile &&
141 test "z$oldmode" = z100644 &&
142 test "z$newhex" = "z$ZERO_OID" &&
143 test "z$newmode" = z100644 &&
144 oh=$(git rev-parse --verify HEAD:file) &&
145 test "z$oh" = "z$oldhex"
150 test_expect_success 'diff attribute should apply only to diff' '
152 git log -p -1 HEAD |
153 grep "^diff --git a/file b/file"
157 test_expect_success 'diff attribute and --no-ext-diff' '
159 git diff --no-ext-diff |
160 grep "^diff --git a/file b/file"
164 test_expect_success 'GIT_EXTERNAL_DIFF trumps diff.external' '
165 >.gitattributes &&
166 test_config diff.external "echo ext-global" &&
167 GIT_EXTERNAL_DIFF="echo ext-env" git diff | grep ext-env
170 test_expect_success 'attributes trump GIT_EXTERNAL_DIFF and diff.external' '
171 test_config diff.foo.command "echo ext-attribute" &&
172 test_config diff.external "echo ext-global" &&
173 echo "file diff=foo" >.gitattributes &&
174 GIT_EXTERNAL_DIFF="echo ext-env" git diff | grep ext-attribute
177 test_expect_success 'no diff with -diff' '
178 echo >.gitattributes "file -diff" &&
179 git diff | grep Binary
182 echo NULZbetweenZwords | perl -pe 'y/Z/\000/' > file
184 test_expect_success 'force diff with "diff"' '
185 after=$(git hash-object file) &&
186 after=$(git rev-parse --short $after) &&
187 echo >.gitattributes "file diff" &&
188 git diff >actual &&
189 sed -e "s/^index .*/index $before..$after 100644/" \
190 "$TEST_DIRECTORY"/t4020/diff.NUL >expected-diff &&
191 test_cmp expected-diff actual
194 test_expect_success 'GIT_EXTERNAL_DIFF with more than one changed files' '
195 echo anotherfile > file2 &&
196 git add file2 &&
197 git commit -m "added 2nd file" &&
198 echo modified >file2 &&
199 GIT_EXTERNAL_DIFF=echo git diff
202 test_expect_success 'GIT_EXTERNAL_DIFF path counter/total' '
203 write_script external-diff.sh <<-\EOF &&
204 echo $GIT_DIFF_PATH_COUNTER of $GIT_DIFF_PATH_TOTAL >>counter.txt
206 >counter.txt &&
207 cat >expect <<-\EOF &&
208 1 of 2
209 2 of 2
211 GIT_EXTERNAL_DIFF=./external-diff.sh git diff &&
212 test_cmp expect counter.txt
215 test_expect_success 'GIT_EXTERNAL_DIFF generates pretty paths' '
216 touch file.ext &&
217 git add file.ext &&
218 echo with extension > file.ext &&
219 GIT_EXTERNAL_DIFF=echo git diff file.ext | grep ......_file\.ext &&
220 git update-index --force-remove file.ext &&
221 rm file.ext
224 echo "#!$SHELL_PATH" >fake-diff.sh
225 cat >> fake-diff.sh <<\EOF
226 cat $2 >> crlfed.txt
228 chmod a+x fake-diff.sh
230 keep_only_cr () {
231 tr -dc '\015'
234 test_expect_success 'external diff with autocrlf = true' '
235 test_config core.autocrlf true &&
236 GIT_EXTERNAL_DIFF=./fake-diff.sh git diff &&
237 test $(wc -l < crlfed.txt) = $(cat crlfed.txt | keep_only_cr | wc -c)
240 test_expect_success 'diff --cached' '
241 test_config core.autocrlf true &&
242 git add file &&
243 git update-index --assume-unchanged file &&
244 echo second >file &&
245 git diff --cached >actual &&
246 test_cmp expected-diff actual
249 test_expect_success 'clean up crlf leftovers' '
250 git update-index --no-assume-unchanged file &&
251 rm -f file* &&
252 git reset --hard
255 test_expect_success 'submodule diff' '
256 git init sub &&
257 ( cd sub && test_commit sub1 ) &&
258 git add sub &&
259 test_tick &&
260 git commit -m "add submodule" &&
261 ( cd sub && test_commit sub2 ) &&
262 write_script gather_pre_post.sh <<-\EOF &&
263 echo "$1 $4" # path, mode
264 cat "$2" # old file
265 cat "$5" # new file
267 GIT_EXTERNAL_DIFF=./gather_pre_post.sh git diff >actual &&
268 cat >expected <<-EOF &&
269 sub 160000
270 Subproject commit $(git rev-parse HEAD:sub)
271 Subproject commit $(cd sub && git rev-parse HEAD)
273 test_cmp expected actual
276 test_done