在git pre-receive hook中访问已更改的文件路径 [英] Access the changed files path in git pre-receive hook

查看:1369
本文介绍了在git pre-receive hook中访问已更改的文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在远程仓库上写了一个git pre-receive钩子,以确保推送的代码符合我们公司的内部指导。



我能够找到所有需要检查的文件,但是,我没有使用普通文件操作打开这些文件的路径(例如 cat git_working_directory / file_name 会抛出没有这样的文件或目录错误)。 验证代码的应用程序需要文件路径作为参数,以便它可以打开文件并运行它的检查。



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

我想知道是否有一个临时位置,只要这些文件被推送到git中,这些文件就会保存在git中,以便我可以将该目录传递给应用程序运行支票?



更新:



我可以结帐到临时位置在那里运行支票,这可能是一个选择,但考虑到开发商经常推动的事实,有时甚至同时又回购非常大,这个选项似乎并不可行。我正在寻找更多的解决方案,我可以使用该文件的路径,如果它在某种程度上可用的话。

解决方案


我想知道是否有一个临时位置,这些文件一旦被推送就会将文件保存在git中,以便我可以将该目录传递给应用程序以运行检查?


不,没有这样的地方。这些文件存储为blob,可以减少为增量和/或压缩,所以不能保证它们在准备使用状态下可用。



< blockquote>

检查标准的应用程序需要文件路径作为参数,以便它可以打开文件并运行它的检查。


如果您使用的是Linux系统,您可以将 / dev / stdin 作为输入文件并通过管道输入文件。

#!/ bin / sh
while readrecerev newrev refname;做
git diff --name-only $ oldrev $ newrev |同时读取文件;做
git show $ newrev:$ file |验证/ dev / stdin ||出口1
完成
完成


I'm writing a git pre-receive hook on the remote repo to make sure pushed code is consistent with our company's internal guidelines.

I'm able to find all the files that are to be checked when a pre-receive hook is fired, however, I don't have the path to these files to open them using normal file operations(e.g. cat git_working_directory/file_name would throw No such file or directory error). The application which validates the code requires the file path as an argument so that it can open the file and run its checks.

Consider this scenario: the developer created a new file and pushed it to the server and the pre-receive hook is triggered. At this stage the new file is not saved on to the remote's working directory because the pre-receive hook is still running.

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?

Update:

I could checkout to a temporary location and run the checks there, this could be an option but considering the fact that developers frequently push, sometimes even at the same time and the repo being very big, this option doesn't seem to be feasible. I'm looking more for a solution where I can just use the path to the file if it is somehow available.

解决方案

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?

No, there is no such place. Those files are stored as blobs and can be reduced to deltas and/or compressed, so there's no guarantee they'll be available anywhere in 'ready-to-consume' state.

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.

If you're on linux you could just point /dev/stdin as input file and put the files through pipe.

#!/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 pre-receive hook中访问已更改的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆