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:
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
out, error = p.communicate()
if not error:
print "Stopping execution!\r"
['git', 'checkout', 'master']
"Your branch is up-to-date with 'origin/master'.\n"
"Switched to branch 'master'\n"
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
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)