gpt4 book ai didi

python - 无法使用 python 自动执行 git fetch && git checkout

转载 作者:太空宇宙 更新时间:2023-11-03 17:00:53 24 4
gpt4 key购买 nike

我已经自动化了 git checkout 用户想要输入的特定分支的过程。我正在使用以下带有子进程的 python 代码来执行此操作,

from bitbucket.bitbucket import Bitbucket
from sys import argv
import subprocess

prompt = '> '

print "Enter the name of branch you need to clone: "
user_branch = raw_input(prompt)

print "You Entered: ",user_branch


print "this will do a git status"
cmd = subprocess.Popen(["git", "status"], stdout=subprocess.PIPE)
output = cmd.communicate()[0]
print output
#for line in output: # output.split("\n"):
if ("Untracked files:" or "Changes not staged for commit") in output:
print "Need to do a Git stash"
subprocess.Popen(["git", "stash"])
subprocess.Popen(["git fetch && git checkout"+(user_branch)])
else:
print "simply do git checkout userbranch"
subprocess.Popen(["git","pull"])
subprocess.Popen(["git fetch && git checkout"+(user_branch)])

这会抛出:

Enter the name of branch you need to clone: 
> release_4_0
You Entered: release_4_0
this will do a git status
On branch master
Your branch is up-to-date with 'origin/master'.

nothing to commit, working directory clean

simply do git checkout userbranch
Traceback (most recent call last):
File "git_updater.py", line 25, in <module>
subprocess.Popen(["git fetch && git checkout"+(user_branch)])
File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

我只想输入用户想要 checkout 的分支名称。这有道理吗?

最佳答案

subprocess.Popen(["git fetch && git checkout"+(user_branch)])

应该是

subprocess.Popen("git fetch && git checkout %s"%user_branch,shell=True)

或者你可以做类似的事情

subprocess.Popen(["git", "fetch"]).communicate()
subprocess.Popen(["git", "checkout", user_branch])

问题是,如果参数是一个列表,它需要逗号分隔的文件和参数(我不确定 && 将如何工作)

关于python - 无法使用 python 自动执行 git fetch && git checkout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34999800/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com