Idwer Vollering | eb6887e | 2020-01-06 16:34:32 +0100 | [diff] [blame] | 1 | #!/usr/bin/env bash |
Edward O'Callaghan | 4139c15 | 2015-01-03 02:08:33 +1100 | [diff] [blame] | 2 | # git pre-commit hook that runs an clang-format stylecheck. |
| 3 | # Features: |
| 4 | # - abort commit when commit does not comply with the style guidelines |
| 5 | # - create a patch of the proposed style changes |
| 6 | # |
| 7 | # modifications for clang-format by rene.milk@wwu.de |
| 8 | # This file is part of a set of unofficial pre-commit hooks available |
| 9 | # at github. |
| 10 | # Link: https://github.com/githubbrowser/Pre-commit-hooks |
| 11 | # Contact: David Martin, david.martin.mailbox@googlemail.com |
| 12 | |
| 13 | # DESCR: Checking that VCS 'staged' source conforms to coding style |
| 14 | |
| 15 | ################################################################## |
| 16 | # SETTINGS |
| 17 | # set path to clang-format binary |
Alex James | 7aeeb48 | 2019-05-09 10:40:46 -0500 | [diff] [blame] | 18 | CLANG_FORMAT="$(command -v clang-format)" |
Edward O'Callaghan | 4139c15 | 2015-01-03 02:08:33 +1100 | [diff] [blame] | 19 | |
| 20 | # remove any older patches from previous commits. Set to true or false. |
| 21 | # DELETE_OLD_PATCHES=false |
| 22 | DELETE_OLD_PATCHES=false |
| 23 | |
| 24 | # only parse files with the extensions in FILE_EXTS. Set to true or false. |
| 25 | # if false every changed file in the commit will be parsed with clang-format. |
| 26 | # if true only files matching one of the extensions are parsed with clang-format. |
| 27 | # PARSE_EXTS=true |
| 28 | PARSE_EXTS=true |
| 29 | |
| 30 | # file types to parse. Only effective when PARSE_EXTS is true. |
| 31 | # FILE_EXTS=".c .h .cpp .hpp" |
| 32 | FILE_EXTS=".c .h .cpp .hpp .cc .hh .cxx" |
| 33 | |
| 34 | ################################################################## |
| 35 | # There should be no need to change anything below this line. |
| 36 | |
| 37 | # Reference: http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac |
| 38 | canonicalize_filename () { |
| 39 | local target_file=$1 |
| 40 | local physical_directory="" |
| 41 | local result="" |
| 42 | |
| 43 | # Need to restore the working directory after work. |
| 44 | pushd `pwd` > /dev/null |
| 45 | |
| 46 | cd "$(dirname "$target_file")" |
| 47 | target_file=`basename $target_file` |
| 48 | |
| 49 | # Iterate down a (possible) chain of symlinks |
| 50 | while [ -L "$target_file" ] |
| 51 | do |
| 52 | target_file=$(readlink "$target_file") |
| 53 | cd "$(dirname "$target_file")" |
| 54 | target_file=$(basename "$target_file") |
| 55 | done |
| 56 | |
| 57 | # Compute the canonicalized name by finding the physical path |
| 58 | # for the directory we're in and appending the target file. |
| 59 | physical_directory=`pwd -P` |
| 60 | result="$physical_directory"/"$target_file" |
| 61 | |
| 62 | # restore the working directory after work. |
| 63 | popd > /dev/null |
| 64 | |
| 65 | echo "$result" |
| 66 | } |
| 67 | |
| 68 | # exit on error |
| 69 | set -e |
| 70 | |
| 71 | # check whether the given file matches any of the set extensions |
| 72 | matches_extension() { |
| 73 | local filename=$(basename "$1") |
| 74 | local extension=".${filename##*.}" |
| 75 | local ext |
| 76 | |
| 77 | for ext in $FILE_EXTS; do [[ "$ext" == "$extension" ]] && return 0; done |
| 78 | |
| 79 | return 1 |
| 80 | } |
| 81 | |
| 82 | # necessary check for initial commit |
| 83 | if git rev-parse --verify HEAD >/dev/null 2>&1 ; then |
| 84 | against=HEAD |
| 85 | else |
| 86 | # Initial commit: diff against an empty tree object |
| 87 | against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 |
| 88 | fi |
| 89 | |
| 90 | if [ ! -x "$CLANG_FORMAT" ] ; then |
| 91 | printf "Warning: clang-format executable not found.\n" |
| 92 | printf "Set the correct path in $(canonicalize_filename "$0").\n" |
| 93 | printf "Skipping clang-format style check test.\n" |
| 94 | # exit 1 |
| 95 | exit 0 |
| 96 | fi |
| 97 | |
| 98 | # create a random filename to store our generated patch |
| 99 | prefix="pre-commit-clang-format" |
| 100 | suffix="$(date +%s)" |
| 101 | patch="/tmp/$prefix-$suffix.patch" |
| 102 | |
| 103 | # clean up any older clang-format patches |
| 104 | $DELETE_OLD_PATCHES && rm -f /tmp/$prefix*.patch |
| 105 | |
| 106 | # create one patch containing all changes to the files |
| 107 | git diff-index --cached --diff-filter=ACMR --name-only $against -- | while read file; |
| 108 | do |
| 109 | # ignore file if we do check for file extensions and the file |
| 110 | # does not match any of the extensions specified in $FILE_EXTS |
| 111 | if $PARSE_EXTS && ! matches_extension "$file"; then |
| 112 | continue; |
| 113 | fi |
| 114 | |
| 115 | # clang-format our sourcefile, create a patch with diff and append it to our $patch |
| 116 | # The sed call is necessary to transform the patch from |
| 117 | # --- $file timestamp |
| 118 | # +++ - timestamp |
| 119 | # to both lines working on the same file and having a a/ and b/ prefix. |
| 120 | # Else it can not be applied with 'git apply'. |
| 121 | "$CLANG_FORMAT" -style=file "$file" | \ |
| 122 | diff -u "$file" - | \ |
| 123 | sed -e "1s|--- |--- a/|" -e "2s|+++ -|+++ b/$file|" >> "$patch" |
| 124 | done |
| 125 | |
| 126 | # if no patch has been generated all is ok, clean up the file stub and exit |
| 127 | if [ ! -s "$patch" ] ; then |
| 128 | printf "Files in this commit comply with the clang-format rules.\n" |
| 129 | rm -f "$patch" |
| 130 | exit 0 |
| 131 | fi |
| 132 | |
| 133 | # a patch has been created, notify the user and exit |
| 134 | printf "\nThe following differences were found between the code to commit " |
| 135 | printf "and the clang-format rules:\n\n" |
| 136 | cat "$patch" |
| 137 | |
| 138 | printf "\nYou can apply these changes with:\n git apply $patch\n" |
| 139 | printf "(may need to be called from the root directory of your repository)\n" |
Edward O'Callaghan | 4139c15 | 2015-01-03 02:08:33 +1100 | [diff] [blame] | 140 | |
Angel Pons | ce828b6 | 2019-08-31 02:46:42 +0200 | [diff] [blame] | 141 | # FIXME: clang-format is currently unusable, so don't abort the commit. |
| 142 | # printf "Aborting commit. Apply changes and commit again or skip checking with" |
| 143 | # printf " --no-verify (not recommended).\n" |
| 144 | # exit 1 |
| 145 | |
| 146 | exit 0 |