Skip to content

Commit 36617af

Browse files
apelissegitster
authored andcommitted
diff: add --ignore-blank-lines option
The goal of the patch is to introduce the GNU diff -B/--ignore-blank-lines as closely as possible. The short option is not available because it's already used for "break-rewrites". When this option is used, git-diff will not create hunks that simply add or remove empty lines, but will still show empty lines addition/suppression if they are close enough to "valuable" changes. There are two differences between this option and GNU diff -B option: - GNU diff doesn't have "--inter-hunk-context", so this must be handled - The following sequence looks like a bug (context is displayed twice): $ seq 5 >file1 $ cat <<EOF >file2 change 1 2 3 4 5 change EOF $ diff -u -B file1 file2 --- file1 2013-06-08 22:13:04.471517834 +0200 +++ file2 2013-06-08 22:13:23.275517855 +0200 @@ -1,5 +1,7 @@ +change 1 2 + 3 4 5 @@ -3,3 +5,4 @@ 3 4 5 +change So here is a more thorough description of the option: - real changes are interesting - blank lines that are close enough (less than context size) to interesting changes are considered interesting (recursive definition) - "context" lines are used around each hunk of interesting changes - If two hunks are separated by less than "inter-hunk-context", they will be merged into one. The implementation does the "interesting changes selection" in a single pass. Signed-off-by: Antoine Pelisse <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent edca415 commit 36617af

File tree

10 files changed

+439
-8
lines changed

10 files changed

+439
-8
lines changed

Documentation/diff-options.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,9 @@ endif::git-format-patch[]
439439
differences even if one line has whitespace where the other
440440
line has none.
441441

442+
--ignore-blank-lines::
443+
Ignore changes whose lines are all blank.
444+
442445
--inter-hunk-context=<lines>::
443446
Show the context between diff hunks, up to the specified number
444447
of lines, thereby fusing hunks that are close to each other.

diff.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3593,6 +3593,8 @@ int diff_opt_parse(struct diff_options *options, const char **av, int ac)
35933593
DIFF_XDL_SET(options, IGNORE_WHITESPACE_CHANGE);
35943594
else if (!strcmp(arg, "--ignore-space-at-eol"))
35953595
DIFF_XDL_SET(options, IGNORE_WHITESPACE_AT_EOL);
3596+
else if (!strcmp(arg, "--ignore-blank-lines"))
3597+
DIFF_XDL_SET(options, IGNORE_BLANK_LINES);
35963598
else if (!strcmp(arg, "--patience"))
35973599
options->xdl_opts = DIFF_WITH_ALG(options, PATIENCE_DIFF);
35983600
else if (!strcmp(arg, "--histogram"))

t/t4015-diff-whitespace.sh

Lines changed: 345 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,351 @@ EOF
142142
git diff --ignore-space-at-eol > out
143143
test_expect_success 'another test, with --ignore-space-at-eol' 'test_cmp expect out'
144144

145+
test_expect_success 'ignore-blank-lines: only new lines' '
146+
test_seq 5 >x &&
147+
git update-index x &&
148+
test_seq 5 | sed "/3/i \\
149+
" >x &&
150+
git diff --ignore-blank-lines >out &&
151+
>expect &&
152+
test_cmp out expect
153+
'
154+
155+
test_expect_success 'ignore-blank-lines: only new lines with space' '
156+
test_seq 5 >x &&
157+
git update-index x &&
158+
test_seq 5 | sed "/3/i \ " >x &&
159+
git diff -w --ignore-blank-lines >out &&
160+
>expect &&
161+
test_cmp out expect
162+
'
163+
164+
test_expect_success 'ignore-blank-lines: after change' '
165+
cat <<-\EOF >x &&
166+
1
167+
2
168+
169+
3
170+
4
171+
5
172+
173+
6
174+
7
175+
EOF
176+
git update-index x &&
177+
cat <<-\EOF >x &&
178+
change
179+
180+
1
181+
2
182+
3
183+
4
184+
5
185+
6
186+
187+
7
188+
EOF
189+
git diff --inter-hunk-context=100 --ignore-blank-lines >out.tmp &&
190+
cat <<-\EOF >expected &&
191+
diff --git a/x b/x
192+
--- a/x
193+
+++ b/x
194+
@@ -1,6 +1,7 @@
195+
+change
196+
+
197+
1
198+
2
199+
-
200+
3
201+
4
202+
5
203+
EOF
204+
compare_diff_patch expected out.tmp
205+
'
206+
207+
test_expect_success 'ignore-blank-lines: before change' '
208+
cat <<-\EOF >x &&
209+
1
210+
2
211+
212+
3
213+
4
214+
5
215+
6
216+
7
217+
EOF
218+
git update-index x &&
219+
cat <<-\EOF >x &&
220+
221+
1
222+
2
223+
3
224+
4
225+
5
226+
227+
6
228+
7
229+
change
230+
EOF
231+
git diff --inter-hunk-context=100 --ignore-blank-lines >out.tmp &&
232+
cat <<-\EOF >expected &&
233+
diff --git a/x b/x
234+
--- a/x
235+
+++ b/x
236+
@@ -4,5 +4,7 @@
237+
3
238+
4
239+
5
240+
+
241+
6
242+
7
243+
+change
244+
EOF
245+
compare_diff_patch expected out.tmp
246+
'
247+
248+
test_expect_success 'ignore-blank-lines: between changes' '
249+
cat <<-\EOF >x &&
250+
1
251+
2
252+
3
253+
4
254+
5
255+
256+
257+
6
258+
7
259+
8
260+
9
261+
10
262+
EOF
263+
git update-index x &&
264+
cat <<-\EOF >x &&
265+
change
266+
1
267+
2
268+
269+
3
270+
4
271+
5
272+
6
273+
7
274+
8
275+
276+
9
277+
10
278+
change
279+
EOF
280+
git diff --ignore-blank-lines >out.tmp &&
281+
cat <<-\EOF >expected &&
282+
diff --git a/x b/x
283+
--- a/x
284+
+++ b/x
285+
@@ -1,5 +1,7 @@
286+
+change
287+
1
288+
2
289+
+
290+
3
291+
4
292+
5
293+
@@ -8,5 +8,7 @@
294+
6
295+
7
296+
8
297+
+
298+
9
299+
10
300+
+change
301+
EOF
302+
compare_diff_patch expected out.tmp
303+
'
304+
305+
test_expect_success 'ignore-blank-lines: between changes (with interhunkctx)' '
306+
test_seq 10 >x &&
307+
git update-index x &&
308+
cat <<-\EOF >x &&
309+
change
310+
1
311+
2
312+
313+
3
314+
4
315+
5
316+
317+
6
318+
7
319+
8
320+
9
321+
322+
10
323+
change
324+
EOF
325+
git diff --inter-hunk-context=2 --ignore-blank-lines >out.tmp &&
326+
cat <<-\EOF >expected &&
327+
diff --git a/x b/x
328+
--- a/x
329+
+++ b/x
330+
@@ -1,10 +1,15 @@
331+
+change
332+
1
333+
2
334+
+
335+
3
336+
4
337+
5
338+
+
339+
6
340+
7
341+
8
342+
9
343+
+
344+
10
345+
+change
346+
EOF
347+
compare_diff_patch expected out.tmp
348+
'
349+
350+
test_expect_success 'ignore-blank-lines: scattered spaces' '
351+
test_seq 10 >x &&
352+
git update-index x &&
353+
cat <<-\EOF >x &&
354+
change
355+
1
356+
2
357+
3
358+
359+
4
360+
361+
5
362+
363+
6
364+
365+
366+
7
367+
368+
8
369+
9
370+
10
371+
change
372+
EOF
373+
git diff --inter-hunk-context=4 --ignore-blank-lines >out.tmp &&
374+
cat <<-\EOF >expected &&
375+
diff --git a/x b/x
376+
--- a/x
377+
+++ b/x
378+
@@ -1,3 +1,4 @@
379+
+change
380+
1
381+
2
382+
3
383+
@@ -8,3 +15,4 @@
384+
8
385+
9
386+
10
387+
+change
388+
EOF
389+
compare_diff_patch expected out.tmp
390+
'
391+
392+
test_expect_success 'ignore-blank-lines: spaces coalesce' '
393+
test_seq 6 >x &&
394+
git update-index x &&
395+
cat <<-\EOF >x &&
396+
change
397+
1
398+
2
399+
3
400+
401+
4
402+
403+
5
404+
405+
6
406+
change
407+
EOF
408+
git diff --inter-hunk-context=4 --ignore-blank-lines >out.tmp &&
409+
cat <<-\EOF >expected &&
410+
diff --git a/x b/x
411+
--- a/x
412+
+++ b/x
413+
@@ -1,6 +1,11 @@
414+
+change
415+
1
416+
2
417+
3
418+
+
419+
4
420+
+
421+
5
422+
+
423+
6
424+
+change
425+
EOF
426+
compare_diff_patch expected out.tmp
427+
'
428+
429+
test_expect_success 'ignore-blank-lines: mix changes and blank lines' '
430+
test_seq 16 >x &&
431+
git update-index x &&
432+
cat <<-\EOF >x &&
433+
change
434+
1
435+
2
436+
437+
3
438+
4
439+
5
440+
change
441+
6
442+
7
443+
8
444+
445+
9
446+
10
447+
11
448+
change
449+
12
450+
13
451+
14
452+
453+
15
454+
16
455+
change
456+
EOF
457+
git diff --ignore-blank-lines >out.tmp &&
458+
cat <<-\EOF >expected &&
459+
diff --git a/x b/x
460+
--- a/x
461+
+++ b/x
462+
@@ -1,8 +1,11 @@
463+
+change
464+
1
465+
2
466+
+
467+
3
468+
4
469+
5
470+
+change
471+
6
472+
7
473+
8
474+
@@ -9,8 +13,11 @@
475+
9
476+
10
477+
11
478+
+change
479+
12
480+
13
481+
14
482+
+
483+
15
484+
16
485+
+change
486+
EOF
487+
compare_diff_patch expected out.tmp
488+
'
489+
145490
test_expect_success 'check mixed spaces and tabs in indent' '
146491
147492
# This is indented with SP HT SP.

xdiff/xdiff.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ extern "C" {
3939
#define XDF_DIFF_ALGORITHM_MASK (XDF_PATIENCE_DIFF | XDF_HISTOGRAM_DIFF)
4040
#define XDF_DIFF_ALG(x) ((x) & XDF_DIFF_ALGORITHM_MASK)
4141

42+
#define XDF_IGNORE_BLANK_LINES (1 << 7)
43+
4244
#define XDL_EMIT_FUNCNAMES (1 << 0)
4345
#define XDL_EMIT_COMMON (1 << 1)
4446
#define XDL_EMIT_FUNCCONTEXT (1 << 2)

0 commit comments

Comments
 (0)