Skip to content

Instantly share code, notes, and snippets.

@lispyclouds
Last active November 27, 2022 16:11
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lispyclouds/b2d87b22ca29b80bcb8591a0eb5e4af6 to your computer and use it in GitHub Desktop.
Save lispyclouds/b2d87b22ca29b80bcb8591a0eb5e4af6 to your computer and use it in GitHub Desktop.
A commit message helper supporting story numbers and Co-authored-by
#!/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 <rahul080327@gmail.com>"
["fb"]="Foo Bar <foo.bar@baz.qux>"
)
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}"
@lispyclouds
Copy link
Author

Usage:

  • Have this in your PATH as a file called commit for example
  • Make it executable
  • Run commit from within a git repo directory

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