gpt4 book ai didi

linux - 创建一个 git 预接收 Hook

转载 作者:太空宇宙 更新时间:2023-11-04 09:22:49 27 4
gpt4 key购买 nike

序言

我想从我的 pre-push hook 创建一个 pre-receive hook。现在,环顾网络和 SO,我发现了许多与特定问题有关的问题,和/或专注于钩子(Hook)的描述,而不是实际显示它(我在看着你,git-scm)。

Gist

所以无论如何,据我所知,pre-receive hook 是在没有参数 的情况下调用的。那我怎么获取数据呢?在这样的钩子(Hook)中,我会看到自己想要掌握的数据非常多,例如:

  • pushername(请原谅双关语)
  • 提交信息
  • 时间戳
  • 更改文件
  • 目标分支

但老实说,我不知道如何获取数据 - 我知道人们这样做,因为我已经看到了这样的脚本。

假设

我想假设它是 bash 可行的,因为配置越少越好,amirite?

真题

编写预接收 Hook ,如何收集有关触发它的推送的数据?

最佳答案

这已记录在案,请参阅 https://git-scm.com/docs/githooks#pre-receive :

This hook executes once for the receive operation. It takes no arguments, but for each ref to be updated it receives on standard input a line of the format:

<old-value> SP <new-value> SP <ref-name> LF

where <old-value> is the old object name stored in the ref, <new-value> is the new object name to be stored in the ref and <ref-name> is the full name of the ref. When creating a new ref, <old-value> is 40 0.

请注意,您可以接收多个分支的更新。当您在 bash 中编写预接收 Hook 时,您可以从:

while read old new ref; do
# do something with each $old $new $ref
done

$ref将是全名,例如 refs/heads/my-branch . $old$new是提交对象的 SHA-1 名称。

要获取提交消息、作者等,您可以使用 $old 调用 git 命令和 $new ,例如 git log $old..$new (请注意,一个分支上可能会推送多个新提交)。

关于linux - 创建一个 git 预接收 Hook ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40039533/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com