|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -uo pipefail |
| 4 | +IFS=$'\n\t' |
| 5 | + |
| 6 | +# Loops through all text files tracked by Git. |
| 7 | +git grep -zIl '' | |
| 8 | +while IFS= read -rd '' f; do |
| 9 | + # Exclude csproj and hdr files. |
| 10 | + if [[ "$f" == *"csproj" ]]; then |
| 11 | + continue |
| 12 | + elif [[ "$f" == *"hdr" ]]; then |
| 13 | + continue |
| 14 | + fi |
| 15 | + # Ensures that files are UTF-8 formatted. |
| 16 | + recode UTF-8 "$f" 2> /dev/null |
| 17 | + # Ensures that files have LF line endings. |
| 18 | + dos2unix "$f" 2> /dev/null |
| 19 | + # Ensures that files do not contain a BOM. |
| 20 | + sed -i '1s/^\xEF\xBB\xBF//' "$f" |
| 21 | + # Ensures that files end with newline characters. |
| 22 | + tail -c1 < "$f" | read -r _ || echo >> "$f"; |
| 23 | + # Remove trailing space characters. |
| 24 | + sed -z -i 's/\x20\x0A/\x0A/g' "$f" |
| 25 | +done |
| 26 | + |
| 27 | +git diff > patch.patch |
| 28 | +FILESIZE="$(stat -c%s patch.patch)" |
| 29 | +MAXSIZE=5 |
| 30 | + |
| 31 | +# If no patch has been generated all is OK, clean up, and exit. |
| 32 | +if (( FILESIZE < MAXSIZE )); then |
| 33 | + printf "Files in this commit comply with the formatting rules.\n" |
| 34 | + rm -f patch.patch |
| 35 | + exit 0 |
| 36 | +fi |
| 37 | + |
| 38 | +# A patch has been created, notify the user, clean up, and exit. |
| 39 | +printf "\n*** The following differences were found between the code " |
| 40 | +printf "and the formatting rules:\n\n" |
| 41 | +cat patch.patch |
| 42 | +printf "\n*** Aborting, please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\n" |
| 43 | +rm -f patch.patch |
| 44 | +exit 1 |
0 commit comments