Created
July 15, 2013 20:31
-
-
Save forivall/6003165 to your computer and use it in GitHub Desktop.
Don't allow git --all when files are currently staged. ~ An interesting lesson in git's use of environment vars (GIT_INDEX_FILE)
This file contains hidden or 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
| # don't allow --all when we have staged files. It's usually a mistake | |
| # depends on procfs and won't work through gitaliases | |
| while read -d $'\0' arg ; do | |
| if [[ "$arg" == -a* || "$arg" == -[b-zB-Z]*a* || "$arg" == '--all' ]] ; then | |
| if (( $((unset GIT_INDEX_FILE; git diff --cached --name-only)|wc -l) > 0 )) ; then | |
| echo 'using --all while files are cached. don'\''t do that.' | |
| exit 1 | |
| fi | |
| fi | |
| done < /proc/$PPID/cmdline |
Author
Awesome, glad to hear when my random bits get extra life. I've completely forgotten about the --all option for git commit, and i just always do git add -u as a separate stage these days. Also, i've been working on macos most of the time (though looking to switch back to linux), so no procfs for me!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome, thanks! For me (11 years later) the pre-commit hook is a subprocess of the git process so I had to add
with
ps -o ppid= $PPIDto get the pid of the parent process and the pipe throughxargsto trim a leading whitespace. And then instead of reading from/proc/$PPID/cmdlinewe read fromdone < /proc/$parent_pid/cmdline.