From 47824c7263f9a4e10fbc60a944de741e0ead73b2 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Fri, 25 Feb 2011 20:18:02 +0100 Subject: [PATCH] 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. --- ChangeLog | 10 ++++++++++ top/maint.mk | 17 ++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6cae638218..b33e6faca2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2011-02-25 Jim Meyering + + 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 dirname: move m4/dos.m4 functionality into lib/dosname.h diff --git a/top/maint.mk b/top/maint.mk index ff2fbbb382..90c22cfed9 100644 --- a/top/maint.mk +++ b/top/maint.mk @@ -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. -- 2.30.2