3 # SPDX-License-Identifier: GPL-2.0-only
5 # DESCR: Check that files end with a single newline
8 cd -- "$
(dirname "$0")" > /dev/null 2>&1 || return
12 # shellcheck source=helper_functions.sh
13 .
"${LINTDIR}/helper_functions.sh"
16 INCLUDED_DIRS_AND_FILES
='util/* src/* payloads/* configs/* Makefile *.inc'
17 EXCLUDED_DIRS
='src/vendorcode/\|cbfstool/lzma/\|cbfstool/lz4/\|Documentation/\|build/\|3rdparty/\|\.git/\|coreboot-builds/\|util/nvidia/cbootimage/\|^util/goswid/vendor'
18 EXCLUDED_FILES
='\.gif$\|\.jpg$\|\.cksum$\|\.bin$\|\.vbt$\|\.hex$\|\.ico$\|\.o$\|\.bz2$\|\.xz$\|^.tmpconfig\|\.pyc$\|_shipped$\|sha256$\|\.png$\|\.patch$\|\.apcb$'
20 HAVE_FILE
=$
(command -v file 1>/dev
/null
2>&1; echo $?
)
22 is_eligible_executable
() {
23 if [ "$HAVE_FILE" -ne 0 ]; then
26 if { LC_ALL
=C
; file --brief "$filename" |
grep -Eqw \
27 "^(Bourne shell|POSIX shell|Perl|Python) script"; };
35 test_for_final_newline
() {
36 while read filename
; do
37 # Only check regular files and script executables
38 if [ -f "$filename" ] && { [ ! -x "$filename" ] || \
39 is_eligible_executable
"$filename"; };
41 # Verify that there is a newline at the end
42 # $() strips trailing newlines
43 if [ -n "$(tail -c 1 "$filename")" ]; then
44 echo "$filename has no final newline."
46 # Verify that the file ends with only a single newline
47 # and that the file isn't empty
48 elif [ -z "$(tail -c 2 "$filename")" ] && \
49 [ -n "$(head -n 5 "$filename")" ]; then
50 echo "$filename has multiple final newlines."
56 for directory
in $INCLUDED_DIRS_AND_FILES ; do
57 ${FIND_FILES} "${directory}" |
sed 's|^\./||' |
sort | \
58 grep -v "$EXCLUDED_DIRS" | \
59 grep -v "$EXCLUDED_FILES" | \
60 test_for_final_newline
&
64 # wait for tests to finish.