Git的挂钩,修改提交文件 [英] Git hook, modify commit files
问题描述
我想写的Git pre-commit钩子脚本,它应该写在修改过的文件的开头提交的日期。我的问题是,我不能修改的文件添加到previous提交。当我试图调用混帐再犯运行递归。我怎么可以编写脚本,这在修改的文件的结尾?
追加修改时间我的code:
#!/斌/庆典files_modified =`git的差异指数--name只HEAD`在$ files_modified F;做
如果[[$ F == * .groovy作为]];然后
$线= $(头-1 F)
如果[[$线==/ * %%]];
然后
SED -i 1D
科幻
回声/ %%+ $(日期+M_%D_%%Y)+%% \\\\>> F
git的补充˚F
科幻
DONE
git的承诺--amend #recursive
出口
您不能修改在 pre 的commit钩子提交。结果
和你正在做的是类似于关键字扩展机制,这是不使用Git(或任何DVCS)最佳实践,如解释说,<一个href=\"http://stackoverflow.com/questions/1127177/to-put-the-$p$pfix-revision-number-to-$c$cs-by-git-svn/1127241#1127241\">To把preFIX &LT;修订数方式&gt;
至codeS通过的Git / SVN
其它方法包括:
- 生成与你它(的然后的提交)想要的信息,一个单独的文件。结果
例如见扩大混帐SHA1信息化为签而不进行归档?。 - 存储git的注意,信息(即从提交分离并不会改变SHA1)。结果
请参阅添加Git的说明,以一个blob 。
I'm trying to write git pre-commit hook script, it should write date of commit at the begining of modified files. My problem is that i can't add modified files to previous commit. When i trying invoke git commit again it runs recursive. How i can write script, which append time of modification at the end of modified files?
My code:
#!/bin/bash
files_modified=`git diff-index --name-only HEAD`
for f in $files_modified; do
if [[ $f == *.groovy ]]; then
$line = $(head -1 f)
if [[ $line == "/%%*" ]];
then
sed -i 1d
fi
echo "/%% " + $(date +"%m_%d_%Y") + " %%\\" >> f
git add f
fi
done
git commit --amend #recursive
exit
You cannot amend a commit in a pre commit hook.
And what you are doing is similar to the keyword expansion mechanism, which is not a best practice with Git (or any DVCS), as explained in "To put the prefix ?<revision-number>
to codes by Git/Svn".
Other approaches include:
- generating a separate file with the information you want in it (and then commit).
See for instance "Expanding Git SHA1 information into a checkin without archiving?". - storing that information in git note (that are separates from commits and don't change the SHA1).
See "Adding Git notes to a blob".
这篇关于Git的挂钩,修改提交文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!