当我跑的时候
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).发布于 2021-02-11 01:52:15
我的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,这实际上是相对较长的,因为只有在ls -a完成时才会发生EOF -同时,git init仍然在运行。根据git init的工作速度,我们可能在这里找到.git目录,也可能找不到。如果这需要足够长的时间,git init也可能完成。幸运的是,我的系统上的git init比ls -a慢得多,因此我看到了与您相同的效果。(我发现.git目录有时还不存在。)
最后,我们运行git add -A。git init可能仍在运行,也可能不在运行。事实证明,它仍然在运行,而且还不足以将.git目录建立为Git存储库。因此,git add -A抱怨说,您观察到的错误。
如果我们将proc.wait()添加到git init-ideally之后,我们应该检查返回代码,或者在这里简单地使用subprocess.check_call或subprocess.run --问题就解决了。
https://stackoverflow.com/questions/66146181
复制相似问题