tags:

views:

258

answers:

5

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?

+3  A: 

Well, the directory can also be ignored by the .gitignore file - so you need to check for a .git repository, and if there is one, parse the .gitignore to see whether that directory is indeed in the git repository.

What exactly do you want to do? There may be a simpler way to do this.

EDIT: Do you mean "Is this directory the root of a GIT repository" or, do you mean "Is this directory part of a GIT repository" ?

For the first one, then just check if there is a .git -- since that's at the root, and you're done. For the second one, once you've determined that you're inside a GIT repository, you need to check .gitignore for the subdirectory in question.

Trevoke
I've got a python tool that wraps various version control programs I use, so I can get status, e.g., independent of what VC is use. Git is a new addition for me. I don't need to be so robust as to consider .gitignore, really.
Grumdrig
The latter. [15 characters :]
Grumdrig
Staying consistent to your current method is probably the best (unless of course, your tool does something different for each VCS).It's probably, in the long run, simpler to stick to the directory traversal system to figure out what is what, and then go to custom actions, if necessary.
Trevoke
+4  A: 

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.

John Hyland
Thanks! I'm going with `os.path.isdir(".git") or os.system('git rev-parse 2> /dev/null > /dev/null') == 0`
Grumdrig
A: 

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.

roe
+6  A: 

Just found this in git help rev-parse

git rev-parse --is-inside-work-tree

prints true if it is in the work tree, false if it's in the '.git' tree, and fatal error if it's neither. Both 'true' and 'false' are printed on stdout with an exit status of 0, the fatal error is printed on stderr with an exit status of 128.

roe
+1  A: 

Add this to your .bash_profile, and your prompt will always show the active git branch and whether you have uncommitted changes.

function parse_git_dirty {
  [[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "*"
}
function parse_git_branch {
  git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/[\1$(parse_git_dirty)]/"
}

export PS1=' \[\033[0;33m\]\w\[\033[00m\]\[\033[01;00m\]$(parse_git_branch): ' #PS1='\w> '

You'll see this:

 ~: 
 ~: cd code/scala-plugin/
 ~/code/scala-plugin[master*]: 
retronym
Don't use porcelain (user interface) commands in scripts: they output can change. Use `git rev-parse` and `git symbolic-ref` instead. See also `__git_ps1` function in ' contrib/completion/git-completion.bash'
Jakub Narębski