Skip to content

Instantly share code, notes, and snippets.

@eroullit
Created September 29, 2011 11:58
Show Gist options
  • Save eroullit/1250603 to your computer and use it in GitHub Desktop.
Save eroullit/1250603 to your computer and use it in GitHub Desktop.
git pre-commit indent hook
#!/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
@ravensnowbird
Copy link

How can we use this gist ? :)

@nicodevs
Copy link

+1, can you post an usage example?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment