Last active
November 27, 2022 16:11
-
-
Save lispyclouds/b2d87b22ca29b80bcb8591a0eb5e4af6 to your computer and use it in GitHub Desktop.
A commit message helper supporting story numbers and Co-authored-by
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
#!/usr/bin/env bash | |
set -eo pipefail | |
changed=$(git status --porcelain) | |
is_github_repo=$(git config --get remote.origin.url; echo $?) | |
declare -A github_users | |
github_users=( | |
["rd"]="Rahul De <[email protected]>" | |
["fb"]="Foo Bar <[email protected]>" | |
) | |
function validate-author() { | |
if [ "${1}" == "" ]; then | |
echo -e "Author $1 not recognized: ${!github_users[@]}" >&2 | |
exit 1 | |
fi | |
} | |
function co-authors() { | |
local name=$(git config --get user.name) | |
local email=$(git config --get user.email) | |
local co_author | |
while IFS='/' read -ra accounts; do for acc in ${accounts[@]}; do | |
co_author="${github_users[${acc}]}" | |
validate-author "${co_author}" | |
if [ "${co_author,,}" != "${name,,} <${email,,}>" ]; then | |
echo "Co-authored-by: ${co_author}" | |
fi | |
done; done <<< "$@" | |
} | |
if [ "$changed" == "" ]; then | |
echo "Nothing to commit" | |
exit 1 | |
fi | |
echo -e "\e[1m Git Status: \e[0m" | |
git status -s | |
echo "" | |
touch /tmp/commit_pair | |
touch /tmp/commit_story | |
story=$(< /tmp/commit_story) | |
read -p "story: " -i "${story}" -e story | |
echo "${story}" > /tmp/commit_story | |
pair=$(< /tmp/commit_pair) | |
read -p "co-authors (shortcuts separated by /): " -i "${pair}" -e pair | |
co_authors="$(co-authors "$pair")" | |
echo "${pair}" > /tmp/commit_pair | |
read -p "message: " -e message | |
git commit --cleanup=verbatim -m "[${story}] ${message}"$'\n'$'\n'$'\n'"${co_authors}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
PATH
as a file calledcommit
for examplecommit
from within a git repo directory