Unable to automate git fetch && git checkout using python
Question: I have automated the process of git checkout a particular branch which a user
want to enter. I am using the following python code with subprocess to do so ,

    
    
    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)])
    

This throws :

    
    
    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
    

I want to input just the branch name that user wants to checkout . Does this
makes sense ?


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

should be

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

or you could do somehting like

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

the problem is if the argument is a list it expects comma separated file and
arguments (and im not sure how the && would work)

