Skip to content

Instantly share code, notes, and snippets.

@Lauszus
Last active April 20, 2021 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lauszus/31a3a43e35fabbf69ee58f9f425e69a2 to your computer and use it in GitHub Desktop.
Save Lauszus/31a3a43e35fabbf69ee58f9f425e69a2 to your computer and use it in GitHub Desktop.
clang-format pre-commit hook
#!/bin/sh
# Check if this is the 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
# Check if it parses the clang-format style
git diff --staged --submodule=diff -U0 --no-color $against | clang-format-diff -p1 > .clang-format.diff
if [ -s .clang-format.diff ]; then
echo "clang-format style violation:\n"
cat .clang-format.diff
exit_code=1
else
exit_code=0
fi
rm .clang-format.diff
exit $exit_code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment