确定目录是否在git控制之下 [英] Determine if directory is under git control
问题描述
我怎么知道给定的目录是否是git respository的一部分?
(以下是python,但是bash或其他东西都可以。)
os.path.isdir('。svn')
会告诉你当前目录是否受Subversion控制。 Mercurial和Git的仓库顶部只有一个.hg / .git,所以对于 hg
我可以使用
os.system('hg -q stat 2> / dev / null> / dev / null')== 0)
但 git status
如果没有更改,则返回非零(错误)退出状态。 b
$ b
正在迭代寻找 .git
的路径,我自己可以做的最好?
在ruby中,如果当前目录在git中, system('git rev-parse')
将返回true回购,否则为假。我想象pythonic等价物应该是相似的。
编辑:果然:
#在一个git仓库中运行ipython
>>>系统('git rev-parse')
0
#不在git repo中
>>>系统('git rev-parse')
32768
请注意, STDERR,当你不在回购中时,如果这对你很重要。
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')
will tell you if the current directory is controlled by Subversion. Mercurial and Git just have a .hg/.git at the top of the repository, so for hg
I can use
os.system('hg -q stat 2> /dev/null > /dev/null') == 0)
but git status
returns a nonzero (error) exit status if nothing's changed.
Is iterating up the path looking for .git
myself the best I can do?
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.
这篇关于确定目录是否在git控制之下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!