pre-commit 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/bin/sh
  2. # AUTHOR: supperthomas
  3. if git rev-parse --verify HEAD >/dev/null 2>&1
  4. then
  5. against=HEAD
  6. else
  7. # Initial commit: diff against an empty tree object
  8. against=$(git hash-object -t tree /dev/null)
  9. fi
  10. # We only filter the file name with c or cpp file.
  11. changed_files=$(git diff-index --cached $against | \
  12. grep -E '[MA] .*\.(c|cpp|cc|cxx)$' | cut -d' ' -f 2)
  13. if which cppcheck > /dev/null; then
  14. if [ -n "$changed_files" ]; then
  15. cppcheck --enable=warning,performance,portability --inline-suppr --error-exitcode=1 --platform=win64 --force $changed_files
  16. err=$?
  17. if [ $err -ne 0 ]; then
  18. echo "[rt-thread][cppcheck] we found a obvious fault, please fix the error then commit again"
  19. exit $err
  20. else
  21. echo "[rt-thread][cppcheck] cppcheck ok."
  22. fi
  23. fi
  24. else
  25. echo "cppcheck does not exist"
  26. fi
  27. # We only filter the file name with c or cpp or h file.
  28. # astyle only astyle the added file[because of code reivewer], if you want change modify file, please use [MA]
  29. changed_files=$(git diff-index --cached $against | \
  30. grep -E '[MA] .*\.(c|cpp|h)$' | cut -d' ' -f 2)
  31. if which astyle > /dev/null; then
  32. if [ -n "$changed_files" ]; then
  33. astyle --style=allman --indent=spaces=4 --indent=spaces=4 --indent=spaces=4 --pad-header --pad-header --pad-header --align-pointer=name --lineend=linux --convert-tabs --verbose $changed_files
  34. err=$?
  35. if [ $err -ne 0 ]; then
  36. echo "[rt-thread][astyle] we found a obvious fault, please fix the error then commit again"
  37. exit $err
  38. else
  39. echo "[rt-thread][astyle] astyle file ok"
  40. fi
  41. fi
  42. else
  43. echo "astyle does not exist"
  44. fi
  45. # We only filter the file name with c or cpp file.
  46. changed_files=$(git diff-index --cached $against | \
  47. grep -E '[MA] .*\.(c|cpp|h)$' | cut -d' ' -f 2)
  48. # formatting check
  49. # https://github.com/mysterywolf/formatting
  50. # formatting cmd ref https://github.com/supperthomas/git_auto_script
  51. if which formatting > /dev/null; then
  52. if [ -n "$changed_files" ]; then
  53. formatting $changed_files
  54. echo "[rt-thread] formatting $changed_files is ok"
  55. git add $changed_files
  56. exit 0
  57. fi
  58. else
  59. echo "formatting does not exist"
  60. fi
  61. exit 0