File: clang-format-pre-commit

package info (click to toggle)
cli11 2.4.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 2,120 kB
  • sloc: cpp: 23,299; python: 129; sh: 64; makefile: 11; ruby: 7
file content (36 lines) | stat: -rwxr-xr-x 820 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/usr/bin/env bash

# To use:
# ln -s scripts/clang-format.hook .git/hooks/pre-commit

# Based loosely on https://github.com/andrewseidl/githook-clang-format

format_file() {
    file="${1}"
    case "$file" in
    *.hpp | *.cpp | .c | *.cc | *.cu | *.h )
        if [ -f "${1}" ] ; then
            clang-format -i -style=file -sort-includes "${1}"
            if git diff-files --quiet -- "${1}" ; then
                echo "Already nicely formatted: ${1}"
            else
                git add "${1}"
                echo "Reformatting file: ${1}"
            fi
        fi
        ;;
    *)
        ;;
    esac
}

case "${1}" in
  --about )
    echo "Runs clang-format on source files"
    ;;
  * )
    for file in `git diff-index --cached --name-only HEAD` ; do
      format_file "${file}"
    done
    ;;
esac