预提交中的Git diff调用抛出“致命:无法读取[SHA1]". [英] Git diff call in pre-commit throws "fatal: unable to read [SHA1]"

查看:109
本文介绍了预提交中的Git diff调用抛出“致命:无法读取[SHA1]".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Windows中工作,并尝试在存储库的预提交脚本(Python)中运行git diff命令.我的Python调用看起来像这样:

I am working in windows and attempting to run a git diff command in the pre-commit script (Python) of a repository. My Python call looks like this:

repo_dir = 'D:/git/current_uic/src/gtc/resource'
cmd = ['diff', '--name-only']
print(Popen(['git', '--git-dir={}'.format(repo_dir + '/.git'), 
             '--work-tree={}'.format(repo_dir)] + cmd,
            stdin=PIPE, stdout=PIPE).communicate())

每当我去提交"D:/git/current_uic/src/gtc"存储库时,都会得到以下信息:

Whenever I go to commit in the "D:/git/current_uic/src/gtc" repo, I get the following:

fatal: unable to read 6ff96bd371691b9e93520e133ebc4d84c74cd0f6

请注意,这是'D:/git/current_uic/src/gtc'存储库的预提交钩子,并且'D:/git/current_uic/src/gtc/resource'是'D:的子模块: /git/current_uic/src/gtc".还要注意,如果我弹出打开Git bash并运行以下命令:

Note that this is a pre-commit hook for the 'D:/git/current_uic/src/gtc' repository and that 'D:/git/current_uic/src/gtc/resource' is a submodule of 'D:/git/current_uic/src/gtc'. Also note that if I pop open Git bash and run the following:

git --git-dir=D:/git/current_uic/src/gtc/resource/.git 
    --work-tree=D:/git/current_uic/src/gtc/resource diff --name-only

或者如果我直接从Git bash运行脚本,无论工作目录如何,我都能得到我想要的东西.

or if I just run the script straight from Git bash I get exactly what I want, regardless of working directory.

关于这里发生的事情有什么想法吗?

Any ideas as to what is going on here?

推荐答案

问题:

运行钩子后,Git会设置一些可通过钩子脚本访问的环境变量.问题在于,Git本身使用了这些环境变量,当钩子被触发时,Git设置/使用它们的正常方法似乎被设置的值所覆盖.在此特定实例中,环境变量GIT_INDEX_FILE已设置为索引文件的路径,该索引文件对应于已调用钩子的存储库(D:/git/current_uic/src/.git/modules/gtc/index),导致(错误的)索引和(正确的)更改树之间不匹配.

Upon running a hook, Git sets some environment variables that are accessible by the hook script. The problem is that Git itself uses these environment variables, and the normal way in which Git sets/uses them seems to be overridden by the values set when the hook gets fired off. In this particular instance, the environment variable GIT_INDEX_FILE has been set to the path to the index file corresponding to the repository which had called the hook (D:/git/current_uic/src/.git/modules/gtc/index), causing a mismatch between the (incorrect) index and the (correct) change tree.

修复:

在挂钩脚本中,在进行任何git调用之前,将环境变量GIT_INDEX_FILE设置为正确的值.在这种情况下,您可以执行以下操作:

In the hook script, set the environment variable GIT_INDEX_FILE to the correct value before making any git calls. In this case, you could do the following:

set GIT_INDEX_FILE=D:/git/current_uic/src/.git/modules/gtc/modules/resource/index
git --git-dir=D:/git/current_uic/src/gtc/resource/.git 
    --work-tree=D:/git/current_uic/src/gtc/resource diff --name-only

其他信息

有关这些Git环境变量及其设置的钩子的更多信息,请参见此处.

More information about these Git environment variables and which hooks set them can be found here.

这篇关于预提交中的Git diff调用抛出“致命:无法读取[SHA1]".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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