Fix the calls to pre- and post-receive custom hooks

@dblessing here's my proposed solution. It took long as I had to test many edge cases to make sure everytthing works fine. For clarity, I don't think it's worth it further refactoring the pre- and post-receive functions. Tell me if you need further feedback on my commit.