无法自动执行git获取&&;使用python进行git签出
我已经自动化了git签出用户想要进入的特定分支的过程。我使用下面的python代码和子流程来实现这一点无法自动执行git获取&&;使用python进行git签出,python,git,bitbucket,Python,Git,Bitbucket,我已经自动化了git签出用户想要进入的特定分支的过程。我使用下面的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 E
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
输入需要克隆的分支的名称:
>发布4\u 0
您已输入:release_4_0
这将做一个git状态
论分行行长
您的分支机构是最新的“原始/主”分支机构。
没有要提交的内容,正在清理目录
只需执行git签出userbranch即可
回溯(最近一次呼叫最后一次):
文件“git_updater.py”,第25行,在
subprocess.Popen([“git fetch&&git checkout”+(用户分支)])
文件“/usr/lib/python2.7/subprocess.py”,第710行,在__
错误读取,错误写入)
文件“/usr/lib/python2.7/subprocess.py”,第1327行,在执行子进程中
引发子对象异常
OSError:[Errno 2]没有这样的文件或目录
我只想输入用户想要签出的分支名称。这有意义吗
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])
问题是,如果参数是一个列表,它需要逗号分隔的文件和参数(我不确定&&将如何工作)这是一种类型,mybad。。更新问题
&&
将不起作用&&
是一个shell,默认情况下,子流程
在不调用shell的情况下运行进程。您可以将shell=True
添加到Popen()
构造函数中,也可以在Python中执行相同的操作(从git fetch
中检索返回代码,如果返回代码为零,则只调用git checkout
,即使用两个子进程调用),第25行,在subprocess.Popen(“git fetch&&git checkout%s”%user\u branch)文件/usr/lib/python2.7/subprocess.py”中,第710行,在init errread,errwrite)文件/usr/lib/python2.7/subprocess.py中,第1327行,在执行子进程引发子进程异常OSError:[Errno 2]没有这样的文件或目录尝试“或”OptionTanks伙计:)可以用或类似的东西@kindall很棒的解释