[英]Python: fatal: not a git repository Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set)
当我跑
proc = subprocess.Popen(['git', 'add', '-A'], stdout=subprocess.PIPE)
我收到这个错误
fatal: not a git repository (or any parent up to mount point /media)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
但是跑步
os.system('git add -A')
完美地完成工作。
如果您认为该文件夹没有.git
文件,
proc = subprocess.Popen(['ls', '-a'], stdout=subprocess.PIPE)
表明它已经在 cwd 中。
为什么Popen
不能暂存文件,也不能提交,而os.system
两者都做?
更新:
这是我失败的 MWE
import subprocess
import os
cwd = os.getcwd()
proj_path = os.path.join(cwd, 'newproj')
os.makedirs(proj_path)
os.chdir(proj_path)
proc = subprocess.Popen(['git', 'init'], stdout=subprocess.PIPE)
proc = subprocess.Popen(['ls', '-a'], stdout=subprocess.PIPE)
print(proc.stdout.read().decode('ascii'))
proc = subprocess.Popen(['git', 'add', '-A'], stdout=subprocess.PIPE)
out, err = proc.communicate()
if err:
print('Error:\n', err.decode())
print(out.decode('ascii'))
output
.
..
.git
fatal: not a git repository (or any parent up to mount point /media)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).
我的 Python 版本比你的稍差,但我能够重现该问题,这实际上非常简单:
proc = subprocess.Popen(['git', 'init'], stdout=subprocess.PIPE)
proc = subprocess.Popen(['ls', '-a'], stdout=subprocess.PIPE)
print(proc.stdout.read().decode('ascii'))
proc = subprocess.Popen(['git', 'add', '-A'], stdout=subprocess.PIPE)
请注意缺少对proc.wait()
或类似的任何调用。 这意味着我们剥离了一个git init
并且不要等待它。
接下来,我们运行ls -a
。 在这里,我们确实等待了一点——足够长的时间以将其 output 读取到 EOF,这实际上是相对较长的,因为 EOF 只发生因为ls -a
完成——同时git init
仍在运行。 根据git init
的工作速度,我们可能会或可能不会在此处找到.git
目录。 如果这需要足够长的时间, git init
也可能会完成。 幸运的是,我系统上的git init
比ls -a
慢得多,我看到的效果和你一样。 (好吧,我发现.git
目录有时还不存在。)
最后,我们运行git add -A
。 git init
可能仍在运行,也可能未运行。 事实证明,它仍在运行,并且还没有达到将.git
目录建立为 Git存储库的程度。 所以git add -A
抱怨,你观察到的错误。
如果我们在git init
之后添加proc.wait()
理想情况下,我们应该检查返回码,或者在这里简单地使用subprocess.check_call
或subprocess.run
——问题就消失了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.