blob: b5a503f3cfe9d752742ff13c41ff25e17f2eb5be [file] [log] [blame]
Martin Roth2e03fbc2016-07-28 16:20:59 -06001#!/bin/sh
2# This file is part of the coreboot project.
3#
4# Copyright (C) 2016 Google Inc.
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; version 2 of the License.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# DESCR: Check that files end with a single newline
16
17LC_ALL=C export LC_ALL
18
Martin Roth31e0d422018-06-10 11:07:16 -060019PIDS=""
20INCLUDED_DIRS_AND_FILES='util/* src/* payloads/* configs/* Makefile *.inc'
Martin Roth2e03fbc2016-07-28 16:20:59 -060021EXCLUDED_DIRS='src/vendorcode/\|util/romcc/\|cbfstool/lzma/\|cbfstool/lz4/\|Documentation/\|build/\|3rdparty/\|\.git/\|coreboot-builds/\|util/nvidia/cbootimage/'
Martin Rothdea13332018-05-04 09:19:07 -060022EXCLUDED_FILES='\.jpg$\|\.cksum$\|\.bin$\|\.vbt$\|\.hex$\|\.ico$\|\.o$\|\.bz2$\|\.xz$\|^.tmpconfig\|\.pyc$\|_shipped$\|sha256$\|\.png$\|\.patch$'
Martin Roth2e03fbc2016-07-28 16:20:59 -060023
24# Use git ls-files if the code is in a git repo, otherwise use find.
Alex Thiessen73f19dc2018-01-16 23:05:48 +000025if [ -n "$(command -v git)" ] && \
26 [ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = "true" ]
27then
Martin Roth2e03fbc2016-07-28 16:20:59 -060028 FIND_FILES="git ls-files"
29else
Martin Roth31e0d422018-06-10 11:07:16 -060030 FIND_FILES="find"
Martin Roth2e03fbc2016-07-28 16:20:59 -060031fi
32
Alex Thiessen0e329812018-01-18 22:55:07 +000033HAVE_FILE=$(command -v file 1>/dev/null 2>&1; echo $?)
34
35is_eligible_executable() {
36 if [ "$HAVE_FILE" -ne 0 ]; then
37 return 1
38 fi
39 if { LC_ALL=C; file --brief "$filename" | grep -Eqw \
40 "^(Bourne shell|POSIX shell|Perl|Python) script"; };
41 then
42 return 0
43 else
44 return 1
45 fi
46}
47
Martin Roth2e03fbc2016-07-28 16:20:59 -060048test_for_final_newline() {
49 while read filename; do
Alex Thiessen0e329812018-01-18 22:55:07 +000050 # Only check regular files and script executables
51 if [ -f "$filename" ] && { [ ! -x "$filename" ] || \
52 is_eligible_executable "$filename"; };
53 then
Martin Roth2e03fbc2016-07-28 16:20:59 -060054 # Verify that there is a newline at the end
55 # $() strips trailing newlines
56 if [ -n "$(tail -c 1 "$filename")" ]; then
57 echo "$filename has no final newline."
58
59 # Verify that the file ends with only a single newline
60 # and that the file isn't empty
61 elif [ -z "$(tail -c 2 "$filename")" ] && \
62 [ -n "$(head -n 5 "$filename")" ]; then
63 echo "$filename has multiple final newlines."
64 fi
65 fi
66 done
67}
68
Martin Roth31e0d422018-06-10 11:07:16 -060069for directory in $INCLUDED_DIRS_AND_FILES ; do
70 ${FIND_FILES} ${directory} | sed 's|^\./||' | sort | \
71 grep -v "$EXCLUDED_DIRS" | \
72 grep -v "$EXCLUDED_FILES" | \
73 test_for_final_newline &
74 PIDS="$PIDS $!"
75done
76
77# wait for tests to finish.
78for pid in $PIDS; do
79 wait "$pid"
80done