3 test_description='external diff interface test'
7 test_expect_success setup '
12 git commit -m initial &&
16 before=$(git hash-object file) &&
17 before=$(git rev-parse --short $before) &&
19 git commit -m second &&
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' '
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"
65 GIT_EXTERNAL_DIFF=echo git diff --no-ext-diff >actual &&
67 test_cmp expect actual
70 test_expect_success 'diff.external' '
73 test_config diff.external echo &&
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 &&
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' '
101 git config diff.parrot.command echo &&
103 echo >.gitattributes "file diff=parrot" &&
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' '
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" &&
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' '
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' '
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" &&
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 &&
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
207 cat >expect <<-\EOF &&
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' '
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 &&
224 echo "#!$SHELL_PATH" >fake-diff.sh
225 cat >> fake-diff.sh <<\EOF
228 chmod a+x fake-diff.sh
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 &&
243 git update-index --assume-unchanged 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 &&
255 test_expect_success 'submodule diff' '
257 ( cd sub && test_commit sub1 ) &&
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
267 GIT_EXTERNAL_DIFF=./gather_pre_post.sh git diff >actual &&
268 cat >expected <<-EOF &&
270 Subproject commit $(git rev-parse HEAD:sub)
271 Subproject commit $(cd sub && git rev-parse HEAD)
273 test_cmp expected actual