Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make it easier to have clang-format as a git 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-3.8)
41
42 # git subcommand that we want to us
43 for name in git-clang-format-3.9 git-clang-format-3.8 git-clang-format ; do
44  where=$(which $name)
45  if [ x != "x$where" ] ; then
46    GIT_SUBCOMMAND=`echo $name|sed 's/git-//'`
47    break
48  fi
49 done
50 if [ -z $GIT_SUBCOMMAND ] ; then
51   echo "Cannot find git-clang-format (in version >=3.8). Did you install the right package?"
52   exit 1
53 fi
54
55 # remove any older patches from previous commits. Set to true or false.
56 DELETE_OLD_PATCHES=false
57
58 # only parse files with the extensions in FILE_EXTS. Set to true or false.
59 # if false every changed file in the commit will be parsed with clang-format.
60 # if true only files matching one of the extensions are parsed with clang-format.
61 PARSE_EXTS=true
62
63 # file types to parse. Only effective when PARSE_EXTS is true.
64 # FILE_EXTS=".c .h .cpp .hpp"
65 FILE_EXTS=".c .h .cpp .hpp .cc .hh .cxx"
66
67 ##################################################################
68 # There should be no need to change anything below this line.
69
70 # Reference: http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
71 canonicalize_filename () {
72     local target_file=$1
73     local physical_directory=""
74     local result=""
75
76     # Need to restore the working directory after work.
77     pushd `pwd` > /dev/null
78
79     cd "$(dirname "$target_file")"
80     target_file=`basename $target_file`
81
82     # Iterate down a (possible) chain of symlinks
83     while [ -L "$target_file" ]
84     do
85         target_file=$(readlink "$target_file")
86         cd "$(dirname "$target_file")"
87         target_file=$(basename "$target_file")
88     done
89
90     # Compute the canonicalized name by finding the physical path
91     # for the directory we're in and appending the target file.
92     physical_directory=`pwd -P`
93     result="$physical_directory"/"$target_file"
94
95     # restore the working directory after work.
96     popd > /dev/null
97
98     echo "$result"
99 }
100
101 # exit on error
102 set -e
103
104 # check whether the given file matches any of the set extensions
105 matches_extension() {
106     local filename=$(basename "$1")
107     local extension=".${filename##*.}"
108     local ext
109
110     for ext in $FILE_EXTS; do [[ "$ext" == "$extension" ]] && return 0; done
111
112     return 1
113 }
114
115 # necessary check for initial commit
116 if git rev-parse --verify HEAD >/dev/null 2>&1 ; then
117     against=HEAD
118 else
119     # Initial commit: diff against an empty tree object
120     against=foobaridonotexist
121 fi
122
123 if [ ! -x "$CLANG_FORMAT" ] ; then
124     printf "Error: clang-format executable not found. sudo apt-get install clang-format-3.8\n"
125     printf "Set the correct path in $(canonicalize_filename "$0").\n"
126     exit 1
127 fi
128
129 # create a random filename to store our generated patch
130 prefix="pre-commit-clang-format"
131 suffix="$(date +%s)"
132 patch="/tmp/$prefix-$suffix.patch"
133
134 # clean up any older clang-format patches
135 $DELETE_OLD_PATCHES && rm -f /tmp/$prefix*.patch
136
137 # create one patch containing all changes to the files
138 git diff-index --cached --diff-filter=ACMR --name-only $against -- | while read file;
139 do
140     # ignore file if we do check for file extensions and the file
141     # does not match any of the extensions specified in $FILE_EXTS
142     if $PARSE_EXTS && ! matches_extension "$file"; then
143         continue;
144     fi
145
146     # clang-format our sourcefile, create a patch with diff and append it to our $patch
147     # The sed call is necessary to transform the patch from
148     #    --- $file timestamp
149     #    +++ - timestamp
150     # to both lines working on the same file and having a a/ and b/ prefix.
151     # Else it can not be applied with 'git apply'.
152     git ${GIT_SUBCOMMAND} --binary ${CLANG_FORMAT} --diff -q >> "$patch"
153     #"$CLANG_FORMAT" -style=file "$file" | \
154         #diff -u "$file" - | \
155         #sed -e "1s|--- |--- a/|" -e "2s|+++ -|+++ b/$file|" >> "$patch"
156 done
157
158 # if no patch has been generated all is ok, clean up the file stub and exit
159 if [ ! -s "$patch" ] ; then
160     printf "Files in this commit comply with the clang-format rules.\n"
161     rm -f "$patch"
162     exit 0
163 fi
164
165 # a patch has been created, notify the user and exit
166 printf "\nThe following differences were found between the code to commit "
167 printf "and the clang-format rules:\n\n"
168 cat "$patch"
169
170 printf "\nYou can apply these changes and readd the files with:\n"
171 printf "  git apply $patch &&  git apply --cached $patch\n"
172 printf "(call this command from the root directory of your repository)\n"
173 printf "\n\n\n"
174 printf "Aborting commit. Apply changes and commit again.\n"
175 # printf "Skip checking with: --no-verify (not recommended).\n"
176
177 exit 1