Posted on • Originally published attech.serhatteker.com on
Auto Add Date to Git Commit Messages
In previous post —Auto Add Git Commit Message, we saw how to add auto text into commit messages.
Now I want to use a concrete commit message template for allGitcommits
. It will be like:$yearnum/$weeknum/$daynum $unix_timestamp | version:
.
Yes, addingdate to a commit would be unnecessary sinceGit already logging date and you can see them withgit log
.
However I am going to use this template for myJournal, therefore for lookups this template makes things easy and quick.
Formessage I am going to use linux's defaultdate
—/bin/date
. You can find below$MESSAGE
that I will add tocommits
:
# NumsYNUM=$(date +%y)WNUM=$(date +%U-d"$DATE + 1 week")DAYNUM=$(date +%w)# Unix timestampUNIX_SEC=$(date +%s)MESSAGE="Add y$YNUM:w$WNUM:d0$DAYNUM$UNIX_SEC | ver:"
To append this every commit message use below code:
COMMIT_MSG_FILE=$1echo"$MESSAGE">>$COMMIT_MSG_FILE
So when yougit commit
it appends ourmessage and it will be like: "Add y20:w21:d02 1588701600 | ver:"
In this approach there is one thing that I want to improve: Instead ofappend I wantprependmessage so it will be onfirst line.
In order to prependmessage to commit we will usesed
andregex
:
sed-i"1s/^/$MESSAGE/"$COMMIT_MSG_FILE
In the end wholesolution can be found below:
.git/hooks/prepare-commit-message
#!/bin/sh## A git hook script to prepare the commit log message.## It adds below date message to commits:# $yearnum/$weeknum/$daynum $unix_timestamp | version:## To enable this hook, rename this file to "prepare-commit-msg".# Add it below '.git/hooks/' and make it executable:# chmod +x prepare-commit-msgCOMMIT_MSG_FILE=$1# NumsYNUM=$(date +%y)WNUM=$(date +%U-d"$DATE + 1 week")DAYNUM=$(date +%w)# Unix time in milisecondUNIX_SEC=$(date +%s)MESSAGE="Add y$YNUM:w$WNUM:d0$DAYNUM$UNIX_SEC | ver:"sed-i"1s/^/$MESSAGE/"$COMMIT_MSG_FILE
All done!
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse