在 pre-commit hook 中将 Git 提交信息保存到文件中是否可行?

3

我正在寻找一种方法来创建一个提交消息的日志,每个提交消息都有时间戳,以此来跟踪我在这个星期做了什么?有人能提供一个这样的示例吗?


4
git log --since='1 week ago' --author=you@example.com - Jonathan Wakely
1个回答

1

你可以创建一个 commit.sh bash 脚本来简化这个过程。

#!/bin/bash

## your timestamp
NOW=$(date +%Y.%m.%d-%H:%M:%S)

## your source file
g=main.sh

## get the current version by extracting from the first 3 lines 
## ideally they are comments, containing the version
cv=`head -n 3 $g | grep "version" | sed 's/[^0-9.]*//g'`

## if you want a new version automatically incremented
nv=`echo $cv | awk -F. -v OFS=. 'NF==1{print ++$NF}; NF>1{if(length($NF+1)>length($NF))$(NF-1)++; $NF=sprintf("%0*d", length($NF), ($NF+1)%(10^length($NF))); print}'`

## automatically apply it in your sourcecode
echo "Updateing $g from $cv to $nv $NOW"
## re-print the first 3 lines of your code - this is bash here
cat $g > $g.bak
echo '#!/bin/bash' > $g
echo '# Last update:'$NOW >> $g
echo '# version '$nv >> $g
tail -n +4 $g.bak >> $g
rm $g.bak

## git action
git add . --all
git commit -m "$nv $1"
git push

## If you want a log of your commits
echo "$NOW - Updated $g from $cv to $nv - $1" >> $0.log

所以你可以:
./commit.sh "I worked a lot on this, now it's done."

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接