# Hooks

## git hooks


### pre-commit-msg
This example adds the branch name as prefix to the commit message:

* create the file `~/.githooks/prepare-commit-msg`
  ```sh
  #!/bin/sh
  #
  # Automatically adds branch name every commit message.
  #
  NAME=$(git branch | grep '*' | sed 's/* //' | cut -d '_' -f 1)
  
  echo "[$NAME]"': '$(cat "$1") > "$1"
  ```
* make the file executable `sudo chmod +x ~/.githooks/prepare-commit-msg`
* execute `git config --global core.hooksPath ~/.githooks` to use this hook at every repository

### pre-push example

* create the file `~/.githooks/pre-push`
  ```sh
  while true; do
      read -r -p "Have you executed 'composer test' already? (y|n): " yn < /dev/tty
      case $yn in
          [Yy]*) exit 0 ;;
          [Nn]*)
          echo "\e[31mPlease execute the command first \e[39m";
          exit 1;
          ;;
          *) echo "Please answer [y]es or [n]o." ;;
      esac
  done
  ```

* execute `git config --global core.hooksPath ~/.githooks` to use this hook at every repository