Python:致命:不是 git 存储库停止在文件系统边界(GIT_DISCOVERY_ACROSS_FILESYSTEM 未设置)
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'))
输出
.
..
.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
。在这里我们确实等待了一点——足够长的时间来读取它的输出到 EOF,这实际上是相对长的,因为 EOF 只发生因为 ls -a
完成——同时 git init
仍然是 运行ning .根据 git init
工作的速度,我们可能会或可能不会在这里找到 .git
目录。如果这需要足够长的时间,git init
也可能会完成。幸运的是,我系统上的 git init
比 ls -a
慢得多,我看到的效果与您相同。 (好吧,我发现 .git
目录有时还不存在。)
最后,我们运行git add -A
。 git init
可能还是 运行ning,也可能不是。事实证明,它 是 仍然是 运行ning,并且还没有足够远以将 .git
目录建立为 Git 存储库。所以 git add -A
抱怨你观察到的错误。
如果我们在 git init
之后添加 proc.wait()
——理想情况下,我们应该检查 return 代码,或者在这里简单地使用 subprocess.check_call
或 subprocess.run
—问题消失了。
当我运行
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'))
输出
.
..
.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
。在这里我们确实等待了一点——足够长的时间来读取它的输出到 EOF,这实际上是相对长的,因为 EOF 只发生因为 ls -a
完成——同时 git init
仍然是 运行ning .根据 git init
工作的速度,我们可能会或可能不会在这里找到 .git
目录。如果这需要足够长的时间,git init
也可能会完成。幸运的是,我系统上的 git init
比 ls -a
慢得多,我看到的效果与您相同。 (好吧,我发现 .git
目录有时还不存在。)
最后,我们运行git add -A
。 git init
可能还是 运行ning,也可能不是。事实证明,它 是 仍然是 运行ning,并且还没有足够远以将 .git
目录建立为 Git 存储库。所以 git add -A
抱怨你观察到的错误。
如果我们在 git init
之后添加 proc.wait()
——理想情况下,我们应该检查 return 代码,或者在这里简单地使用 subprocess.check_call
或 subprocess.run
—问题消失了。