Git的挂钩,修改提交文件 [英] Git hook, modify commit files

查看:189
本文介绍了Git的挂钩,修改提交文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写的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

其它方法包括:

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:

这篇关于Git的挂钩,修改提交文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆