我将如何在 Git 中编写预合并挂钩?

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

How would I write a pre-merge hook in Git?

gitmergegithooks

提问by joshin4colours

The question says it all. Is there a way to perform an action before a merge? I'm guessing there's a way to make use of a pre-commithook, but I'm not quite sure.

问题说明了一切。有没有办法在合并之前执行操作?我猜有一种方法可以使用pre-commit钩子,但我不太确定。

采纳答案by Kousha

You can try using the prepare-commit-msghook. The second argument will be merge"if the commit is a merge or a .git/MERGE_MSGfile exists". A non-zero exit status will abort the commit.

您可以尝试使用prepare-commit-msg钩子。第二个参数将是merge“如果提交是合并或.git/MERGE_MSG文件存在”。非零退出状态将中止提交。

I don't think this will work with a fast-forward merge, since there won't be a commit message.

我认为这不适用于快进合并,因为不会有提交消息。

More info on hooks: https://www.kernel.org/pub/software/scm/git/docs/githooks.html#_prepare_commit_msg

关于钩子的更多信息:https: //www.kernel.org/pub/software/scm/git/docs/githooks.html#_prepare_commit_msg

回答by VonC

With Git 2.24 (Q4 2019), no need for script wrapper, or prepare-message hook.

使用 Git 2.24(2019 年第四季度),无需脚本包装器或准备消息挂钩。

A new "pre-merge-commit" hook has been introduced.

引入了一个新的“ pre-merge-commit”钩子。

See commit bc40ce4, commit 6098817, commit a1f3dd7(07 Aug 2019) by Michael J Gruber (mjg).
See commit f78f6c7(07 Aug 2019) by Josh Steadmon (steadmon).
(Merged by Junio C Hamano -- gitster--in commit f76bd8c, 18 Sep 2019)

请参阅Michael J Gruber ( ) 的commit bc40ce4commit 6098817commit a1f3dd7(07 Aug 2019 )。 请参阅Josh Steadmon ( )提交的 f78f6c7(2019 年 8 月 7 日(由Junio C Hamano合并-- --提交 f76bd8c 中,2019 年 9 月 18 日)mjg
steadmon
gitster

git-merge: honor pre-merge-commithook

git-mergedoes not honor the pre-commit hook when doing automatic merge commits, and for compatibility reasons this is going to stay.

Introduce a pre-merge-commithook which is called for an automatic merge commit just like pre-commit is called for a non-automatic merge commit (or any other commit).

git-merge: 荣誉pre-merge-commit

git-merge在进行自动合并提交时不遵守预提​​交钩子,出于兼容性原因,这将保留。

引入一个pre-merge-commit用于自动合并提交的钩子,就像为非自动合并提交(或任何其他提交)调用预提交一样。

The documentationnow includes:

文档现在包括:

pre-merge-commit

This hook is invoked by git-merge.
It takes no parameters, and is invoked after the merge has been carried out successfully and before obtaining the proposed commit log message to make a commit.
Exiting with a non-zero status from this script causes the git mergecommand to abort beforecreating a commit.

The default 'pre-merge-commit' hook, when enabled, runs the 'pre-commit' hook, if the latter is enabled.

This hook is invoked with the environment variable GIT_EDITOR=:if the command will not bring up an editor to modify the commit message.

If the merge cannot be carried out automatically, the conflicts need to be resolved and the result committed separately (see git-merge).
At that point, this hook will not be executed, but the 'pre-commit' hook will, if it is enabled.

合并前提交

该钩子由 调用git-merge
它不接受任何参数,在合并成功执行之后,在获取建议的提交日志消息以进行提交之前调用。
从此脚本git merge以非零状态退出会导致命令创建提交之前中止。

默认的“pre-merge-commit”钩子在启用时运行“pre-commit”钩子,如果后者被启用。

GIT_EDITOR=:如果该命令不会打开编辑器来修改提交消息,则会使用环境变量调用此挂钩。

如果无法自动执行合并,则需要解决冲突并单独提交结果(请参阅 参考资料git-merge)。
那时,这个钩子不会被执行,但是“ pre-commit”钩子会,如果它被启用。

回答by ElectRocnic

Another nice workaround would be to add a shell script, call it like you want, then add these lines to the script:

另一个不错的解决方法是添加一个 shell 脚本,根据需要调用它,然后将这些行添加到脚本中:

git() {
    if [ "" == "merge" ]; then
        echo "seems to work like a charme"
    fi
    command git "$@"
}

git "$@"

Then make an

然后做一个

alias git="./my-pre-merge-script.sh"

Then you are good to go. You just added your own pre-merge hook. I know, that you do not have access to whatever arguments git would pass to a real pre-merge hook, but you can prepare files or whatever you want to prepare for merge now; I personally am very happy with this approach: I spent 2 or 3 whole days to find something for pre-merge, then I had to go with the pre-commit-msg which I did not find accurate enough for my needs. This solves all my problems. Hope this helps anybody in the future.

然后你就可以走了。您刚刚添加了自己的预合并挂钩。我知道,您无权访问 git 将传递给真正的预合并挂钩的任何参数,但是您现在可以准备文件或任何您想要为合并做准备的东西;我个人对这种方法非常满意:我花了 2 到 3 天的时间来寻找预合并的内容,然后我不得不使用 pre-commit-msg,我认为它不够准确,无法满足我的需求。这解决了我所有的问题。希望这对未来的任何人都有帮助。