#!/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