HOME ABOUT CONTACT ARTICLES

Editting a commit message


Docker is a set of coupled software-as-a-service and platform-as-a-service products that use operating-system-level virtualization to develop and deliver software in packages called containers.

Before beginning, you will need to install Docker. You can find installation instructions at https://docs.docker.com/install/

Steps

1) Create post-commit file

Create a file named post-commit in the directory ${repo_folder}/.git/hooks/ like such:

touch ${repo_folder}/.git/hooks/post-commit


2) Grant execution on the file

To allow Git to be able to execute the script post commit, it will need execution permissions:

chmod +x ${repo_folder}/.git/hooks/post-commit


3) Write the bash script

Git executes the file on a completion of a commit. We can script this using bash, as an example, the following bash file prints the committing user and the branch name to the terminal:

#!/bin/sh

main() {
echo "[Debug] Thanks for the commit, here are some details:"
echo "[Debug] Committing with name $(git config --global user.name)"
echo "[Debug] Committing with email $(git config --global user.email)"
}

main "$@"


4) Make a test commit

You can verify the image built by running the following command:

luke@nebulae:~$ git commit -m "This is a test"
luke@nebulae:~$ git push
 [Debug] Thanks for the commit, here are some details:
 [Debug] Committing with name Luke Pritchard
 [Debug] Committing with email me@lpritchard.com"
 Enumerating objects: 4, done.
 Counting objects: 100% (4/4), done.
 Delta compression using up to 8 threads
 Compressing objects: 100% (2/2), done.
 Writing objects: 100% (3/3), 265 bytes | 265.00 KiB/s, done.
 Total 3 (delta 1), reused 1 (delta 0)
 ...


5.1) Add some colour (optional)

To make things neater we can add some colour. Heres a list of unix colour codes:
https://github.com/ryanoasis/public-bash-scripts/blob/master/unix-color-codes.sh
The below example sets below printed statement to be orange:

echo "\033[1;30m[Debug] Thanks for the commit, here are some details:\033[0m"
Adding in the unix colour code for each echo our output will now be:
luke@nebulae:~$ git commit -m "This is a test"
luke@nebulae:~$ git push
[Debug] Thanks for the commit, here are some details:
[Debug] Committing with name Luke Pritchard
[Debug] Committing with email me@lpritchard.com"
 ...


5.2) Add an exit status (optional)

To conditionally prevent the commit from being made, you can exit the script with a status other than 0:

commit_name=$(git config --global user.name)
if [[ "${commit_name}" != "Luke Pritchard" ]]; then
 echo "Exitting since commiter is not Luke"
 exit 1
fi