Determine if directory is under git control

后端 未结 10 1359
醉酒成梦
醉酒成梦 2021-01-30 12:25

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         


        
10条回答
  •  隐瞒了意图╮
    2021-01-30 13:09

    With gitpython, you can make a function like this:

    import git
    
    ...
    
    def is_git_repo(path):
        try:
            _ = git.Repo(path).git_dir
            return True
        except git.exc.InvalidGitRepositoryError:
            return False
    

提交回复
热议问题