Using a branch description in the commit message

In Chapter 3, Branching, Merging, and Options, we mentioned that you can set a description to your branch, and this information can be retrieved from a script using the git config --get branch.<branchname> description command. In this example, we will take this information and use it for the commit message.

We will be using the prepare-commit-msg hook. The prepare-commit-msg hook is executed every time you want to commit, and the hook can be set to anything you wish to check for before you actually see the commit message editor.

Getting ready

We need a clone and a branch to get started on this exercise, so we will clone jgit again to the chapter7.5 folder:

$ git clone https://git.eclipse.org/r/jgit/jgit chapter7.5
Cloning into 'chapter7.5'...
remote: Counting objects: 2170, done
remote: Finding sources: 100% (364/364)
remote: Total 45977 (delta 87), reused 45906 (delta 87)
Receiving objects: 100% (45977/45977), 10.60 MiB | 1.74 MiB/s, done.
Resolving deltas: 100% (24651/24651), done.
Checking connectivity... done.
Checking out files: 100% (1577/1577), done.

Checkout a local descriptioInCommit branch that tracks the origin/stable-3.2 branch:

$ cd chapter7.5
$ git checkout -b descriptioInCommit  --track origin/stable-3.2
Branch descriptioInCommit set up to track remote branch stable-3.2 from origin.
Switched to a new branch 'descriptioInCommit'

How to do it...

We will start by setting the description for our local branch. Then, we will create the hook that can extract this information and put it in the commit message.

We have our local descriptioInCommit branch for which we need to set a description. We will use the --edit-description Git branch to add a description to our local branch. This opens the description editor, and you can type in a message by performing the following steps:

  1. When you execute the command, the description editor will open and you can type in a message:
     $ git branch  --edit-description descriptioInCommit
    
  2. Now, type in the following message:
    Remote agent not connection to server
    
    When the remote agent is trying to connect
    it will fail as network services are not up
    and running when remote agent tries the first time
    
  3. You should write your branch description just as you write your commit messages. It makes more sense only then to reuse the description in the commit. Now, we will verify whether we have a message with the following description:
    $ git config --get branch.descriptioInCommit.description
    Remote agent not connection to server
    
    When the remote agent is trying to connect
    it will fail as network services are not up
    and running when remote agent tries the first time
    
  4. As expected, we have the desired output. Now, we can continue with creating the hook that will take the description and use it.

    Next, we will check if we have a description for the hook, and if we do, we will use that description as the commit message.

  5. First, we will ensure that we can get the information into the commit message at our desired position. There are many ways to do this, and I have settled on the following one: open the prepare-commit-msg hook file and type in the following script:
    #!/bin/bash
    BRANCH=$(git branch | grep '*'| sed 's/*//g'|  sed 's/ //g')
    DESCRIPTION=$(git config --get branch.${BRANCH}.description)
    echo $BRANCH
    #echo "$DESCRIPTION"
    if [ -z "$DESCRIPTION" ]; then
      echo "No desc for branch using default template"
    else
      # using tr to convert newlines to #
       # else sed will have a problem.
      DESCRIPTION=$(echo "$DESCRIPTION" | tr -s '
    ' '#')
       # replacing # with 
    
      DESCRIPTION=$(echo "$DESCRIPTION" | sed 's/#/\n/g')
      # replacing the first 
     with 
    
    
      DESCRIPTION=$(echo "$DESCRIPTION" | sed 's/\n/\n\n/')
    
      echo "$DESCRIPTION"
      using 
      sed -i "1 i$DESCRIPTION" $1
    fi
    
  6. Now, we can try to create a commit and see whether the message is being displayed as predicted. Use git commit --allow-empty to generate an empty commit but also to trigger the prepare-commit-msg hook:
    $ git commit --allow-empty
    
  7. You should get the message editor with our branch description as the commit message as follows:
    Remote agent not connection to server
    
    When the remote agent is trying to connect
    it will fail as network services are not up
    and running when remote agent tries the first time
    
    # Please enter the commit message for your changes. Lines starting
    # with '#' will be ignored, and an empty message aborts the commit.
    # On branch descriptioInCommit
    # Your branch is up-to-date with 'origin/stable-3.2'.
    #
    # Untracked files:
    #       hen the remote agent is trying to connect
    #
    
  8. This is as we expected. Save the commit message and close the editor. Try using the git log -1 command to verify whether we have the following message in our commit:
    $ git log -1
    commit 92447c6aac2f6d675f8aa4cb88e5abdfa46c90b0
    Author: Rasmus Voss <[email protected]>
    Date:   Sat Mar 15 00:19:35 2014 +0100
    
        Remote agent not connection to server
    
        When the remote agent is trying to connect
        it will fail as network services are not up
        and running when remote agent tries the first time
    
  9. You should get something similar to a commit message that is the same as our branch description. However, what about an empty branch description? How will our hook handle that? We can try again with a new branch named noDescriptionBranch, use git checkout to create it, and check it, as shown in the following command:
    $ git checkout -b noDescriptionBranch
    Switched to a new branch 'noDescriptionBranch'
    
  10. Now, we will make yet another empty commit to see whether the commit message will be as follows:
    $ git commit --allow-empty
    
  11. You should get the commit message editor with the default commit message text as follows:
    # Please enter the commit message for your changes. Lines starting
    # with '#' will be ignored, and an empty message aborts the commit.
    # On branch noDescriptionBranch
    # Untracked files:
    #       hen the remote agent is trying to connect
    #
    

This is all as we expected. This script can be combined with the next exercise that will take content from a defect system as well.

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

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