Created
January 23, 2020 13:11
-
-
Save estliberitas/b7c9d103888df56dfac13b27edb0c946 to your computer and use it in GitHub Desktop.
A "commit-msg" hook to prepend a ticket ID to the first line of the commit message, given that the current branch name has a certain format.
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 | |
# Functions | |
get_current_branch() { | |
local branch_name=$(git symbolic-ref -q HEAD) | |
branch_name=${branch_name##refs/heads/} | |
branch_name=${branch_name:-HEAD} | |
echo "${branch_name}" | |
} | |
get_ticket_id() { | |
local branch_name="${1}" | |
echo "${branch_name}" | perl -ne 'if (/^\w+\/([A-Z]+-\d+)/) { print "$1"; }' | |
} | |
prepend_ticket_id() { | |
local commit_file="${1}" | |
local ticket_id="${2}" | |
local temp_file="$(mktemp)" | |
local title="$(head -1 "${commit_file}" | sed -E 's/^[A-Z]+-[0-9]+ //')" | |
local body="$(tail -n +2 "${commit_file}")" | |
echo "${ticket_id} ${title}" > "${temp_file}" | |
echo "${body}" >> "${temp_file}" | |
mv "${temp_file}" "${commit_file}" | |
} | |
# Main | |
commit_file="${1}" | |
current_branch="$(get_current_branch)" | |
ticket_id="$(get_ticket_id "${current_branch}")" | |
if [ -n "${ticket_id}" ]; then | |
prepend_ticket_id "${commit_file}" "${ticket_id}" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment