Refactor pre commit hook

Added whitespace protection and simplified some parts
I would like to have a short comment on this line, why it works as expected (for documentation).