format.sh 1.2 KB

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