2 # git pre-commit hook that runs an clang-format stylecheck.
4 # - abort commit when commit does not comply with the style guidelines
5 # - create a patch of the proposed style changes
7 # modifications for clang-format by rene.milk@wwu.de
8 # Link: https://github.com/githubbrowser/Pre-commit-hooks
9 # Contact: David Martin, david.martin.mailbox@googlemail.com
11 # DESCR: Checking that VCS 'staged' source conforms to coding style
13 ##################################################################
15 # set path to clang-format binary
16 CLANG_FORMAT
="$(command -v clang-format)"
18 # remove any older patches from previous commits. Set to true or false.
19 # DELETE_OLD_PATCHES=false
20 DELETE_OLD_PATCHES
=false
22 # only parse files with the extensions in FILE_EXTS. Set to true or false.
23 # if false every changed file in the commit will be parsed with clang-format.
24 # if true only files matching one of the extensions are parsed with clang-format.
28 # file types to parse. Only effective when PARSE_EXTS is true.
29 # FILE_EXTS=".c .h .cpp .hpp"
30 FILE_EXTS
=".c .h .cpp .hpp .cc .hh .cxx"
32 ##################################################################
33 # There should be no need to change anything below this line.
35 # Reference: http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
36 canonicalize_filename
() {
38 local physical_directory
=""
41 # Need to restore the working directory after work.
42 pushd `pwd` > /dev
/null
44 cd "$(dirname "$target_file")"
45 target_file
=`basename $target_file`
47 # Iterate down a (possible) chain of symlinks
48 while [ -L "$target_file" ]
50 target_file
=$
(readlink
"$target_file")
51 cd "$(dirname "$target_file")"
52 target_file
=$
(basename "$target_file")
55 # Compute the canonicalized name by finding the physical path
56 # for the directory we're in and appending the target file.
57 physical_directory
=`pwd -P`
58 result
="$physical_directory"/"$target_file"
60 # restore the working directory after work.
69 # check whether the given file matches any of the set extensions
71 local filename
=$
(basename "$1")
72 local extension
=".${filename##*.}"
75 for ext
in $FILE_EXTS; do [[ "$ext" == "$extension" ]] && return 0; done
80 # necessary check for initial commit
81 if git rev-parse
--verify HEAD
>/dev
/null
2>&1 ; then
84 # Initial commit: diff against an empty tree object
85 against
=4b825dc642cb6eb9a060e54bf8d69288fbee4904
88 if [ ! -x "$CLANG_FORMAT" ] ; then
89 printf "Warning: clang-format executable not found.\n"
90 printf "Set the correct path in $(canonicalize_filename "$0").\n"
91 printf "Skipping clang-format style check test.\n"
96 # create a random filename to store our generated patch
97 prefix
="pre-commit-clang-format"
99 patch="/tmp/$prefix-$suffix.patch"
101 # clean up any older clang-format patches
102 $DELETE_OLD_PATCHES && rm -f /tmp
/$prefix*.
patch
104 # create one patch containing all changes to the files
105 git diff-index
--cached --diff-filter=ACMR
--name-only $against -- |
while read file;
107 # ignore file if we do check for file extensions and the file
108 # does not match any of the extensions specified in $FILE_EXTS
109 if $PARSE_EXTS && ! matches_extension
"$file"; then
113 # clang-format our sourcefile, create a patch with diff and append it to our $patch
114 # The sed call is necessary to transform the patch from
115 # --- $file timestamp
117 # to both lines working on the same file and having a a/ and b/ prefix.
118 # Else it can not be applied with 'git apply'.
119 "$CLANG_FORMAT" -style=file "$file" | \
120 diff -u "$file" - | \
121 sed -e "1s|--- |--- a/|" -e "2s|+++ -|+++ b/$file|" >> "$patch"
124 # if no patch has been generated all is ok, clean up the file stub and exit
125 if [ ! -s "$patch" ] ; then
126 printf "Files in this commit comply with the clang-format rules.\n"
131 # a patch has been created, notify the user and exit
132 printf "\nThe following differences were found between the code to commit "
133 printf "and the clang-format rules:\n\n"
136 printf "\nYou can apply these changes with:\n git apply $patch\n"
137 printf "(may need to be called from the root directory of your repository)\n"
139 # FIXME: clang-format is currently unusable, so don't abort the commit.
140 # printf "Aborting commit. Apply changes and commit again or skip checking with"
141 # printf " --no-verify (not recommended).\n"