Georg P.
Georg P.

Reputation: 3164

zsh: Find out VCS of current directory

I'd like to create a zsh widget to perform common tasks of a version control systems with a single button. E.g. pressing F1 should call "svn status", if the current directory is part of a Subversion checkout. If it is in a git repository, it should call "git status -s".

Now, creating the widget is no big deal. But how do I determine which VCS is in the current directory?

I know about vcs_info and I use a lot. But I couldn't find any way to retrieve the most basic information, it provides. Any ideas?

Upvotes: 1

Views: 172

Answers (2)

Georg P.
Georg P.

Reputation: 3164

Thanks to all for comments. My solution now looks like this:

vcs-status() {
   setopt no_autopushd
   cmd=""
   cur_dir=`pwd`
   while [[ `pwd` != "/" ]]; do
      if [[ -d .svn ]]; then
         cmd="svn status^M"
         break
      elif [[ -d .git ]]; then
         cmd="git status -s^M"
         break
      fi
      cd ..
   done
   cd $cur_dir
   if [[ -n $cmd ]]; then
      zle -U $cmd
   fi
   setopt autopushd
}
zle -N vcs-status
bindkey "\eOP"  vcs-status

Suggestions to cool zsh'ish improvements are welcome :-)

I also implemented the suggestion of Thomas, using svn info and git rev-parse. But then I noticed a problem: If I have an SVN working copy checked out somewhere inside a GIT working copy (yes, this is necessary occationally), or vice versa, I would only find the first one I'm checking. With the solution above, I find the "inner-most", which is what I want. In fact, this is a problem that also has been annoying me about vcs_info for a while.

Upvotes: 0

Jens
Jens

Reputation: 72697

What about testing the existence of the specific meta data directories?

 if test -d CVS; then
    # CVS
 elif test -d .hg; then
    # Mercurial
 elif test -d .git; then
    # Git
 elif test -d .svn; then
    # Subversion
 else
    # unknown
 fi

Upvotes: 2

Related Questions