git post-commit hook - 已提交文件上的脚本
我可以在某个地方看到一个示例提交后挂钩来在每个提交的文件上运行脚本吗?
例如。
git add file1
git add file2
git commit -am "my commit"
并且钩子执行:
myscript -myparams "file1"
myscript -myparams "file2"
最好是有参数来提交命令来运行这个钩子或不运行,例如。 git commit -X
...执行此提交后挂钩。最终是像 git-commitx 这样的别名。
与索引中文件的预提交挂钩相同的版本怎么样?当脚本在其中一个文件上执行失败时,我可以中止提交吗?
编辑:
这是我现在所拥有的:
#!/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 ----------------------------------------------
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 ----------------------------------------------
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您在每个文件上执行的脚本无法设置为在某个参数失败时立即失败,则您无法使用该 xargs 设置。你可以这样做:
但也许
phpmd
已经为你做这件事了?无法完全理解您的问题,但如果确实如此,您所要做的就是确保它是挂钩中的最后一个命令。管道的退出状态是其中最后一个命令的退出状态 (phpmd
),shell 脚本的退出状态是它运行的最后一个命令的退出状态 - 所以如果phpmd
以错误状态退出,钩子脚本将会,如果它是预提交钩子,这将导致 git 中止提交。至于 git-commit 控制此钩子调用的选项,你有点运气不好,除非你认为
--no-verify
(它抑制所有提交钩子)足够好。别名...最干净的方法是在别名中设置一个环境变量,可能是: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:
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 ifphpmd
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: