Determine if directory is under git control

后端 未结 10 1554
醉酒成梦
醉酒成梦 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:06

    For the record, use git status or similar, this is just for completeness: :)

    Searching upward a tree is no biggie really, in bash you can do this simple one-liner (if you put it on one line...) ;) Returns 0 if one is found, 1 otherwise.

    d=`pwd`
    while [ "$d" != "" ]; do
      [ -d "$d"/.git ] && exit 0
      d=${d%/*}
    done
    exit 1
    

    will search upward looking for a .git folder.

提交回复
热议问题