svn commit: r339160 - in stable/11/usr.bin/diff: . tests

Kyle Evans kevans at FreeBSD.org
Wed Oct 3 17:16:20 UTC 2018


Author: kevans
Date: Wed Oct  3 17:16:18 2018
New Revision: 339160
URL: https://svnweb.freebsd.org/changeset/base/339160

Log:
  MFC r338039: 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.

Added:
  stable/11/usr.bin/diff/tests/Bflag_C.out
     - copied unchanged from r338039, head/usr.bin/diff/tests/Bflag_C.out
  stable/11/usr.bin/diff/tests/Bflag_D.out
     - copied unchanged from r338039, head/usr.bin/diff/tests/Bflag_D.out
  stable/11/usr.bin/diff/tests/Bflag_F.out
     - copied unchanged from r338039, head/usr.bin/diff/tests/Bflag_F.out
Modified:
  stable/11/usr.bin/diff/TODO
  stable/11/usr.bin/diff/diff.1
  stable/11/usr.bin/diff/diff.c
  stable/11/usr.bin/diff/diff.h
  stable/11/usr.bin/diff/diffreg.c
  stable/11/usr.bin/diff/tests/Makefile
  stable/11/usr.bin/diff/tests/diff_test.sh
Directory Properties:
  stable/11/   (props changed)

Modified: stable/11/usr.bin/diff/TODO
==============================================================================
--- stable/11/usr.bin/diff/TODO	Wed Oct  3 17:14:40 2018	(r339159)
+++ stable/11/usr.bin/diff/TODO	Wed Oct  3 17:16:18 2018	(r339160)
@@ -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

Modified: stable/11/usr.bin/diff/diff.1
==============================================================================
--- stable/11/usr.bin/diff/diff.1	Wed Oct  3 17:14:40 2018	(r339159)
+++ stable/11/usr.bin/diff/diff.1	Wed Oct  3 17:16:18 2018	(r339160)
@@ -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.

Modified: stable/11/usr.bin/diff/diff.c
==============================================================================
--- stable/11/usr.bin/diff/diff.c	Wed Oct  3 17:14:40 2018	(r339159)
+++ stable/11/usr.bin/diff/diff.c	Wed Oct  3 17:16:18 2018	(r339160)
@@ -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");
 

Modified: stable/11/usr.bin/diff/diff.h
==============================================================================
--- stable/11/usr.bin/diff/diff.h	Wed Oct  3 17:14:40 2018	(r339159)
+++ stable/11/usr.bin/diff/diff.h	Wed Oct  3 17:16:18 2018	(r339160)
@@ -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

Modified: stable/11/usr.bin/diff/diffreg.c
==============================================================================
--- stable/11/usr.bin/diff/diffreg.c	Wed Oct  3 17:14:40 2018	(r339159)
+++ stable/11/usr.bin/diff/diffreg.c	Wed Oct  3 17:16:18 2018	(r339160)
@@ -81,6 +81,7 @@ __FBSDID("$FreeBSD$");
 #include <fcntl.h>
 #include <paths.h>
 #include <regex.h>
+#include <stdbool.h>
 #include <stddef.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -1071,6 +1072,31 @@ restart:
 			}
 		}
 		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) {

Copied: stable/11/usr.bin/diff/tests/Bflag_C.out (from r338039, head/usr.bin/diff/tests/Bflag_C.out)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/11/usr.bin/diff/tests/Bflag_C.out	Wed Oct  3 17:16:18 2018	(r339160, copy of r338039, head/usr.bin/diff/tests/Bflag_C.out)
@@ -0,0 +1,2 @@
+1a2
+>  

Copied: stable/11/usr.bin/diff/tests/Bflag_D.out (from r338039, head/usr.bin/diff/tests/Bflag_D.out)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/11/usr.bin/diff/tests/Bflag_D.out	Wed Oct  3 17:16:18 2018	(r339160, copy of r338039, head/usr.bin/diff/tests/Bflag_D.out)
@@ -0,0 +1,2 @@
+1a2
+> C

Copied: stable/11/usr.bin/diff/tests/Bflag_F.out (from r338039, head/usr.bin/diff/tests/Bflag_F.out)
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ stable/11/usr.bin/diff/tests/Bflag_F.out	Wed Oct  3 17:16:18 2018	(r339160, copy of r338039, head/usr.bin/diff/tests/Bflag_F.out)
@@ -0,0 +1,4 @@
+7c8
+< G
+---
+> X

Modified: stable/11/usr.bin/diff/tests/Makefile
==============================================================================
--- stable/11/usr.bin/diff/tests/Makefile	Wed Oct  3 17:14:40 2018	(r339159)
+++ stable/11/usr.bin/diff/tests/Makefile	Wed Oct  3 17:16:18 2018	(r339160)
@@ -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 \

Modified: stable/11/usr.bin/diff/tests/diff_test.sh
==============================================================================
--- stable/11/usr.bin/diff/tests/diff_test.sh	Wed Oct  3 17:14:40 2018	(r339159)
+++ stable/11/usr.bin/diff/tests/diff_test.sh	Wed Oct  3 17:16:18 2018	(r339160)
@@ -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
 }


More information about the svn-src-all mailing list