git 判断路径是否是 git 仓库

git 判断路径是否是 git 仓库

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

参考链接:https://stackoverflow.com/questions/2044574/determine-if-directory-is-under-git-control

原文地址:https://www.cnblogs.com/ibingshan/p/11040467.html