我正在远程仓库上编写一个 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/