Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Serhat Teker
Serhat Teker

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:"
Enter fullscreen modeExit fullscreen mode

To append this every commit message use below code:

COMMIT_MSG_FILE=$1echo"$MESSAGE">>$COMMIT_MSG_FILE
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

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
Enter fullscreen modeExit fullscreen mode

All done!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Uomo Universale | Software Engineer | Entrepreneur | builds systems | py:go:js |
  • Location
    127.0.0.1
  • Work
    Software Engineer
  • Joined

More fromSerhat Teker

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp