pau*_*ulo 12 python git subprocess
本质上,我试图使用子进程调用来签出特定 sha 哈希处的 git 提交。
但是,我不断收到错误subprocess.CalledProcessError: Command '['git', 'checkout', '62bbce43e']' returned non-zero exit status 128.
这是我的代码如下:
with open(filename) as inputfile:
reader = csv.reader(inputfile, delimiter=",")
linecount = 0
for row in reader:
if linecount == 0:
linecount += 1
else:
repo = str(row[0])
sha = str(row[2])
specificfile = str(row[3])
linenum = int(row[4])
cl("cd", repo)
subprocess.check_output(['git', 'checkout', sha])
print("checkout done")
git("checkout", "-")
Run Code Online (Sandbox Code Playgroud)
调用subprocess.check_output()
实际上返回输出(您也可以通过传递参数来获取错误输出stderr
)。您可能想看看它是否给您一个错误解释发生了什么。
由于您收到异常(意味着调用未完成,因此可能无法返回输出),因此您应该能够从异常成员之一获取输出:
try:
output = subprocess.check_output(['git', 'checkout', sha], stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print("Exception on process, rc=", e.returncode, "output=", e.output)
Run Code Online (Sandbox Code Playgroud)
我确实知道的一件事是,如果您实际上不在Git 存储库中git
,某些命令往往会返回 128 。所以我会查看你的路线之后的路径,其中:cl("cd", repo)
os.system("pwd") # use "cd" for Windows.
Run Code Online (Sandbox Code Playgroud)
如果您的进程在子进程cd
中运行,则不会影响当前进程,因此您可能根本不必在 Git 存储库中。这肯定可以解释返回码。128
举例来说,以下记录显示了当我尝试git
在存储库之外运行命令时会发生什么:
>>> try:
... output = subprocess.check_output(['git', 'checkout', '12345'])
... except subprocess.CalledProcessError as e:
... print(e.returncode, e.output)
...
128 b'fatal: not a git repository (or any of the parent directories): .git\n'
Run Code Online (Sandbox Code Playgroud)
如果事实证明您位于错误的目录中(即,该cl("cd", repo)
语句正在运行子进程来更改目录),则应该使用Python-blessed方法来更改目录(a):
import os
os.chdir(path)
Run Code Online (Sandbox Code Playgroud)
这实际上更改了直接进程(Python 解释器)的目录,而不是瞬态子进程。
(a)这实际上是一个很好的建议——尽可能多地使用 Python 特定的东西(因为它主要是跨平台的),而不是生成一个子 shell(本质上是特定于平台的)。
归档时间: |
|
查看次数: |
39027 次 |
最近记录: |