Skip to content

Instantly share code, notes, and snippets.

@otanistudio
Created April 1, 2014 00:43
Show Gist options
  • Save otanistudio/9905547 to your computer and use it in GitHub Desktop.
Save otanistudio/9905547 to your computer and use it in GitHub Desktop.
git pre-commit hook for checking tabs, but only in certain files. Just as an example, this one only looks for Obj-C, Obj-C++, C++, JSON and header files
if
git diff --cached --name-only | egrep '(\.(mm|m|h|cpp|json)$)' | xargs git diff --cached | egrep '^\+.*'$'\t' > /dev/null
then
echo "Tabs found in your commit. Please use (four) spaces and re-commit."
exit 1
fi
@jmccabe
Copy link

jmccabe commented Sep 6, 2021

Have you noticed any issues with, e.g. Ubuntu 16.04 using this script?

What I've found is that, if the git diff --cached --name-only | egrep '(\.(mm|m|h|cpp|json)$)' bit returns no matches, the result of the rest of the line depends on all the files that have changed. So, let's say you have a load of .cpp files, and a .cproject file. If you've only changed the .cproject file and staged it, and it has tabs in it, it will be picked up in the xargs processing and your commit will be rejected.

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