blob: 4ea4536124ecf035dc0e178bb3463388d6660505 [file] [log] [blame]
Martin Roth0ad5fbd2020-12-24 12:06:38 -07001#!/usr/bin/env sh
Martin Roth2e03fbc2016-07-28 16:20:59 -06002#
Patrick Georgi7333a112020-05-08 20:48:04 +02003# SPDX-License-Identifier: GPL-2.0-only
4
Martin Roth2e03fbc2016-07-28 16:20:59 -06005# DESCR: Check that files end with a single newline
6
Martin Rothd81debd2022-06-03 00:06:57 -06007LINTDIR="$(
8 cd -- "$(dirname "$0")" > /dev/null 2>&1 || return
9 pwd -P
10)"
11
12# shellcheck source=helper_functions.sh
13. "${LINTDIR}/helper_functions.sh"
Martin Roth2e03fbc2016-07-28 16:20:59 -060014
Martin Roth31e0d422018-06-10 11:07:16 -060015PIDS=""
16INCLUDED_DIRS_AND_FILES='util/* src/* payloads/* configs/* Makefile *.inc'
Martin Roth95b5b022022-10-17 07:53:16 -060017EXCLUDED_DIRS='src/vendorcode/\|cbfstool/lzma/\|cbfstool/lz4/\|Documentation/\|build/\|3rdparty/\|\.git/\|coreboot-builds/\|util/nvidia/cbootimage/\|^util/goswid/vendor'
Matt DeVillier024c5c92023-12-15 15:44:23 -060018EXCLUDED_FILES='\.gif$\|\.jpg$\|\.cksum$\|\.bin$\|\.vbt$\|\.hex$\|\.ico$\|\.o$\|\.bz2$\|\.xz$\|^.tmpconfig\|\.pyc$\|_shipped$\|sha256$\|\.png$\|\.patch$\|\.apcb$'
Martin Roth2e03fbc2016-07-28 16:20:59 -060019
Alex Thiessen0e329812018-01-18 22:55:07 +000020HAVE_FILE=$(command -v file 1>/dev/null 2>&1; echo $?)
21
22is_eligible_executable() {
23 if [ "$HAVE_FILE" -ne 0 ]; then
24 return 1
25 fi
26 if { LC_ALL=C; file --brief "$filename" | grep -Eqw \
27 "^(Bourne shell|POSIX shell|Perl|Python) script"; };
28 then
29 return 0
30 else
31 return 1
32 fi
33}
34
Martin Roth2e03fbc2016-07-28 16:20:59 -060035test_for_final_newline() {
36 while read filename; do
Alex Thiessen0e329812018-01-18 22:55:07 +000037 # Only check regular files and script executables
38 if [ -f "$filename" ] && { [ ! -x "$filename" ] || \
39 is_eligible_executable "$filename"; };
40 then
Martin Roth2e03fbc2016-07-28 16:20:59 -060041 # 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."
45
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."
51 fi
52 fi
53 done
54}
55
Martin Roth31e0d422018-06-10 11:07:16 -060056for directory in $INCLUDED_DIRS_AND_FILES ; do
Martin Rothd81debd2022-06-03 00:06:57 -060057 ${FIND_FILES} "${directory}" | sed 's|^\./||' | sort | \
Martin Roth31e0d422018-06-10 11:07:16 -060058 grep -v "$EXCLUDED_DIRS" | \
59 grep -v "$EXCLUDED_FILES" | \
60 test_for_final_newline &
61 PIDS="$PIDS $!"
62done
63
64# wait for tests to finish.
65for pid in $PIDS; do
66 wait "$pid"
67done