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.
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'
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:
$ git branch --edit-description descriptioInCommit
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
$ 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
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.
#!/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
git commit --allow-empty
to generate an empty commit but also to trigger the prepare-commit-msg hook:$ git commit --allow-empty
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 #
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
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'
$ git commit --allow-empty
# 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.