git hooks bash -- 获取提交信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3321460/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:22:23  来源:igfitidea点击:

git hooks bash -- getting the commit message

gitbash

提问by myusuf3

I am writing a git hook, to run on a commit to the main branch. I need it to parse and look for some text in the the commit message. How do I reference the commit message in my bash script?

我正在编写一个 git 钩子,以在提交到主分支时运行。我需要它来解析并在提交消息中查找一些文本。如何在我的 bash 脚本中引用提交消息?

Also I would like to know how to run the hook only on commits to the main branch. so developers can quash there local commits and commit to the main branch when they have fixed a bug or something.

另外我想知道如何仅在提交到主分支时运行钩子。因此,开发人员可以在修复错误或其他问题时取消本地提交并提交到主分支。

Please and thank you.

谢谢,麻烦您了。

采纳答案by Cascabel

The answer depends slightly on what you're trying to do with the commit message. There are three hooks you could be asking about:

答案在一定程度上取决于您尝试对提交消息执行的操作。您可能会询问三个钩子:

  • prepare-commit-msgis run immediately after the default message is prepared, before the user edits it. The first argument is the name of the file with the commit message. (The second argument indicates where the message came from.)

  • commit-msgis run after the commit message is edited/finalized, but before the commit takes place. If you want to fail the commit if the user's commit message is bad, or to modify the message, you want this, and the single argument is the name of the file with the commit message.

  • post-commitis run after the commit. It has no arguments, but you can of course get the message from git log -n 1 HEAD(probably with --format=format:%s%n%bor some such). If all you want to do is look for something in the message, and notify based on it, you should use this.

  • prepare-commit-msg在准备好默认消息之后,在用户编辑它之前立即运行。第一个参数是带有提交消息的文件名。(第二个参数表示消息的来源。)

  • commit-msg在提交消息被编辑/完成之后,但在提交发生之前运行。如果您想在用户的提交消息不好的情况下使提交失败,或者要修改消息,则需要这样做,并且单个参数是带有提交消息的文件的名称。

  • post-commit在提交后运行。它没有参数,但您当然可以从git log -n 1 HEAD(可能带有--format=format:%s%n%b或类似的)获取消息。如果您只想在消息中查找某些内容并根据它进行通知,则应该使用它。

All of this material is taken from the githooks manpage

所有这些材料都取自githooks 联机帮助页

As for running it only on the main branch, all you need is something like:

至于仅在主分支上运行它,您需要的只是:

if [ "$(git symbolic-ref HEAD)" == "refs/head/master" ]; then
    # do your stuff
fi

回答by dada

With following command:

使用以下命令:

cat $1

cat $1

will print your commit message in commit-msg

将在 commit-msg 中打印您的提交消息