From e68edb8cf06a796453378b98d963692c838c400f Mon Sep 17 00:00:00 2001 From: Kyle Evans Date: Sun, 19 Aug 2018 03:57:20 +0000 Subject: [PATCH] diff(1): Implement -B/--ignore-blank-lines As noted by cem in r338035, coccinelle invokes diff(1) with the -B flag. This was not previously implemented here, so one was forced to create a link for GNU diff to /usr/local/bin/diff Implement the -B flag and add some primitive tests for it. It is implemented in the same fashion that -I is implemented; each chunk's lines are scanned, and if a non-blank line is encountered then the chunk will be output. Otherwise, it's skipped. MFC after: 2 weeks --- usr.bin/diff/TODO | 1 - usr.bin/diff/diff.1 | 14 ++++++++------ usr.bin/diff/diff.c | 14 +++++++++----- usr.bin/diff/diff.h | 1 + usr.bin/diff/diffreg.c | 26 ++++++++++++++++++++++++++ usr.bin/diff/tests/Bflag_C.out | 2 ++ usr.bin/diff/tests/Bflag_D.out | 2 ++ usr.bin/diff/tests/Bflag_F.out | 4 ++++ usr.bin/diff/tests/Makefile | 3 +++ usr.bin/diff/tests/diff_test.sh | 17 +++++++++++++++++ 10 files changed, 72 insertions(+), 12 deletions(-) create mode 100644 usr.bin/diff/tests/Bflag_C.out create mode 100644 usr.bin/diff/tests/Bflag_D.out create mode 100644 usr.bin/diff/tests/Bflag_F.out diff --git a/usr.bin/diff/TODO b/usr.bin/diff/TODO index 537643dd8903..fd58d64ace51 100644 --- a/usr.bin/diff/TODO +++ b/usr.bin/diff/TODO @@ -5,7 +5,6 @@ * make a libsdiff and use that directly to avoid duplicating the code to be implemented: ---ignore-blank-lines --horizon-lines --ignore-tab-expansion --line-format diff --git a/usr.bin/diff/diff.1 b/usr.bin/diff/diff.1 index 0618756aefb0..2ceb273ead1d 100644 --- a/usr.bin/diff/diff.1 +++ b/usr.bin/diff/diff.1 @@ -30,7 +30,7 @@ .\" @(#)diff.1 8.1 (Berkeley) 6/30/93 .\" $FreeBSD$ .\" -.Dd April 20, 2017 +.Dd August 18, 2018 .Dt DIFF 1 .Os .Sh NAME @@ -38,7 +38,7 @@ .Nd differential file and directory comparator .Sh SYNOPSIS .Nm diff -.Op Fl abdipTtw +.Op Fl aBbdipTtw .Oo .Fl c | e | f | .Fl n | q | u @@ -67,7 +67,7 @@ .Op Fl L Ar label | Fl -label Ar label .Ar file1 file2 .Nm diff -.Op Fl abdilpTtw +.Op Fl aBbdilpTtw .Op Fl I Ar pattern | Fl -ignore-matching-lines Ar pattern .Op Fl L Ar label | Fl -label Ar label .Op Fl -brief @@ -93,7 +93,7 @@ .Fl C Ar number | -context Ar number .Ar file1 file2 .Nm diff -.Op Fl abdiltw +.Op Fl aBbdiltw .Op Fl I Ar pattern | Fl -ignore-matching-lines Ar pattern .Op Fl -brief .Op Fl -changed-group-format Ar GFMT @@ -118,7 +118,7 @@ .Fl D Ar string | Fl -ifdef Ar string .Ar file1 file2 .Nm diff -.Op Fl abdilpTtw +.Op Fl aBbdilpTtw .Op Fl I Ar pattern | Fl -ignore-matching-lines Ar pattern .Op Fl L Ar label | Fl -label Ar label .Op Fl -brief @@ -144,7 +144,7 @@ .Fl U Ar number | Fl -unified Ar number .Ar file1 file2 .Nm diff -.Op Fl abdilNPprsTtw +.Op Fl aBbdilNPprsTtw .Oo .Fl c | e | f | .Fl n | q | u @@ -300,6 +300,8 @@ if files contain binary characters. Use of this option forces .Nm to produce a diff. +.It Fl B Fl -ignore-blank-lines +Causes chunks that include only blank lines to be ignored. .It Fl b Causes trailing blanks (spaces and tabs) to be ignored, and other strings of blanks to compare equal. diff --git a/usr.bin/diff/diff.c b/usr.bin/diff/diff.c index e12697c4e89e..dbb6528c254f 100644 --- a/usr.bin/diff/diff.c +++ b/usr.bin/diff/diff.c @@ -66,6 +66,7 @@ static struct option longopts[] = { { "ed", no_argument, 0, 'e' }, { "forward-ed", no_argument, 0, 'f' }, { "speed-large-files", no_argument, NULL, 'H' }, + { "ignore-blank-lines", no_argument, 0, 'B' }, { "ignore-matching-lines", required_argument, 0, 'I' }, { "ignore-case", no_argument, 0, 'i' }, { "paginate", no_argument, NULL, 'l' }, @@ -164,6 +165,9 @@ main(int argc, char **argv) case 'h': /* silently ignore for backwards compatibility */ break; + case 'B': + dflags |= D_SKIPBLANKLINES; + break; case 'I': push_ignore_pats(optarg); break; @@ -447,18 +451,18 @@ void usage(void) { (void)fprintf(stderr, - "usage: diff [-abdilpTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n" + "usage: diff [-aBbdilpTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n" " [--no-ignore-case] [--normal] [--strip-trailing-cr] [--tabsize]\n" " [-I pattern] [-L label] file1 file2\n" - " diff [-abdilpTtw] [-I pattern] [-L label] [--ignore-case]\n" + " diff [-aBbdilpTtw] [-I pattern] [-L label] [--ignore-case]\n" " [--no-ignore-case] [--normal] [--strip-trailing-cr] [--tabsize]\n" " -C number file1 file2\n" - " diff [-abdiltw] [-I pattern] [--ignore-case] [--no-ignore-case]\n" + " diff [-aBbdiltw] [-I pattern] [--ignore-case] [--no-ignore-case]\n" " [--normal] [--strip-trailing-cr] [--tabsize] -D string file1 file2\n" - " diff [-abdilpTtw] [-I pattern] [-L label] [--ignore-case]\n" + " diff [-aBbdilpTtw] [-I pattern] [-L label] [--ignore-case]\n" " [--no-ignore-case] [--normal] [--tabsize] [--strip-trailing-cr]\n" " -U number file1 file2\n" - " diff [-abdilNPprsTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n" + " diff [-aBbdilNPprsTtw] [-c | -e | -f | -n | -q | -u] [--ignore-case]\n" " [--no-ignore-case] [--normal] [--tabsize] [-I pattern] [-L label]\n" " [-S name] [-X file] [-x pattern] dir1 dir2\n"); diff --git a/usr.bin/diff/diff.h b/usr.bin/diff/diff.h index 87452058220f..1801587f9e68 100644 --- a/usr.bin/diff/diff.h +++ b/usr.bin/diff/diff.h @@ -67,6 +67,7 @@ #define D_EXPANDTABS 0x100 /* Expand tabs to spaces */ #define D_IGNOREBLANKS 0x200 /* Ignore white space changes */ #define D_STRIPCR 0x400 /* Strip trailing cr */ +#define D_SKIPBLANKLINES 0x800 /* Skip blank lines */ /* * Status values for print_status() and diffreg() return values diff --git a/usr.bin/diff/diffreg.c b/usr.bin/diff/diffreg.c index 47825e1daf46..b872badbc599 100644 --- a/usr.bin/diff/diffreg.c +++ b/usr.bin/diff/diffreg.c @@ -79,6 +79,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -1000,6 +1001,31 @@ change(char *file1, FILE *f1, char *file2, FILE *f2, int a, int b, int c, int d, } return; } + if (*pflags & D_SKIPBLANKLINES) { + char *line; + /* + * All lines in the change, insert, or delete must not be + * empty for the change to be ignored. + */ + if (a <= b) { /* Changes and deletes. */ + for (i = a; i <= b; i++) { + line = preadline(fileno(f1), + ixold[i] - ixold[i - 1], ixold[i - 1]); + if (*line != '\0') + goto proceed; + } + } + if (a > b || c <= d) { /* Changes and inserts. */ + for (i = c; i <= d; i++) { + line = preadline(fileno(f2), + ixnew[i] - ixnew[i - 1], ixnew[i - 1]); + if (*line != '\0') + goto proceed; + } + } + return; + + } proceed: if (*pflags & D_HEADER && diff_format != D_BRIEF) { diff_output("%s %s %s\n", diffargs, file1, file2); diff --git a/usr.bin/diff/tests/Bflag_C.out b/usr.bin/diff/tests/Bflag_C.out new file mode 100644 index 000000000000..c7f130aff396 --- /dev/null +++ b/usr.bin/diff/tests/Bflag_C.out @@ -0,0 +1,2 @@ +1a2 +> diff --git a/usr.bin/diff/tests/Bflag_D.out b/usr.bin/diff/tests/Bflag_D.out new file mode 100644 index 000000000000..4c1170334935 --- /dev/null +++ b/usr.bin/diff/tests/Bflag_D.out @@ -0,0 +1,2 @@ +1a2 +> C diff --git a/usr.bin/diff/tests/Bflag_F.out b/usr.bin/diff/tests/Bflag_F.out new file mode 100644 index 000000000000..9dc5e5694048 --- /dev/null +++ b/usr.bin/diff/tests/Bflag_F.out @@ -0,0 +1,4 @@ +7c8 +< G +--- +> X diff --git a/usr.bin/diff/tests/Makefile b/usr.bin/diff/tests/Makefile index 62a0ab5c6188..78744d3bed8e 100644 --- a/usr.bin/diff/tests/Makefile +++ b/usr.bin/diff/tests/Makefile @@ -5,6 +5,9 @@ PACKAGE= tests ATF_TESTS_SH= diff_test ${PACKAGE}FILES+= \ + Bflag_C.out \ + Bflag_D.out \ + Bflag_F.out \ input1.in \ input2.in \ input_c1.in \ diff --git a/usr.bin/diff/tests/diff_test.sh b/usr.bin/diff/tests/diff_test.sh index 557eea049d4e..01942192e504 100755 --- a/usr.bin/diff/tests/diff_test.sh +++ b/usr.bin/diff/tests/diff_test.sh @@ -9,6 +9,7 @@ atf_test_case group_format atf_test_case side_by_side atf_test_case brief_format atf_test_case b230049 +atf_test_case Bflag simple_body() { @@ -150,6 +151,21 @@ brief_format_body() diff -Nrq A D } +Bflag_body() +{ + atf_check -x 'printf "A\nB\n" > A' + atf_check -x 'printf "A\n\nB\n" > B' + atf_check -x 'printf "A\n \nB\n" > C' + atf_check -x 'printf "A\nC\nB\n" > D' + atf_check -x 'printf "A\nB\nC\nD\nE\nF\nG\nH" > E' + atf_check -x 'printf "A\n\nB\nC\nD\nE\nF\nX\nH" > F' + + atf_check -s exit:0 -o inline:"" diff -B A B + atf_check -s exit:1 -o file:"$(atf_get_srcdir)/Bflag_C.out" diff -B A C + atf_check -s exit:1 -o file:"$(atf_get_srcdir)/Bflag_D.out" diff -B A D + atf_check -s exit:1 -o file:"$(atf_get_srcdir)/Bflag_F.out" diff -B E F +} + atf_init_test_cases() { atf_add_test_case simple @@ -161,4 +177,5 @@ atf_init_test_cases() atf_add_test_case side_by_side atf_add_test_case brief_format atf_add_test_case b230049 + atf_add_test_case Bflag }