blob: 74f1f03ecc46fe3af62780ab5d9ef98c57d6f9b6 [file] [log] [blame]
Martin Roth2e03fbc2016-07-28 16:20:59 -06001#!/bin/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
7LC_ALL=C export LC_ALL
8
Martin Roth31e0d422018-06-10 11:07:16 -06009PIDS=""
10INCLUDED_DIRS_AND_FILES='util/* src/* payloads/* configs/* Makefile *.inc'
Elyes HAOUAS8297fa12020-02-22 10:37:49 +010011EXCLUDED_DIRS='src/vendorcode/\|cbfstool/lzma/\|cbfstool/lz4/\|Documentation/\|build/\|3rdparty/\|\.git/\|coreboot-builds/\|util/nvidia/cbootimage/'
Martin Rothdea13332018-05-04 09:19:07 -060012EXCLUDED_FILES='\.jpg$\|\.cksum$\|\.bin$\|\.vbt$\|\.hex$\|\.ico$\|\.o$\|\.bz2$\|\.xz$\|^.tmpconfig\|\.pyc$\|_shipped$\|sha256$\|\.png$\|\.patch$'
Martin Roth2e03fbc2016-07-28 16:20:59 -060013
14# Use git ls-files if the code is in a git repo, otherwise use find.
Alex Thiessen73f19dc2018-01-16 23:05:48 +000015if [ -n "$(command -v git)" ] && \
16 [ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = "true" ]
17then
Martin Roth2e03fbc2016-07-28 16:20:59 -060018 FIND_FILES="git ls-files"
19else
Martin Roth31e0d422018-06-10 11:07:16 -060020 FIND_FILES="find"
Martin Roth2e03fbc2016-07-28 16:20:59 -060021fi
22
Alex Thiessen0e329812018-01-18 22:55:07 +000023HAVE_FILE=$(command -v file 1>/dev/null 2>&1; echo $?)
24
25is_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 Roth2e03fbc2016-07-28 16:20:59 -060038test_for_final_newline() {
39 while read filename; do
Alex Thiessen0e329812018-01-18 22:55:07 +000040 # Only check regular files and script executables
41 if [ -f "$filename" ] && { [ ! -x "$filename" ] || \
42 is_eligible_executable "$filename"; };
43 then
Martin Roth2e03fbc2016-07-28 16:20:59 -060044 # 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 Roth31e0d422018-06-10 11:07:16 -060059for 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 $!"
65done
66
67# wait for tests to finish.
68for pid in $PIDS; do
69 wait "$pid"
70done