git-clone 和结账后钩子

发布于 2024-08-19 14:11:33 字数 718 浏览 5 评论 0 原文

根据手册,post-checkout 挂钩在 git checkout 之后运行(正如预期的那样),但也在 git clone 之后运行(除非您通过了--no-checkout)。

很好,现在,考虑以下因素:在

  • 之间同步之前,您没有本地存储库
  • git clone钩子未在远程
  • 存储在与 一起使用的自定义模板目录中存储的钩子 - -template 被复制为不可执行的,因此不会git clone 之后执行(这实际上并不正确,正如 Jefromi 在他的回答中所述,但钩子仍然没有执行)

git clone 之后似乎不可能执行 post-checkout 钩子。尽管如此,http://git-scm.com/docs 上的 githooks 手册页/githooks 明确说明了避免执行它的方法,以及执行时传递的参数,这表明可以在 git-clone 后执行自定义挂钩。

那么,怎么可能呢?我显然在这里遗漏了一些东西。

事实证明

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).

Very well, now, considering the following:

  • you don't have a local repository before a 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)

It seems impossible that a post-checkout hook ever gets executed after a git clone. 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.

So, how is it possible? I am obviously missing something here.

Turns out

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

深陷 2024-08-26 14:11:33

我想您可以进行自定义安装 - 重命名 .../share/git-core/templates/hooks 中的挂钩以删除 .sample 后缀。

您还可以创建一个模板目录,其中充满指向存储库内的 hooks 目录的符号链接(例如 post-checkout -> ../../hooks/post-checkout)。然后,如果克隆的存储库包含该特定钩子,它将被执行。

不过,你是对的,在大多数情况下这不会发生。

编辑:我刚刚测试了它,并且 --template 选项似乎确实保留了权限,因此这是实现这一目标的更直接的方法。你看到什么表明它剥夺了那一点?

关于版本的最终决定权:您正在在线查看比您正在使用的版本更新的 git 文档。此功能已在 dfa7a6c 中添加(克隆:运行 post - 结帐时的结帐钩子); git describe 表示直到 之前才包含此内容v1.6.2-rc2

I suppose you could make a custom installation - rename the hooks in .../share/git-core/templates/hooks to remove the .sample suffix.

You could also make a template directory full of symlinks to a hooks directory inside the repository, (e.g. post-checkout -> ../../hooks/post-checkout). Then if the cloned repo contained that particular hook, it'd get executed.

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. 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. This feature was added in dfa7a6c (clone: run post-checkout hook when checking out); git describe says this wasn't included until v1.6.2-rc2.

静赏你的温柔 2024-08-26 14:11:33

来自 githooks 文档

git-init运行时,一些示例钩子被复制到新存储库的hooks目录中,但默认情况下它们都被禁用。要启用挂钩,请通过删除其 .sample 后缀来重命名它。

此初始化作为创建克隆的一部分进行 - 请注意 builtin-clone.c

例如:

$ 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

From the githooks documentation:

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. To enable a hook, rename it by removing its .sample suffix.

This initialization takes place as part of creating a clone—note the call to init_db in builtin-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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文