Debian Codegraph @

util/lint/lint-extended-015-final-newlines

raw symbols SCIP
1 #!/usr/bin/env sh
2 #
3 # SPDX-License-Identifier: GPL-2.0-only
4
5 # DESCR: Check that files end with a single newline
6
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"
14
15 PIDS=""
16 INCLUDED_DIRS_AND_FILES='util/* src/* payloads/* configs/* Makefile *.mk'
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 ^util/riscv/starfive-jh7110-spl-tool"
29 EXCLUDED_FILES='\.gif$\|\.jpg$\|\.cksum$\|\.bin$\|\.vbt$\|\.hex$\|\.ico$\|\.o$\|\.bz2$\|\.xz$\|^.tmpconfig\|\.pyc$\|_shipped$\|sha256$\|\.png$\|\.patch$\|\.apcb$'
30
31 HAVE_FILE=$(command -v file 1>/dev/null 2>&1; echo $?)
32
33 is_eligible_executable() {
34 if [ "$HAVE_FILE" -ne 0 ]; then
35 return 1
36 fi
37 if { LC_ALL=C; file --brief "$filename" | grep -Eqw \
38 "^(Bourne shell|POSIX shell|Perl|Python) script"; };
39 then
40 return 0
41 else
42 return 1
43 fi
44 }
45
46 test_for_final_newline() {
47 while read filename; do
48 # Only check regular files and script executables
49 if [ -f "$filename" ] && { [ ! -x "$filename" ] || \
50 is_eligible_executable "$filename"; };
51 then
52 # Verify that there is a newline at the end
53 # $() strips trailing newlines
54 if [ -n "$(tail -c 1 "$filename")" ]; then
55 echo "$filename has no final newline."
56
57 # Verify that the file ends with only a single newline
58 # and that the file isn't empty
59 elif [ -z "$(tail -c 2 "$filename")" ] && \
60 [ -n "$(head -n 5 "$filename")" ]; then
61 echo "$filename has multiple final newlines."
62 fi
63 fi
64 done
65 }
66
67 for dir_or_file in $INCLUDED_DIRS_AND_FILES; do
68 if [ -e "${dir_or_file}" ] || [ -d "${dir_or_file}" ]; then
69 ${FIND_FILES} "${dir_or_file}" ${FINDOPTS} | sed 's|^\./||' | sort | \
70 grep -v "$EXCLUDED_DIRS" | \
71 grep -v "$EXCLUDED_FILES" | \
72 test_for_final_newline &
73 PIDS="$PIDS $!"
74 fi
75 done
76
77 # wait for tests to finish.
78 for pid in $PIDS; do
79 wait "$pid"
80 done