Git commit automation
armcknight opened this issue · 2 comments
armcknight commented
- add a Git commit template
- add the JIRA ticket/name automation script from the Git Workflow document to
.git/hooks/prepare-commit-msg
:
#!/bin/bash
PREPENDED_MSG=$(git rev-parse --abbrev-ref HEAD | sed -E -e 's/([a-zA-Z])-([0-9])/\1###\2/g' -e 's/([^a-zA-Z])-/\1 /g' -e 's/-([^0-9])/ \1/g' -e 's/###/-/g' | awk -F/ '$0=$NF')
sed -i.back "1s/^\s*$/$PREPENDED_MSG/" "$1"
- Maybe?: Add a check to .git/hooks/pre-commit to check for a link to the jira ticket and reject if not present
... we would need a way to bootstrap these into a developer's local clone, via e.g. a make init
as mentioned in #129
armcknight commented
We can even dispense with the template if we just use the prepare-commit-msg
hook to write the entire thing. Experimenting locally, I wound up with
#!/bin/bash
# get branch name which should be formatted as <project id>-<ticket number>-<friendly name with words delimited by hyphens>, remove any fix|feat|etc/<username> path prefixes
BRANCH_NAME=$(basename $(git rev-parse --abbrev-ref HEAD))
# separated by hyphens, get the first two groups, which should be project id and ticket number, leave as hyphenated
TICKET_ID=$(echo $BRANCH_NAME | cut -d'-' -f 1,2)
# separated by hyphens, remove first two groups (project id, ticket number) and for the remainder, replace hyphens with spaces
TICKET_NAME=$(echo $BRANCH_NAME | cut -d'-' -f3- | sed s/\-/\ /g)
TICKET_URL="https://raizlabs.atlassian.net/browse/$TICKET_ID"
PREPENDED_MSG=$(cat <<COMMIT_MESSAGE_TEMPLATE
##################################
# So, you want to make a commit? #
##################################
# Please take the time to create a great commit for the benefit of
# future you and other maintainers! See the guidelines for more
# information at https://rpo365.sharepoint.com/:w:/r/sites/RP-Practices/Shared%20Documents/Product%20Engineering/Development%20Guides/Git%20Workflow.docx?d=wfb5201b4c39947b5b5f20a98a2760033&csf=1&e=479EbP.
###########################################################################################
# Commit Subject (50 chars): <feat|fix|test|refact|style|docs|chore>: <short description> #
###########################################################################################
subject
################################################################
# Commit Body (long-form description wrapped to 72 characters) #
################################################################
# <body here>
######################################################################################
# Automated ticket info/url injection (from .git/hooks/prepare-commit-msg, #
# maintained at https://github.com/raizlabs/ios-template/scripts/prepare-commit-msg) #
######################################################################################
$TICKET_NAME: $TICKET_URL
COMMIT_MESSAGE_TEMPLATE
)
echo "$PREPENDED_MSG" > $1
which prepopulated the commit message like so running on my branch:
##################################
# So, you want to make a commit? #
##################################
# Please take the time to create a great commit for the benefit of
# future you and other maintainers! See the guidelines at for more
# information at https://rpo365.sharepoint.com/:w:/r/sites/RP-Practices/Shared%20Documents/Product%20Engineering/Development%20Guides/Git%20Workflow.docx?d=wfb5201b4c39947b5b5f20a98a2760033&csf=1&e=479EbP.
###########################################################################################
# Commit Subject (50 chars): <feat|fix|test|refact|style|docs|chore>: <short description> #
###########################################################################################
subject
################################################################
# Commit Body (long-form description wrapped to 72 characters) #
################################################################
# <body here>
######################################################################################
# Automated ticket info/url injection (from .git/hooks/prepare-commit-msg, #
# maintained at https://github.com/raizlabs/ios-template/scripts/prepare-commit-msg) #
######################################################################################
roundtrip success: https://raizlabs.atlassian.net/browse/XCI-4179
armcknight commented
Need to do more work on the above to work with the following:
-
git commit --amend [--no-edit]
git rebase
:- rewording
- squashing