[[ $- != *i* ]] && return
unset dir_color rc_color user_id_color root_id_color init_vcs_color clean_vcs_color
unset modified_vcs_color added_vcs_color addmoded_vcs_color untracked_vcs_color op_vcs_color detached_vcs_color
conf=git-prompt.conf; [[ -r $conf ]] && . $conf
conf=/etc/git-prompt.conf; [[ -r $conf ]] && . $conf
conf=~/.git-prompt.conf; [[ -r $conf ]] && . $conf
unset conf
git_module=${git_module:-on}
svn_module=${svn_module:-off}
hg_module=${hg_module:-on}
vim_module=${vim_module:-on}
error_bell=${error_bell:-off}
cwd_cmd=${cwd_cmd:-\\w}
cols=`tput colors`
if [[ -n "$cols" && $cols -ge 8 ]]; then
dir_color=${dir_color:-CYAN}
rc_color=${rc_color:-red}
user_id_color=${user_id_color:-blue}
root_id_color=${root_id_color:-magenta}
else
dir_color=${dir_color:-bw_bold}
rc_color=${rc_color:-bw_bold}
fi
unset cols
prompt_char=${prompt_char:-'>'}
root_prompt_char=${root_prompt_char:-'>'}
init_vcs_color=${init_vcs_color:-WHITE}
clean_vcs_color=${clean_vcs_color:-blue}
modified_vcs_color=${modified_vcs_color:-red}
added_vcs_color=${added_vcs_color:-green}
addmoded_vcs_color=${addmoded_vcs_color:-yellow}
untracked_vcs_color=${untracked_vcs_color:-BLUE}
op_vcs_color=${op_vcs_color:-MAGENTA}
detached_vcs_color=${detached_vcs_color:-RED}
max_file_list_length=${max_file_list_length:-100}
upcase_hostname=${upcase_hostname:-on}
aj_max=20
unset PARSE_VCS_STATUS
[[ $git_module = "on" ]] && type git >&/dev/null && PARSE_VCS_STATUS+="parse_git_status"
[[ $svn_module = "on" ]] && type svn >&/dev/null && PARSE_VCS_STATUS+="||parse_svn_status"
[[ $hg_module = "on" ]] && type hg >&/dev/null && PARSE_VCS_STATUS+="||parse_hg_status"
PARSE_VCS_STATUS+="||return"
black='\['`tput sgr0; tput setaf 0`'\]'
red='\['`tput sgr0; tput setaf 1`'\]'
green='\['`tput sgr0; tput setaf 2`'\]'
yellow='\['`tput sgr0; tput setaf 3`'\]'
blue='\['`tput sgr0; tput setaf 4`'\]'
magenta='\['`tput sgr0; tput setaf 5`'\]'
cyan='\['`tput sgr0; tput setaf 6`'\]'
white='\['`tput sgr0; tput setaf 7`'\]'
BLACK='\['`tput setaf 0; tput bold`'\]'
RED='\['`tput setaf 1; tput bold`'\]'
GREEN='\['`tput setaf 2; tput bold`'\]'
YELLOW='\['`tput setaf 3; tput bold`'\]'
BLUE='\['`tput setaf 4; tput bold`'\]'
MAGENTA='\['`tput setaf 5; tput bold`'\]'
CYAN='\['`tput setaf 6; tput bold`'\]'
WHITE='\['`tput setaf 7; tput bold`'\]'
bw_bold='\['`tput bold`'\]'
on=''
off=': '
bell="\[`eval ${!error_bell} tput bel`\]"
colors_reset='\['`tput sgr0`'\]'
init_vcs_color=${!init_vcs_color}
modified_vcs_color=${!modified_vcs_color}
untracked_vcs_color=${!untracked_vcs_color}
clean_vcs_color=${!clean_vcs_color}
added_vcs_color=${!added_vcs_color}
op_vcs_color=${!op_vcs_color}
addmoded_vcs_color=${!addmoded_vcs_color}
detached_vcs_color=${!detached_vcs_color}
unset PROMPT_COMMAND
if [[ -z "$TERM" || ("$TERM" = "dumb" && -z "$INSIDE_EMACS") || -n "$MC_SID" ]]; then
unset PROMPT_COMMAND
PS1="\w$prompt_char "
return 0
fi
screen_marker="sCRn"
if [[ $LC_CTYPE =~ "UTF" && $TERM != "linux" ]]; then
elipses_marker="…"
else
elipses_marker="..."
fi
export who_where
cwd_truncate() {
cwd=${PWD/$HOME/\~}
case $1 in
full)
return
;;
last)
cwd=${PWD##/*/}
[[ $PWD == $HOME ]] && cwd="~"
return
;;
*)
if [[ ${BASH_VERSINFO[0]} -eq 3 && ${BASH_VERSINFO[1]} -le 1 || ${BASH_VERSINFO[0]} -lt 3 ]] ; then
return
fi
;;
esac
local cwd_max_length=$1
exp31='[[ "$cwd" =~ (~?/)(.*/)([^/]*)$ ]]'
if eval $exp31 ; then
local path_head=${BASH_REMATCH[1]}
local path_middle=${BASH_REMATCH[2]}
local path_last_dir=${BASH_REMATCH[3]}
local cwd_middle_max=$(( $cwd_max_length - ${#path_last_dir} ))
[[ $cwd_middle_max < 0 ]] && cwd_middle_max=0
if [[ ${#path_middle} -gt $(( $cwd_middle_max + ${#elipses_marker} + 5 )) ]]; then
middle_tail=${path_middle:${#path_middle}-${cwd_middle_max}}
exp31='[[ $middle_tail =~ [^/]*/(.*)$ ]]'
eval $exp31
middle_tail=${BASH_REMATCH[1]}
if [[ $(( ${#path_middle} - ${#middle_tail})) -gt 4 ]]; then
cwd=$path_head$elipses_marker$middle_tail$path_last_dir
fi
fi
fi
return
}
set_shell_label() {
xterm_label() { echo -n "]2;${@}" ; }
screen_label() {
xterm_label "$screen_marker $plain_who_where $@"
[ "$STY" ] && screen -S $STY -X title "$*"
}
case $TERM in
screen*)
screen_label "$*"
;;
xterm* | rxvt* | gnome-terminal | konsole | eterm | wterm )
xterm_label "$plain_who_where $@"
;;
*)
;;
esac
}
export -f set_shell_label
id=`id -un`
id=${id#$default_user}
tty=`tty`
tty=`echo $tty | sed "s:/dev/pts/:p:; s:/dev/tty::" `
tty=`echo $tty | sed "s:/dev/vc/:vc:" `
if [[ "$TERM" = "screen" ]] ; then
tty="$WINDOW"
fi
case $TERM in
xterm* | rxvt* | gnome-terminal | konsole | eterm | wterm ) unset tty ;;
*);;
esac
dir_color=${!dir_color}
rc_color=${!rc_color}
user_id_color=${!user_id_color}
root_id_color=${!root_id_color}
host=${HOSTNAME}
host=${host#$default_host}
uphost=`echo ${host} | tr a-z A-Z`
if [[ $upcase_hostname = "on" ]]; then
host=${uphost}
fi
host_color=${uphost}_host_color
host_color=${!host_color}
if [[ -z $host_color && -x /usr/bin/cksum ]] ; then
cksum_color_no=`echo $uphost | cksum | awk '{print $1%7}'`
color_index=(green yellow blue magenta cyan white)
host_color=${color_index[cksum_color_no]}
fi
host_color=${!host_color}
host=${host%.$default_domain}
if [[ -n $id || -n $host ]] ; then
[[ -n $id && -n $host ]] && at='@' || at=''
color_who_where="${id}${host:+$host_color$at$host}${tty:+ $tty}"
plain_who_where="${id}$at$host"
color_who_where="$color_who_where "
plain_who_where="$plain_who_where "
if [ "$id" == "root" ] ; then
user_id_color=$root_id_color
prompt_char="$root_prompt_char"
fi
color_who_where="$user_id_color$color_who_where$colors_reset"
else
color_who_where=''
fi
parse_svn_status() {
[[ -d .svn ]] || return 1
vcs=svn
eval `
svn info |
sed -n "
s@^URL[^/]*//@repo_dir=@p
s/^Revision: /rev=/p
"
`
unset status modified added clean init added mixed untracked op detached
eval `svn status 2>/dev/null |
sed -n '
s/^A... \([^.].*\)/modified=modified; modified_files[${#modified_files[@]}]=\"\1\";/p
s/^M... \([^.].*\)/modified=modified; modified_files[${#modified_files[@]}]=\"\1\";/p
s/^\?... \([^.].*\)/untracked=untracked; untracked_files[${#untracked_files[@]}]=\"\1\";/p
'
`
[[ -z $modified ]] && [[ -z $untracked ]] && clean=clean
vcs_info=svn:r$rev
}
parse_hg_status() {
[[ -d ./.hg/ ]] || return 1
vcs=hg
unset status modified added clean init added mixed untracked op detached
eval `hg status 2>/dev/null |
sed -n '
s/^M \([^.].*\)/modified=modified; modified_files[${#modified_files[@]}]=\"\1\";/p
s/^A \([^.].*\)/added=added; added_files[${#added_files[@]}]=\"\1\";/p
s/^R \([^.].*\)/added=added;/p
s/^! \([^.].*\)/modified=modified;/p
s/^? \([^.].*\)/untracked=untracked; untracked_files[${#untracked_files[@]}]=\\"\1\\";/p
'`
branch=`hg branch 2> /dev/null`
[[ -z $modified ]] && [[ -z $untracked ]] && [[ -z $added ]] && clean=clean
vcs_info=${branch/default/D}
}
parse_git_status() {
git_dir=`[[ $git_module = "on" ]] && git rev-parse --git-dir 2> /dev/null`
[[ -n ${git_dir/./} ]] || return 1
vcs=git
file_regex='\([^/]*\/\?\).*'
added_files=()
modified_files=()
untracked_files=()
unset branch status modified added clean init added mixed untracked op detached
eval " $(
git status 2>/dev/null |
sed -n '
s/^# On branch /branch=/p
s/^nothing to commit (working directory clean)/clean=clean/p
s/^# Initial commit/init=init/p
/^# Changes to be committed:/,/^# [A-Z]/ {
s/^# Changes to be committed:/added=added;/p
s/^# modified: '"$file_regex"'/ [[ \" ${added_files[*]} \" =~ \" \1 \" ]] || added_files[${#added_files[@]}]=\"\1\"/p
s/^# new file: '"$file_regex"'/ [[ \" ${added_files[*]} \" =~ \" \1 \" ]] || added_files[${#added_files[@]}]=\"\1\"/p
s/^# renamed:[^>]*> '"$file_regex"'/ [[ \" ${added_files[*]} \" =~ \" \1 \" ]] || added_files[${#added_files[@]}]=\"\1\"/p
s/^# copied:[^>]*> '"$file_regex"'/ [[ \" ${added_files[*]} \" =~ \" \1 \" ]] || added_files[${#added_files[@]}]=\"\1\"/p
}
/^# Changed but not updated:/,/^# [A-Z]/ {
s/^# Changed but not updated:/modified=modified;/p
s/^# modified: '"$file_regex"'/ [[ \" ${modified_files[*]} \" =~ \" \1 \" ]] || modified_files[${#modified_files[@]}]=\"\1\"/p
s/^# unmerged: '"$file_regex"'/ [[ \" ${modified_files[*]} \" =~ \" \1 \" ]] || modified_files[${#modified_files[@]}]=\"\1\"/p
}
/^# Untracked files:/,/^[^#]/{
s/^# Untracked files:/untracked=untracked;/p
s/^# '"$file_regex"'/ [[ \" ${untracked_files[*]} ${modified_files[*]} ${added_files[*]} \" =~ \" \1 \" ]] || untracked_files[${#untracked_files[@]}]=\"\1\"/p
}
'
)"
if ! grep -q "^ref:" $git_dir/HEAD 2>/dev/null; then
detached=detached
fi
unset op
if [[ -d "$git_dir/.dotest" ]] ; then
if [[ -f "$git_dir/.dotest/rebasing" ]] ; then
op="rebase"
elif [[ -f "$git_dir/.dotest/applying" ]] ; then
op="am"
else
op="am/rebase"
fi
elif [[ -f "$git_dir/.dotest-merge/interactive" ]] ; then
op="rebase -i"
elif [[ -d "$git_dir/.dotest-merge" ]] ; then
op="rebase -m"
elif [[ -f "$git_dir/MERGE_HEAD" ]] ; then
op="merge"
elif [[ -f "$git_dir/index.lock" ]] ; then
op="locked"
else
[[ -f "$git_dir/BISECT_LOG" ]] && op="bisect"
fi
rawhex=`git rev-parse HEAD 2>/dev/null`
rawhex=${rawhex/HEAD/}
rawhex=${rawhex:0:6}
branch=${branch/master/M}
if [[ $init ]]; then
vcs_info=M$white=init
else
if [[ "$detached" ]] ; then
branch="<detached:`git name-rev --name-only HEAD 2>/dev/null`"
elif [[ "$op" ]]; then
branch="$op:$branch"
if [[ "$op" == "merge" ]] ; then
branch+="<--$(git name-rev --name-only $(<$git_dir/MERGE_HEAD))"
fi
fi
vcs_info="$branch$white=$rawhex"
fi
}
parse_vcs_status() {
unset file_list modified_files untracked_files added_files
unset vcs vcs_info
unset status modified untracked added init detached
unset file_list modified_files untracked_files added_files
[[ $vcs_ignore_dir_list =~ $PWD ]] && return
eval $PARSE_VCS_STATUS
unset status
status=${op:+op}
status=${status:-$detached}
status=${status:-$clean}
status=${status:-$modified}
status=${status:-$added}
status=${status:-$untracked}
status=${status:-$init}
: ${status?prompt internal error: git status}
eval vcs_color="\${${status}_vcs_color}"
if [[ $vim_module = "on" ]] ; then
unset vim_glob vim_file vim_files
old_nullglob=`shopt -p nullglob`
shopt -s nullglob
vim_glob=`echo .*.swp`
eval $old_nullglob
if [[ $vim_glob ]]; then
vim_file=${vim_glob#.}
vim_file=${vim_file%.swp}
[[ .${vim_file}.swp -nt $vim_file ]] && vim_files=$vim_file
fi
fi
unset file_list
[[ ${added_files[0]} ]] && file_list+=" "$added_vcs_color${added_files[@]}
[[ ${modified_files[0]} ]] && file_list+=" "$modified_vcs_color${modified_files[@]}
[[ ${untracked_files[0]} ]] && file_list+=" "$untracked_vcs_color${untracked_files[@]}
[[ ${vim_files} ]] && file_list+=" "${RED}VIM:${vim_files}
file_list=${file_list:+:$file_list}
if [[ ${#file_list} -gt $max_file_list_length ]] ; then
file_list=${file_list:0:$max_file_list_length}
if [[ $max_file_list_length -gt 0 ]] ; then
file_list="${file_list% *} $elipses_marker"
fi
fi
head_local="(${vcs_info}$vcs_color${file_list}$vcs_color)"
head_local="${head_local+$vcs_color$head_local }"
}
disable_set_shell_label() {
trap - DEBUG >& /dev/null
}
enable_set_shell_label() {
disable_set_shell_label
trap '[[ -z "$BASH_SOURCE" && ($BASH_COMMAND != prompt_command_function) ]] &&
set_shell_label $BASH_COMMAND' DEBUG >& /dev/null
}
j (){
: ${1? usage: j dir-beginning}
for (( i=(aj_idx+1)%aj_max; i != aj_idx%aj_max; i=++i%aj_max )) ; do
if [[ ${aj_dir_list[$i]} =~ ^.*/$1[^/]*$ ]] ; then
cd "${aj_dir_list[$i]}"
return
fi
done
echo '?'
}
alias jumpstart='echo ${aj_dir_list[@]}'
prompt_command_function() {
rc="$?"
if [[ "$rc" == "0" ]]; then
rc=""
else
rc="$rc_color$rc$colors_reset$bell "
fi
cwd=${PWD/$HOME/\~}
set_shell_label "${cwd##[/~]*/}/"
parse_vcs_status
if [[ ${aj_dir_list[aj_idx%aj_max]} != $PWD ]] ; then
aj_dir_list[++aj_idx%aj_max]="$PWD"
fi
eval "${cwd_cmd/\\/cwd=\\\\}"
PS1="$colors_reset$rc$head_local$color_who_where$dir_color$cwd$tail_local$dir_color$prompt_char $colors_reset"
unset head_local tail_local pwd
}
PROMPT_COMMAND=prompt_command_function
enable_set_shell_label
unset rc id tty modified_files file_list