Git服务器挂钩:获取正在推送的文件的内容? [英] Git server hook: get contents of files being pushed?
问题描述
我试图在我的团队中执行一些编码规则。为此,我编写了一些客户端钩子,这些钩子都可以正常工作,但是现在我希望在开发人员将其修改推送到中央存储库时运行相同的检查。但他们不工作。
这是我想要做的:
我想遍历逐行推送文件,检查编码惯例违规行为,如果我找到了,就拒绝推送,同时显示行号+违规行为。
在我的预先提交客户端我可以通过调用 git diff --cached --name-status --diff-filter = AM
来获得修改后的文件列表,而 git cat-file -p:filename
获取第一次调用中检索到的每个文件以获取文件的全部文本。 当我尝试在服务器端 update
钩子中做同样的事情时,我得到一个空字符串
我也尝试调用 git show --pretty =format:--name-only newrev code>(其中newrev是我作为更新钩子的参数获得的SHA,
git diff-tree -r --name-only --no-commit-id< tree-ish >
以及其他一些我在网上找到的东西,但我无法清楚地了解正在发生的事情以及我应该调用的内容。
你能帮助我吗?
您必须对脚本进行更改,因为服务器上没有工作副本并且 git diff --cached
可以与临时区域(或索引)一起工作,而当服务器收到推送请求时,索引为空。
简单地使用 git diff --name-status< sha-old>< sha-new>
来代替 sha-old
和 sha-new
是发送给钩子作为参数,你会得到与提交前运行 git diff --cached
相同的输出。
<至于检查文件内容,你可以使用
git-show sha-new:/ path / to / file
I am trying to enforce some coding rules in my team. To that end, I've written a number of client-side hooks, which work all right, but now I want the same checks to run when a developer pushes their modifications to the central repository. But they don't work.
Here is what I want to do:
I want to traverse the pushed files line by line, check for coding conventions violations and if I find any, reject the push, also showing the line numbers + violations.
In my pre-commit client side hook I was able to do that by calling git diff --cached --name-status --diff-filter=AM
to get the list of modified files, and git cat-file -p :filename
for each of the files retrieved in the first call to get the whole text of the files.
When I try to do the same in my server-side update
hook, I get an empty string (for the list of the files).
I also tried calling git show --pretty="format:" --name-only newrev
(where newrev is the SHA I get as a parameter to the update hook, git diff-tree -r --name-only --no-commit-id <tree-ish>
, and some other things I find on the net, but I can't get a clear understanding of what is going on and what I should call.
Can you help me?
You have to make changes to your script because there's no working copy on the server side, and git diff --cached
works with a staging area (or index), while your index is empty when the server receives a push.
Simply use git diff --name-status <sha-old> <sha-new>
instead, with sha-old
and sha-new
being the refs sent to the hooks as an argument, and you'll get the same output as running git diff --cached
before a commit.
As for checking file content, you can use git-show sha-new:/path/to/file
这篇关于Git服务器挂钩:获取正在推送的文件的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!