3

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

Joel
  • 43
  • 1
  • 7
  • Take a look here for a sample code which you can use.http://stackoverflow.com/questions/35400416/what-are-some-more-forceful-ways-than-a-gitignore-to-keep-force-files-out-of/35400472#35400472 – CodeWizard Feb 16 '16 at 17:44
  • Could you explain the relevant piece there? The above hook does everything but seemingly the `git add change-log.md` line, which appears to have no affect. – Joel Feb 16 '16 at 17:53
  • Why do you have it inside quotes? `git add change-log.md` try it without it. you need to use them if you decide to return value. – CodeWizard Feb 16 '16 at 17:55
  • Quotes does not affect function. – Joel Feb 16 '16 at 18:03
  • And does the path to the file is the right one? – CodeWizard Feb 16 '16 at 18:06
  • Yes. You can easily test this script, I've provided both the code, and the input. You simply need to make a new git repository to place the hook in, and create a change-log.md with text that matches the regex. – Joel Feb 16 '16 at 18:07

2 Answers2

0

Adding a sleep 5m after editing the file before the call to git add, then dropping to a shell and typing git add change-log.md gave me the error that fatal: Unable to create '/home/jjshoe/test/.git/index.lock': File exists.

Which makes total sense.

Joel
  • 43
  • 1
  • 7
0

One (somewhat hacky) thing you could do is add a post-commit hook with the following single line:

 git commit --amend -C HEAD --no-verify change-log.md

This will add change-log.md to each commit if it has changed, and do nothing if it has not.

David Deutsch
  • 17,443
  • 4
  • 47
  • 54
  • The downside here is the new SHA as a result, this will cause CI to trigger again. – Joel Feb 17 '16 at 16:56
  • @Joel, doesn't your CI only run on push? Or do you have some kind of CI locally on your machine or something? – David Deutsch Feb 17 '16 at 17:11
  • CI polls for changes. – Joel Feb 18 '16 at 16:59
  • But does it poll your actual machine, or does it poll the remote? If the latter, it is no problem because you are never pushing the non `--amend` commit. If the former (which is odd), in most cases the amend would happen so quickly that I doubt CI could get two polls in. – David Deutsch Feb 18 '16 at 18:00