git - 在 git pre-receive hook 中访问更改的文件路径

标签 git githooks git-remote

我正在远程仓库上编写一个 git 预接收 Hook ,以确保推送的代码符合我们公司的内部准则。

当预接收 Hook 被触发时,我能够找到所有要检查的文件,但是,我没有这些文件的路径,无法使用正常文件操作打开它们(例如 cat git_working_directory/file_name 会抛出 No such file or directory 错误)。 验证代码的应用程序需要文件路径作为参数,以便它可以打开文件并运行检查。

考虑这种情况:开发人员创建了一个新文件并将其推送到服务器,并触发了预接收 Hook 。在此阶段,新文件不会保存到远程的工作目录中,因为预接收 Hook 仍在运行。

我想知道是否有一个临时位置,文件在推送后立即保存在 git 中,以便我可以将该目录传递给应用程序以运行检查?/p>

更新:

我可以 checkout 到一个临时位置并在那里运行检查,这可能是一个选项,但考虑到开发人员经常推送的事实,有时甚至是在同一时间推送,而且 repo 非常大,这个选项似乎并不适用可行。我正在寻找一种解决方案,如果它以某种方式可用,我可以只使用文件的路径。

最佳答案

I'm wondering if there is a temporary location where the files are saved in git as soon as they are pushed so that I can pass that directory to the application to run the checks?

不,没有这样的地方。这些文件以 blob 形式存储,可以减少为增量和/或压缩,因此无法保证它们在“准备好使用”状态的任何地方都可用。

The application which checks for the standards requires the file path as an argument so that it can open the file and run its checks.

如果你在 linux 上,你可以将 /dev/stdin 作为输入文件并通过管道放置文件。

#!/bin/sh
while read oldrev newrev refname; do
 git diff --name-only $oldrev $newrev | while read file; do
   git show $newrev:$file | validate /dev/stdin || exit 1
     done
done

关于git - 在 git pre-receive hook 中访问更改的文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28414640/

相关文章:

git - 如何使用 Git 删除不需要的文件引用

git - 将 gitignore 文件转换为 tfignore。从我开始的默认 ionic 应用程序

git - 创建一个 git 钩子(Hook)以防止 merge 到本地存储库中的特定分支,但允许从远程 pull

git - post-receive hook -- 如果传入文件类型为 *.cgi,则将权限设置为 755

git - 不能同时创建本地和远程分支(跟踪)

git - 更新远程服务器以从重命名的 Git 存储库中提取

git - 两个 git 存储库,彼此作为 Remote

Gitkraken 讨厌我在 SourceTree 中工作的 SSH key

git - VSTS : Different merge commit message

git - 使用裸存储库 Hook 将服务器恢复到过去的提交