Determine if directory is under git control

后端 未结 10 1372
醉酒成梦
醉酒成梦 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 12:55

    In ruby, system('git rev-parse') will return true if the current directory is in a git repo, and false otherwise. I imagine the pythonic equivalent should work similarly.

    EDIT: Sure enough:

    # in a git repo, running ipython
    >>> system('git rev-parse')
    0
    
    # not in a git repo
    >>> system('git rev-parse')
    32768
    

    Note that there is some output on STDERR when you aren't in a repo, if that matters to you.

提交回复
热议问题