Skip to content

Instantly share code, notes, and snippets.

@karadza3a
Created November 28, 2016 17:10
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 karadza3a/b2347a20da2041fadf11d56b0b29cf9e to your computer and use it in GitHub Desktop.
Save karadza3a/b2347a20da2041fadf11d56b0b29cf9e to your computer and use it in GitHub Desktop.
Add this to .git/hooks/pre-commit and make sure it's executable.
#!/bin/bash
# brew install clang-format
STYLE=$(git config --get hooks.clangformat.style)
if [ -n "${STYLE}" ] ; then
STYLEARG="-style=${STYLE}"
else
STYLEARG="-style=file"
fi
format_file() {
file="${1}"
if [[ $file =~ .*\.[mh]$ ]];then
clang-format -i ${STYLEARG} $file
git add ${1}
fi
}
case "${1}" in
--about )
echo "Runs clang-format on source files"
;;
* )
for file in `git diff-index --cached --name-only HEAD` ; do
format_file "${file}"
done
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment