Skip to content

Instantly share code, notes, and snippets.

@StanleyMasinde
Forked from am-kantox/smart-commit.sh
Created February 20, 2021 12:47
Show Gist options
  • Save StanleyMasinde/82db5bb64b4651a22de978f9046ae5b0 to your computer and use it in GitHub Desktop.
Save StanleyMasinde/82db5bb64b4651a22de978f9046ae5b0 to your computer and use it in GitHub Desktop.
Bash script that prefixes commits with a JIRA (CO-400)
#!/usr/bin/env bash
# based on https://github.com/sbimochan/smart-commit/blob/master/commit
# am@aleksei  feature/CO-400-some-branch-name$ ./smart-commit.sh "Foo bar."
# → [feature/CO-400-some-branch-name f70ebbf167] CO-400: Foo bar.
set -euo pipefail
if [ -z "${1:-}" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
echo "Create a commit prefixed with the current branch name."
echo
echo "Usage:"
echo " smart-commit.sh MESSAGE"
echo
echo "Example:"
echo " smart-commit.sh \"Hello world!\""
exit 1
fi
CURRENT_BRANCH="$(git rev-parse --abbrev-ref HEAD)"
GIT_ROOT_DIRECTORY=$(git rev-parse --show-toplevel)
IGNORED_BRANCHES=("develop" "master")
CUSTOM_IGNORED_PATH="$GIT_ROOT_DIRECTORY/.smart-commit-ignore"
if [ -f "$CUSTOM_IGNORED_PATH" ]; then
CUSTOM_BRANCHES=$(cat "$CUSTOM_IGNORED_PATH")
BRANCHES=($CUSTOM_BRANCHES)
IGNORED_BRANCHES=(${IGNORED_BRANCHES[@]} ${BRANCHES[@]})
fi
IS_IGNORED=false
for branch in "${IGNORED_BRANCHES[@]}"; do
if [ "$CURRENT_BRANCH" == $branch ]; then
IS_IGNORED=true
break
fi
done
CURRENT_BRANCH_JIRA=$(echo $CURRENT_BRANCH | sed "s/.*\/\([A-Z]\+\-[0-9]\+\).*/\1/p" -n)
if [ "$IS_IGNORED" == false ] && [ ${#CURRENT_BRANCH_JIRA} -gt 0 ]; then
git commit -S -m "$CURRENT_BRANCH_JIRA: $1"
else
git commit -S -m "$1"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment