tags:

views:

278

answers:

6

I am writing a series of script for zsh/git management.

How do I check if the current directory is a git repo? (When I'm not in a git repo, I don't wnat to execute a bunch of commands and get a bunch of 'fatal, not git repo' responses).

Thanks!

+2  A: 

A very fast option is http://vc.gerg.ca/hg/vcprompt/ .. multi-VC, in C, no dependencies other than itself.

peritus
+1  A: 

Copied from the bash completion file the following is a naive way to do it

# Copyright (C) 2006,2007 Shawn O. Pearce <[email protected]>
# Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
# Distributed under the GNU General Public License, version 2.0.

...

if [ -d .git ]; the
  echo .git;
else
  git rev-parse --git-dir 2> /dev/null;
fi;

You could either wrap that in a function or use it in a script.

Condensed into a one line condition suitable for bash or zsh

[ -d .git ] || git rev-parse --git-dir > /dev/null 2>&1
jabbie
Why not just use git rev-parse?
William Pursell
@William Pursell Why fork when you don't need to? Mainly for speed in the trivial case.
jabbie
A: 

Not sure if there is a publicly accessible/documented way to do this (there are some internal git functions which you can use/abuse in the git source itself)

You could do something like;

if ! git ls-files >& /dev/null; then
  echo "not in git"
fi
James
A: 
William Pursell
A: 

You might want to have a look at these zsh functions. I use some of those to create a fancy zsh prompt when I'm in a working directory of a git repository. Note that the script is not only useful for prompts: it offers generic functions like zgit_hasuntracked().

paprika
A: 

Have you checked functions already in zsh distribution? See this blog entry about vcs_info in zsh.

MBO