| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | # To use: |
| 4 | # ln -s scripts/clang-format.hook .git/hooks/pre-commit |
| 5 | |
| 6 | # Based loosely on https://github.com/andrewseidl/githook-clang-format |
| 7 | |
| 8 | format_file() { |
| 9 | file="${1}" |
| 10 | case "$file" in |
| 11 | *.hpp | *.cpp | .c | *.cc | *.cu | *.h ) |
| 12 | if [ -f "${1}" ] ; then |
| 13 | clang-format -i -style=file -sort-includes "${1}" |
| 14 | if git diff-files --quiet -- "${1}" ; then |
| 15 | echo "Already nicely formatted: ${1}" |
| 16 | else |
| 17 | git add "${1}" |
| 18 | echo "Reformatting file: ${1}" |
| 19 | fi |
| 20 | fi |
| 21 | ;; |
| 22 | *) |
| 23 | ;; |
| 24 | esac |
| 25 | } |
| 26 | |
| 27 | case "${1}" in |
| 28 | --about ) |
| 29 | echo "Runs clang-format on source files" |
| 30 | ;; |
| 31 | * ) |
| 32 | for file in `git diff-index --cached --name-only HEAD` ; do |
| 33 | format_file "${file}" |
| 34 | done |
| 35 | ;; |
| 36 | esac |