Created
May 24, 2022 19:16
-
-
Save sangdth/1f7c68e4b56f788a80ec0331593133a0 to your computer and use it in GitHub Desktop.
Extract Jira code from branch name
This file contains 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 | |
# Copy this file into .git/hooks/prepare-commit-msg file | |
# Usage: Your branch must follow the name convention: `CODE-123-Lorem-ipsum-telnet` | |
# This hook will try to extract the `CODE-123` and add it into your commit message. | |
# For example, `git commit -m "fix something"` will create a commit message content: | |
# `CODE-123 fix something` | |
COMMIT_MSG_FILE=$1 | |
# Ignore if branch name is: master/main/develop/dev | |
if [ -z "$NAMES_TO_SKIP" ]; then | |
NAMES_TO_SKIP=(master main develop dev) | |
fi | |
# Get code in branch name with format DEV-123 | |
CODE_NAME=$(git symbolic-ref --short HEAD | ggrep -oP "(?<=feature\/|bugfix\/)([a-zA-Z]+)-(\d+)" | awk '{print $1}') | |
BRANCH_EXCLUDED=$(printf "%s\n" "${NAMES_TO_SKIP[@]}" | grep -c "^$CODE_NAME$") | |
# If commit message already have it, do not duplicate | |
BRANCH_IN_COMMIT=$(grep -c "$CODE_NAME" $1) | |
if [ -n "$CODE_NAME" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && ! [[ $BRANCH_IN_COMMIT -ge 1 ]]; then | |
# Add code name to commit message | |
sed -i.bak -e "1s/^/$CODE_NAME /" "$COMMIT_MSG_FILE" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment