summaryrefslogtreecommitdiffstats
path: root/misc/hooks/pre-commit-clang-format
diff options
context:
space:
mode:
authorGeorge Marques <george@gmarqu.es>2021-08-18 11:03:52 -0300
committerBastiaan Olij <mux213@gmail.com>2021-09-27 23:08:08 +1000
commite4ed48976a962b67e9585cc2d20d11f115ef7949 (patch)
tree7830ad6926b5cd14a91784b07c2eff5b77e3f533 /misc/hooks/pre-commit-clang-format
parentee708668944430a7f1d69e8faf7b3f3160432dc2 (diff)
downloadredot-cpp-e4ed48976a962b67e9585cc2d20d11f115ef7949.tar.gz
Replace bindgins to work with extensions
Diffstat (limited to 'misc/hooks/pre-commit-clang-format')
-rwxr-xr-x[-rw-r--r--]misc/hooks/pre-commit-clang-format123
1 files changed, 109 insertions, 14 deletions
diff --git a/misc/hooks/pre-commit-clang-format b/misc/hooks/pre-commit-clang-format
index db241ad..81bc412 100644..100755
--- a/misc/hooks/pre-commit-clang-format
+++ b/misc/hooks/pre-commit-clang-format
@@ -15,28 +15,37 @@
##################################################################
# SETTINGS
-# Set path to clang-format binary
-# CLANG_FORMAT="/usr/bin/clang-format"
-CLANG_FORMAT=`which clang-format`
+# Set path to clang-format binary.
+CLANG_FORMAT=`which clang-format 2>/dev/null`
# 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 .mm .inc .java .glsl"
# 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
+PYGMENTIZE=`which pygmentize 2>/dev/null`
+if [ ! -z "$PYGMENTIZE" ]; then
+ READER="pygmentize -l diff"
+else
+ READER=cat
+fi
+
+# Path to zenity
+ZENITY=`which zenity 2>/dev/null`
+
+# Path to xmessage
+XMSG=`which xmessage 2>/dev/null`
+
+# Path to powershell (Windows only)
+PWSH=`which powershell 2>/dev/null`
##################################################################
# There should be no need to change anything below this line.
@@ -65,12 +74,44 @@ else
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
+# To get consistent formatting, we recommend contributors to use the same
+# clang-format version as CI.
+RECOMMENDED_CLANG_FORMAT_MAJOR_MIN="11"
+RECOMMENDED_CLANG_FORMAT_MAJOR_MAX="12"
+
if [ ! -x "$CLANG_FORMAT" ] ; then
- printf "Error: clang-format executable not found.\n"
+ message="Error: clang-format executable not found. Please install clang-format $RECOMMENDED_CLANG_FORMAT_MAJOR.x.x."
+
+ if [ ! -t 1 ] ; then
+ if [ -x "$ZENITY" ] ; then
+ $ZENITY --error --title="Error" --text="$message"
+ exit 1
+ elif [ -x "$XMSG" ] ; then
+ $XMSG -center -title "Error" "$message"
+ exit 1
+ elif [ \( \( "$OSTYPE" = "msys" \) -o \( "$OSTYPE" = "win32" \) \) -a \( -x "$PWSH" \) ]; then
+ winmessage="$(canonicalize_filename "./.git/hooks/winmessage.ps1")"
+ $PWSH -noprofile -executionpolicy bypass -file "$winmessage" -center -title "Error" --text "$message"
+ exit 1
+ fi
+ fi
+ printf "$message\n"
printf "Set the correct path in $(canonicalize_filename "$0").\n"
exit 1
fi
+# The returned string can be inconsistent depending on where clang-format comes from.
+# Example output strings reported by `clang-format --version`:
+# - Ubuntu: "Ubuntu clang-format version 11.0.0-2"
+# - Fedora: "clang-format version 11.0.0 (Fedora 11.0.0-2.fc33)"
+CLANG_FORMAT_VERSION="$(clang-format --version | sed "s/[^0-9\.]*\([0-9\.]*\).*/\1/")"
+CLANG_FORMAT_MAJOR="$(echo "$CLANG_FORMAT_VERSION" | cut -d. -f1)"
+
+if [[ "$CLANG_FORMAT_MAJOR" -lt "$RECOMMENDED_CLANG_FORMAT_MAJOR_MIN" || "$CLANG_FORMAT_MAJOR" -gt "$RECOMMENDED_CLANG_FORMAT_MAJOR_MAX" ]]; then
+ echo "Warning: Your clang-format binary is the wrong version ($CLANG_FORMAT_VERSION, expected between $RECOMMENDED_CLANG_FORMAT_MAJOR_MIN.x.x and $RECOMMENDED_CLANG_FORMAT_MAJOR_MAX.x.x)."
+ echo " Consider upgrading or downgrading clang-format as formatting may not be applied correctly."
+fi
+
# create a random filename to store our generated patch
prefix="pre-commit-clang-format"
suffix="$(date +%s)"
@@ -86,6 +127,12 @@ do
if grep -q "thirdparty" <<< $file; then
continue;
fi
+ if grep -q "platform/android/java/lib/src/com" <<< $file; then
+ continue;
+ fi
+ if grep -q "\-so_wrap." <<< $file; then
+ continue;
+ fi
# ignore file if we do check for file extensions and the file
# does not match any of the extensions specified in $FILE_EXTS
@@ -114,14 +161,62 @@ 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
+if [ -t 1 ] ; then
+ $READER "$patch"
+ printf "\n"
+ # Allows us to read user input below, assigns stdin to keyboard
+ exec < /dev/tty
+ terminal="1"
+else
+ cat "$patch"
+ printf "\n"
+ # Allows non zero zenity/powershell output
+ set +e
+ terminal="0"
+fi
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
+ if [ $terminal = "0" ] ; then
+ if [ -x "$ZENITY" ] ; then
+ ans=$($ZENITY --text-info --filename="$patch" --width=800 --height=600 --title="Do you want to apply that patch?" --ok-label="Apply" --cancel-label="Do not apply" --extra-button="Apply and stage")
+ if [ "$?" = "0" ] ; then
+ yn="Y"
+ else
+ if [ "$ans" = "Apply and stage" ] ; then
+ yn="S"
+ else
+ yn="N"
+ fi
+ fi
+ elif [ -x "$XMSG" ] ; then
+ $XMSG -file "$patch" -buttons "Apply":100,"Apply and stage":200,"Do not apply":0 -center -default "Do not apply" -geometry 800x600 -title "Do you want to apply that patch?"
+ ans=$?
+ if [ "$ans" = "100" ] ; then
+ yn="Y"
+ elif [ "$ans" = "200" ] ; then
+ yn="S"
+ else
+ yn="N"
+ fi
+ elif [ \( \( "$OSTYPE" = "msys" \) -o \( "$OSTYPE" = "win32" \) \) -a \( -x "$PWSH" \) ]; then
+ winmessage="$(canonicalize_filename "./.git/hooks/winmessage.ps1")"
+ $PWSH -noprofile -executionpolicy bypass -file "$winmessage" -file "$patch" -buttons "Apply":100,"Apply and stage":200,"Do not apply":0 -center -default "Do not apply" -geometry 800x600 -title "Do you want to apply that patch?"
+ ans=$?
+ if [ "$ans" = "100" ] ; then
+ yn="Y"
+ elif [ "$ans" = "200" ] ; then
+ yn="S"
+ else
+ yn="N"
+ fi
+ else
+ printf "Error: zenity, xmessage, or powershell executable not found.\n"
+ exit 1
+ fi
+ else
+ read -p "Do you want to apply that patch (Y - Apply, N - Do not apply, S - Apply and stage files)? [Y/N/S] " yn
+ fi
case $yn in
[Yy] ) git apply $patch;
printf "The patch was applied. You can now stage the changes and commit again.\n\n";