Martin Roth | 0ad5fbd | 2020-12-24 12:06:38 -0700 | [diff] [blame] | 1 | #!/usr/bin/env sh |
Martin Roth | 2e03fbc | 2016-07-28 16:20:59 -0600 | [diff] [blame] | 2 | # |
Patrick Georgi | 7333a11 | 2020-05-08 20:48:04 +0200 | [diff] [blame] | 3 | # SPDX-License-Identifier: GPL-2.0-only |
| 4 | |
Martin Roth | 2e03fbc | 2016-07-28 16:20:59 -0600 | [diff] [blame] | 5 | # DESCR: Check that files end with a single newline |
| 6 | |
Martin Roth | d81debd | 2022-06-03 00:06:57 -0600 | [diff] [blame] | 7 | LINTDIR="$( |
| 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 Roth | 2e03fbc | 2016-07-28 16:20:59 -0600 | [diff] [blame] | 14 | |
Martin Roth | 31e0d42 | 2018-06-10 11:07:16 -0600 | [diff] [blame] | 15 | PIDS="" |
| 16 | INCLUDED_DIRS_AND_FILES='util/* src/* payloads/* configs/* Makefile *.inc' |
Martin Roth | 95b5b02 | 2022-10-17 07:53:16 -0600 | [diff] [blame] | 17 | EXCLUDED_DIRS='src/vendorcode/\|cbfstool/lzma/\|cbfstool/lz4/\|Documentation/\|build/\|3rdparty/\|\.git/\|coreboot-builds/\|util/nvidia/cbootimage/\|^util/goswid/vendor' |
Sean Rhodes | c9bc7a7 | 2021-11-10 11:22:19 +0000 | [diff] [blame] | 18 | EXCLUDED_FILES='\.gif$\|\.jpg$\|\.cksum$\|\.bin$\|\.vbt$\|\.hex$\|\.ico$\|\.o$\|\.bz2$\|\.xz$\|^.tmpconfig\|\.pyc$\|_shipped$\|sha256$\|\.png$\|\.patch$' |
Martin Roth | 2e03fbc | 2016-07-28 16:20:59 -0600 | [diff] [blame] | 19 | |
Alex Thiessen | 0e32981 | 2018-01-18 22:55:07 +0000 | [diff] [blame] | 20 | HAVE_FILE=$(command -v file 1>/dev/null 2>&1; echo $?) |
| 21 | |
| 22 | is_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 Roth | 2e03fbc | 2016-07-28 16:20:59 -0600 | [diff] [blame] | 35 | test_for_final_newline() { |
| 36 | while read filename; do |
Alex Thiessen | 0e32981 | 2018-01-18 22:55:07 +0000 | [diff] [blame] | 37 | # Only check regular files and script executables |
| 38 | if [ -f "$filename" ] && { [ ! -x "$filename" ] || \ |
| 39 | is_eligible_executable "$filename"; }; |
| 40 | then |
Martin Roth | 2e03fbc | 2016-07-28 16:20:59 -0600 | [diff] [blame] | 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." |
| 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 Roth | 31e0d42 | 2018-06-10 11:07:16 -0600 | [diff] [blame] | 56 | for directory in $INCLUDED_DIRS_AND_FILES ; do |
Martin Roth | d81debd | 2022-06-03 00:06:57 -0600 | [diff] [blame] | 57 | ${FIND_FILES} "${directory}" | sed 's|^\./||' | sort | \ |
Martin Roth | 31e0d42 | 2018-06-10 11:07:16 -0600 | [diff] [blame] | 58 | grep -v "$EXCLUDED_DIRS" | \ |
| 59 | grep -v "$EXCLUDED_FILES" | \ |
| 60 | test_for_final_newline & |
| 61 | PIDS="$PIDS $!" |
| 62 | done |
| 63 | |
| 64 | # wait for tests to finish. |
| 65 | for pid in $PIDS; do |
| 66 | wait "$pid" |
| 67 | done |