#! /usr/bin/zsh # Those can be used, for instance to construct the prompt: __ZSH_GIT_BASEDIR="" __ZSH_GIT_SUBDIR="" __ZSH_GIT_BRANCH="" __ZSH_GIT_ACTION="" __ZSH_GIT_STATUS="" __ZSH_GIT_VARS_INVALID=1 __ZSH_GIT_STATUS_INVALID=1 git_chpwd() { # On cd, invalidate git status in prompt __ZSH_GIT_VARS_INVALID=1 __ZSH_GIT_BASEDIR="" } git_preexec() { # On git command, invalidate git status in prompt case "$1" in git*) __ZSH_GIT_VARS_INVALID=1 ;; esac # *any* command could invalidate the repository status (new file, ...) __ZSH_GIT_STATUS_INVALID=1 } git_get_status() { # Return only git status local gitstat gitstatus gitstat=$(LANG=C git status 2> /dev/null | grep '\(Untracked\|Changes\|Changed but not updated:\)') # 'fix for mcedit parser gitstatus="" if [[ $(echo ${gitstat} | grep -c "^Changes to be committed:$") > 0 ]]; then gitstatus='✚' fi if [[ $(echo ${gitstat} | grep -c "^Changed but not updated:$") > 0 || \ $(echo ${gitstat} | grep -c "^Changes not staged for commit:$") > 0 ]]; then gitstatus="${gitstatus}✹" fi if [[ $(echo ${gitstat} | grep -c "^Untracked files:$") > 0 ]]; then gitstatus="${gitstatus}★" fi if [[ -z $gitstatus ]]; then gitstatus="%{${fg_bold[green]}%}✔%{$reset_color%}" else gitstatus="%{${fg_bold[yellow]}%}$gitstatus%{$reset_color%}" fi echo $gitstatus } git_parse() { local git_dir ref base_dir sub_dir action branch gitstat gitstatus # If nothing has been invalidated if [[ "${__ZSH_GIT_VARS_INVALID}" == "0" && "${__ZSH_GIT_STATUS_INVALID}" == "0" ]]; then return fi # If only status has been invalidated if [[ "${__ZSH_GIT_VARS_INVALID}" == "0" && "${__ZSH_GIT_STATUS_INVALID}" == "1" ]]; then __ZSH_GIT_STATUS=$(git_get_status) __ZSH_GIT_STATUS_INVALID=0 return fi # Git prompt variables are invalid. Update them. git_dir=$(git rev-parse --git-dir 2> /dev/null) || return if [[ "$(git rev-parse --is-bare-repository)" == "true" ]]; then base_dir=${$(readlink -f "$git_dir")/$HOME/'~'} sub_dir=${$(pwd)#$(readlink -f "$git_dir")} else base_dir=${$(readlink -f "$git_dir/..")/$HOME/'~'} sub_dir=${$(pwd)#$(readlink -f "$git_dir/..")} fi sub_dir=${sub_dir#/} ref=$(git symbolic-ref HEAD 2> /dev/null) || return action="" if [ -d "$git_dir/../.dotest" ]; then if [ -f "$git_dir/../.dotest/rebasing" ]; then action="-rebase" elif [ -f "$git_dir/../.dotest/applying" ]; then action="-am" else action="-am-rebase" fi branch="$ref" elif [ -f "$git_dir/.dotest-merge/interactive" ]; then action="-rebase-i" branch="$(cat "$git_dir/.dotest-merge/head-name")" elif [ -d "$git_dir/.dotest-merge" ]; then action="-rebase-m" branch="$(cat "$git_dir/.dotest-merge/head-name")" elif [ -f "$git_dir/MERGE_HEAD" ]; then action="-merge" branch="$ref" else test -f "$git_dir/BISECT_LOG" && action="-bisect" branch="$(git symbolic-ref HEAD 2>/dev/null)" || \ branch="$(git describe --exact-match HEAD 2>/dev/null)" || \ branch="$(cut -c1-7 "$git_dir/HEAD")..." fi __ZSH_GIT_BASEDIR="${base_dir}" __ZSH_GIT_SUBDIR="${sub_dir}" __ZSH_GIT_BRANCH="${branch#refs/heads/}" __ZSH_GIT_ACTION="${action}" __ZSH_GIT_STATUS=`git_get_status` __ZSH_GIT_VARS_INVALID=0 __ZSH_GIT_STATUS_INVALID=0 } chpwd_functions+='git_chpwd' preexec_functions+='git_preexec' precmd_functions+='git_parse'