git post-commit hook - 已提交文件上的脚本

发布于 2024-09-16 11:39:27 字数 838 浏览 6 评论 0原文

我可以在某个地方看到一个示例提交后挂钩来在每个提交的文件上运行脚本吗?

例如。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏璃陌 2024-09-23 11:39:27

假设您在每个文件上执行的脚本无法设置为在某个参数失败时立即失败,则您无法使用该 xargs 设置。你可以这样做:

#!/bin/bash

# for a pre-commit hook, use --cached instead of HEAD^ HEAD
IFS=

但也许 phpmd 已经为你做这件事了?无法完全理解您的问题,但如果确实如此,您所要做的就是确保它是挂钩中的最后一个命令。管道的退出状态是其中最后一个命令的退出状态 (phpmd),shell 脚本的退出状态是它运行的最后一个命令的退出状态 - 所以如果 phpmd 以错误状态退出,钩子脚本将会,如果它是预提交钩子,这将导致 git 中止提交。

至于 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...
\n' git diff --name-only HEAD^ HEAD | grep '\.php

但也许 phpmd 已经为你做这件事了?无法完全理解您的问题,但如果确实如此,您所要做的就是确保它是挂钩中的最后一个命令。管道的退出状态是其中最后一个命令的退出状态 (phpmd),shell 脚本的退出状态是它运行的最后一个命令的退出状态 - 所以如果 phpmd 以错误状态退出,钩子脚本将会,如果它是预提交钩子,这将导致 git 中止提交。

至于 git-commit 控制此钩子调用的选项,你有点运气不好,除非你认为 --no-verify (它抑制所有提交钩子)足够好。别名...最干净的方法是在别名中设置一个环境变量,可能是:


 |
while read file; do
    # exit immediately if the script fails
    my_script "$file" || exit $?
done

但也许 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:

#!/bin/bash

# for a pre-commit hook, use --cached instead of HEAD^ HEAD
IFS=

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...
\n' git diff --name-only HEAD^ HEAD | grep '\.php

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:


 |
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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文