Skip to content

Instantly share code, notes, and snippets.

@seunggabi
Last active November 1, 2023 08:03
Show Gist options
  • Save seunggabi/8fa40a3585d37519db5beab5f0563766 to your computer and use it in GitHub Desktop.
Save seunggabi/8fa40a3585d37519db5beab5f0563766 to your computer and use it in GitHub Desktop.
prepare-commit-msg
#!/bin/bash
if [ -z "${SKIP_BRANCH}" ]; then
SKIP_BRANCH=(master develop release hotfix)
fi
NAME=$(git symbolic-ref --short HEAD)
NAME="${NAME##*/}"
JIRA=`echo ${NAME} | egrep -o '.+-[0-9]+_' | egrep -o '.+-[0-9]+'`
ISSUE=`echo ${NAME} | egrep -o '#[0-9]+_' | egrep -o '#[0-9]+'`
EXCLUDED=$(printf "%s\n" "${SKIP_BRANCH[@]}" | grep -c "^${NAME}$")
PREFIX=""
if [[ -n ${JIRA} ]]; then
PREFIX="[${JIRA}] "
fi
if [[ -n ${ISSUE} ]]; then
PREFIX="${PREFIX}(${ISSUE}) "
fi
PREFIX_ESCAPE=`echo ${PREFIX} | sed -e 's/\[/\\\\[/g' | sed -e 's/\]/\\\\]/g'`
DONE=$(grep -c "${PREFIX_ESCAPE}" $1)
if ! [[ ${EXCLUDED} -eq 1 ]] && ! [[ ${DONE} -ge 1 ]]; then
sed -i.bak -e "1s/^/${PREFIX}/" $1
fi
@seunggabi
Copy link
Author

seunggabi commented May 27, 2022

start

  • repository
cp git/hooks/prepare-commit-msg .git/hooks/prepare-commit-msg
chmod a+x .git/hooks/prepare-commit-msg
  • global
mkdir -p ~/.git-templates/hooks
git config --global init.templatedir '~/.git-templates'
curl https://gist.githubusercontent.com/seunggabi/8fa40a3585d37519db5beab5f0563766/raw/aca1ace939f4ff853d39f21db1685d5a226d1490/prepare-commit-msg >> ~/.git-templates/hooks/prepare-commit-msg
chmod a+x ~/.git-templates/hooks/prepare-commit-msg

# each repository
rm .git/hooks/prepare-commit-msg
git init

example

branch: feature/DT-1252_#3_prepare-commit-msg
commit: [DT-1252] (#3) feat: add prepare-commit-msg

branch: feature/DT-1252_prepare-commit-msg
commit: [DT-1252] feat: add prepare-commit-msg

branch: feature/#3_prepare-commit-msg
commit: (#3) feat: add prepare-commit-msg

@gonyykim
Copy link

New clean up codes.
#!/bin/bash

if [ -z "${SKIP_BRANCH}" ]; then
SKIP_BRANCH=(master develop release hotfix)
fi

NAME=$(git symbolic-ref --short HEAD)
NAME="${NAME##*/}"

JIRA=$(echo "${NAME}" | grep -E -o '.+-[0-9]+' | grep -E -o '.+-[0-9]+')
ISSUE=$(echo "${NAME}" | grep -E -o '#[0-9]+
' | grep -E -o '#[0-9]+')

EXCLUDED=$(printf "%s\n" "${SKIP_BRANCH[@]}" | grep -c "^${NAME}$")

PREFIX=""
if [[ -n ${JIRA} ]]; then
PREFIX="[${JIRA}] "
fi
if [[ -n ${ISSUE} ]]; then
PREFIX="${PREFIX}(${ISSUE}) "
fi

PREFIX_ESCAPE=$(echo "${PREFIX}" | sed -e 's/[/\\[/g' | sed -e 's/]/\\]/g')
DONE=$(grep -c "${PREFIX_ESCAPE}" "$1")

if ! [[ ${EXCLUDED} -eq 1 ]] && ! [[ ${DONE} -ge 1 ]]; then
sed -i.bak -e "1s/^/${PREFIX}/" "$1"
fi

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