bash git post-commit hook - 提交文件上的脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3616648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
git post-commit hook - script on committed files
提问by takeshin
Can I see somewhere an example post-commit hook to run a script on each committed file?
我可以在某处看到一个示例 post-commit 钩子来在每个提交的文件上运行脚本吗?
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.
最好是有参数来提交命令来运行这个钩子,例如。git commit -X... 执行这个提交后钩子。最终像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 ----------------------------------------------
采纳答案by Cascabel
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:
假设您在每个文件上执行的脚本无法设置为当它在一个参数上失败时立即失败,您就不能使用该 xargs 设置。你可以做这样的事情:
#!/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 phpmdwill 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 phpmdexits with error status, the hook script will, and if it's a pre-commit hook, this will cause git to abort the commit.
但也许phpmd已经为你做这件事了?无法从您的问题中完全理解,但如果确实如此,您所要做的就是确保它是钩子中的最后一个命令。管道的退出状态是其中最后一个命令的退出状态 ( phpmd),而 shell 脚本的退出状态是它运行的最后一个命令的退出状态 - 所以如果phpmd以错误状态退出,钩子脚本将,如果它是预提交钩子,这将导致 git 中止提交。
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:
至于 git-commit 控制此钩子调用的选项,您有点不走运,除非您认为--no-verify(抑制所有提交钩子)足够好。别名...最简洁的方法是在别名中设置一个环境变量,可能是:
# 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...

