pre-commit 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 [ -n "$changed_files" ]; then
  14. cppcheck --enable=warning,performance,portability --inline-suppr --error-exitcode=1 --platform=win64 --force $changed_files
  15. err=$?
  16. if [ $err -ne 0 ]; then
  17. echo "[rt-thread][cppcheck] we found a obvious fault, please fix the error then commit again"
  18. exit $err
  19. else
  20. echo "[rt-thread][cppcheck] cppcheck ok."
  21. fi
  22. fi
  23. # We only filter the file name with c or cpp or h file.
  24. # astyle only astyle the added file[because of code reivewer], if you want change modify file, please use [MA]
  25. changed_files=$(git diff-index --cached $against | \
  26. grep -E '[MA] .*\.(c|cpp|h)$' | cut -d' ' -f 2)
  27. if [ -n "$changed_files" ]; then
  28. 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
  29. err=$?
  30. if [ $err -ne 0 ]; then
  31. echo "[rt-thread][astyle] we found a obvious fault, please fix the error then commit again"
  32. exit $err
  33. else
  34. echo "[rt-thread][astyle] astyle file ok"
  35. fi
  36. fi
  37. # We only filter the file name with c or cpp file.
  38. changed_files=$(git diff-index --cached $against | \
  39. grep -E '[MA] .*\.(c|cpp|h)$' | cut -d' ' -f 2)
  40. # formatting check
  41. # https://github.com/mysterywolf/formatting
  42. # formatting cmd ref https://github.com/supperthomas/git_auto_script
  43. if [ -n "$changed_files" ]; then
  44. formatting $changed_files
  45. echo "[rt-thread] formatting $changed_files is ok"
  46. git add $changed_files
  47. exit 0
  48. fi
  49. exit 0