使用Python的子进程将git repo推送到GitHub返回退出代码128
我目前正在尝试使用git编写我自己的模块。出于这个原因,我想避免使用GitPython。使用Python的子进程将git repo推送到GitHub返回退出代码128
我在此刻推代码如下:
class Git(object):
def __call_git(self,*args):
a = [self.path+"git"] + list(args)
return subprocess.check_output(a,stdin=subprocess.PIPE)
def push(self,username,password,remote="origin",branch="master"):
url = self.__call_git("remote","get-url",remote).replace("\n","")
url = "".join(["https://",username,":",password,"@",url[8:]])
output = self.__call_git("push","--repo",url,branch)
return output
这将尝试通过执行命令git push --repo https://username:[email protected]/repo.git
以避免与标准输入合作,通过用户名和密码的Git推向饭桶。这个工程在命令行上,但每当我尝试运行此代码,我得到以下输出:
Traceback (most recent call last):
File "<pyshell#81>", line 1, in <module>
g.push("username","password")
File "/home/git.py", line 68, in push
output = self.__call_git("push","--repo",url,branch)
File "/home/git.py", line 37, in __call_git
return subprocess.check_output(a,stdin=subprocess.PIPE)
File "/usr/lib/python2.7/subprocess.py", line 574, in check_output
raise CalledProcessError(retcode, cmd, output=output)
CalledProcessError: Command '['git', 'push', '--repo', 'https://username:[email protected]/repo.git', 'master']' returned non-zero exit status 128
的Git似乎反应这样每当push
命令参与,即使是像git push
。使用类似subprocess.Popen
和设置stdin=PIPE,stdout=PIPE,stderr=PIPE
,process.poll()
仍然给128和process.stdout.read()
什么也没有。
任何人都可以解释为什么会发生这种情况,并提供解决我的问题?
---编辑---
在从here一些建议,我已经跑了一堆Git命令与GIT_TRACE=1
。我已经看到git push
是我目前使用的唯一一条有trace: run_command
行的命令。
19:57:21.694468 run-command.c:334 trace: run_command: 'git-remote-https'
这让我怀疑原因可能在于上述链接中的其他内容。
即使在环境条件下,我也无法从子流程运行
git push origin master
。我认为它与由lfs预推钩创建的竞争条件有关。子进程调用git push
,git push
调用git lfs
,git push
在lfs或lfs杀死stdout/return代码之前结束。 Git push通常在命令行中正常工作,所以人们会认为通过suprocess调用它可以起到相同的作用,因为模块会将相同的命令发送到shell环境。
虽然这个问题并不在于lfs对我来说,也许它存在于对git-remote-https
的调用中?
当我已经与使用子我不得不添加shell=True
到我的子功能call.In的情况下运行git问题:
subprocess.check_output(...other args, shell=True)
您可能需要阅读中的安全注意事项在你做这个之前,在子流程文档中。
我相信这只是使空间分离的工作 - 它只会让我从列表切换到字符串的参数。 –
你是对的,从列表切换到字符串解决了我的一些问题。不过,我正在编写一个内部工具,用于控制传递给子进程的命令。诚然,这可能不适合你。抱歉,我无法提供更多帮助! – rahvin74
128是git的一个非常常见的错误代码,并可能意味着一切。您应该抓取流程的输出并打印出来。 –
@KlausD。我试过了,根本没有输出。即使使用'subprocess.Popen'和设置'stdin = PIPE,stdout = PIPE,stderr = PIPE','process.poll()'给出128和'process.stdout.read()'什么都不给。我会将这些信息添加到问题中。 –
尝试'subprocess.check_output()'。 –