2013-09-02 02:31:05 -04:00
|
|
|
function enrich {
|
|
|
|
flag=$1
|
|
|
|
symbol=$2
|
2013-09-02 13:00:26 -04:00
|
|
|
if [[ -n $3 ]]
|
|
|
|
then
|
|
|
|
coloron=$3
|
|
|
|
else
|
|
|
|
coloron=${on}
|
|
|
|
fi
|
2013-09-08 07:03:32 -04:00
|
|
|
if [ ${use_color_off} == false -a ${flag} == false ]; then symbol=" "; fi
|
2013-09-02 13:00:26 -04:00
|
|
|
if [[ $flag == true ]]; then color="${coloron}"; else color="${off}"; fi
|
2013-09-04 16:56:51 -04:00
|
|
|
PS1="${PS1}${color}${symbol}${reset} "
|
2013-09-02 02:31:05 -04:00
|
|
|
}
|
2013-09-01 18:59:21 -04:00
|
|
|
|
2013-09-01 18:02:16 -04:00
|
|
|
function build_prompt {
|
|
|
|
PS1=""
|
2013-09-03 02:35:50 -04:00
|
|
|
# Symbols
|
|
|
|
if [[ -z "${is_a_git_repo_symbol}" ]]; then is_a_git_repo_symbol="❤"; fi
|
|
|
|
if [[ -z "${has_untracked_files_symbol}" ]]; then has_untracked_files_symbol="∿"; fi
|
|
|
|
if [[ -z "${has_adds_symbol}" ]]; then has_adds_symbol="+"; fi
|
|
|
|
if [[ -z "${has_deletions_symbol}" ]]; then has_deletions_symbol="-"; fi
|
|
|
|
if [[ -z "${has_deletions_cached_symbol}" ]]; then has_deletions_cached_symbol="✖"; fi
|
|
|
|
if [[ -z "${has_modifications_symbol}" ]]; then has_modifications_symbol="✎"; fi
|
2013-09-04 02:45:20 -04:00
|
|
|
if [[ -z "${has_modifications_cached_symbol}" ]]; then has_modifications_cached_symbol="☲"; fi
|
2013-09-04 02:38:57 -04:00
|
|
|
if [[ -z "${ready_to_commit_symbol}" ]]; then ready_to_commit_symbol="→"; fi
|
2013-09-03 02:35:50 -04:00
|
|
|
if [[ -z "${is_on_a_tag_symbol}" ]]; then is_on_a_tag_symbol="⌫"; fi
|
|
|
|
if [[ -z "${needs_to_merge_symbol}" ]]; then needs_to_merge_symbol="ᄉ"; fi
|
|
|
|
if [[ -z "${has_upstream_symbol}" ]]; then has_upstream_symbol="⇅"; fi
|
|
|
|
if [[ -z "${detached_symbol}" ]]; then detached_symbol="⚯"; fi
|
|
|
|
if [[ -z "${can_fast_forward_symbol}" ]]; then can_fast_forward_symbol="»"; fi
|
2013-09-03 17:20:44 -04:00
|
|
|
if [[ -z "${has_diverged_symbol}" ]]; then has_diverged_symbol="Ⴤ"; fi
|
2013-09-03 02:35:50 -04:00
|
|
|
if [[ -z "${rebase_tracking_branch_symbol}" ]]; then rebase_tracking_branch_symbol="↶"; fi
|
|
|
|
if [[ -z "${merge_tracking_branch_symbol}" ]]; then merge_tracking_branch_symbol="ᄉ"; fi
|
2013-09-04 14:17:21 -04:00
|
|
|
if [[ -z "${should_push_symbol}" ]]; then should_push_symbol="↑"; fi
|
2013-09-04 16:36:13 -04:00
|
|
|
if [[ -z "${has_stashes_symbol}" ]]; then has_stashes_symbol="★"; fi
|
|
|
|
|
2013-09-03 17:35:13 -04:00
|
|
|
|
|
|
|
# flags
|
|
|
|
if [[ -z "${display_has_upstream}" ]]; then display_has_upstream=false; fi
|
|
|
|
if [[ -z "${display_tag}" ]]; then display_tag=false; fi
|
2013-09-03 12:47:06 -04:00
|
|
|
if [[ -z "${display_tag_name}" ]]; then display_tag_name=true; fi
|
2013-09-03 13:11:22 -04:00
|
|
|
if [[ -z "${two_lines}" ]]; then two_lines=true; fi
|
2013-09-03 02:35:50 -04:00
|
|
|
if [[ -z "${finally}" ]]; then finally="\w ∙ "; fi
|
2013-09-08 07:03:32 -04:00
|
|
|
if [[ -z "${use_color_off}" ]]; then use_color_off=false; fi
|
2013-09-03 02:35:50 -04:00
|
|
|
|
2013-09-02 13:00:26 -04:00
|
|
|
# Colors
|
2013-09-02 02:49:50 -04:00
|
|
|
on="\[\033[0;37m\]"
|
2013-09-02 02:31:05 -04:00
|
|
|
off="\[\033[1;30m\]"
|
2013-09-04 16:56:51 -04:00
|
|
|
red="\[\033[0;31m\]"
|
2013-09-03 13:11:22 -04:00
|
|
|
green="\[\033[0;32m\]"
|
|
|
|
yellow="\[\033[0;33m\]"
|
2013-09-04 16:56:51 -04:00
|
|
|
violet="\[\033[0;35m\]"
|
2013-09-02 02:31:05 -04:00
|
|
|
branch_color="\[\033[0;34m\]"
|
2013-09-05 02:29:12 -04:00
|
|
|
# blinking="\[\033[1;5;17m\]"
|
2013-09-01 18:02:16 -04:00
|
|
|
reset="\[\033[0m\]"
|
|
|
|
|
2013-09-02 13:00:26 -04:00
|
|
|
# Git info
|
|
|
|
current_commit_hash=$(git rev-parse HEAD 2> /dev/null)
|
|
|
|
current_commit_hash_abbrev=$(git rev-parse --short HEAD 2> /dev/null)
|
|
|
|
if [[ -n $current_commit_hash ]]; then is_a_git_repo=true; else is_a_git_repo=false; fi
|
2013-09-02 17:27:36 -04:00
|
|
|
|
2013-09-11 14:18:35 -04:00
|
|
|
number_of_logs=$(git log --pretty=oneline 2>/dev/null| wc -l)
|
2013-09-02 17:27:36 -04:00
|
|
|
current_branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null);
|
|
|
|
|
|
|
|
if [[ $current_branch == "HEAD" ]]; then detached=true; else detached=false; fi
|
|
|
|
|
2013-09-03 17:20:44 -04:00
|
|
|
|
2013-09-02 17:27:36 -04:00
|
|
|
if [ $is_a_git_repo == true -a $number_of_logs == 0 ]; then just_init=true; fi
|
|
|
|
if [ $is_a_git_repo == true -a $number_of_logs -gt 0 ]; then
|
|
|
|
|
2013-09-02 13:00:26 -04:00
|
|
|
upstream=$(git rev-parse --symbolic-full-name --abbrev-ref @{upstream} 2> /dev/null)
|
|
|
|
if [[ $upstream != "@{upstream}" ]]; then has_upstream=true; else has_upstream=false; upstream=""; fi
|
2013-09-02 13:12:27 -04:00
|
|
|
|
|
|
|
number_of_modifications=$(git status --short 2> /dev/null|grep --count -e ^\.M)
|
|
|
|
if [[ ${number_of_modifications} -gt 0 ]] ; then has_modifications=true; else has_modifications=false; fi
|
2013-09-04 02:38:57 -04:00
|
|
|
number_of_modifications_cached=$(git status --short 2> /dev/null|grep --count -e ^M)
|
|
|
|
if [[ ${number_of_modifications_cached} -gt 0 ]] ; then has_modifications_cached=true; else has_modifications_cached=false; fi
|
2013-09-02 16:59:09 -04:00
|
|
|
|
|
|
|
number_of_adds=$(git status --short 2> /dev/null|grep --count -e ^\A)
|
|
|
|
if [[ ${number_of_adds} -gt 0 ]] ; then has_adds=true; else has_adds=false; fi
|
|
|
|
|
|
|
|
number_of_deletions=$(git status --short 2> /dev/null|grep --count -e ^.D)
|
|
|
|
if [[ ${number_of_deletions} -gt 0 ]] ; then has_deletions=true; else has_deletions=false; fi
|
|
|
|
number_of_deletions_cached=$(git status --short 2> /dev/null|grep --count -e ^D)
|
|
|
|
if [[ ${number_of_deletions_cached} -gt 0 ]] ; then has_deletions_cached=true; else has_deletions_cached=false; fi
|
|
|
|
|
2013-09-04 02:38:57 -04:00
|
|
|
numbers_of_files_cached=$(git status --short 2> /dev/null|grep --count -e ^[MAD])
|
|
|
|
numbers_of_files_not_cached=$(git status --short 2> /dev/null|grep --count -e ^.[MAD\?])
|
|
|
|
if [ ${numbers_of_files_cached} -gt 0 -a ${numbers_of_files_not_cached} -eq 0 ] ; then ready_to_commit=true; else ready_to_commit=false; fi
|
2013-09-02 13:12:27 -04:00
|
|
|
|
|
|
|
number_of_untracked_files=$(git status --short 2> /dev/null|grep --count -e ^\?\?)
|
|
|
|
if [[ ${number_of_untracked_files} -gt 0 ]] ; then has_untracked_files=true; else has_untracked_files=false; fi
|
2013-09-03 02:57:36 -04:00
|
|
|
|
2013-09-03 17:35:13 -04:00
|
|
|
tag_at_current_commit=$(git describe --exact-match --tags ${current_commit_hash} 2>/dev/null)
|
2013-09-03 16:02:15 -04:00
|
|
|
|
2013-09-05 02:29:12 -04:00
|
|
|
if [[ -n "${tag_at_current_commit}" ]]; then is_on_a_tag=true; else is_on_a_tag=false; fi;
|
2013-09-04 14:17:21 -04:00
|
|
|
|
|
|
|
commits_ahead=0
|
|
|
|
commits_behind=0
|
|
|
|
has_diverged=false
|
|
|
|
can_fast_forward=false
|
2013-09-03 17:20:44 -04:00
|
|
|
can_fast_forward=false
|
2013-09-04 14:17:21 -04:00
|
|
|
|
2013-09-11 11:54:57 -04:00
|
|
|
commits_ahead=$(git log --pretty=oneline --topo-order --left-right ${current_commit_hash}...${upstream} | grep -c "^<" )
|
|
|
|
commits_behind=$(git log --pretty=oneline --topo-order --left-right ${current_commit_hash}...${upstream} | grep -c "^>" )
|
2013-09-04 14:17:21 -04:00
|
|
|
|
|
|
|
if [ ${commits_ahead} -gt 0 -a ${commits_behind} -gt 0 ];
|
2013-09-03 17:20:44 -04:00
|
|
|
then
|
2013-09-04 14:17:21 -04:00
|
|
|
has_diverged=true
|
|
|
|
fi
|
|
|
|
if [ ${commits_ahead} -eq 0 -a ${commits_behind} -gt 0 ];
|
|
|
|
then
|
|
|
|
can_fast_forward=true
|
2013-09-03 16:02:15 -04:00
|
|
|
fi
|
|
|
|
|
2013-09-03 17:20:44 -04:00
|
|
|
will_rebase=$(git config --get branch.${current_branch}.rebase 2>/dev/null)
|
2013-09-04 16:36:13 -04:00
|
|
|
|
|
|
|
number_of_stashes=$(git stash list | wc -l)
|
|
|
|
if [[ ${number_of_stashes} -gt 0 ]]; then has_stashes=true; else has_stashes=false; fi
|
2013-09-05 02:29:12 -04:00
|
|
|
else
|
|
|
|
is_on_a_tag=false
|
2013-09-02 02:49:50 -04:00
|
|
|
fi
|
2013-09-03 02:35:50 -04:00
|
|
|
|
2013-09-02 02:49:50 -04:00
|
|
|
if [[ ${is_a_git_repo} == true ]]
|
|
|
|
then
|
2013-09-04 16:56:51 -04:00
|
|
|
enrich ${is_a_git_repo} "${is_a_git_repo_symbol}" "${violet}"
|
2013-09-04 16:36:13 -04:00
|
|
|
enrich ${has_stashes} "${has_stashes_symbol}" "${yellow}"
|
2013-09-04 16:56:51 -04:00
|
|
|
enrich ${has_untracked_files} "${has_untracked_files_symbol}" "${red}"
|
|
|
|
enrich ${has_adds} "${has_adds_symbol}" "${yellow}"
|
2013-09-03 01:47:52 -04:00
|
|
|
|
2013-09-04 16:56:51 -04:00
|
|
|
enrich ${has_deletions} "${has_deletions_symbol}" "${red}"
|
|
|
|
enrich ${has_deletions_cached} "${has_deletions_cached_symbol}" "${yellow}"
|
2013-09-03 01:47:52 -04:00
|
|
|
|
2013-09-04 16:56:51 -04:00
|
|
|
enrich ${has_modifications} "${has_modifications_symbol}" "${red}"
|
|
|
|
enrich ${has_modifications_cached} "${has_modifications_cached_symbol}" "${yellow}"
|
2013-09-04 02:38:57 -04:00
|
|
|
enrich ${ready_to_commit} "${ready_to_commit_symbol}" "${green}"
|
2013-09-03 01:47:52 -04:00
|
|
|
|
2013-09-04 16:56:51 -04:00
|
|
|
enrich ${detached} "${detached_symbol}" "${red}"
|
2013-09-02 16:59:09 -04:00
|
|
|
|
2013-09-03 17:35:13 -04:00
|
|
|
if [[ ${display_has_upstream} == true ]]; then
|
|
|
|
enrich ${has_upstream} "${has_upstream_symbol}"
|
|
|
|
fi
|
2013-09-02 13:00:26 -04:00
|
|
|
if [[ ${detached} == true ]]
|
|
|
|
then
|
2013-09-02 17:27:36 -04:00
|
|
|
if [[ ${just_init} == true ]]; then
|
2013-09-04 16:56:51 -04:00
|
|
|
PS1="${PS1} ${red}detached"
|
2013-09-02 17:27:36 -04:00
|
|
|
else
|
2013-09-03 02:35:50 -04:00
|
|
|
PS1="${PS1} ${on}(${current_commit_hash_abbrev})"
|
2013-09-02 17:27:36 -04:00
|
|
|
fi
|
2013-09-02 13:00:26 -04:00
|
|
|
else
|
2013-09-02 13:15:33 -04:00
|
|
|
if [[ $has_upstream == true ]]
|
|
|
|
then
|
2013-09-05 02:29:12 -04:00
|
|
|
if [[ ${will_rebase} == true ]];
|
2013-09-03 17:20:44 -04:00
|
|
|
then
|
|
|
|
type_of_upstream="${rebase_tracking_branch_symbol}";
|
|
|
|
else
|
|
|
|
type_of_upstream="${merge_tracking_branch_symbol}";
|
2013-09-03 13:11:22 -04:00
|
|
|
fi
|
2013-09-04 14:17:21 -04:00
|
|
|
|
|
|
|
|
2013-09-03 17:20:44 -04:00
|
|
|
|
|
|
|
if [[ ${has_diverged} == true ]]; then
|
2013-09-04 14:17:21 -04:00
|
|
|
PS1="${PS1} -${commits_behind} ${has_diverged_symbol} +${commits_ahead}"
|
2013-09-03 17:20:44 -04:00
|
|
|
else
|
|
|
|
|
2013-09-04 14:17:21 -04:00
|
|
|
if [[ ${commits_behind} -gt 0 ]]; then
|
|
|
|
PS1="${PS1} ${on} -${commits_behind} ${can_fast_forward_symbol} "
|
2013-09-03 17:20:44 -04:00
|
|
|
fi
|
2013-09-04 14:17:21 -04:00
|
|
|
if [[ ${commits_ahead} -gt 0 ]]; then
|
|
|
|
PS1="${PS1} ${on} ${should_push_symbol} +${commits_ahead}"
|
2013-09-03 17:20:44 -04:00
|
|
|
fi
|
2013-09-04 14:17:21 -04:00
|
|
|
|
2013-09-03 03:14:58 -04:00
|
|
|
fi
|
2013-09-03 17:20:44 -04:00
|
|
|
|
|
|
|
PS1="${PS1} (${green}${current_branch}${reset} ${type_of_upstream} ${upstream//\/$current_branch/})"
|
2013-09-02 13:15:33 -04:00
|
|
|
else
|
2013-09-03 13:11:22 -04:00
|
|
|
PS1="${PS1} ${on}(${green}${current_branch}${reset})"
|
2013-09-02 13:15:33 -04:00
|
|
|
fi
|
2013-09-02 13:00:26 -04:00
|
|
|
fi
|
|
|
|
|
2013-09-05 16:12:34 -04:00
|
|
|
if [[ ${display_tag} == true ]]; then
|
|
|
|
PS1="${PS1} ${yellow}${is_on_a_tag_symbol}${reset}"
|
|
|
|
fi
|
2013-09-03 17:20:44 -04:00
|
|
|
if [ ${display_tag_name} == true -a ${is_on_a_tag} == true ];
|
|
|
|
then
|
|
|
|
PS1="${PS1} ${yellow}[${tag_at_current_commit}]${reset}"
|
|
|
|
fi
|
2013-09-04 02:47:18 -04:00
|
|
|
fi
|
2013-09-05 02:29:12 -04:00
|
|
|
|
2013-09-04 02:47:18 -04:00
|
|
|
if [ ${two_lines} == true -a ${is_a_git_repo} == true ];
|
|
|
|
then
|
|
|
|
break="\n";
|
|
|
|
else
|
|
|
|
break="";
|
2013-09-02 02:49:50 -04:00
|
|
|
fi
|
2013-09-03 17:20:44 -04:00
|
|
|
|
2013-09-03 02:35:50 -04:00
|
|
|
PS1="${PS1}${reset}${break}${finally}"
|
2013-09-01 18:02:16 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
PROMPT_COMMAND=build_prompt
|
2013-09-03 02:35:50 -04:00
|
|
|
|