Skip to content

Instantly share code, notes, and snippets.

@niklas-ourmachinery
Created July 24, 2018 15:15
Show Gist options
  • Save niklas-ourmachinery/072792a5bcb553a55ae5cd324c805f17 to your computer and use it in GitHub Desktop.
Save niklas-ourmachinery/072792a5bcb553a55ae5cd324c805f17 to your computer and use it in GitHub Desktop.
#!/bin/bash
# This file should be installed in .git/hooks/pre-commit
EXITCODE=0
format_file() {
clang-format -style=file ${1} | diff ${1} - > /dev/null
if [ $? -ne 0 ]
then
echo "clang-format mismatch in ${1}"
clang-format -i -style=file ${1}
EXITCODE=1
fi
}
case "${1}" in
--about )
echo "Runs clang-format on source files"
;;
* )
for file in `git diff-index --cached --diff-filter=ACMRTUXB --name-only HEAD | egrep "(\.c$|\.h$|\.cpp$|\.inl$|\.m$|\.mm$)"` ; do
format_file "${file}"
done
;;
esac
exit $EXITCODE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment