git的post-commit钩子 - 脚本上提交的文件 [英] git post-commit hook - script on committed files

查看:270
本文介绍了git的post-commit钩子 - 脚本上提交的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能看到的地方为例post-commit钩子来运行每个提交的文件的脚本?

如。

  git的补充文件1
git的补充文件2git的承诺-am我的承诺

和钩执行

 的MyScript -myparams文件1
MyScript可-myparams文件2

最好的是有参数commit命令来运行这个钩子与否,如。 git的承诺-X ...执行这个职位commit钩子。最终,像混帐commitx 的别名。

如何在同一版本上的索引文件pre-commit钩子?我可以放弃的时候执行脚本的文件之一无法提交?

编辑:

这是我现在有:

 #!/ bin / sh的
#git的/挂钩/后提交
DIR =`pwd`
回声----------------------------------------------
为呼应文件COMMITED PHP混乱检测结果:
回声----------------------------------------------
git的展示 - pretty =格式:--name只| grep的'\\ $的.php| xargs的-i phpmd $ DIR /'{}'文本codeSIZE,未使用$
回声----------------------------------------------


解决方案

假设你对每个文件执行该脚本不能设置失败时立即失败的一个参数,你不能使用xargs的设置。你可以做这样的事情:

 #!/斌/庆典#为pre-commit钩子,用--cached而不是HEAD HEAD ^
IFS = $'\\ n'
git的差异只是--name-HEAD ^ HEAD | grep的'\\ $的.php|
同时读取文件;做
    #退出立即如果脚本失败
    my_script$文件||退出$?
DONE

但也许 phpmd 就已经为你做这个?不能完全从你的问题理解的,但如果这样做,所有你需要做的是确保它在钩上一个命令。管道的退出状态是最后一个命令在它( phpmd )的退出状态,和shell脚本的退出状态是最后一个命令的退出状态它跑了 - 所以,如果 phpmd 错误状态退出,钩子脚本,如果这是一个pre-commit钩子,这将导致git的中止犯<。 / p>

作为一个选项,以混帐致力于控制此挂钩的调用,你种的运气,除非你认为 - 无验证(其中SUP presses所有提交钩子)足够好了。别名...这样做最彻底的方法是设置环境变量的别名,大概是:

 #gitconfig
【别名】
    commitx =!run_my_hook = 1; git的承诺#脚本
#!/斌/庆典
#如果变量的空,立即退出
如果[-z$ run_my_hook];然后
    退出0;
科幻
#脚本的其余部分在这里...

Can I see somewhere an example post-commit hook to run a script on each committed file?

eg.

git add file1
git add file2

git commit -am "my commit"

and the hook executes:

myscript -myparams "file1"
myscript -myparams "file2"

The best would be to have parameter to commit command to run this hook or not, eg. git commit -X… executes this post commit hook. Eventually an alias like git-commitx.

How about the same version as a pre-commit hook on files in index? Can I abort a commit when execution of the scripts fails on one of the files?

Edit:

Here is what I have now:

#!/bin/sh                                     
#.git/hooks/post-commit
dir=`pwd`
echo ----------------------------------------------
echo PHP Mess Detector results for commited files:
echo ----------------------------------------------
git show --pretty="format:" --name-only  | grep '\.php$' | xargs -i phpmd $dir/'{}' text codesize,unused$
echo ----------------------------------------------

解决方案

Assuming the script you execute on each file can't be set up to fail immediately when it fails on one argument, you can't use that xargs setup. You'll could do something like this:

#!/bin/bash

# for a pre-commit hook, use --cached instead of HEAD^ HEAD
IFS=$'\n'
git diff --name-only HEAD^ HEAD | grep '\.php$' |
while read file; do
    # exit immediately if the script fails
    my_script "$file" || exit $?
done

But perhaps phpmd will do this for you already? Couldn't quite understand from your question, but if it does, all you have to do is make sure it's the last command in the hook. The exit status of a pipeline is the exit status of the last command in it (phpmd), and the exit status of a shell script is the exit status of the last command it ran - so if phpmd exits with error status, the hook script will, and if it's a pre-commit hook, this will cause git to abort the commit.

As for an option to git-commit to control invocation of this hook, you're kind of out of luck, unless you consider --no-verify (which suppresses all commit hooks) good enough. An alias... the cleanest way to do that would be to set an environment variable in the alias, probably:

# gitconfig
[alias]
    commitx = "!run_my_hook=1; git commit"

# your script
#!/bin/bash
# if the variable's empty, exit immediately
if [ -z "$run_my_hook" ]; then
    exit 0;
fi
# rest of script here...

这篇关于git的post-commit钩子 - 脚本上提交的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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