Add a fun Mario Bros sound on git commit



I wanted to make my own day a little more fun so I added a Mario Bro's vine sound on all of my repos when I make commits. I'm sure there are many ways to go about it but here is how I did it on a Mac.  

*Note this way works only when you do not have a local post-commit file in your repo if you need both global and local it looks like the steps are a little trickier see this stack overflow for more details.  You could always use a different git hook as well. 


1. Enable git templates:

git config --global init.templatedir '~/.git-templates'

2. Create a directory to hold the global hooks:

mkdir -p ~/.git-templates/hooks


4. Add the hook file.  (post-commit or whatever git hook file you want)
touch ~/.git-templates/hooks/post-commit
*Important do not add the .sh file type or it will run

5. Make sure the hook is executable.

chmod a+x ~/.git-templates/hooks/post-commit


6. Add your sound file to the global hooks folder
(~.git-template/hooks)
I downloaded the sound from here:  https://themushroomkingdom.net/media/smb/wav.  
I used the smb_vine.wav and renamed it to `mario_vine.wav`

mv mario_vine.wav ~.git-templates/hooks


7.  Add the script to the post-commit file. 

#!/bin/sh

# Get the directory of the current script
script_dir=$(dirname "${BASH_SOURCE[0]}")

echo "Mario time!"

afplay -v 0.3 $script_dir/mario_vine.wav >/dev/null 2>&1 &


`-v 0.3` tells the system to play this at 30% of the max volume (adjust this as needed)

Omit this if you would like the default volume.


By using `>/dev/null 2>&1 &` the command's output and errors are effectively silenced and the command is executed in the background, allowing the script or process to proceed without being blocked.


8. Reinitialize the repo. (if it is an existing repo) From within the repo you are testing with.
git init

NOTE if you already have a hook defined in your local git repo, this will not overwrite it so your commit will not work. 


9. Make a commit and enjoy

Troubleshooting:


If you make changes after you have initialized the repo it will not override the local file it generates and you will not see any changes. 


In the project repo you are in, from the root, cd into the .git/hooks folder


cd .git/hooks



Important only do the next step if you are sure you didn't originally have the post-commit script
Delete the post-commit and reinitialize `git init`  the script to see changes
rm post-commit

git init

@thanks to https://coderwall.com/p/jp7d5q/create-a-global-git-commit-hook for the steps on adding the git commit hook.