cbfs: Remove remnants of ext-win-*
[coreboot2.git] / util / lint / lint-extended-015-final-newlines
blob18ca75b7e60066a21304679deb291b06dfb93700
1 #!/usr/bin/env sh
3 # SPDX-License-Identifier: GPL-2.0-only
5 # DESCR: Check that files end with a single newline
7 LINTDIR="$(
8 cd -- "$(dirname "$0")" > /dev/null 2>&1 || return
9 pwd -P
12 # shellcheck source=helper_functions.sh
13 . "${LINTDIR}/helper_functions.sh"
15 PIDS=""
16 INCLUDED_DIRS_AND_FILES='util/* src/* payloads/* configs/* Makefile *.inc'
17 EXCLUDED_DIRS="\
18 src/vendorcode/\|\
19 cbfstool/lzma/\|\
20 cbfstool/lz4/\|\
21 Documentation/\|\
22 build/\|\
23 3rdparty/\|\
24 \.git/\|\
25 coreboot-builds/\|\
26 util/nvidia/cbootimage/\|\
27 ^util/goswid/vendor"
28 EXCLUDED_FILES='\.gif$\|\.jpg$\|\.cksum$\|\.bin$\|\.vbt$\|\.hex$\|\.ico$\|\.o$\|\.bz2$\|\.xz$\|^.tmpconfig\|\.pyc$\|_shipped$\|sha256$\|\.png$\|\.patch$\|\.apcb$'
30 HAVE_FILE=$(command -v file 1>/dev/null 2>&1; echo $?)
32 is_eligible_executable() {
33 if [ "$HAVE_FILE" -ne 0 ]; then
34 return 1
36 if { LC_ALL=C; file --brief "$filename" | grep -Eqw \
37 "^(Bourne shell|POSIX shell|Perl|Python) script"; };
38 then
39 return 0
40 else
41 return 1
45 test_for_final_newline() {
46 while read filename; do
47 # Only check regular files and script executables
48 if [ -f "$filename" ] && { [ ! -x "$filename" ] || \
49 is_eligible_executable "$filename"; };
50 then
51 # Verify that there is a newline at the end
52 # $() strips trailing newlines
53 if [ -n "$(tail -c 1 "$filename")" ]; then
54 echo "$filename has no final newline."
56 # Verify that the file ends with only a single newline
57 # and that the file isn't empty
58 elif [ -z "$(tail -c 2 "$filename")" ] && \
59 [ -n "$(head -n 5 "$filename")" ]; then
60 echo "$filename has multiple final newlines."
63 done
66 for directory in $INCLUDED_DIRS_AND_FILES ; do
67 ${FIND_FILES} "${directory}" | sed 's|^\./||' | sort | \
68 grep -v "$EXCLUDED_DIRS" | \
69 grep -v "$EXCLUDED_FILES" | \
70 test_for_final_newline &
71 PIDS="$PIDS $!"
72 done
74 # wait for tests to finish.
75 for pid in $PIDS; do
76 wait "$pid"
77 done