maint.mk: detect missing-NL-at-EOF, too
authorJim Meyering <meyering@redhat.com>
Fri, 25 Feb 2011 19:18:02 +0000 (20:18 +0100)
committerJim Meyering <meyering@redhat.com>
Fri, 25 Feb 2011 19:18:57 +0000 (20:18 +0100)
* top/maint.mk (sc_prohibit_empty_lines_at_EOF): Adjust so that
it also detects when a file lacks a newline at EOF.
(require_exactly_one_NL_at_EOF_): Renamed from
detect_empty_lines_at_EOF_.  I opted not to rename the rule,
since people may well have .x-sc_... file names tied to the
existing name.  Suggested by Eric Blake.

ChangeLog
top/maint.mk

index 6cae638..b33e6fa 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2011-02-25  Jim Meyering  <meyering@redhat.com>
+
+       maint.mk: detect missing-NL-at-EOF, too
+       * top/maint.mk (sc_prohibit_empty_lines_at_EOF): Adjust so that
+       it also detects when a file lacks a newline at EOF.
+       (require_exactly_one_NL_at_EOF_): Renamed from
+       detect_empty_lines_at_EOF_.  I opted not to rename the rule,
+       since people may well have .x-sc_... file names tied to the
+       existing name.  Suggested by Eric Blake.
+
 2011-02-25  Paul Eggert  <eggert@cs.ucla.edu>
 
        dirname: move m4/dos.m4 functionality into lib/dosname.h
index ff2fbbb..90c22cf 100644 (file)
@@ -778,17 +778,22 @@ sc_prohibit_cvs_keyword:
 #   perl -ln -0777 -e '/\n(\n+)$/ and print "$ARGV: ".length $1' ...
 # but that would be far less efficient, reading the entire contents
 # of each file, rather than just the last two bytes of each.
+# In addition, while the code below detects both blank lines and a missing
+# newline at EOF, the above detects only the former.
 #
 # This is a perl script that is expected to be the single-quoted argument
 # to a command-line "-le".  The remaining arguments are file names.
-# Print the name of each file that ends in two or more newline bytes.
+# Print the name of each file that ends in exactly one newline byte.
+# I.e., warn if there are blank lines (2 or more newlines), or if the
+# last byte is not a newline.  However, currently we don't complain
+# about any file that contains exactly one byte.
 # Exit nonzero if at least one such file is found, otherwise, exit 0.
 # Warn about, but otherwise ignore open failure.  Ignore seek/read failure.
 #
 # Use this if you want to remove trailing empty lines from selected files:
 #   perl -pi -0777 -e 's/\n\n+$/\n/' files...
 #
-detect_empty_lines_at_EOF_ =                                           \
+require_exactly_one_NL_at_EOF_ =                                       \
   foreach my $$f (@ARGV)                                               \
     {                                                                  \
       open F, "<", $$f or (warn "failed to open $$f: $$!\n"), next;    \
@@ -798,12 +803,14 @@ detect_empty_lines_at_EOF_ =                                              \
       defined $$p and $$p = sysread F, $$last_two_bytes, 2;            \
       close F;                                                         \
       $$c = "ignore read failure";                                     \
-      $$p && $$last_two_bytes eq "\n\n" and (print $$f), $$fail=1;     \
+      $$p && ($$last_two_bytes eq "\n\n"                               \
+              || substr ($$last_two_bytes,1) ne "\n")                  \
+          and (print $$f), $$fail=1;                                   \
     }                                                                  \
   END { exit defined $$fail }
 sc_prohibit_empty_lines_at_EOF:
-       @perl -le '$(detect_empty_lines_at_EOF_)' $$($(VC_LIST_EXCEPT)) \
-          || { echo '$(ME): the above files end with empty line(s)'     \
+       @perl -le '$(require_exactly_one_NL_at_EOF_)' $$($(VC_LIST_EXCEPT)) \
+          || { echo '$(ME): empty line(s) or no newline at EOF'        \
                1>&2; exit 1; } || :;                                   \
 
 # Make sure we don't use st_blocks.  Use ST_NBLOCKS instead.