LoginSignup
42
40

More than 5 years have passed since last update.

ブランチ名 + 作業状態 + stash数 をzshのプロンプトに表示

Last updated at Posted at 2012-06-13

uasiさんの ZshでGitのカレントブランチを右プロンプトに表示。コミット済みのきれいな状態だと緑色、未コミットの編集がある場合は赤色で表示される。 を愛用されている方は多いと思います。これを使うと

  • 作業ブランチ
  • 未コミットファイルの有無
  • mergeやrebaseの途中であるか

が分かりますが、僕はこれに加えてstashの数が分かるようにしています。方法は zsh で git の stash の数出すようにしたら便利だった。 を見て知りました。

最終的な .zshrc の設定は以下のようになります。

# git stash count
function git_prompt_stash_count {
  local COUNT=$(git stash list 2>/dev/null | wc -l | tr -d ' ')
  if [ "$COUNT" -gt 0 ]; then
    echo " ($COUNT)"
  fi
}

setopt prompt_subst
autoload -Uz VCS_INFO_get_data_git; VCS_INFO_get_data_git 2> /dev/null

function rprompt-git-current-branch {
  local name st color action

  if [[ "$PWD" =~ '/\.git(/.*)?$' ]]; then
    return
  fi

  name=$(basename "`git symbolic-ref HEAD 2> /dev/null`")
  if [[ -z $name ]]; then
    return
  fi

  st=`git status 2> /dev/null`
  if [[ -n `echo "$st" | grep "^nothing to"` ]]; then
    color=${fg[blue]}
  elif [[ -n `echo "$st" | grep "^nothing added"` ]]; then
    color=${fg[yellow]}
  elif [[ -n `echo "$st" | grep "^# Untracked"` ]]; then
    color=${fg_bold[red]}
  else
    color=${fg[red]}
  fi

  gitdir=`git rev-parse --git-dir 2> /dev/null`
  action=`VCS_INFO_git_getaction "$gitdir"` && action="($action)"

  # %{...%} surrounds escape string
  echo "%{$color%}$name$action`git_prompt_stash_count`$color%{$reset_color%}"
}

# how to use
PROMPT='`rprompt-git-current-branch`'
42
40
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
42
40