Better git support.

This commit is contained in:
Jacobi Carter 2012-02-15 01:23:29 -06:00
parent 81c1244557
commit 3e023c77ce
3 changed files with 125 additions and 3 deletions

View file

@ -25,7 +25,8 @@
untrack-ignored = !git-untracked-ignored
info = !git-info
amend = commit --amend -C HEAD
r = log --graph --oneline --all -40 --decorate=short
r = "!_(){ git log --graph --oneline --all -40 --decorate=short --color=always $@ | php -r '$log = Array(); foreach( explode( \"\\n\", trim( shell_exec( \"git log --all --pretty=format:\\\"%h %Cgreen(%cr) %C(bold blue)<%an>%Creset\\\" --abbrev-commit --date=relative -5000\" ) ) ) as $line ) { list( $commit, $decor ) = explode( \" \", $line, 2 ); $log[ $commit ] = $decor; } foreach( explode( \"\\n\", trim( file_get_contents( \"php://stdin\" ) ) ) as $line ) { if( preg_match( \"#([a-f0-9]{7})#\", $line, $m ) ) { echo $line . \" \" . $log[ $m[ 1 ] ] . \"\\n\"; } else { echo $line . \"\\n\"; } }'; } ; _"
dc = diff --cached
[color]
diff = auto
status = auto

View file

@ -43,7 +43,127 @@ case "${TERM}" in
fi
;;
esac
GREEN=$'%{\e[01;32m%}'
RED=$'%{\e[01;31m%}'
GRAY=$'%{\e[01;30m%}'
PURPLE=$'%{\e[00;35m%}'
YELLOW=$'%{\e[01;33m%}'
function havegit () {
if which git > /dev/null
then
local g="$(git rev-parse --git-dir 2>/dev/null)"
if [ -n "$g" ]
then
true
else
false
fi
else
false
fi
}
ZSH_THEME_GIT_PROMPT_UNTRACKED="?"
ZSH_THEME_GIT_PROMPT_ADDED="+"
ZSH_THEME_GIT_PROMPT_MODIFIED="!"
ZSH_THEME_GIT_PROMPT_DELETED="-"
ZSH_THEME_GIT_PROMPT_UNMERGED="&"
ZSH_THEME_GIT_PROMPT_RENAMED=">"
git_prompt_status() {
INDEX=$(git status --porcelain 2> /dev/null)
STATUS=""
if $(echo "$INDEX" | grep '^?? ' &> /dev/null); then
STATUS="$ZSH_THEME_GIT_PROMPT_UNTRACKED$STATUS"
fi
if $(echo "$INDEX" | grep '^A ' &> /dev/null); then
STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS"
elif $(echo "$INDEX" | grep '^M ' &> /dev/null); then
STATUS="$ZSH_THEME_GIT_PROMPT_ADDED$STATUS"
fi
if $(echo "$INDEX" | grep '^ M ' &> /dev/null); then
STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
elif $(echo "$INDEX" | grep '^AM ' &> /dev/null); then
STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
elif $(echo "$INDEX" | grep '^ T ' &> /dev/null); then
STATUS="$ZSH_THEME_GIT_PROMPT_MODIFIED$STATUS"
fi
if $(echo "$INDEX" | grep '^R ' &> /dev/null); then
STATUS="$ZSH_THEME_GIT_PROMPT_RENAMED$STATUS"
fi
if $(echo "$INDEX" | grep '^ D ' &> /dev/null); then
STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
elif $(echo "$INDEX" | grep '^AD ' &> /dev/null); then
STATUS="$ZSH_THEME_GIT_PROMPT_DELETED$STATUS"
fi
if $(echo "$INDEX" | grep '^UU ' &> /dev/null); then
STATUS="$ZSH_THEME_GIT_PROMPT_UNMERGED$STATUS"
fi
echo $STATUS
}
function updateprompt () {
if havegit ; then
local gitdir="$(readlink -m $(git rev-parse --git-dir 2>/dev/null))"
local repodir="$(dirname "${gitdir}")"
local reponame="$(basename "${repodir}")"
local curdir="$(pwd)"
local repopath=".${curdir#${repodir}}"
local additional
local branch
if [ -d "$gitdir/rebase-apply" ]; then
if test -f "$gitdir/rebase-apply/rebasing"; then
additional="|${PURPLE}REBASE"
elif test -f "$gitdir/rebase-apply/applying"; then
additional="|${PURPLE}AM"
else
additional="|${PURPLE}AM/REBASE"
fi
branch="$(git symbolic-ref HEAD 2>/dev/null)"
elif [ -f "$gitdir/rebase-merge/interactive" ]; then
additional="|${PURPLE}REBASE-i"
branch="$(cat "$gitdir/rebase-merge/head-name")"
elif [ -d "$gitdir/rebase-merge" ]; then
additional="|${PURPLE}REBASE-m"
branch="$(cat "$gitdir/rebase-merge/head-name")"
elif [ -f "$gitdir/MERGE_HEAD" ]; then
additional="|${PURPLE}MERGING"
branch="$(git symbolic-ref HEAD 2>/dev/null)"
else
if [ -f "$gitdir/BISECT_LOG" ]; then
additional="|${PURPLE}BISECTING"
fi
if ! branch="$(git symbolic-ref HEAD 2>/dev/null)"; then
if ! branch="$(git describe --exact-match HEAD 2>/dev/null)"; then
branch="$(cut -c1-7 "$gitdir/HEAD")..."
fi
fi
fi
branch="${branch##refs/heads/}"
local remote=${$(git rev-parse --verify ${branch}@{upstream} --symbolic-full-name 2>/dev/null)/refs\/remotes\/}
local remotepart=""
local -a gitstatus
if [[ -n ${remote} ]] ; then
# for git prior to 1.7
# ahead=$(git rev-list origin/${hook_com[branch]}..HEAD | wc -l)
local ahead=$(git rev-list ${branch}@{upstream}..HEAD 2>/dev/null | wc -l)
gitstatus+=( "${GREEN}+${ahead}${GRAY}" )
PROMPT=${RESETCOLOR}${HOSTCOLOR}$'%n@'${LHOSTNAME}${BLACKCOLOR}':'${PATHCOLOR}$'%45<...<%~ %(?..'${BOLDERRORCOLOR}$')%(!.#.$) '${RESETCOLOR}
# for git prior to 1.7
# behind=$(git rev-list HEAD..origin/${hook_com[branch]} | wc -l)
local behind=$(git rev-list HEAD..${branch}@{upstream} 2>/dev/null | wc -l)
gitstatus+=( "${RED}-${behind}${GRAY}" )
RPROMPT=${RESETCOLOR}$'%1(j.'${JOBCOLOR}$'[%j].)%(?..'${ERRORCOLOR}$'[%?])'${CLOCKCOLOR}$'[%t]'${RESETCOLOR}
remotepart="${RESETCOLOR}${GRAY}[${remote} ${(j:/:)gitstatus}]${RESETCOLOR}"
fi
local stashpart=""
if [[ -s ${gitdir}/refs/stash ]] ; then
stashes=$(git stash list 2>/dev/null | wc -l)
stashpart="${RESETCOLOR}${GRAY}(${GREEN}${stashes}${GRAY} stashed)${RESETCOLOR}"
fi
local stats="$(git_prompt_status)"
PROMPT=${RESETCOLOR}${HOSTCOLOR}$'%n@'${LHOSTNAME}${BLACKCOLOR}':'${PATHCOLOR}"${reponame} ${RED}${branch}${YELLOW}${stats}${BLACKCOLOR}${additional} ${PATHCOLOR}"$'%45<...<'"${repopath}"' %(?..'${BOLDERRORCOLOR}$')%(!.#.$) '${RESETCOLOR}
RPROMPT=${RESETCOLOR}${stashpart}${remotepart}$'%1(j.'${JOBCOLOR}$'[%j].)%(?..'${ERRORCOLOR}$'[%?])'${CLOCKCOLOR}$'[%t]'${RESETCOLOR}
else
PROMPT=${RESETCOLOR}${HOSTCOLOR}$'%n@'${LHOSTNAME}${BLACKCOLOR}':'${PATHCOLOR}$'%45<...<%~ %(?..'${BOLDERRORCOLOR}$')%(!.#.$) '${RESETCOLOR}
RPROMPT=${RESETCOLOR}$'%1(j.'${JOBCOLOR}$'[%j].)%(?..'${ERRORCOLOR}$'[%?])'${CLOCKCOLOR}$'[%t]'${RESETCOLOR}
fi
}
updateprompt

View file

@ -1,6 +1,7 @@
# terminal titles
precmd()
{
updateprompt
local termtitle
termtitle=`print -P "%n@%m[%l]"`