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 |
Edward O'Callaghan | 4139c15 | 2015-01-03 02:08:33 +1100 | [diff] [blame] | 8 | # Link: https://github.com/githubbrowser/Pre-commit-hooks |
| 9 | # Contact: David Martin, david.martin.mailbox@googlemail.com |
| 10 | |
| 11 | # DESCR: Checking that VCS 'staged' source conforms to coding style |
| 12 | |
| 13 | ################################################################## |
| 14 | # SETTINGS |
| 15 | # set path to clang-format binary |
Alex James | 7aeeb48 | 2019-05-09 10:40:46 -0500 | [diff] [blame] | 16 | CLANG_FORMAT="$(command -v clang-format)" |
Edward O'Callaghan | 4139c15 | 2015-01-03 02:08:33 +1100 | [diff] [blame] | 17 | |
| 18 | # remove any older patches from previous commits. Set to true or false. |
| 19 | # DELETE_OLD_PATCHES=false |
| 20 | DELETE_OLD_PATCHES=false |
| 21 | |
| 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. |
| 25 | # PARSE_EXTS=true |
| 26 | PARSE_EXTS=true |
| 27 | |
| 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" |
| 31 | |
| 32 | ################################################################## |
| 33 | # There should be no need to change anything below this line. |
| 34 | |
| 35 | # Reference: http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac |
| 36 | canonicalize_filename () { |
| 37 | local target_file=$1 |
| 38 | local physical_directory="" |
| 39 | local result="" |
| 40 | |
| 41 | # Need to restore the working directory after work. |
| 42 | pushd `pwd` > /dev/null |
| 43 | |
| 44 | cd "$(dirname "$target_file")" |
| 45 | target_file=`basename $target_file` |
| 46 | |
| 47 | # Iterate down a (possible) chain of symlinks |
| 48 | while [ -L "$target_file" ] |
| 49 | do |
| 50 | target_file=$(readlink "$target_file") |
| 51 | cd "$(dirname "$target_file")" |
| 52 | target_file=$(basename "$target_file") |
| 53 | done |
| 54 | |
| 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" |
| 59 | |
| 60 | # restore the working directory after work. |
| 61 | popd > /dev/null |
| 62 | |
| 63 | echo "$result" |
| 64 | } |
| 65 | |
| 66 | # exit on error |
| 67 | set -e |
| 68 | |
| 69 | # check whether the given file matches any of the set extensions |
| 70 | matches_extension() { |
| 71 | local filename=$(basename "$1") |
| 72 | local extension=".${filename##*.}" |
| 73 | local ext |
| 74 | |
| 75 | for ext in $FILE_EXTS; do [[ "$ext" == "$extension" ]] && return 0; done |
| 76 | |
| 77 | return 1 |
| 78 | } |
| 79 | |
| 80 | # necessary check for initial commit |
| 81 | if git rev-parse --verify HEAD >/dev/null 2>&1 ; then |
| 82 | against=HEAD |
| 83 | else |
| 84 | # Initial commit: diff against an empty tree object |
| 85 | against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 |
| 86 | fi |
| 87 | |
| 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" |
| 92 | # exit 1 |
| 93 | exit 0 |
| 94 | fi |
| 95 | |
| 96 | # create a random filename to store our generated patch |
| 97 | prefix="pre-commit-clang-format" |
| 98 | suffix="$(date +%s)" |
| 99 | patch="/tmp/$prefix-$suffix.patch" |
| 100 | |
| 101 | # clean up any older clang-format patches |
| 102 | $DELETE_OLD_PATCHES && rm -f /tmp/$prefix*.patch |
| 103 | |
| 104 | # create one patch containing all changes to the files |
| 105 | git diff-index --cached --diff-filter=ACMR --name-only $against -- | while read file; |
| 106 | do |
| 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 |
| 110 | continue; |
| 111 | fi |
| 112 | |
| 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 |
| 116 | # +++ - 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" |
| 122 | done |
| 123 | |
| 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" |
| 127 | rm -f "$patch" |
| 128 | exit 0 |
| 129 | fi |
| 130 | |
| 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" |
| 134 | cat "$patch" |
| 135 | |
| 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" |
Edward O'Callaghan | 4139c15 | 2015-01-03 02:08:33 +1100 | [diff] [blame] | 138 | |
Angel Pons | ce828b6 | 2019-08-31 02:46:42 +0200 | [diff] [blame] | 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" |
| 142 | # exit 1 |
| 143 | |
| 144 | exit 0 |