Last active
August 12, 2019 14:43
-
-
Save miguelortizdev/d0ef61355cedffdcf61846a19274734c to your computer and use it in GitHub Desktop.
Git Hooks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Creamos una variable en el bash para almacenar las ramas a la que no deseamos aplicar la regla si dicha variable no existe | |
| if [ -z "$BRANCHES_TO_SKIP" ]; then | |
| BRANCHES_TO_SKIP=(master develop test) | |
| fi | |
| # Obtenemos el correo configurado en el equipo | |
| USER_EMAIL=$(git config user.email) | |
| # Obtenemos el nombre de la rama actual | |
| BRANCH_NAME=$(git symbolic-ref --short HEAD) | |
| BRANCH_NAME="${BRANCH_NAME##*/}" | |
| # Verificamos que la rama actual no es una d elas excluidas | |
| BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$") | |
| BRANCH_IN_COMMIT=$(grep -c "\[$BRANCH_NAME\]" $1) | |
| # Si la rama actual no esta excluida entonces modificamos el mensaje para la confirmación actual | |
| if [ -n "$BRANCH_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && ! [[ $BRANCH_IN_COMMIT -ge 1 ]]; then | |
| sed -i.bak -e "1s/$/ :: [$BRANCH_NAME] - $USER_EMAIL/" $1 | |
| fi |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Installation
To install globally, copy
prepare-commit-msgto/usr/local/share/git-core/templates/hooksand execute:To install per-repository, copy
prepare-commit-msgto/path/to/repo/.git/hooks/prepare-commit-msgand mark it as executable.Usage
The branch name will be automatically appended to commit messages using
git commitorgit commit -m "message".To include a branch description in your desired branch, execute:
This will open your editor, type a description, save, and exit.
Notes
If you'd like to use this commit message hook in an existing repository, after copying the file to the proper location, run
git initwithin the repository for the hook to be initialized.