【问题标题】:Python: fatal: not a git repository Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set)Python:致命:不是 git 存储库停止在文件系统边界(未设置 GIT_DISCOVERY_ACROSS_FILESYSTEM)
【发布时间】:2021-02-10 22:41:52
【问题描述】:

当我跑步时

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).

【问题讨论】:

  • @lesmana,如果没有设置 cwd,那么 ls -a 怎么会在 cwd 上工作?
  • 那是……奇怪。你有minimal reproducible example吗?
  • @torek,添加了 MWE。
  • 我不确定它会有什么帮助。另一个问题是类似的 git add 不起作用。这是在黑暗中拍摄的。我无法重现您的问题并且感到困惑。这是在您添加最小的工作示例之前。现在很明显这是一个竞争条件。您必须沟通或等待每个子流程。

标签: python python-3.x git git-commit python-3.8


【解决方案1】:

我的 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 仍在运行。根据git init 的工作速度,我们可能会也可能不会在此处找到.git 目录。如果这需要足够长的时间,git init 也可能会完成。幸运的是,我系统上的git initls -a 慢得多,我看到的效果和你一样。 (嗯,我发现.git 目录有时还不存在。)

最后,我们运行git add -Agit init 可能仍在运行,也可能未运行。事实证明,它正在 仍在运行,并且还没有达到将.git 目录建立为 Git 存储库 的程度。所以git add -A 抱怨你观察到的错误。

如果我们在git init 之后添加proc.wait()——理想情况下,我们应该检查返回码,或者在这里简单地使用subprocess.check_callsubprocess.run——问题就消失了。

【讨论】:

    猜你喜欢
    • 2016-05-01
    • 2016-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-11
    • 2015-07-01
    • 1970-01-01
    • 2023-04-05
    相关资源
    最近更新 更多