The git rev-parse
command returns 0 if the current directory is in a git repo and an error code otherwise.
It is thus necessary to call the bash command, suppressing stdout and stderr, in order to get the return code.
import os, subprocess
def is_git_repo():
"Check whether this is a git repo"
# we use the exit code
with open(os.devnull, "w") as d:
return not bool(subprocess.call('git rev-parse', shell=True,
stdout=d, stderr=d))