Using external information in the commit message

The commit hook is executed when you close the commit message editor. It can, among other things, be used to manipulate the commit message or review the commit message by machine to check whether it has a specific format.

In this recipe, we will be manipulating and checking the content of a commit message.

Getting ready

To start this exercise, we just need to create a branch and check it out. We need to disable the current prepare-commit-msg hook; we can do this by simply renaming it. Now, we can start working on the commit-msg hook by using the following command:

git checkout -b commit-msg-example
Switched to a new branch 'commit-msg-example'
$ mv .git/hooks/prepare-commit-msg .git/hooks/prepare-commit-msg.example

How to do it...

What we want to do in the first example is to check whether the defect information is correct. There is no need to release a commit that refers to a defect that does not exist:

  1. We will start by testing the commit-msg hook. First, make a copy of the current hook, then we will force the hook to exit with a non-zero value that will abort the creation of the commit:
    $ cp .git/hooks/commit-msg.sample .git/hooks/commit-msg
    
  2. Now, open the file in your preferred editor and add the following lines to the file:
    #!/bin/bash
    echo "you are not allowed to commit"
    exit 1
    
  3. Now, we will try to make a commit and see what happens as follows:
    $ echo "Frogs, scallops, and coco shell" >> fishtank.txt
    $ git add fishtank.txt
    $ git commit
    
  4. The editor will open and you can write a small commit message. Then, close the editor. You should see the you are not allowed to commit message, and if you check with git log -1, you will see that you don't have a commit with the message you just used, as follows:
    you are not allowed to commit
    $ git log -1
    commit 70cad5f7a2c3f6a8a4781da9c7bb21b87886b462
    Author: Rasmus Voss <[email protected]>
    Date:   Thu Mar 6 08:25:21 2014 +0100
    
        somebody keeps erasing my changes.
        You have a dirty workarea are you sure you wish to commit ?
    
  5. As you can see, the commit message hook is executed after you close the message editor, whereas the prepare-commit-msg hook is executed before the message editor. To validate, if we have a proper reference to the hook in our commit message, we will be checking whether a specific error is available for the Jenkins-CI project. Replace the lines in the commit-msg hook so that it looks like the following command:
    #!/bin/bash
    JIRA_ID=$(cat $1 | grep jenkins | sed 's/jenkins //g')
    ISSUE_INFO=$(curl https://issues.jenkins-ci.org/browse/JENKINS-${JIRA_ID} | grep "The issue you are trying to view does not exist.")
    if [ "${ISSUE_INFO}" ]; then 
      echo "Jenkins issue ${JIRA_ID} does not exist"
      echo "Please try again"
      exit 1
    else
      TITLE=$(curl https://issues.jenkins-ci.org/browse/JENKINS-$JIRA_ID} | grep -E "<title>.*</title>")
      echo "Jenkins issue ${JIRA_ID}"
      echo "${TITLE}"
      exit 0
    fi
    
  6. We are using Curl to retrieve the web page, and we are using grep to find a line with The issue you are trying to view does not exist; if we find this text, we know that the ID does not exist. Now we should create a commit and see what happens if we put in the wrong ID, jenkins 384895, or an ID that exists as jenkins 3157. To check this, we will create a commit as follows:
    $ echo "more water" >> fishtank.txt
    $ git add fishtank.txt
    $ git commit
    
  7. In the commit message, write something such as Feature cascading… as a commit message subject. Then, in the body of the commit message, insert jenkins 384895. This is the important part as the hook will use that number to look it up on the Jenkins issue tracker:
    Feature: Cascading...
    
    jenkins 384895
    
  8. You should end up with the following output:
    Jenkins issue 384895 does not exist
    Please try again
    
  9. This is what we expected. Now, verify with git status whether the change has not been committed:
    $ git status
    On branch commit-msg-example
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
            modified:   fishtank.txt
    
  10. Now we will try to commit again; this time, we will be using the correct JIRA ID:
    $ git commit
    
  11. Key in a commit message like the previous one; this time make sure the Jenkins issue ID is one that exists. You can use 3157:
    Feature: Cascading...
    
    jenkins 3157
    
  12. Saving the commit message should result in an output as follows. We can clean it some more by removing the title HTML tags:
    <title>[#JENKINS-3157] Feature request: cascading project settings - Jenkins JIRA</title>
    [commit-msg-example 3d39ca3] Feature: Cascading...
    1 file changed, 2 insertions(+)
    
  13. As you can see, we can get information to output. We could also add this information to the commit message itself. Then, we can change and insert this as the else clause in the script:
    TITLE=$(curl https://issues.jenkins-ci.org/browse/JENKINS-${JIRA_ID} | grep -E "<title>.*</title>")
    TITLE=$(echo ${TITLE} | sed 's/^<title>//' |sed 's/</title>$//')
    echo "${TITLE}" >> $1
    echo "Jenkins issue ${JIRA_ID}"
    echo "${TITLE}"
      exit 0
    
  14. To test, we will create a commit again, and in the message, we need to specify the JIRA ID that exists:
    $ echo "Shrimps and mosquitos" >> fishtank.txt
    $ git add fishtank.txt
    $ git commit
    After saving the commit message editor you will get an output similar like this. 
    Jenkins issue 3157
    [#JENKINS-3157] Feat
    [commit-msg-example 6fa2cb4] More cascading
    1 file changed, 1 insertion(+)
    
  15. To verify whether we got the information in the message, we will use git log -1 again:
    $ git log -1
    commit 6fa2cb47989e12b05cd2689aa92244cb244426fc
    Author: Rasmus Voss <[email protected]>
    Date:   Thu Mar 6 09:46:18 2014 +0100
    
        More cascading
    
        jenkins 3157
        [#JENKINS-3157] Feature request: cascading project settings - Jenkins JIRA
    

As expected, we have the information at the end of the commit. In these examples, we are just discarding the commit message if the JIRA ID does not exist; this is a little harsh on the developer. So, you can combine this with the prepare-commit-msg hook. So, if commit-msg halts the commit process, then save the message temporarily so that the prepare-commit-msg hook can use that message when the developer tries again.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset