Clemenz Clemenz - 16 days ago 8
Git Question

Python subprocess git checkout returns error even if there is no error

I am writing a script to automate some GIT tasks. I know about GITPython but I prefer not using that. Even tough it may be easier.

I got the following piece of code:

def executeCommand(cmd):
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
out, error = p.communicate()
if not error:
return out.strip()
else:
print repr(out)
print repr(error)
print "Stopping execution!\r"
exit(0)


This piece of code given the command
['git', 'checkout', 'master']
does indeed checkout the master. BUT it will stop executing, apparently because the error variable is not empty. I tried to print the outputs on error which is the following:

"Your branch is up-to-date with 'origin/master'.\n"
"Switched to branch 'master'\n"
Stopping execution!


As you can see, it succesfully checked out the master branch (Also verified by
git status
). But why is the second line filled in the error variable? How to handle this? Or is there no way of correctly handling this? Or should i not do any error checking? What is the best solution/explaination for this?

Thanks!

Answer

Its not an error ! its the output on stderr :)

If you are on a linux terminal, you can test this by running the following

git checkout master 2>/dev/null

# You shouldn't see any output. 
# This is because git is writing those messages to stderr.

According to subprocess docs

communicate() returns a tuple (stdoutdata, stderrdata)

i.e. the output on stdout and stderr.

If you want to check for errors, you should use the returncode attribute.

in your case

stdout, stderr = p.communicate()
if p.returncode == 0:
    return stdout.strip()

else:
    # handle error
    print repr(stdout)
    print repr(stderr)
    print "stopping execution!\r"
    exit(0)
Comments