changeset 14394:74d1558ae43f

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.
author Jim Meyering <meyering@redhat.com>
date Fri, 25 Feb 2011 20:18:02 +0100
parents 9f47f8c334f2
children bf7bdef206dd
files ChangeLog top/maint.mk
diffstat 2 files changed, 22 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- 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
--- a/top/maint.mk
+++ b/top/maint.mk
@@ -778,17 +778,22 @@
 #   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 @@
       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.