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

posted @ 2019-06-17 16:29  ibingshan  阅读(2045)  评论(0编辑  收藏  举报