|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# git pre-commit hook that runs a clang-format stylecheck. |
| 4 | +# Features: |
| 5 | +# - abort commit when commit does not comply with the style guidelines |
| 6 | +# - create a patch of the proposed style changes |
| 7 | +# Modifications for clang-format by [email protected] |
| 8 | + |
| 9 | +# This file is part of a set of unofficial pre-commit hooks available |
| 10 | +# at github. |
| 11 | +# Link: https://github.com/githubbrowser/Pre-commit-hooks |
| 12 | +# Contact: David Martin, [email protected] |
| 13 | + |
| 14 | +# Some quality of life modifications made for Godot Engine. |
| 15 | + |
| 16 | +################################################################## |
| 17 | +# SETTINGS |
| 18 | +# Set path to clang-format binary |
| 19 | +# CLANG_FORMAT="/usr/bin/clang-format" |
| 20 | +CLANG_FORMAT=`which clang-format` |
| 21 | + |
| 22 | +# Remove any older patches from previous commits. Set to true or false. |
| 23 | +# DELETE_OLD_PATCHES=false |
| 24 | +DELETE_OLD_PATCHES=false |
| 25 | + |
| 26 | +# Only parse files with the extensions in FILE_EXTS. Set to true or false. |
| 27 | +# If false every changed file in the commit will be parsed with clang-format. |
| 28 | +# If true only files matching one of the extensions are parsed with clang-format. |
| 29 | +# PARSE_EXTS=true |
| 30 | +PARSE_EXTS=true |
| 31 | + |
| 32 | +# File types to parse. Only effective when PARSE_EXTS is true. |
| 33 | +# FILE_EXTS=".c .h .cpp .hpp" |
| 34 | +FILE_EXTS=".c .h .cpp .hpp .cc .hh .cxx .m .mm .inc .java .glsl" |
| 35 | + |
| 36 | +# Use pygmentize instead of cat to parse diff with highlighting. |
| 37 | +# Install it with `pip install pygments` (Linux) or `easy_install Pygments` (Mac) |
| 38 | +# READER="pygmentize -l diff" |
| 39 | +READER=cat |
| 40 | + |
| 41 | +################################################################## |
| 42 | +# There should be no need to change anything below this line. |
| 43 | + |
| 44 | +. "$(dirname -- "$0")/canonicalize_filename.sh" |
| 45 | + |
| 46 | +# exit on error |
| 47 | +set -e |
| 48 | + |
| 49 | +# check whether the given file matches any of the set extensions |
| 50 | +matches_extension() { |
| 51 | + local filename=$(basename "$1") |
| 52 | + local extension=".${filename##*.}" |
| 53 | + local ext |
| 54 | + |
| 55 | + for ext in $FILE_EXTS; do [[ "$ext" == "$extension" ]] && return 0; done |
| 56 | + |
| 57 | + return 1 |
| 58 | +} |
| 59 | + |
| 60 | +# necessary check for initial commit |
| 61 | +if git rev-parse --verify HEAD >/dev/null 2>&1 ; then |
| 62 | + against=HEAD |
| 63 | +else |
| 64 | + # Initial commit: diff against an empty tree object |
| 65 | + against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 |
| 66 | +fi |
| 67 | + |
| 68 | +if [ ! -x "$CLANG_FORMAT" ] ; then |
| 69 | + printf "Error: clang-format executable not found.\n" |
| 70 | + printf "Set the correct path in $(canonicalize_filename "$0").\n" |
| 71 | + exit 1 |
| 72 | +fi |
| 73 | + |
| 74 | +# create a random filename to store our generated patch |
| 75 | +prefix="pre-commit-clang-format" |
| 76 | +suffix="$(date +%s)" |
| 77 | +patch="/tmp/$prefix-$suffix.patch" |
| 78 | + |
| 79 | +# clean up any older clang-format patches |
| 80 | +$DELETE_OLD_PATCHES && rm -f /tmp/$prefix*.patch |
| 81 | + |
| 82 | +# create one patch containing all changes to the files |
| 83 | +git diff-index --cached --diff-filter=ACMR --name-only $against -- | while read file; |
| 84 | +do |
| 85 | + # ignore thirdparty files |
| 86 | + if grep -q "thirdparty" <<< $file; then |
| 87 | + continue; |
| 88 | + fi |
| 89 | + |
| 90 | + # ignore file if we do check for file extensions and the file |
| 91 | + # does not match any of the extensions specified in $FILE_EXTS |
| 92 | + if $PARSE_EXTS && ! matches_extension "$file"; then |
| 93 | + continue; |
| 94 | + fi |
| 95 | + |
| 96 | + # clang-format our sourcefile, create a patch with diff and append it to our $patch |
| 97 | + # The sed call is necessary to transform the patch from |
| 98 | + # --- $file timestamp |
| 99 | + # +++ - timestamp |
| 100 | + # to both lines working on the same file and having a/ and b/ prefix. |
| 101 | + # Else it can not be applied with 'git apply'. |
| 102 | + "$CLANG_FORMAT" -style=file "$file" | \ |
| 103 | + diff -u "$file" - | \ |
| 104 | + sed -e "1s|--- |--- a/|" -e "2s|+++ -|+++ b/$file|" >> "$patch" |
| 105 | +done |
| 106 | + |
| 107 | +# if no patch has been generated all is ok, clean up the file stub and exit |
| 108 | +if [ ! -s "$patch" ] ; then |
| 109 | + printf "Files in this commit comply with the clang-format rules.\n" |
| 110 | + rm -f "$patch" |
| 111 | + exit 0 |
| 112 | +fi |
| 113 | + |
| 114 | +# a patch has been created, notify the user and exit |
| 115 | +printf "\nThe following differences were found between the code to commit " |
| 116 | +printf "and the clang-format rules:\n\n" |
| 117 | +$READER "$patch" |
| 118 | +printf "\n" |
| 119 | + |
| 120 | +# Allows us to read user input below, assigns stdin to keyboard |
| 121 | +exec < /dev/tty |
| 122 | + |
| 123 | +while true; do |
| 124 | + read -p "Do you want to apply that patch (Y - Apply, N - Do not apply, S - Apply and stage files)? [Y/N/S] " yn |
| 125 | + case $yn in |
| 126 | + [Yy] ) git apply $patch; |
| 127 | + printf "The patch was applied. You can now stage the changes and commit again.\n\n"; |
| 128 | + break |
| 129 | + ;; |
| 130 | + [Nn] ) printf "\nYou can apply these changes with:\n git apply $patch\n"; |
| 131 | + printf "(may need to be called from the root directory of your repository)\n"; |
| 132 | + printf "Aborting commit. Apply changes and commit again or skip checking with"; |
| 133 | + printf " --no-verify (not recommended).\n\n"; |
| 134 | + break |
| 135 | + ;; |
| 136 | + [Ss] ) git apply $patch; |
| 137 | + git diff-index --cached --diff-filter=ACMR --name-only $against -- | while read file; |
| 138 | + do git add $file; |
| 139 | + done |
| 140 | + printf "The patch was applied and the changed files staged. You can now commit.\n\n"; |
| 141 | + break |
| 142 | + ;; |
| 143 | + * ) echo "Please answer yes or no." |
| 144 | + ;; |
| 145 | + esac |
| 146 | +done |
| 147 | +exit 1 # we don't commit in any case |
0 commit comments