summaryrefslogtreecommitdiff
path: root/misc
diff options
context:
space:
mode:
authorRémi Verschelde <rverschelde@gmail.com>2017-02-12 00:51:14 +0100
committerRémi Verschelde <rverschelde@gmail.com>2017-02-12 00:51:31 +0100
commit0e4ee5935a000f5d6de63d3c8ad15e1baf80cd0e (patch)
treec2eae10c125abcf3e0f52be2cdb57b45d4758d2d /misc
parent503c98ead43e15189c36375be3298be2b1e258a2 (diff)
clang-format: Add pre-commit hook
Derived from https://github.com/githubbrowser/Pre-commit-hooks and https://gitlab.cern.ch/GeantV/geant/blob/master/hooks/pre-commit-clang-format
Diffstat (limited to 'misc')
-rw-r--r--misc/hooks/README.md18
-rwxr-xr-xmisc/hooks/canonicalize_filename.sh48
-rwxr-xr-xmisc/hooks/pre-commit50
-rwxr-xr-xmisc/hooks/pre-commit-clang-format142
4 files changed, 258 insertions, 0 deletions
diff --git a/misc/hooks/README.md b/misc/hooks/README.md
new file mode 100644
index 0000000000..6ec90fcc54
--- /dev/null
+++ b/misc/hooks/README.md
@@ -0,0 +1,18 @@
+# Git hooks for Godot Engine
+
+This folder contains git hooks meant to be installed locally by Godot Engine
+contributors to make sure they comply with our requirements.
+
+## List of hooks
+
+- Pre-commit hook for clang-format: Applies clang-format to the staged files
+ before accepting a commit; blocks the commit and generates a patch if the
+ style is not respected.
+ Should work on Linux and macOS. You may need to edit the file if your
+ clang-format binary is not in the $PATH, or if you want to enable colored
+ output with pygmentize.
+
+## Installation
+
+Copy all the files from this folder into your .git/hooks folder, and make sure
+the hooks and helper scripts are executable.
diff --git a/misc/hooks/canonicalize_filename.sh b/misc/hooks/canonicalize_filename.sh
new file mode 100755
index 0000000000..5eecabf5bc
--- /dev/null
+++ b/misc/hooks/canonicalize_filename.sh
@@ -0,0 +1,48 @@
+#!/bin/sh
+
+# Provide the canonicalize filename (physical filename with out any symlinks)
+# like the GNU version readlink with the -f option regardless of the version of
+# readlink (GNU or BSD).
+
+# This file is part of a set of unofficial pre-commit hooks available
+# at github.
+# Link: https://github.com/githubbrowser/Pre-commit-hooks
+# Contact: David Martin, david.martin.mailbox@googlemail.com
+
+###########################################################
+# There should be no need to change anything below this line.
+
+# Canonicalize by recursively following every symlink in every component of the
+# specified filename. This should reproduce the results of the GNU version of
+# readlink with the -f option.
+#
+# Reference: http://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac
+canonicalize_filename () {
+ local target_file="$1"
+ local physical_directory=""
+ local result=""
+
+ # Need to restore the working directory after work.
+ local working_dir="`pwd`"
+
+ cd -- "$(dirname -- "$target_file")"
+ target_file="$(basename -- "$target_file")"
+
+ # Iterate down a (possible) chain of symlinks
+ while [ -L "$target_file" ]
+ do
+ target_file="$(readlink -- "$target_file")"
+ cd -- "$(dirname -- "$target_file")"
+ target_file="$(basename -- "$target_file")"
+ done
+
+ # Compute the canonicalized name by finding the physical path
+ # for the directory we're in and appending the target file.
+ physical_directory="`pwd -P`"
+ result="$physical_directory/$target_file"
+
+ # restore the working directory after work.
+ cd -- "$working_dir"
+
+ echo "$result"
+}
diff --git a/misc/hooks/pre-commit b/misc/hooks/pre-commit
new file mode 100755
index 0000000000..fc50ed70e7
--- /dev/null
+++ b/misc/hooks/pre-commit
@@ -0,0 +1,50 @@
+#!/bin/sh
+# Git pre-commit hook that runs multiple hooks specified in $HOOKS.
+# Make sure this script is executable. Bypass hooks with git commit --no-verify.
+
+# This file is part of a set of unofficial pre-commit hooks available
+# at github.
+# Link: https://github.com/githubbrowser/Pre-commit-hooks
+# Contact: David Martin, david.martin.mailbox@googlemail.com
+
+
+###########################################################
+# CONFIGURATION:
+# pre-commit hooks to be executed. They should be in the same .git/hooks/ folder
+# as this script. Hooks should return 0 if successful and nonzero to cancel the
+# commit. They are executed in the order in which they are listed.
+#HOOKS="pre-commit-compile pre-commit-uncrustify"
+HOOKS="pre-commit-clang-format"
+###########################################################
+# There should be no need to change anything below this line.
+
+. "$(dirname -- "$0")/canonicalize_filename.sh"
+
+# exit on error
+set -e
+
+# Absolute path to this script, e.g. /home/user/bin/foo.sh
+SCRIPT="$(canonicalize_filename "$0")"
+
+# Absolute path this script is in, thus /home/user/bin
+SCRIPTPATH="$(dirname -- "$SCRIPT")"
+
+
+for hook in $HOOKS
+do
+ echo "Running hook: $hook"
+ # run hook if it exists
+ # if it returns with nonzero exit with 1 and thus abort the commit
+ if [ -f "$SCRIPTPATH/$hook" ]; then
+ "$SCRIPTPATH/$hook"
+ if [ $? != 0 ]; then
+ exit 1
+ fi
+ else
+ echo "Error: file $hook not found."
+ echo "Aborting commit. Make sure the hook is in $SCRIPTPATH and executable."
+ echo "You can disable it by removing it from the list in $SCRIPT."
+ echo "You can skip all pre-commit hooks with --no-verify (not recommended)."
+ exit 1
+ fi
+done
diff --git a/misc/hooks/pre-commit-clang-format b/misc/hooks/pre-commit-clang-format
new file mode 100755
index 0000000000..f7620fe2c5
--- /dev/null
+++ b/misc/hooks/pre-commit-clang-format
@@ -0,0 +1,142 @@
+#!/bin/bash
+
+# git pre-commit hook that runs an clang-format stylecheck.
+# Features:
+# - abort commit when commit does not comply with the style guidelines
+# - create a patch of the proposed style changes
+# Modifications for clang-format by rene.milk@wwu.de
+
+# This file is part of a set of unofficial pre-commit hooks available
+# at github.
+# Link: https://github.com/githubbrowser/Pre-commit-hooks
+# Contact: David Martin, david.martin.mailbox@googlemail.com
+
+# Some quality of life modifications made for Godot Engine.
+
+##################################################################
+# SETTINGS
+# Set path to clang-format binary
+# CLANG_FORMAT="/usr/bin/clang-format"
+CLANG_FORMAT=`which clang-format`
+
+# Remove any older patches from previous commits. Set to true or false.
+# DELETE_OLD_PATCHES=false
+DELETE_OLD_PATCHES=false
+
+# Only parse files with the extensions in FILE_EXTS. Set to true or false.
+# If false every changed file in the commit will be parsed with clang-format.
+# If true only files matching one of the extensions are parsed with clang-format.
+# PARSE_EXTS=true
+PARSE_EXTS=true
+
+# File types to parse. Only effective when PARSE_EXTS is true.
+# FILE_EXTS=".c .h .cpp .hpp"
+FILE_EXTS=".c .h .cpp .hpp .cc .hh .cxx .m"
+
+# Use pygmentize instead of cat to parse diff with highlighting.
+# Install it with `pip install pygments` (Linux) or `easy_install Pygments` (Mac)
+# READER="pygmentize -l diff"
+READER=cat
+
+##################################################################
+# There should be no need to change anything below this line.
+
+. "$(dirname -- "$0")/canonicalize_filename.sh"
+
+# exit on error
+set -e
+
+# check whether the given file matches any of the set extensions
+matches_extension() {
+ local filename=$(basename "$1")
+ local extension=".${filename##*.}"
+ local ext
+
+ for ext in $FILE_EXTS; do [[ "$ext" == "$extension" ]] && return 0; done
+
+ return 1
+}
+
+# necessary check for initial commit
+if git rev-parse --verify HEAD >/dev/null 2>&1 ; then
+ against=HEAD
+else
+ # Initial commit: diff against an empty tree object
+ against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
+fi
+
+if [ ! -x "$CLANG_FORMAT" ] ; then
+ printf "Error: clang-format executable not found.\n"
+ printf "Set the correct path in $(canonicalize_filename "$0").\n"
+ exit 1
+fi
+
+# create a random filename to store our generated patch
+prefix="pre-commit-clang-format"
+suffix="$(date +%s)"
+patch="/tmp/$prefix-$suffix.patch"
+
+# clean up any older clang-format patches
+$DELETE_OLD_PATCHES && rm -f /tmp/$prefix*.patch
+
+# create one patch containing all changes to the files
+git diff-index --cached --diff-filter=ACMR --name-only $against -- | while read file;
+do
+ # ignore file if we do check for file extensions and the file
+ # does not match any of the extensions specified in $FILE_EXTS
+ if $PARSE_EXTS && ! matches_extension "$file"; then
+ continue;
+ fi
+
+ # clang-format our sourcefile, create a patch with diff and append it to our $patch
+ # The sed call is necessary to transform the patch from
+ # --- $file timestamp
+ # +++ - timestamp
+ # to both lines working on the same file and having a a/ and b/ prefix.
+ # Else it can not be applied with 'git apply'.
+ "$CLANG_FORMAT" -style=file "$file" | \
+ diff -u "$file" - | \
+ sed -e "1s|--- |--- a/|" -e "2s|+++ -|+++ b/$file|" >> "$patch"
+done
+
+# if no patch has been generated all is ok, clean up the file stub and exit
+if [ ! -s "$patch" ] ; then
+ printf "Files in this commit comply with the clang-format rules.\n"
+ rm -f "$patch"
+ exit 0
+fi
+
+# a patch has been created, notify the user and exit
+printf "\nThe following differences were found between the code to commit "
+printf "and the clang-format rules:\n\n"
+$READER "$patch"
+printf "\n"
+
+# Allows us to read user input below, assigns stdin to keyboard
+exec < /dev/tty
+
+while true; do
+ read -p "Do you want to apply that patch (Y - Apply, N - Do not apply, S - Apply and stage files)? [Y/N/S] " yn
+ case $yn in
+ [Yy] ) git apply $patch;
+ printf "The patch was applied. You can now stage the changes and commit again.\n\n";
+ break
+ ;;
+ [Nn] ) printf "\nYou can apply these changes with:\n git apply $patch\n";
+ printf "(may need to be called from the root directory of your repository)\n";
+ printf "Aborting commit. Apply changes and commit again or skip checking with";
+ printf " --no-verify (not recommended).\n\n";
+ break
+ ;;
+ [Ss] ) git apply $patch;
+ git diff-index --cached --diff-filter=ACMR --name-only $against -- | while read file;
+ do git add $file;
+ done
+ printf "The patch was applied and the changed files staged. You can now commit.\n\n";
+ break
+ ;;
+ * ) echo "Please answer yes or no."
+ ;;
+ esac
+done
+exit 1 # we don't commit in any case