------- .git-prompt.sh --------
Copyright (C) 2006,2007 Shawn O. Pearce [email protected]
__git_printf_supports_v= printf -v __git_printf_supports_v -- '%s' yes >/dev/null 2>&1
__git_ps1_show_upstream () { local key value local svn_remote svn_url_pattern count n local upstream=git legacy="" verbose=""
svn_remote=()
local output="$(git config -z --get-regexp '^(svn-remote...url|bash.showupstream)$' 2>/dev/null | tr '\0\n' '\n ')" while read -r key value; do case "$key" in bash.showupstream) GIT_PS1_SHOWUPSTREAM="$value" if [[ -z "${GIT_PS1_SHOWUPSTREAM}" ]]; then p="" return fi ;; svn-remote..url) svn_remote[$((${#svn_remote[@]} + 1))]="$value" svn_url_pattern+="\|$value" upstream=svn+git # default upstream is SVN if available, else git ;; esac done <<< "$output"
for option in ${GIT_PS1_SHOWUPSTREAM}; do case "$option" in git|svn) upstream="$option" ;; verbose) verbose=1 ;; legacy) legacy=1 ;; esac done
case "$upstream" in
git) upstream="@{upstream}" ;;
svn*)
# get the upstream from the "git-svn-id: ..." in a commit message
# (git-svn uses essentially the same procedure internally)
local -a svn_upstream
svn_upstream=($(git log --first-parent -1
--grep="^git-svn-id: (
if [[ -z "$svn_upstream" ]]; then
# default branch name for checkouts with no layout:
upstream=${GIT_SVN_ID:-git-svn}
else
upstream=${svn_upstream#/}
fi
elif [[ "svn+git" = "$upstream" ]]; then
upstream="@{upstream}"
fi
;;
esac
if [[ -z "$legacy" ]]; then
count="$(git rev-list --count --left-right
"$upstream"...HEAD 2>/dev/null)"
else
# produce equivalent output to --count for older versions of git
local commits
if commits="$(git rev-list --left-right "$upstream"...HEAD 2>/dev/null)"
then
local commit behind=0 ahead=0
for commit in $commits
do
case "$commit" in
"<"*) ((behind++)) ;;
*) ((ahead++)) ;;
esac
done
count="$behind $ahead"
else
count=""
fi
fi
if [[ -z "$verbose" ]]; then case "$count" in "") # no upstream p="" ;; "0 0") # equal to upstream p="=" ;; "0 "*) # ahead of upstream p=">" ;; *" 0") # behind upstream p="<" ;; ) # diverged from upstream p="<>" ;; esac else case "$count" in "") # no upstream p="" ;; "0 0") # equal to upstream p=" u=" ;; "0 ") # ahead of upstream p=" u+${count#0 }" ;; *" 0") # behind upstream p=" u-${count% 0}" ;; ) # diverged from upstream p=" u+${count# }-${count% *}" ;; esac fi
}
__git_ps1_colorize_gitstring () { if [[ -n ${ZSH_VERSION-} ]]; then local c_red='%F{red}' local c_green='%F{green}' local c_lblue='%F{blue}' local c_clear='%f' else # Using [ and ] around colors is necessary to prevent # issues with command line editing/browsing/completion! local c_red='[\e[31m]' local c_green='[\e[32m]' local c_lblue='[\e[1;34m]' local c_clear='[\e[0m]' fi local bad_color=$c_red local ok_color=$c_green local flags_color="$c_lblue"
local branch_color="" if [ $detached = no ]; then branch_color="$ok_color" else branch_color="$bad_color" fi c="$branch_color$c"
z="$c_clear$z" if [ "$w" = "*" ]; then w="$bad_color$w" fi if [ -n "$i" ]; then i="$ok_color$i" fi if [ -n "$s" ]; then s="$flags_color$s" fi if [ -n "$u" ]; then u="$bad_color$u" fi r="$c_clear$r" }
__git_ps1 () { local pcmode=no local detached=no local ps1pc_start='\u@\h:\w ' local ps1pc_end='$ ' local printf_format=' (%s)'
case "$#" in 2|3) pcmode=yes ps1pc_start="$1" ps1pc_end="$2" printf_format="${3:-$printf_format}" ;; 0|1) printf_format="${1:-$printf_format}" ;; *) return ;; esac
local repo_info rev_parse_exit_code
repo_info="$(git rev-parse --git-dir --is-inside-git-dir
--is-bare-repository --is-inside-work-tree
--short HEAD 2>/dev/null)"
rev_parse_exit_code="$?"
if [ -z "$repo_info" ]; then if [ $pcmode = yes ]; then #In PC mode PS1 always needs to be set PS1="$ps1pc_start$ps1pc_end" fi return fi
local short_sha if [ "$rev_parse_exit_code" = "0" ]; then short_sha="${repo_info##$'\n'}" repo_info="${repo_info%$'\n'}" fi local inside_worktree="${repo_info##$'\n'}" repo_info="${repo_info%$'\n'}" local bare_repo="${repo_info##$'\n'}" repo_info="${repo_info%$'\n'}" local inside_gitdir="${repo_info##$'\n'}" local g="${repo_info%$'\n'}"
local r="" local b="" local step="" local total="" if [ -d "$g/rebase-merge" ]; then read b 2>/dev/null <"$g/rebase-merge/head-name" read step 2>/dev/null <"$g/rebase-merge/msgnum" read total 2>/dev/null <"$g/rebase-merge/end" if [ -f "$g/rebase-merge/interactive" ]; then r="|REBASE-i" else r="|REBASE-m" fi else if [ -d "$g/rebase-apply" ]; then read step 2>/dev/null <"$g/rebase-apply/next" read total 2>/dev/null <"$g/rebase-apply/last" if [ -f "$g/rebase-apply/rebasing" ]; then read b 2>/dev/null <"$g/rebase-apply/head-name" r="|REBASE" elif [ -f "$g/rebase-apply/applying" ]; then r="|AM" else r="|AM/REBASE" fi elif [ -f "$g/MERGE_HEAD" ]; then r="|MERGING" elif [ -f "$g/CHERRY_PICK_HEAD" ]; then r="|CHERRY-PICKING" elif [ -f "$g/REVERT_HEAD" ]; then r="|REVERTING" elif [ -f "$g/BISECT_LOG" ]; then r="|BISECTING" fi
if [ -n "$b" ]; then
:
elif [ -h "$g/HEAD" ]; then
# symlink symbolic ref
b="$(git symbolic-ref HEAD 2>/dev/null)"
else
local head=""
if ! read head 2>/dev/null <"$g/HEAD"; then
if [ $pcmode = yes ]; then
PS1="$ps1pc_start$ps1pc_end"
fi
return
fi
# is it a symbolic ref?
b="${head#ref: }"
if [ "$head" = "$b" ]; then
detached=yes
b="$(
case "${GIT_PS1_DESCRIBE_STYLE-}" in
(contains)
git describe --contains HEAD ;;
(branch)
git describe --contains --all HEAD ;;
(describe)
git describe HEAD ;;
(* | default)
git describe --tags --exact-match HEAD ;;
esac 2>/dev/null)" ||
b="$short_sha..."
b="($b)"
fi
fi
fi
if [ -n "$step" ] && [ -n "$total" ]; then r="$r $step/$total" fi
local w="" local i="" local s="" local u="" local c="" local p=""
if [ "true" = "$inside_gitdir" ]; then if [ "true" = "$bare_repo" ]; then c="BARE:" else b="GIT_DIR!" fi elif [ "true" = "$inside_worktree" ]; then if [ -n "${GIT_PS1_SHOWDIRTYSTATE-}" ] && [ "$(git config --bool bash.showDirtyState)" != "false" ] then git diff --no-ext-diff --quiet --exit-code || w="*" if [ -n "$short_sha" ]; then git diff-index --cached --quiet HEAD -- || i="+" else i="#" fi fi if [ -n "${GIT_PS1_SHOWSTASHSTATE-}" ] && [ -r "$g/refs/stash" ]; then s="$" fi
if [ -n "${GIT_PS1_SHOWUNTRACKEDFILES-}" ] &&
[ "$(git config --bool bash.showUntrackedFiles)" != "false" ] &&
git ls-files --others --exclude-standard --error-unmatch -- '*' >/dev/null 2>/dev/null
then
u="%${ZSH_VERSION+%}"
fi
if [ -n "${GIT_PS1_SHOWUPSTREAM-}" ]; then
__git_ps1_show_upstream
fi
fi
local z="${GIT_PS1_STATESEPARATOR-" "}"
if [
local f="$w$i$s$u" local gitstring="$c${b##refs/heads/}${f:+$z$f}$r$p"
if [
export CLICOLOR=1 export LSCOLORS=GxFxCxDxBxegedabagaced
source ~/.git-prompt.sh
export PS1="[\033[1;34m]![\033[0m] [\033[1;35m]\u[\033[0m]:[\033[1;35m]\w[\033[0m]$(__git_ps1 '(%s)') $ "
alias pg-start='pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start' alias pg-stop='pg_ctl -D /usr/local/var/postgres stop -s -m fast'
alias ls='ls -lahPfG'
alias gs='git status'