Author: kevans
Date: Sun Aug 19 03:57:20 2018
New Revision: 338039
URL: https://svnweb.freebsd.org/changeset/base/338039

Log:
  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

Added:
  head/usr.bin/diff/tests/Bflag_C.out   (contents, props changed)
  head/usr.bin/diff/tests/Bflag_D.out   (contents, props changed)
  head/usr.bin/diff/tests/Bflag_F.out   (contents, props changed)
Modified:
  head/usr.bin/diff/TODO
  head/usr.bin/diff/diff.1
  head/usr.bin/diff/diff.c
  head/usr.bin/diff/diff.h
  head/usr.bin/diff/diffreg.c
  head/usr.bin/diff/tests/Makefile
  head/usr.bin/diff/tests/diff_test.sh

Modified: head/usr.bin/diff/TODO
==============================================================================
--- head/usr.bin/diff/TODO      Sun Aug 19 01:14:46 2018        (r338038)
+++ head/usr.bin/diff/TODO      Sun Aug 19 03:57:20 2018        (r338039)
@@ -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: head/usr.bin/diff/diff.1
==============================================================================
--- head/usr.bin/diff/diff.1    Sun Aug 19 01:14:46 2018        (r338038)
+++ head/usr.bin/diff/diff.1    Sun Aug 19 03:57:20 2018        (r338039)
@@ -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: head/usr.bin/diff/diff.c
==============================================================================
--- head/usr.bin/diff/diff.c    Sun Aug 19 01:14:46 2018        (r338038)
+++ head/usr.bin/diff/diff.c    Sun Aug 19 03:57:20 2018        (r338039)
@@ -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: head/usr.bin/diff/diff.h
==============================================================================
--- head/usr.bin/diff/diff.h    Sun Aug 19 01:14:46 2018        (r338038)
+++ head/usr.bin/diff/diff.h    Sun Aug 19 03:57:20 2018        (r338039)
@@ -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: head/usr.bin/diff/diffreg.c
==============================================================================
--- head/usr.bin/diff/diffreg.c Sun Aug 19 01:14:46 2018        (r338038)
+++ head/usr.bin/diff/diffreg.c Sun Aug 19 03:57:20 2018        (r338039)
@@ -79,6 +79,7 @@ __FBSDID("$FreeBSD$");
 #include <fcntl.h>
 #include <paths.h>
 #include <regex.h>
+#include <stdbool.h>
 #include <stddef.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -999,6 +1000,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) {

Added: head/usr.bin/diff/tests/Bflag_C.out
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ head/usr.bin/diff/tests/Bflag_C.out Sun Aug 19 03:57:20 2018        
(r338039)
@@ -0,0 +1,2 @@
+1a2
+>  

Added: head/usr.bin/diff/tests/Bflag_D.out
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ head/usr.bin/diff/tests/Bflag_D.out Sun Aug 19 03:57:20 2018        
(r338039)
@@ -0,0 +1,2 @@
+1a2
+> C

Added: head/usr.bin/diff/tests/Bflag_F.out
==============================================================================
--- /dev/null   00:00:00 1970   (empty, because file is newly added)
+++ head/usr.bin/diff/tests/Bflag_F.out Sun Aug 19 03:57:20 2018        
(r338039)
@@ -0,0 +1,4 @@
+7c8
+< G
+---
+> X

Modified: head/usr.bin/diff/tests/Makefile
==============================================================================
--- head/usr.bin/diff/tests/Makefile    Sun Aug 19 01:14:46 2018        
(r338038)
+++ head/usr.bin/diff/tests/Makefile    Sun Aug 19 03:57:20 2018        
(r338039)
@@ -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: head/usr.bin/diff/tests/diff_test.sh
==============================================================================
--- head/usr.bin/diff/tests/diff_test.sh        Sun Aug 19 01:14:46 2018        
(r338038)
+++ head/usr.bin/diff/tests/diff_test.sh        Sun Aug 19 03:57:20 2018        
(r338039)
@@ -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
 }
_______________________________________________
[email protected] mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "[email protected]"

Reply via email to