Bu örneğimizde GIT commit-msg hook'u kullanarak orjinal commit mesajını otomatik olarak değiştireceğiz. Eğer standartlara uyulmamış ise, kullanıcıdan mesajı değiştirip değiştirmeyeceği sorulacak. Eğer isterse otomatik olarak değiştirme işlemi yapılacak. Eğer istemezse hiçbir şey olmamış gibi devam edilecek. Kullanıcıyı zorlama diye birşey söz konusu değil.


Mantık


Proje yönetim araçları (örneğin JIRA, Pivotal, Assembla vs.) kullanıldığında, genelde üzerinde çalıştığımız işlerin numaraları oluyor. Bu numaraları ileride bize kolaylık sağlamaları amacıyla, branch isimlerinde ve commit mesajlarında kullanırsak iyi olabilir. Bu örnek kullanıcıyı bu konuda teşvik ediyor. Bir takım kurallar olduğu için kod içindeki yorumları okumanız gerekir çünkü kurallar tüm branch ve mesajlar için geçerli değildir.


commit-msg


Aşağıdaki kodu kullanarak commit-msg ismindeki dosyayı yaratın.


vagrant@other:~/git$ nano .git/hooks/commit-msg

#!/bin/bash

currentBranch=$(git rev-parse --abbrev-ref HEAD)

# Is branch "feature" or "hotfix"
if [[ $currentBranch == ticket/* ]] || [[ $currentBranch == hotfix/* ]] ;
then
error=false
ticketType=$(echo $currentBranch | cut -d'/' -f 1)
ticketNumber=$(echo $currentBranch | cut -d'/' -f 2 | cut -d'-' -f 1)
currentCommitMessage=`echo "$(cat $1)" | head -1`

# If no commit message given then abort
if [ -z "$currentCommitMessage" ] ;
then
printf "\n\033[0;31mAborting the commit due to empty commit message.\033[0m\n\n"
exit 1;
fi

# If the ticket/branch is "hotfix" and doesn't start with a number then no more checks
if [[ $ticketType == hotfix ]] && ! [[ $ticketNumber =~ ^[0-9]+$ ]] ;
then
exit 0;
fi

# If the ticket/branch doesn't start with a number then set error
if ! [[ $ticketNumber =~ ^[0-9]+$ ]] ;
then
printf "\n\033[0;31mAborting the commit due to invalid ticket number.\033[0m\n\n"
exit 1;
fi

# If the commit message doesn't start with a number then set error
if [[ $currentCommitMessage != "${ticketNumber} - "* ]] ;
then
error=true
fi

# If error is set
if [ $error == true ] ;
then
printf "\n\033[0;33m[GIT POLICY]\033[0m Commit messages for 'ticket' and 'hotfix' branches must be prefixed with ticket numbers if exists.\n\n"

# Allow cursor hanging
exec < /dev/tty

# Start infinite loop to wait for user's input
while true;
do
proposedCommitMessage="${ticketNumber} - ${currentCommitMessage}"

printf "CURRENT : ${currentCommitMessage}\n"
printf "PROPOSED : ${proposedCommitMessage}\n\n"
read -p "Would you like me to amend the 'current' message as 'proposed' one as shown above? (y/n) " amend

# If user accepts the offer above (message amendment or not)
if [[ $amend =~ ^[Yy]$ ]] ;
then
$(echo $proposedCommitMessage > $1)
printf "\n\033[0;32mThe message has been amended.\033[0m\n\n"
else
printf "\n\033[0;31mThe message has been left intact.\033[0m\n\n"
fi

break
done
fi
fi

Dosyayı çalışabilir kılmak için $ chmod +x .git/hooks/commit-msg komutunu kullanın.


Testler


feature/testing


Bu kullanıcıya herhangi bir mesaj sormayacak çünkü bu bir "feature" branchtır.


vagrant@other:~/git$ git commit -m 'Commiting'
[feature/testing 660d36d] Commiting
1 file changed, 1 insertion(+)

ticket/testing


Bu kullanıcıyı brancha iş numarasını eklemesi için zorlayacak çünkü bu bir "ticket" branchtır.


vagrant@other:~/git$ git commit -m 'Commiting'

Aborting the commit due to invalid ticket number.

ticket/123-testing


Bu kullanıcıya commit mesajına iş numarasını 123 ekleyip eklemeyeceğini soracak çünkü orjinal mesajda ekleme yapılmamış.


vagrant@other:~/git$ git commit -m 'Commiting'

[GIT POLICY] Commit messages for 'ticket' and 'hotfix' branches must be prefixed with ticket numbers if exists.

CURRENT : Commiting
PROPOSED : 123 - Commiting

Would you like me to amend the 'current' message as 'proposed' one as shown above? (y/n) y

The message has been amended.

[ticket/123-testing b4e4b27] 123 - Commiting
1 file changed, 1 insertion(+)

ticket/123-testing


Bu kullanıcıya commit mesajına iş numarasını 123 ekleyip eklemeyeceğini sormayacak çünkü orjinal mesajda daha önceden ekleme yapılmış.


vagrant@other:~/git$ git commit -m '123 - Commiting'
[ticket/123-testing 9803ed3] 123 - Commiting
1 file changed, 1 insertion(+)

hotfix/testing


Bu kullanıcıya commit mesajına iş numarası ekleyip eklemeyeceğini sormayacak çünkü bu bir "hotfix" branchtır ve iş numarası zaten mevcut değil.


vagrant@other:~/git$ git commit -m 'Commiting'
[hotfix/testing ebe8af1] Commiting
1 file changed, 1 insertion(+)

hotfix/123-testing


Bu kullanıcıya commit mesajına iş numarasını 123 ekleyip eklemeyeceğini soracak çünkü orjinal mesajda ekleme yapılmamış.


vagrant@other:~/git$ git commit -m 'Commiting'

[GIT POLICY] Commit messages for 'ticket' and 'hotfix' branches must be prefixed with ticket numbers if exists.

CURRENT : Commiting
PROPOSED : 123 - Commiting

Would you like me to amend the 'current' message as 'proposed' one as shown above? (y/n) y

The message has been amended.

[hotfix/123-testing 11d58a8] 123 - Commiting
1 file changed, 1 insertion(+)