简体   繁体   English

git-clone和post-checkout hook

[英]git-clone and post-checkout hook

According to the manual, the post-checkout hook is run after a git checkout (just as expected) but also after a git clone (unless you pass --no-checkout ). 根据手册, post-checkout hook在git checkout (正如预期)之后运行,但也在git clone (除非你传递--no-checkout )。

Very well, now, considering the following: 很好,现在,考虑以下因素:

  • you don't have a local repository before a git clone 你没有git clone之前的本地存储库
  • hooks are not synced between remotes 钩子不在遥控器之间同步
  • hooks stored in a custom template directory used with --template are copied non-executable and therefore not executed after git clone (that is actually not true as stated by Jefromi in his answer, but the hook is still not executed) 存储在与 --template一起使用的自定义模板目录中的 --template被复制为不可执行的,因此在 git clone之后 不会执行 (实际上这不是Jefromi在他的回答中所说的那样,但钩子仍未执行)

It seems impossible that a post-checkout hook ever gets executed after a git clone . 似乎不可能在git clone之后执行post-checkout钩子。 Still, the githooks man page at http://git-scm.com/docs/githooks explicitely states a way to avoid it being executed, and also parameters passed in case it is executed, which would indicate it is possible to execute a custom hook after a git-clone . 尽管如此, http: githooks上的githooks手册页明确说明了一种避免执行的方法,以及在执行时传递的参数,这表明可以执行自定义git-clone后挂钩。

So, how is it possible? 那么,怎么可能呢? I am obviously missing something here. 我显然在这里遗漏了一些东西。

Turns out 结果

I suppose you could make a custom installation - rename the hooks in .../share/git-core/templates/hooks to remove the .sample suffix. 我想你可以进行自定义安装 - 重命名.../share/git-core/templates/hooks以删除.sample后缀。

You could also make a template directory full of symlinks to a hooks directory inside the repository, (eg post-checkout -> ../../hooks/post-checkout ). 您还可以将一个充满符号链接的模板目录创建到存储库中的钩子目录(例如, post-checkout -> ../../hooks/post-checkout )。 Then if the cloned repo contained that particular hook, it'd get executed. 然后,如果克隆的repo包含该特定的钩子,它就会被执行。

You're right, though, in most cases it will not happen. 不过,你说得对,在大多数情况下都不会发生。

Edit: I just tested it, and the --template option does appear to preserve permissions, so that's a much more straight-forward way to make it happen. 编辑:我刚测试过它,并且--template选项似乎确实保留了权限,因此这是一种更加直接的方式来实现它。 What'd you see to indicate that it stripped that bit? 你看到什么表明它剥离了那一点?

The final say on versions: You're looking at documentation online for a newer version of git than you're using. 关于版本的最终决定权:你正在网上查看更新版本的git而不是你正在使用的文档。 This feature was added in dfa7a6c (clone: run post-checkout hook when checking out); 这个功能是在dfa7a6c中添加的(克隆:签出时运行post-checkout hook); git describe says this wasn't included until v1.6.2-rc2. git describe说直到v1.6.2-rc2才包括这个。

From the githooks documentation : githooks文档

When git-init is run, a handful of example hooks are copied into the hooks directory of the new repository, but by default they are all disabled. 运行git-init时 ,会将一些示例挂钩复制到新存储库的hooks目录中,但默认情况下它们都被禁用。 To enable a hook, rename it by removing its .sample suffix. 要启用挂钩,请通过删除其.sample后缀来重命名它。

This initialization takes place as part of creating a clone—note the call to init_db in builtin-clone.c . 这个初始化发生作为创建克隆,注意调用的一部分init_dbbuiltin-clone.c

For example: 例如:

$ cat /tmp/my-git-template/hooks/post-checkout 
#! /bin/bash
echo "Hello from $0"

$ git clone --template=/tmp/my-git-template file:///tmp/repo.git my-clone
Initialized empty Git repository in /tmp/my-clone/.git/
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
Hello from .git/hooks/post-checkout

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM