We will start by setting the description of 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:
- When you execute the command, the description editor will open and you can type in a message:
$ git branch --edit-description descriptioInCommit
- 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
- You should write your branch description just as you write your commit messages. It make sense 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
- As expected, we have the desired output. Now, we can continue creating the hook that will take the description and use it.
Next, we will check whether we have a description for the hook and, if we do, we will use that description as the commit message.
- 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 we have settled on the following method: open the .git/hook/prepare-commit-msg hook file, type in the following script, and make it executable (chmod +x):
#!/bin/bash BRANCH=$(git branch | grep '*'| sed 's/*//g'| sed 's/ //g') DESCRIPTION=$(git config --get branch.${BRANCH}.description) if [ -z "$DESCRIPTION" ]; then echo "No desc for branch using default template" else # replacing # with n DESCRIPTION=$(echo "$DESCRIPTION" | sed 's/#/ /g') # replacing the first with DESCRIPTION=$(echo "$DESCRIPTION" | sed 's/ / /') # append default commit message
DESCRIPTION=$(echo "$DESCRIPTION" && cat $1)
# and write it all to the commit message echo "$DESCRIPTION" > $1 fi
- 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
- 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 #
- 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: John Doe <[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
- 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'
- Now, we will make yet another empty commit to see whether the commit message is as follows:
$ git commit --allow-empty
- 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
This is all as we expected. This script can be combined with the next exercise, which will take content from a defective system as well.