How can I tell if a given directory is part of a git respository?
(The following is in python, but bash or something would be fine.)
os.path.isdir(\'.svn
Using git rev-parse --is-inside-work-tree
along with subprocess.Popen
, you can check if "true" is printed from the output indicating the directory does have a git repo:
import subprocess
repo_dir = "../path/to/check/"
command = ['git', 'rev-parse', '--is-inside-work-tree']
process = subprocess.Popen(command, stdout=subprocess.PIPE, cwd=repo_dir,
universal_newlines=True)
process_output = process.communicate()[0]
is_git_repo = str(process_output.strip())
if is_git_repo == "true":
print("success! git repo found under {0}".format(repo_dir))
else:
print("sorry. no git repo found under {0}".format(repo_dir))