I've found myself working with a group that has a tedious change-log format. I've decided I'd like to automate creating a change-log entry based on the commit message details. The process is you supply a formatted commit message, I then adjust a change log file on the fly, and try to add it to the current commit.
I've got a commit-msg hook:
#!/bin/bash
GIT_BRANCH=`git branch`
# Only run this hook on the master branch
if [ "$GIT_BRANCH" = '* master' ]
then
# set some variables we'll need
SAMPLE_COMMIT_MESSAGE="minor: #ws-666 Brings the devil back to life"
COMMIT_MESSAGE_FILE=$1
FULL_COMMIT_MESSAGE=$(cat $COMMIT_MESSAGE_FILE)
CURRENT_DATE=$(date +"%Y-%m-%d")
# let's do some pattern matching on the commit to set more variables
[[ $FULL_COMMIT_MESSAGE =~ (ws-[0-9]+)[:space:]*(.*) ]]
JIRA_ISSUE=${BASH_REMATCH[1]^^}
COMMIT_MESSAGE=${BASH_REMATCH[2]}
[[ $FULL_COMMIT_MESSAGE =~ ^(M) ]]
if [ -n "${BASH_REMATCH[1]}" ]
then
CHANGE_IMPACT="_Major_"
fi
[[ $FULL_COMMIT_MESSAGE =~ ^(m) ]]
if [ -n "${BASH_REMATCH[1]}" ]
then
CHANGE_IMPACT="_minor_"
fi
[[ $FULL_COMMIT_MESSAGE =~ ^([pP]) ]]
if [ -n "${BASH_REMATCH[1]}" ]
then
CHANGE_IMPACT="_patch_"
fi
#echo Full Commit Message "$FULL_COMMIT_MESSAGE"
#echo Jira issue $JIRA_ISSUE
#echo Change Type $CHANGE_IMPACT
#echo Commit Message "$COMMIT_MESSAGE"
if [ -z $JIRA_ISSUE ]
then
echo "You failed to specify a jira issue, your commit must look like:"
echo "$SAMPLE_COMMIT_MESSAGE"
exit 1
fi
if [ -z $CHANGE_IMPACT ]
then
echo "You failed to specify a change impact, your commit must look like:"
echo "$SAMPLE_COMMIT_MESSAGE"
exit 1
fi
if [ -z $COMMIT_MESSAGE ]
then
echo "You failed to specify an actual commit message, your commit must look like:"
echo "$SAMPLE_COMMIT_MESSAGE"
exit 1
fi
sed -i -e ":a;N;s/\(# Change log.*\)\n/\1\n\n\* next ($CURRENT_DATE)\n $JIRA_ISSUE\n \* $CHANGE_IMPACT: $COMMIT_MESSAGE/" change-log.md
`git add change-log.md`
echo Running $BASH_SOURCE
set | egrep GIT
echo PWD is $PWD
fi
# For testing, always exit
echo "Exiting, no commit made"
exit 1
I then run the following command:
git checkout -- ../change-log.md ; git commit ../fake -m"Major #ws-123 WeeeeeeEE" ; git status
In git status I see:
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: ../change-log.md
Is it not possible for git to add a file at this stage in the commit process? Other thoughts on how best to do this? I'm thinking I could check if the particular change log entry exists, if not, add it, then exit, asking for that file to be added.
-j