mirror of
https://github.com/rwengine/openrw.git
synced 2024-11-22 02:12:45 +01:00
Add verify-commit script for pre-commit style check
This commit is contained in:
parent
9338c34096
commit
b4f5a1d2cc
46
scripts/verify-commit
Executable file
46
scripts/verify-commit
Executable file
@ -0,0 +1,46 @@
|
||||
#!/bin/bash
|
||||
|
||||
# verify-commit:
|
||||
# Ensures that a diff doesn't introduce code that clang-format would change
|
||||
|
||||
# Can be executed as a pre-commit hook by symlinking this script:
|
||||
# $ ln -s .git/hooks/pre-commit scripts/verify-commit
|
||||
# Or used to verify changes between two commits
|
||||
# $ verify-commit HEAD~2 HEAD
|
||||
|
||||
|
||||
# Determine if we need to check A-B or just cached changes
|
||||
if [ "$#" == 2 ]; then
|
||||
DIFF_ARGS="$1 $2"
|
||||
CAN_ACCEPT=false
|
||||
elif [ "$#" == 0 ]; then
|
||||
DIFF_ARGS="--cached"
|
||||
CAN_ACCEPT=true
|
||||
else
|
||||
echo "Unrecognised arguments"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
CLANG_DIFF=$(git diff ${DIFF_ARGS} -U0 | clang-format-diff -p1)
|
||||
WARNED=""
|
||||
|
||||
# Check the diff and warn if there is anything that doesn't match the style
|
||||
if [ -n "$CLANG_DIFF" ]; then
|
||||
echo "Source does not match expected clang-format"
|
||||
echo "$CLANG_DIFF"
|
||||
WARNED="1"
|
||||
fi
|
||||
|
||||
# Allow the user to override when running as a hook
|
||||
if [ -n "$WARNED" ]; then
|
||||
echo "Warnings detected"
|
||||
if [ "$CAN_ACCEPT" = true ]; then
|
||||
read -p "Are you sure you want to commit? [Y/n] " -n 1 -r
|
||||
echo # (optional) move to a new line
|
||||
if [[ ! $REPLY =~ ^[Yy]+$ ]]; then
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
exit 1
|
||||
fi
|
||||
fi
|
Loading…
Reference in New Issue
Block a user