現在のディレクトリがgitの管理下にあるかどうか判定する方法を思いついたので、 walf443さんの方法 を改良してみました。 こんな感じに、gitで管理されてないディレクトリではブランチ名を表示しなくなります。

ss

実際のzshrcは以下の通り。

   1  _set_env_git_current_branch() {
   2    GIT_CURRENT_BRANCH=$( git branch &> /dev/null | grep '^\*' | cut -b 3- )
   3  }
   4  
   5  _update_rprompt () {
   6    if [ "`git ls-files 2>/dev/null`" ]; then
   7      RPROMPT="[%~:$GIT_CURRENT_BRANCH]"
   8    else
   9      RPROMPT="[%~]"
  10    fi
  11  } 
  12    
  13  precmd() 
  14  { 
  15    _set_env_git_current_branch
  16    _update_rprompt
  17  }
  18  
  19  chpwd()
  20  {
  21    _set_env_git_current_branch
  22    _update_rprompt
  23  }

git ls-filesがgitの管理下以外では何も返さない事を利用しています。

追記

  • 2>/dev/null が抜けていたので追加しました。
posted by Png genki on Fri 26 Dec 2008 at 16:38

Comments:

or Preview
Social Bookmarks
  • Delicious
  • B_entry1159
  • Clip_16_12_w
Services from s21g
twpro(ツイプロ)
Twitterプロフィールを快適検索
地価2009
土地の値段を調べてみよう
MyRestaurant
自分だけのレストラン手帳
Formula
ブログに数式を埋め込める数式コミュニティ