|
| 1 | +#!/bin/sh |
| 2 | +# Git `commit-msg` hook that looks for patterns the branch name |
| 3 | +# and uses the matches as prefix or suffix of every commit title. |
| 4 | +# |
| 5 | +# This is useful if you have convention to use issue number in the branch, |
| 6 | +# and want to add it automatically to the commit title. |
| 7 | +# For example, you have branch like `FOO-9999-add-hook`, on adding the commit `Add Hook`, |
| 8 | +# the hook may add suffix `Add Hook (FOO-9999)` or prefix `[FOO-9999] Add Hook`. |
| 9 | +# |
| 10 | +# Note: you can use `commit --no-verify` to skip `commit-msg` hooks. |
| 11 | +# |
| 12 | +# Example of config: |
| 13 | +# ~~~ |
| 14 | +# [conventional-commit] |
| 15 | +# excluded-branch = master |
| 16 | +# excluded-branch = development |
| 17 | +# prefix-pattern = "FOO-[0-9]+" |
| 18 | +# suffix-pattern = "FOO-[0-9]+" |
| 19 | +# ~~~ |
| 20 | + |
| 21 | +FCR='\033[1;31m' # Red |
| 22 | +FCG='\033[1;32m' # Green |
| 23 | +FCY='\033[0;33m' # Yellow |
| 24 | +FCS='\033[0;37m' # Light gray (silver) |
| 25 | +NC='\033[0m' |
| 26 | + |
| 27 | +report_start() { |
| 28 | + echo "${FCG}$@${NC}" |
| 29 | +} |
| 30 | + |
| 31 | +report_error() { |
| 32 | + echo "${FCR}$@${NC}" |
| 33 | +} |
| 34 | + |
| 35 | +report_done() { |
| 36 | + [[ $# > 0 ]] && echo "${FCG}✓ $@${NC}" || echo "${FCG}✓ Done${NC}" |
| 37 | +} |
| 38 | + |
| 39 | +prefix_pattern=$(git config --get conventional-commit.prefix-pattern) |
| 40 | +suffix_pattern=$(git config --get conventional-commit.suffix-pattern) |
| 41 | + |
| 42 | +if [[ -z ${prefix_pattern} && -z ${suffix_pattern} ]]; then |
| 43 | + exit |
| 44 | +fi |
| 45 | + |
| 46 | +branch_name=$(git rev-parse --abbrev-ref HEAD) |
| 47 | + |
| 48 | +excluded_branches=$(git config --get-all conventional-commit.excluded-branch) |
| 49 | + |
| 50 | +if [[ -z ${excluded_branches} ]]; then |
| 51 | + excluded_branches="master" |
| 52 | +fi |
| 53 | + |
| 54 | +for excluded_branch in ${excluded_branches} ; do |
| 55 | + if [[ "${branch_name}" =~ "${excluded_branch}" ]]; then |
| 56 | + exit |
| 57 | + fi |
| 58 | +done |
| 59 | + |
| 60 | +commit_file=$1 |
| 61 | +commit_title=$(head -n 1 "${commit_file}") |
| 62 | + |
| 63 | +if [[ -z "${commit_title}" ]]; then |
| 64 | + exit |
| 65 | +fi |
| 66 | + |
| 67 | +prefix=$(echo "${branch_name}" | grep -Eo "${prefix_pattern}") |
| 68 | +suffix=$(echo "${branch_name}" | grep -Eo "${suffix_pattern}") |
| 69 | + |
| 70 | +if [[ -z "${prefix}" && -z "${suffix}" ]]; then |
| 71 | + exit |
| 72 | +fi |
| 73 | + |
| 74 | +prefix_exists=$(echo "${commit_title}" | grep -o "^\[${prefix}\] ") |
| 75 | +suffix_exists=$(echo "${commit_title}" | grep -o " (${suffix})$") |
| 76 | + |
| 77 | +commit_title=${commit_title#*]} # remove prefix ending in "." |
| 78 | +commit_title=${commit_title%(*} # remove suffix starting with "." |
| 79 | + |
| 80 | +if [[ -z "${prefix_exists}" ]]; then |
| 81 | + echo "Adding prefix '${prefix}' to the commit title..." |
| 82 | + sed -i -e 's/'"${commit_title}"'/'"[${prefix}] ${commit_title}"'/g' "${commit_file}" |
| 83 | +else |
| 84 | + echo "Prefix '${prefix}' already exists, skipping..." |
| 85 | +fi |
| 86 | + |
| 87 | +if [[ -z "${suffix_exists}" ]]; then |
| 88 | + echo "Adding suffix '${suffix}' to the commit title..." |
| 89 | + sed -i -e 's/'"${commit_title}"'/'"${commit_title} (${suffix})"'/g' "${commit_file}" |
| 90 | +else |
| 91 | + echo "Suffix '${suffix}' already exists, skipping..." |
| 92 | +fi |
| 93 | + |
| 94 | +echo "Title was changed to '$(head -n 1 "${commit_file}")'" |
| 95 | + |
| 96 | +report_done |
| 97 | + |
| 98 | +exit $? |
0 commit comments