Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[git] Added clang-format pre-commit hook.
[simgrid.git] / tools / git-hooks / clang-format.pre-commit
1 #!/bin/bash
2 # Copyright (c) 2015, David Martin
3 # All rights reserved.
4
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
7
8 # * Redistributions of source code must retain the above copyright notice, this
9 #   list of conditions and the following disclaimer.
10
11 # * Redistributions in binary form must reproduce the above copyright notice,
12 #   this list of conditions and the following disclaimer in the documentation
13 #   and/or other materials provided with the distribution.
14
15 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 #  git pre-commit hook that runs an clang-format stylecheck.
26 #  Features:
27 #   - abort commit when commit does not comply with the style guidelines
28 #   - create a patch of the proposed style changes
29
30 # modifications for clang-format by rene.milk@wwu.de
31 # This file is part of a set of unofficial pre-commit hooks available
32 # at github.
33 # Link:    https://github.com/githubbrowser/Pre-commit-hooks
34 # Contact: David Martin, david.martin.mailbox@googlemail.com
35
36
37 ##################################################################
38 # SETTINGS
39 # set path to clang-format binary
40 CLANG_FORMAT=$(which clang-format)
41
42 # remove any older patches from previous commits. Set to true or false.
43 DELETE_OLD_PATCHES=false
44
45 # only parse files with the extensions in FILE_EXTS. Set to true or false.
46 # if false every changed file in the commit will be parsed with clang-format.
47 # if true only files matching one of the extensions are parsed with clang-format.
48 PARSE_EXTS=true
49
50 # file types to parse. Only effective when PARSE_EXTS is true.
51 # FILE_EXTS=".c .h .cpp .hpp"
52 FILE_EXTS=".c .h .cpp .hpp .cc .hh .cxx"
53
54 ##################################################################
55 # There should be no need to change anything below this line.
56
57 # Reference: http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
58 canonicalize_filename () {
59     local target_file=$1
60     local physical_directory=""
61     local result=""
62
63     # Need to restore the working directory after work.
64     pushd `pwd` > /dev/null
65
66     cd "$(dirname "$target_file")"
67     target_file=`basename $target_file`
68
69     # Iterate down a (possible) chain of symlinks
70     while [ -L "$target_file" ]
71     do
72         target_file=$(readlink "$target_file")
73         cd "$(dirname "$target_file")"
74         target_file=$(basename "$target_file")
75     done
76
77     # Compute the canonicalized name by finding the physical path
78     # for the directory we're in and appending the target file.
79     physical_directory=`pwd -P`
80     result="$physical_directory"/"$target_file"
81
82     # restore the working directory after work.
83     popd > /dev/null
84
85     echo "$result"
86 }
87
88 # exit on error
89 set -e
90
91 # check whether the given file matches any of the set extensions
92 matches_extension() {
93     local filename=$(basename "$1")
94     local extension=".${filename##*.}"
95     local ext
96
97     for ext in $FILE_EXTS; do [[ "$ext" == "$extension" ]] && return 0; done
98
99     return 1
100 }
101
102 # necessary check for initial commit
103 if git rev-parse --verify HEAD >/dev/null 2>&1 ; then
104     against=HEAD
105 else
106     # Initial commit: diff against an empty tree object
107     against=foobaridonotexist
108 fi
109
110 if [ ! -x "$CLANG_FORMAT" ] ; then
111     printf "Error: clang-format executable not found.\n"
112     printf "Set the correct path in $(canonicalize_filename "$0").\n"
113     exit 1
114 fi
115
116 # create a random filename to store our generated patch
117 prefix="pre-commit-clang-format"
118 suffix="$(date +%s)"
119 patch="/tmp/$prefix-$suffix.patch"
120
121 # clean up any older clang-format patches
122 $DELETE_OLD_PATCHES && rm -f /tmp/$prefix*.patch
123
124 # create one patch containing all changes to the files
125 git diff-index --cached --diff-filter=ACMR --name-only $against -- | while read file;
126 do
127     # ignore file if we do check for file extensions and the file
128     # does not match any of the extensions specified in $FILE_EXTS
129     if $PARSE_EXTS && ! matches_extension "$file"; then
130         continue;
131     fi
132
133     # clang-format our sourcefile, create a patch with diff and append it to our $patch
134     # The sed call is necessary to transform the patch from
135     #    --- $file timestamp
136     #    +++ - timestamp
137     # to both lines working on the same file and having a a/ and b/ prefix.
138     # Else it can not be applied with 'git apply'.
139     git clang-format --diff -q >> "$patch"
140     #"$CLANG_FORMAT" -style=file "$file" | \
141         #diff -u "$file" - | \
142         #sed -e "1s|--- |--- a/|" -e "2s|+++ -|+++ b/$file|" >> "$patch"
143 done
144
145 # if no patch has been generated all is ok, clean up the file stub and exit
146 if [ ! -s "$patch" ] ; then
147     printf "Files in this commit comply with the clang-format rules.\n"
148     rm -f "$patch"
149     exit 0
150 fi
151
152 # a patch has been created, notify the user and exit
153 printf "\nThe following differences were found between the code to commit "
154 printf "and the clang-format rules:\n\n"
155 cat "$patch"
156
157 printf "\nYou can apply these changes with:\n git apply $patch\n"
158 printf "(may need to be called from the root directory of your repository)\n"
159 printf "\nNote: git apply only modifies your working directory, but not your index, i.e., not what you want to commit.\n"
160 printf "You need to run 'git add' again after you applied this patch OR just run git apply a second time:"
161 printf "\n git apply --cached $patch\n\n"
162 printf "Aborting commit. Apply changes and commit again or skip checking with"
163 printf " --no-verify (not recommended).\n"
164
165 exit 1