Martin Roth | 2e03fbc | 2016-07-28 16:20:59 -0600 | [diff] [blame] | 1 | #!/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 | |
| 17 | LC_ALL=C export LC_ALL |
| 18 | |
| 19 | EXCLUDED_DIRS='src/vendorcode/\|util/romcc/\|cbfstool/lzma/\|cbfstool/lz4/\|Documentation/\|build/\|3rdparty/\|\.git/\|coreboot-builds/\|util/nvidia/cbootimage/' |
| 20 | EXCLUDED_FILES='\.jpg$\|\.cksum$\|\.bin$\|\.hex$\|\.ico$\|\.o$\|\.bz2$\|\.xz$\|^.tmpconfig\|\.pyc$\|_shipped$\|sha256$' |
| 21 | |
| 22 | # Use git ls-files if the code is in a git repo, otherwise use find. |
| 23 | if [ -n "$(command -v git)" ] && [ -d .git ]; then |
| 24 | FIND_FILES="git ls-files" |
| 25 | else |
| 26 | FIND_FILES="find . " |
| 27 | fi |
| 28 | |
| 29 | test_for_final_newline() { |
| 30 | while read filename; do |
| 31 | # Only check non-executable regular files |
| 32 | if [ -f "$filename" ] && [ ! -x "$filename" ]; then |
| 33 | |
| 34 | # Verify that there is a newline at the end |
| 35 | # $() strips trailing newlines |
| 36 | if [ -n "$(tail -c 1 "$filename")" ]; then |
| 37 | echo "$filename has no final newline." |
| 38 | |
| 39 | # Verify that the file ends with only a single newline |
| 40 | # and that the file isn't empty |
| 41 | elif [ -z "$(tail -c 2 "$filename")" ] && \ |
| 42 | [ -n "$(head -n 5 "$filename")" ]; then |
| 43 | echo "$filename has multiple final newlines." |
| 44 | fi |
| 45 | fi |
| 46 | done |
| 47 | } |
| 48 | |
| 49 | ${FIND_FILES} | sed 's|^\./||' | sort | \ |
| 50 | grep -v "$EXCLUDED_DIRS" | \ |
| 51 | grep -v "$EXCLUDED_FILES" | \ |
| 52 | test_for_final_newline |