diff options
author | Jason Gross <jasongross9@gmail.com> | 2018-01-16 18:12:55 +0100 |
---|---|---|
committer | Gaƫtan Gilbert <gaetan.gilbert@skyskimmer.net> | 2018-02-08 17:15:20 +0100 |
commit | 8ab30d25b5ffc6a56e9ca41f446504bba4a726ad (patch) | |
tree | 8936bd55cb6f1b1cd181f6bdd023b898e43f3f32 /dev/tools | |
parent | d31777adb88eb5ba54f68ac7a4cb7a2a29c1fc20 (diff) |
Have the pre-commit hook also fix end-of-file nl
Diffstat (limited to 'dev/tools')
-rwxr-xr-x | dev/tools/check-eof-newline.sh | 22 | ||||
-rwxr-xr-x | dev/tools/pre-commit | 11 |
2 files changed, 32 insertions, 1 deletions
diff --git a/dev/tools/check-eof-newline.sh b/dev/tools/check-eof-newline.sh index 9e4c8661d..c5a654e21 100755 --- a/dev/tools/check-eof-newline.sh +++ b/dev/tools/check-eof-newline.sh @@ -1,12 +1,32 @@ #!/usr/bin/env bash +# Usage: check-eof-newline.sh [--fix] FILES... +# Detect missing end of file newlines for FILES. +# Files are skipped if untracked by git and depending on gitattributes. +# With --fix, automatically append a newline. +# Exit status: 1 if any file had a missing newline, 0 otherwise +# (regardless of --fix). + +FIX= +if [ "$1" = --fix ]; +then + FIX=1 + shift +fi + CODE=0 for f in "$@"; do if git ls-files --error-unmatch "$f" >/dev/null 2>&1 && \ git check-attr whitespace -- "$f" | grep -q -v -e 'unset$' -e 'unspecified$' && \ [ -n "$(tail -c 1 "$f")" ] then - echo "No newline at end of file $f!" + if [ -n "$FIX" ]; + then + echo >> "$f" + echo "Newline appended to file $f!" + else + echo "No newline at end of file $f!" + fi CODE=1 fi done diff --git a/dev/tools/pre-commit b/dev/tools/pre-commit index 59cc84856..0cd0a0b70 100755 --- a/dev/tools/pre-commit +++ b/dev/tools/pre-commit @@ -5,6 +5,17 @@ set -e +CODE=0 +git diff --cached --name-only -z | xargs -0 dev/tools/check-eof-newline.sh --fix || CODE=1 + +if [ $CODE -ne 0 ] +then + 1>&2 echo "Some files had newline errors; they have been fixed in the working tree." + 1>&2 echo "Make sure to add them before committing." + 1>&2 echo "This may fix itself if you were using git commit -a, and you try again." + exit 1 +fi + if git diff-index --check --cached HEAD >/dev/null 2>&1 ; then : |