Skip to content

Instantly share code, notes, and snippets.

@miguelortizdev
Last active August 12, 2019 14:43
Show Gist options
  • Select an option

  • Save miguelortizdev/d0ef61355cedffdcf61846a19274734c to your computer and use it in GitHub Desktop.

Select an option

Save miguelortizdev/d0ef61355cedffdcf61846a19274734c to your computer and use it in GitHub Desktop.
Git Hooks
#!/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
@miguelortizdev
Copy link
Author

Installation

To install globally, copy prepare-commit-msg to /usr/local/share/git-core/templates/hooks and execute:

chmod +x /usr/local/share/git-core/templates/hooks/prepare-commit-msg

To install per-repository, copy prepare-commit-msg to /path/to/repo/.git/hooks/prepare-commit-msg and mark it as executable.

Usage

The branch name will be automatically appended to commit messages using git commit or git commit -m "message".

To include a branch description in your desired branch, execute:

git branch --edit-description

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 init within the repository for the hook to be initialized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment