Skip to content

Instantly share code, notes, and snippets.

@acoomans
Created May 21, 2019 05:39
Show Gist options
  • Save acoomans/5a6d6c31dcd47a93560388586e99f003 to your computer and use it in GitHub Desktop.
Save acoomans/5a6d6c31dcd47a93560388586e99f003 to your computer and use it in GitHub Desktop.
Git pre-commit hook
#!/bin/sh -x
# Run git config --global core.hooksPath /path/to/hooks
if git rev-parse --verify HEAD >/dev/null 2>&1
then
against=HEAD
else
against=$(git hash-object -t tree /dev/null)
fi
changed_files=$(git diff-index --cached $against | \
grep -E '[MA] .*\.(c|cpp)$' | cut -f 2)
if [ -n "$changed_files" ]; then
clang-format -i -style=LLVM $changed_files
exit $?
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment