Martin Roth | 2e03fbc | 2016-07-28 16:20:59 -0600 | [diff] [blame] | 1 | #!/bin/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 | |
| 7 | LC_ALL=C export LC_ALL |
| 8 | |
Martin Roth | 31e0d42 | 2018-06-10 11:07:16 -0600 | [diff] [blame] | 9 | PIDS="" |
| 10 | INCLUDED_DIRS_AND_FILES='util/* src/* payloads/* configs/* Makefile *.inc' |
Elyes HAOUAS | 8297fa1 | 2020-02-22 10:37:49 +0100 | [diff] [blame] | 11 | EXCLUDED_DIRS='src/vendorcode/\|cbfstool/lzma/\|cbfstool/lz4/\|Documentation/\|build/\|3rdparty/\|\.git/\|coreboot-builds/\|util/nvidia/cbootimage/' |
Martin Roth | dea1333 | 2018-05-04 09:19:07 -0600 | [diff] [blame] | 12 | EXCLUDED_FILES='\.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] | 13 | |
| 14 | # Use git ls-files if the code is in a git repo, otherwise use find. |
Alex Thiessen | 73f19dc | 2018-01-16 23:05:48 +0000 | [diff] [blame] | 15 | if [ -n "$(command -v git)" ] && \ |
| 16 | [ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = "true" ] |
| 17 | then |
Martin Roth | 2e03fbc | 2016-07-28 16:20:59 -0600 | [diff] [blame] | 18 | FIND_FILES="git ls-files" |
| 19 | else |
Martin Roth | 31e0d42 | 2018-06-10 11:07:16 -0600 | [diff] [blame] | 20 | FIND_FILES="find" |
Martin Roth | 2e03fbc | 2016-07-28 16:20:59 -0600 | [diff] [blame] | 21 | fi |
| 22 | |
Alex Thiessen | 0e32981 | 2018-01-18 22:55:07 +0000 | [diff] [blame] | 23 | HAVE_FILE=$(command -v file 1>/dev/null 2>&1; echo $?) |
| 24 | |
| 25 | is_eligible_executable() { |
| 26 | if [ "$HAVE_FILE" -ne 0 ]; then |
| 27 | return 1 |
| 28 | fi |
| 29 | if { LC_ALL=C; file --brief "$filename" | grep -Eqw \ |
| 30 | "^(Bourne shell|POSIX shell|Perl|Python) script"; }; |
| 31 | then |
| 32 | return 0 |
| 33 | else |
| 34 | return 1 |
| 35 | fi |
| 36 | } |
| 37 | |
Martin Roth | 2e03fbc | 2016-07-28 16:20:59 -0600 | [diff] [blame] | 38 | test_for_final_newline() { |
| 39 | while read filename; do |
Alex Thiessen | 0e32981 | 2018-01-18 22:55:07 +0000 | [diff] [blame] | 40 | # Only check regular files and script executables |
| 41 | if [ -f "$filename" ] && { [ ! -x "$filename" ] || \ |
| 42 | is_eligible_executable "$filename"; }; |
| 43 | then |
Martin Roth | 2e03fbc | 2016-07-28 16:20:59 -0600 | [diff] [blame] | 44 | # Verify that there is a newline at the end |
| 45 | # $() strips trailing newlines |
| 46 | if [ -n "$(tail -c 1 "$filename")" ]; then |
| 47 | echo "$filename has no final newline." |
| 48 | |
| 49 | # Verify that the file ends with only a single newline |
| 50 | # and that the file isn't empty |
| 51 | elif [ -z "$(tail -c 2 "$filename")" ] && \ |
| 52 | [ -n "$(head -n 5 "$filename")" ]; then |
| 53 | echo "$filename has multiple final newlines." |
| 54 | fi |
| 55 | fi |
| 56 | done |
| 57 | } |
| 58 | |
Martin Roth | 31e0d42 | 2018-06-10 11:07:16 -0600 | [diff] [blame] | 59 | for directory in $INCLUDED_DIRS_AND_FILES ; do |
| 60 | ${FIND_FILES} ${directory} | sed 's|^\./||' | sort | \ |
| 61 | grep -v "$EXCLUDED_DIRS" | \ |
| 62 | grep -v "$EXCLUDED_FILES" | \ |
| 63 | test_for_final_newline & |
| 64 | PIDS="$PIDS $!" |
| 65 | done |
| 66 | |
| 67 | # wait for tests to finish. |
| 68 | for pid in $PIDS; do |
| 69 | wait "$pid" |
| 70 | done |