Detect Missing EOF

While editing files on GitHub/Gitlab, missing an empty line at the bottom of a file in the buffer will result in no newline at the end of file. In the case of regular text files, this isn’t consistent with the POSIX standards.

for file in `git ls-files`; do
  grep -Iq . $file && if ! newline_at_eof $file; then; else echo $file; fi
done

Fred has provided a shell script for detecting EOF on Stack Overflow. I’ve replaced bash with sh because I’m using Zsh.

#!/bin/zsh
if [ -z "$(tail -c 1 "$1")" ]
then
    echo "Newline at end of file!"
    exit 1
else
    echo "No newline at end of file!"
    exit 0
fi
Linux 

No comment

Your email address will not be published. Required fields are marked *.