Skip to content

Instantly share code, notes, and snippets.

@forivall
Created July 15, 2013 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save forivall/6003165 to your computer and use it in GitHub Desktop.
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)
# 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
@olaf-prism
Copy link

Awesome, thanks! For me (11 years later) the pre-commit hook is a subprocess of the git process so I had to add

parent_pid=$(ps -o ppid= $PPID | xargs)

with ps -o ppid= $PPID to get the pid of the parent process and the pipe through xargs to trim a leading whitespace. And then instead of reading from /proc/$PPID/cmdline we read from done < /proc/$parent_pid/cmdline.

@forivall
Copy link
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