Created
September 29, 2011 11:58
-
-
Save eroullit/1250603 to your computer and use it in GitHub Desktop.
git pre-commit indent hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# Hook used to indent all source files before commiting | |
# | |
INDENT="$(git config hooks.indent)" | |
if test ! -x "$INDENT" | |
then | |
echo "Unable to find indent executable on the configuration." | |
echo | |
echo "Please configure it with :" | |
echo " git config hooks.indent /path/to/indent" | |
echo | |
fi | |
# indent / format file by type | |
indent() { | |
# getting against as the current commit | |
if git rev-parse --verify HEAD >/dev/null 2>&1 | |
then | |
local against=HEAD | |
else | |
# Initial commit: diff against an empty tree object | |
local against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 | |
fi | |
# loop on modified files | |
git diff --cached --name-only $against |while read file; | |
do | |
local ext=$(expr "$file" : ".*\(\..*\)") | |
case $ext in | |
.c|.h) | |
__indent_C; | |
;; | |
esac | |
done | |
} | |
# Indent the file with `indent' if this is a C/H file | |
__indent_C() { | |
if test ! -x "$INDENT" | |
then | |
return; | |
fi | |
if test ! -f $file | |
then | |
return; | |
fi | |
echo "Indenting " $file | |
"$INDENT" -linux "$file" | |
git add "$file" | |
} | |
indent |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
+1, can you post an usage example?