git hooks :有克隆钩子吗?

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

git hooks : is there a clone hook?

gitgithooksgit-clone

提问by maxmelbin

We want to store some meta-information about the commit in an external database. During a clone or a checkout, this database should be referred and we copy the meta information to a file in the repo which is cloned. The database is required rather than just using a file is for the sake of indexing and searches etc ...

我们想在外部数据库中存储一些关于提交的元信息。在克隆或结帐期间,应该引用该数据库,我们将元信息复制到被克隆的 repo 中的文件中。数据库是必需的,而不仅仅是使用文件是为了索引和搜索等......

I thought if there is a clone hook, we could trigger this. I couldn't find the clone hooks in the sample in .git/hooks. is there one? is post-checkout hook the only possibility at client side?

我想如果有一个克隆钩子,我们可以触发它。我在 .git/hooks 中的示例中找不到克隆钩子。有吗?结帐后挂钩是客户端的唯一可能性吗?

采纳答案by maxmelbin

ok, one way to do this is to use the clone --templateoption.

好的,一种方法是使用该clone --template选项。

Specify the location where the client side hooks will be stored as value to the --templateswitch. The hooks are copied to the clone, and the post-checkout hook is fired immediately!

指定客户端挂钩将作为值存储到--template交换机的位置。钩子被复制到克隆中,结帐后钩子立即被触发!

回答by larsks

When you clonea remote repository, you can't run any client-side hooks because hooks are local to your working copy, and you're creating one from scratch. When you pullnew changes from a remote repository, git will run your local post-mergehook if it exists.

当您克隆远程存储库时,您无法运行任何客户端钩子,因为钩子对于您的工作副本是本地的,并且您是从头开始创建的。当您从远程存储库中提取新更改时,git 将运行您的本地post-merge钩子(如果存在)。

There is nothing run on the server as the result of a pulloperation. A pushoperation will trigger the servers's updateand post-updatehooks.

作为拉取操作的结果,服务器上没有运行任何内容。一操作将触发服务器的updatepost-update挂钩。

See the Git Bookfor more information.

有关更多信息,请参阅Git 手册

回答by ralphtheninja

No, there isn't any clone hook.

不,没有任何克隆钩子。

回答by ZachB

Since git version 1.6.3, the post-checkouthook runs on git-clone (when run without --no-checkout).

从 git 版本 1.6.3 开始,结帐后挂钩在 git-clone 上运行(不带 运行时--no-checkout)。

It is also run after git-clone[1], unless the --no-checkout (-n) option is used. The first parameter given to the hook is the null-ref, the second the ref of the new HEAD and the flag is always 1.

它也在 git-clone[1] 之后运行,除非使用了 --no-checkout (-n) 选项。给钩子的第一个参数是空引用,第二个参数是新 HEAD 的引用,标志总是 1。

https://git-scm.com/docs/githooks#_post_checkout

https://git-scm.com/docs/githooks#_post_checkout

回答by kskkskksk

I'm late but there is a workaround:

我迟到了,但有一个解决方法:

  1. Capture git cloneexecution and set a variable using trap set_var_on_git_clone DEBUGwhere set_var_on_git_cloneis a user-defined function. (e.g. https://gist.github.com/KeyAmam/a6afcabc3a724fc4a541aca7629c3ff6)

  2. Check the variable in post-checkoutscript and do some stuff in the case. Clear the variable at the end of the script. (e.g. https://gist.github.com/KeyAmam/6a0e8805c0b6a662adb6bcf8118a089a)

  1. 捕获git clone执行并使用trap set_var_on_git_clone DEBUGwhereset_var_on_git_clone是用户定义的函数设置变量。(例如https://gist.github.com/KeyAmam/a6afcabc3a724fc4a541aca7629c3ff6

  2. 检查post-checkout脚本中的变量并在案例中做一些事情。清除脚本末尾的变量。(例如https://gist.github.com/KeyAmam/6a0e8805c0b6a662adb6bcf8118a089a

This only works in Bash but you can do a similar thing in other shells.

这仅适用于 Bash,但您可以在其他 shell 中做类似的事情。