diff: enable indent heuristic by default

The feature was included in v2.11 (released 2016-11-29) and we got no negative feedback. Quite the opposite, all feedback we got was positive. Turn it on by default. Users who dislike the feature can turn it off by setting diff.indentHeuristic (which also configures plumbing commands, see prior patches). The change to t/t4051-diff-function-context.sh is needed because the heuristic shifts the changed hunk in the patch. To get the same result regardless of the heuristic configuration, we modify the test file differently: We insert a completely new line after line 2, instead of simply duplicating it. Helped-by: Jeff King <peff@peff.net> Signed-off-by: Stefan Beller <sbeller@google.com> Signed-off-by: Marc Branchaud <marcnarc@xiplink.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stefan Beller committed May 8, 2017 at 12:03 UTC 33de7163879ca83be7d6e1583c125f84e1e7c329
3 files changed +116 -29
diff.c
+1 -1
@@ -27,7 +27,7 @@
27 #endif
28
29 static int diff_detect_rename_default;
30 -static int diff_indent_heuristic; /* experimental */
30 +static int diff_indent_heuristic = 1;
31 static int diff_rename_limit_default = 400;
32 static int diff_suppress_blank_empty;
33 static int diff_use_color_default = -1;
t/t4051-diff-function-context.sh
+2 -1
@@ -72,7 +72,8 @@ test_expect_success 'setup' '
72
73 # overlap function context of 1st change and -u context of 2nd change
74 grep -v "delete me from hello" <"$dir/hello.c" >file.c &&
75 - sed 2p <"$dir/dummy.c" >>file.c &&
75 + sed "2a\\
76 + extra line" <"$dir/dummy.c" >>file.c &&
77 commit_and_tag changed_hello_dummy file.c &&
78
79 git checkout initial &&
t/t4061-diff-indent.sh
+113 -27
@@ -152,26 +152,28 @@ test_expect_success 'prepare' '
152 EOF
153 '
154
155 +# --- diff tests ----------------------------------------------------------
156 +
157 test_expect_success 'diff: ugly spaces' '
156 - git diff old new -- spaces.txt >out &&
158 + git diff --no-indent-heuristic old new -- spaces.txt >out &&
159 compare_diff spaces-expect out
160 '
161
162 +test_expect_success 'diff: --no-indent-heuristic overrides config' '
163 + git -c diff.indentHeuristic=true diff --no-indent-heuristic old new -- spaces.txt >out2 &&
164 + compare_diff spaces-expect out2
165 +'
166 +
167 test_expect_success 'diff: nice spaces with --indent-heuristic' '
161 - git diff --indent-heuristic old new -- spaces.txt >out-compacted &&
168 + git -c diff.indentHeuristic=false diff --indent-heuristic old new -- spaces.txt >out-compacted &&
169 compare_diff spaces-compacted-expect out-compacted
170 '
171
165 -test_expect_success 'diff: nice spaces with diff.indentHeuristic' '
172 +test_expect_success 'diff: nice spaces with diff.indentHeuristic=true' '
173 git -c diff.indentHeuristic=true diff old new -- spaces.txt >out-compacted2 &&
174 compare_diff spaces-compacted-expect out-compacted2
175 '
176
170 -test_expect_success 'diff: --no-indent-heuristic overrides config' '
171 - git -c diff.indentHeuristic=true diff --no-indent-heuristic old new -- spaces.txt >out2 &&
172 - compare_diff spaces-expect out2
173 -'
174 -
177 test_expect_success 'diff: --indent-heuristic with --patience' '
178 git diff --indent-heuristic --patience old new -- spaces.txt >out-compacted3 &&
179 compare_diff spaces-compacted-expect out-compacted3
@@ -183,7 +185,7 @@ test_expect_success 'diff: --indent-heuristic with --histogram' '
185 '
186
187 test_expect_success 'diff: ugly functions' '
186 - git diff old new -- functions.c >out &&
188 + git diff --no-indent-heuristic old new -- functions.c >out &&
189 compare_diff functions-expect out
190 '
191
@@ -192,42 +194,73 @@ test_expect_success 'diff: nice functions with --indent-heuristic' '
194 compare_diff functions-compacted-expect out-compacted
195 '
196
195 -test_expect_success 'blame: ugly spaces' '
196 - git blame old..new -- spaces.txt >out-blame &&
197 - compare_blame spaces-expect out-blame
198 -'
197 +# --- blame tests ---------------------------------------------------------
198
199 test_expect_success 'blame: nice spaces with --indent-heuristic' '
200 git blame --indent-heuristic old..new -- spaces.txt >out-blame-compacted &&
201 compare_blame spaces-compacted-expect out-blame-compacted
202 '
203
205 -test_expect_success 'blame: nice spaces with diff.indentHeuristic' '
204 +test_expect_success 'blame: nice spaces with diff.indentHeuristic=true' '
205 git -c diff.indentHeuristic=true blame old..new -- spaces.txt >out-blame-compacted2 &&
206 compare_blame spaces-compacted-expect out-blame-compacted2
207 '
208
209 +test_expect_success 'blame: ugly spaces with --no-indent-heuristic' '
210 + git blame --no-indent-heuristic old..new -- spaces.txt >out-blame &&
211 + compare_blame spaces-expect out-blame
212 +'
213 +
214 +test_expect_success 'blame: ugly spaces with diff.indentHeuristic=false' '
215 + git -c diff.indentHeuristic=false blame old..new -- spaces.txt >out-blame2 &&
216 + compare_blame spaces-expect out-blame2
217 +'
218 +
219 test_expect_success 'blame: --no-indent-heuristic overrides config' '
211 - git -c diff.indentHeuristic=true blame --no-indent-heuristic old..new -- spaces.txt >out-blame2 &&
220 + git -c diff.indentHeuristic=true blame --no-indent-heuristic old..new -- spaces.txt >out-blame3 &&
221 git blame old..new -- spaces.txt >out-blame &&
213 - compare_blame spaces-expect out-blame2
222 + compare_blame spaces-expect out-blame3
223 '
224
225 +test_expect_success 'blame: --indent-heuristic overrides config' '
226 + git -c diff.indentHeuristic=false blame --indent-heuristic old..new -- spaces.txt >out-blame-compacted3 &&
227 + compare_blame spaces-compacted-expect out-blame-compacted2
228 +'
229 +
230 +# --- diff-tree tests -----------------------------------------------------
231 +
232 test_expect_success 'diff-tree: nice spaces with --indent-heuristic' '
233 git diff-tree --indent-heuristic -p old new -- spaces.txt >out-diff-tree-compacted &&
234 compare_diff spaces-compacted-expect out-diff-tree-compacted
235 '
236
221 -test_expect_success 'diff-tree: nice spaces with diff.indentHeuristic' '
237 +test_expect_success 'diff-tree: nice spaces with diff.indentHeuristic=true' '
238 git -c diff.indentHeuristic=true diff-tree -p old new -- spaces.txt >out-diff-tree-compacted2 &&
239 compare_diff spaces-compacted-expect out-diff-tree-compacted2
240 '
241
226 -test_expect_success 'diff-tree: --no-indent-heuristic overrides config' '
227 - git -c diff.indentHeuristic=true diff-tree --no-indent-heuristic -p old new -- spaces.txt >out-diff-tree &&
242 +test_expect_success 'diff-tree: ugly spaces with --no-indent-heuristic' '
243 + git diff-tree --no-indent-heuristic -p old new -- spaces.txt >out-diff-tree &&
244 compare_diff spaces-expect out-diff-tree
245 '
246
247 +test_expect_success 'diff-tree: ugly spaces with diff.indentHeuristic=false' '
248 + git -c diff.indentHeuristic=false diff-tree -p old new -- spaces.txt >out-diff-tree2 &&
249 + compare_diff spaces-expect out-diff-tree2
250 +'
251 +
252 +test_expect_success 'diff-tree: --indent-heuristic overrides config' '
253 + git -c diff.indentHeuristic=false diff-tree --indent-heuristic -p old new -- spaces.txt >out-diff-tree-compacted3 &&
254 + compare_diff spaces-compacted-expect out-diff-tree-compacted3
255 +'
256 +
257 +test_expect_success 'diff-tree: --no-indent-heuristic overrides config' '
258 + git -c diff.indentHeuristic=true diff-tree --no-indent-heuristic -p old new -- spaces.txt >out-diff-tree3 &&
259 + compare_diff spaces-expect out-diff-tree3
260 +'
261 +
262 +# --- diff-index tests ----------------------------------------------------
263 +
264 test_expect_success 'diff-index: nice spaces with --indent-heuristic' '
265 git checkout -B diff-index &&
266 git reset --soft HEAD~ &&
@@ -236,7 +269,7 @@ test_expect_success 'diff-index: nice spaces with --indent-heuristic' '
269 git checkout -f master
270 '
271
239 -test_expect_success 'diff-index: nice spaces with diff.indentHeuristic' '
272 +test_expect_success 'diff-index: nice spaces with diff.indentHeuristic=true' '
273 git checkout -B diff-index &&
274 git reset --soft HEAD~ &&
275 git -c diff.indentHeuristic=true diff-index -p old -- spaces.txt >out-diff-index-compacted2 &&
@@ -244,24 +277,50 @@ test_expect_success 'diff-index: nice spaces with diff.indentHeuristic' '
277 git checkout -f master
278 '
279
247 -test_expect_success 'diff-index: --no-indent-heuristic overrides config' '
280 +test_expect_success 'diff-index: ugly spaces with --no-indent-heuristic' '
281 git checkout -B diff-index &&
282 git reset --soft HEAD~ &&
250 - git -c diff.indentHeuristic=true diff-index --no-indent-heuristic -p old -- spaces.txt >out-diff-index &&
283 + git diff-index --no-indent-heuristic -p old -- spaces.txt >out-diff-index &&
284 compare_diff spaces-expect out-diff-index &&
285 git checkout -f master
286 '
287
255 -test_expect_success 'diff-files: nice spaces with diff.indentHeuristic' '
288 +test_expect_success 'diff-index: ugly spaces with diff.indentHeuristic=false' '
289 + git checkout -B diff-index &&
290 + git reset --soft HEAD~ &&
291 + git -c diff.indentHeuristic=false diff-index -p old -- spaces.txt >out-diff-index2 &&
292 + compare_diff spaces-expect out-diff-index2 &&
293 + git checkout -f master
294 +'
295 +
296 +test_expect_success 'diff-index: --indent-heuristic overrides config' '
297 + git checkout -B diff-index &&
298 + git reset --soft HEAD~ &&
299 + git -c diff.indentHeuristic=false diff-index --indent-heuristic -p old -- spaces.txt >out-diff-index-compacted3 &&
300 + compare_diff spaces-compacted-expect out-diff-index-compacted3 &&
301 + git checkout -f master
302 +'
303 +
304 +test_expect_success 'diff-index: --no-indent-heuristic overrides config' '
305 + git checkout -B diff-index &&
306 + git reset --soft HEAD~ &&
307 + git -c diff.indentHeuristic=true diff-index --no-indent-heuristic -p old -- spaces.txt >out-diff-index3 &&
308 + compare_diff spaces-expect out-diff-index3 &&
309 + git checkout -f master
310 +'
311 +
312 +# --- diff-files tests ----------------------------------------------------
313 +
314 +test_expect_success 'diff-files: nice spaces with --indent-heuristic' '
315 git checkout -B diff-files &&
316 git reset HEAD~ &&
258 - git -c diff.indentHeuristic=true diff-files -p spaces.txt >out-diff-files-raw &&
317 + git diff-files --indent-heuristic -p spaces.txt >out-diff-files-raw &&
318 grep -v index out-diff-files-raw >out-diff-files-compacted &&
319 compare_diff spaces-compacted-expect out-diff-files-compacted &&
320 git checkout -f master
321 '
322
264 -test_expect_success 'diff-files: nice spaces with diff.indentHeuristic' '
323 +test_expect_success 'diff-files: nice spaces with diff.indentHeuristic=true' '
324 git checkout -B diff-files &&
325 git reset HEAD~ &&
326 git -c diff.indentHeuristic=true diff-files -p spaces.txt >out-diff-files-raw2 &&
@@ -270,11 +329,38 @@ test_expect_success 'diff-files: nice spaces with diff.indentHeuristic' '
329 git checkout -f master
330 '
331
332 +test_expect_success 'diff-files: ugly spaces with --no-indent-heuristic' '
333 + git checkout -B diff-files &&
334 + git reset HEAD~ &&
335 + git diff-files --no-indent-heuristic -p spaces.txt >out-diff-files-raw &&
336 + grep -v index out-diff-files-raw >out-diff-files &&
337 + compare_diff spaces-expect out-diff-files &&
338 + git checkout -f master
339 +'
340 +
341 +test_expect_success 'diff-files: ugly spaces with diff.indentHeuristic=false' '
342 + git checkout -B diff-files &&
343 + git reset HEAD~ &&
344 + git -c diff.indentHeuristic=false diff-files -p spaces.txt >out-diff-files-raw2 &&
345 + grep -v index out-diff-files-raw2 >out-diff-files &&
346 + compare_diff spaces-expect out-diff-files &&
347 + git checkout -f master
348 +'
349 +
350 +test_expect_success 'diff-files: --indent-heuristic overrides config' '
351 + git checkout -B diff-files &&
352 + git reset HEAD~ &&
353 + git -c diff.indentHeuristic=false diff-files --indent-heuristic -p spaces.txt >out-diff-files-raw3 &&
354 + grep -v index out-diff-files-raw3 >out-diff-files-compacted &&
355 + compare_diff spaces-compacted-expect out-diff-files-compacted &&
356 + git checkout -f master
357 +'
358 +
359 test_expect_success 'diff-files: --no-indent-heuristic overrides config' '
360 git checkout -B diff-files &&
361 git reset HEAD~ &&
276 - git -c diff.indentHeuristic=true diff-files --no-indent-heuristic -p spaces.txt >out-diff-files-raw3 &&
277 - grep -v index out-diff-files-raw3 >out-diff-files &&
362 + git -c diff.indentHeuristic=true diff-files --no-indent-heuristic -p spaces.txt >out-diff-files-raw4 &&
363 + grep -v index out-diff-files-raw4 >out-diff-files &&
364 compare_diff spaces-expect out-diff-files &&
365 git checkout -f master
366 '