Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MartyLake/17ecdf00d6036b0f5773fa7bcd313e69 to your computer and use it in GitHub Desktop.
Save MartyLake/17ecdf00d6036b0f5773fa7bcd313e69 to your computer and use it in GitHub Desktop.
#!/bin/sh
#
# This pre-commit hook checks if clang-format-7
# is installed, and if so, uses the installed version to format
# the staged changes.
#
# Installation instructions from : https://github.com/arduino/ArduinoCore-arc32/wiki/Creating-a-pre-commit-hook-for-clang-format
# cd your-repository
# curl https://gist.githubusercontent.com/MartyLake/17ecdf00d6036b0f5773fa7bcd313e69/raw > .git/hooks/pre-commit
# chmod a+x .git/hooks/pre-commit
base=clang-format-7
format=""
# Redirect output to stderr.
exec 1>&2
# check if clang-format is installed
type "$base" >/dev/null 2>&1 && format="$base"
# no versions of clang-format are installed
if [ -z "$format" ]
then
echo "$base is not installed. Pre-commit hook will not be executed."
exit -1
fi
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
# do the formatting
for file in `git diff-index --cached --name-only $against`
do
"$format" -style=file -i "$file"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment