From 882aa79ebb09f604fb342082c4736ebbb791a34c Mon Sep 17 00:00:00 2001 From: Yehor Lvivski Date: Thu, 7 Nov 2013 12:54:40 +0200 Subject: [PATCH 01/13] simple --- prompt.sh | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/prompt.sh b/prompt.sh index 7df005f..2a9d0d9 100644 --- a/prompt.sh +++ b/prompt.sh @@ -70,27 +70,19 @@ function build_prompt { git_status=$(git status --porcelain 2> /dev/null) - number_of_modifications=$(echo "$git_status" | grep --count -e ^\.M) - if [[ $number_of_modifications -gt 0 ]]; then has_modifications=true; else has_modifications=false; fi + if [[ $git_status =~ ^.M ]]; then has_modifications=true; else has_modifications=false; fi - number_of_modifications_cached=$(echo "$git_status" | grep --count -e ^M) - if [[ ${number_of_modifications_cached} -gt 0 ]]; then has_modifications_cached=true; else has_modifications_cached=false; fi + if [[ $git_status =~ ^M ]]; then has_modifications_cached=true; else has_modifications_cached=false; fi - number_of_adds=$(echo "$git_status" | grep --count -e ^\A) - if [[ $number_of_adds -gt 0 ]]; then has_adds=true; else has_adds=false; fi + if [[ $git_status =~ ^A ]]; then has_adds=true; else has_adds=false; fi - number_of_deletions=$(echo "$git_status" | grep --count -e ^.D) - if [[ $number_of_deletions -gt 0 ]]; then has_deletions=true; else has_deletions=false; fi + if [[ $git_status =~ ^.D ]]; then has_deletions=true; else has_deletions=false; fi - number_of_deletions_cached=$(echo "$git_status" | grep --count -e ^D) - if [[ $number_of_deletions_cached -gt 0 ]]; then has_deletions_cached=true; else has_deletions_cached=false; fi + if [[ $git_status =~ ^D ]]; then has_deletions_cached=true; else has_deletions_cached=false; fi - numbers_of_files_cached=$(echo "$git_status" | grep --count -e ^[MAD]) - numbers_of_files_not_cached=$(echo "$git_status" | grep --count -e ^.[MAD\?]) - if [[ $numbers_of_files_cached -gt 0 && $numbers_of_files_not_cached -eq 0 ]]; then ready_to_commit=true; else ready_to_commit=false; fi + if [[ $git_status =~ ^[MAD] && ! $git_status =~ ^.[MAD\?] ]]; then ready_to_commit=true; else ready_to_commit=false; fi - number_of_untracked_files=$(echo "$git_status" | grep --count -e ^\?\?) - if [[ $number_of_untracked_files -gt 0 ]]; then has_untracked_files=true; else has_untracked_files=false; fi + if [[ $git_status =~ ^\?\? ]]; then has_untracked_files=true; else has_untracked_files=false; fi tag_at_current_commit=$(git describe --exact-match --tags $current_commit_hash 2> /dev/null) if [[ -n $tag_at_current_commit ]]; then is_on_a_tag=true; else is_on_a_tag=false; fi From 19db503029a1c481714ba4b06f73963ce704f2f4 Mon Sep 17 00:00:00 2001 From: Yehor Lvivski Date: Thu, 7 Nov 2013 14:21:19 +0200 Subject: [PATCH 02/13] more bash magic --- prompt.sh | 84 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 46 insertions(+), 38 deletions(-) diff --git a/prompt.sh b/prompt.sh index 2a9d0d9..1ae4594 100644 --- a/prompt.sh +++ b/prompt.sh @@ -1,41 +1,41 @@ # Symbols -: ${is_a_git_repo_symbol:="❤"} -: ${has_untracked_files_symbol:="∿"} -: ${has_adds_symbol:="+"} -: ${has_deletions_symbol:="-"} -: ${has_deletions_cached_symbol:="✖"} -: ${has_modifications_symbol:="✎"} -: ${has_modifications_cached_symbol:="☲"} -: ${ready_to_commit_symbol:="→"} -: ${is_on_a_tag_symbol:="⌫"} -: ${needs_to_merge_symbol:="ᄉ"} -: ${has_upstream_symbol:="⇅"} -: ${detached_symbol:="⚯ "} -: ${can_fast_forward_symbol:="»"} -: ${has_diverged_symbol:="Ⴤ"} -: ${rebase_tracking_branch_symbol:="↶"} -: ${merge_tracking_branch_symbol:="ᄉ"} -: ${should_push_symbol:="↑"} -: ${has_stashes_symbol:="★"} +: ${is_a_git_repo_symbol:='❤'} +: ${has_untracked_files_symbol:='∿'} +: ${has_adds_symbol:='+'} +: ${has_deletions_symbol:='-'} +: ${has_deletions_cached_symbol:='✖'} +: ${has_modifications_symbol:='✎'} +: ${has_modifications_cached_symbol:='☲'} +: ${ready_to_commit_symbol:='→'} +: ${is_on_a_tag_symbol:='⌫'} +: ${needs_to_merge_symbol:='ᄉ'} +: ${has_upstream_symbol:='⇅'} +: ${detached_symbol:='⚯ '} +: ${can_fast_forward_symbol:='»'} +: ${has_diverged_symbol:='Ⴤ'} +: ${rebase_tracking_branch_symbol:='↶'} +: ${merge_tracking_branch_symbol:='ᄉ'} +: ${should_push_symbol:='↑'} +: ${has_stashes_symbol:='★'} # Flags : ${display_has_upstream:=false} : ${display_tag:=false} : ${display_tag_name:=true} : ${two_lines:=true} -: ${finally:="\w ∙ "} +: ${finally:='\w ∙ '} : ${use_color_off:=false} # Colors -: ${on="\[\033[0;37m\]"} -: ${off="\[\033[1;30m\]"} -: ${red="\[\033[0;31m\]"} -: ${green="\[\033[0;32m\]"} -: ${yellow="\[\033[0;33m\]"} -: ${violet="\[\033[0;35m\]"} -: ${branch_color="\[\033[0;34m\]"} -#: ${blinking="\[\033[1;5;17m\]"} -: ${reset="\[\033[0m\]"} +: ${on='\[\033[0;37m\]'} +: ${off='\[\033[1;30m\]'} +: ${red='\[\033[0;31m\]'} +: ${green='\[\033[0;32m\]'} +: ${yellow='\[\033[0;33m\]'} +: ${violet='\[\033[0;35m\]'} +: ${branch_color='\[\033[0;34m\]'} +#: ${blinking='\[\033[1;5;17m\]'} +: ${reset='\[\033[0m\]'} function enrich { flag=$1 @@ -61,12 +61,12 @@ function build_prompt { number_of_logs=$(git log --pretty=oneline 2> /dev/null | wc -l) current_branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null) - if [[ $current_branch == "HEAD" ]]; then detached=true; else detached=false; fi + if [[ $current_branch == 'HEAD' ]]; then detached=true; else detached=false; fi if [[ $is_a_git_repo == true && $number_of_logs == 0 ]]; then just_init=true; fi if [[ $is_a_git_repo == true && $number_of_logs -gt 0 ]]; then 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 + if [[ $upstream != '@{upstream}' ]]; then has_upstream=true; else has_upstream=false; upstream=''; fi git_status=$(git status --porcelain 2> /dev/null) @@ -92,13 +92,21 @@ function build_prompt { commits_diff=$(git log --pretty=oneline --topo-order --left-right ${current_commit_hash}...${upstream} 2> /dev/null) - commits_ahead=$(echo "$commits_diff" | grep -c "^<" ) - commits_behind=$(echo "$commits_diff" | grep -c "^>" ) + OLD_IFS=$IFS # save ifs + IFS=$'\n' - if [[ $commits_ahead -gt 0 && $commits_behind -gt 0 ]]; then + commits_ahead=(${commits_diff//>*/}) + commits_behind=(${commits_diff//<*/}) + + IFS=$OLD_IFS # restore ifs + + commits_ahead=${#commits_ahead[@]} + commits_behind=${#commits_behind[@]} + + if [[ $commits_ahead > 0 && $commits_behind > 0 ]]; then has_diverged=true fi - if [[ $commits_ahead -eq 0 && $commits_behind -gt 0 ]]; then + if [[ $commits_ahead == 0 && $commits_behind > 0 ]]; then can_fast_forward=true fi @@ -145,10 +153,10 @@ function build_prompt { if [[ $has_diverged == true ]]; then PS1="${PS1}-${commits_behind} ${has_diverged_symbol} +${commits_ahead} " else - if [[ $commits_behind -gt 0 ]]; then + if [[ $commits_behind > 0 ]]; then PS1="${PS1}${on} -${commits_behind} ${can_fast_forward_symbol} " fi - if [[ $commits_ahead -gt 0 ]]; then + if [[ $commits_ahead > 0 ]]; then PS1="${PS1}${on} ${should_push_symbol} +${commits_ahead} " fi fi @@ -167,9 +175,9 @@ function build_prompt { fi if [[ $two_lines == true && $is_a_git_repo == true ]]; then - break="\n" + break='\n' else - break="" + break='' fi PS1="${PS1}${reset}${break}${finally}" From bf48ddc00b93c13ae213c6fb9b15ae721ea8267b Mon Sep 17 00:00:00 2001 From: Yehor Lvivski Date: Thu, 7 Nov 2013 14:33:29 +0200 Subject: [PATCH 03/13] better checks --- prompt.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/prompt.sh b/prompt.sh index 1ae4594..8616d52 100644 --- a/prompt.sh +++ b/prompt.sh @@ -103,10 +103,10 @@ function build_prompt { commits_ahead=${#commits_ahead[@]} commits_behind=${#commits_behind[@]} - if [[ $commits_ahead > 0 && $commits_behind > 0 ]]; then + if [[ $commits_ahead -gt 0 && $commits_behind -gt 0 ]]; then has_diverged=true fi - if [[ $commits_ahead == 0 && $commits_behind > 0 ]]; then + if [[ $commits_ahead == 0 && $commits_behind -gt 0 ]]; then can_fast_forward=true fi @@ -153,10 +153,10 @@ function build_prompt { if [[ $has_diverged == true ]]; then PS1="${PS1}-${commits_behind} ${has_diverged_symbol} +${commits_ahead} " else - if [[ $commits_behind > 0 ]]; then + if [[ $commits_behind -gt 0 ]]; then PS1="${PS1}${on} -${commits_behind} ${can_fast_forward_symbol} " fi - if [[ $commits_ahead > 0 ]]; then + if [[ $commits_ahead -gt 0 ]]; then PS1="${PS1}${on} ${should_push_symbol} +${commits_ahead} " fi fi From 1d0a1a058c23edaa90b6819d822a470a68a26133 Mon Sep 17 00:00:00 2001 From: Yehor Lvivski Date: Thu, 7 Nov 2013 16:44:32 +0200 Subject: [PATCH 04/13] move branch detection inside condition, remove extra hash detection --- prompt.sh | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/prompt.sh b/prompt.sh index 8616d52..90e6c2e 100644 --- a/prompt.sh +++ b/prompt.sh @@ -54,17 +54,16 @@ function build_prompt { PS1="" # Git info - current_commit_hash=$(git rev-parse HEAD 2> /dev/null) - current_commit_hash_abbrev=$(git rev-parse --short HEAD 2> /dev/null) + current_commit_hash=$(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 number_of_logs=$(git log --pretty=oneline 2> /dev/null | wc -l) - current_branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null) - - if [[ $current_branch == 'HEAD' ]]; then detached=true; else detached=false; fi if [[ $is_a_git_repo == true && $number_of_logs == 0 ]]; then just_init=true; fi if [[ $is_a_git_repo == true && $number_of_logs -gt 0 ]]; then + current_branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null) + if [[ $current_branch == 'HEAD' ]]; then detached=true; else detached=false; fi + 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 @@ -140,7 +139,7 @@ function build_prompt { if [[ $just_init == true ]]; then PS1="${PS1}${red}detached" else - PS1="${PS1}${on}(${current_commit_hash_abbrev})" + PS1="${PS1}${on}(${current_commit_hash})" fi else if [[ $has_upstream == true ]]; then From 11b67218d14a568d4fd3b852186f80556b6925a6 Mon Sep 17 00:00:00 2001 From: Yehor Lvivski Date: Thu, 7 Nov 2013 19:33:53 +0200 Subject: [PATCH 05/13] limit log for faster execution --- prompt.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prompt.sh b/prompt.sh index 90e6c2e..3488e1f 100644 --- a/prompt.sh +++ b/prompt.sh @@ -57,7 +57,7 @@ function build_prompt { current_commit_hash=$(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 - number_of_logs=$(git log --pretty=oneline 2> /dev/null | wc -l) + number_of_logs=$(git log --pretty=oneline -n1 2> /dev/null | wc -l) if [[ $is_a_git_repo == true && $number_of_logs == 0 ]]; then just_init=true; fi if [[ $is_a_git_repo == true && $number_of_logs -gt 0 ]]; then From babaae4d2eae01fe9ffa263ed1414bcf1f86b51f Mon Sep 17 00:00:00 2001 From: Yehor Lvivski Date: Thu, 7 Nov 2013 20:11:40 +0200 Subject: [PATCH 06/13] silent git stash --- prompt.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prompt.sh b/prompt.sh index 3488e1f..8b4d8d6 100644 --- a/prompt.sh +++ b/prompt.sh @@ -111,7 +111,7 @@ function build_prompt { will_rebase=$(git config --get branch.${current_branch}.rebase 2> /dev/null) - number_of_stashes=$(git stash list | wc -l) + number_of_stashes=$(git stash list 2> /dev/null | wc -l) if [[ $number_of_stashes -gt 0 ]]; then has_stashes=true; else has_stashes=false; fi else is_on_a_tag=false From 11e23a2c251974580698357e7dbd56a04108f793 Mon Sep 17 00:00:00 2001 From: Yehor Lvivski Date: Fri, 8 Nov 2013 01:16:02 +0200 Subject: [PATCH 07/13] super fast stash detection --- prompt.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/prompt.sh b/prompt.sh index 8b4d8d6..acad366 100644 --- a/prompt.sh +++ b/prompt.sh @@ -54,7 +54,7 @@ function build_prompt { PS1="" # Git info - current_commit_hash=$(git rev-parse --short HEAD 2> /dev/null) + current_commit_hash=$(git rev-parse HEAD 2> /dev/null) if [[ -n $current_commit_hash ]]; then is_a_git_repo=true; else is_a_git_repo=false; fi number_of_logs=$(git log --pretty=oneline -n1 2> /dev/null | wc -l) @@ -111,7 +111,7 @@ function build_prompt { will_rebase=$(git config --get branch.${current_branch}.rebase 2> /dev/null) - number_of_stashes=$(git stash list 2> /dev/null | wc -l) + number_of_stashes=$(cat .git/refs/stash 2> /dev/null | wc -l) if [[ $number_of_stashes -gt 0 ]]; then has_stashes=true; else has_stashes=false; fi else is_on_a_tag=false @@ -139,7 +139,7 @@ function build_prompt { if [[ $just_init == true ]]; then PS1="${PS1}${red}detached" else - PS1="${PS1}${on}(${current_commit_hash})" + PS1="${PS1}${on}(${current_commit_hash:0:7})" fi else if [[ $has_upstream == true ]]; then From 40b5d21fd3d4c343fee82de664bd8e073b0ad19c Mon Sep 17 00:00:00 2001 From: Yehor Lvivski Date: Fri, 8 Nov 2013 01:36:12 +0200 Subject: [PATCH 08/13] fixes --- prompt.sh | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/prompt.sh b/prompt.sh index acad366..b9c63ff 100644 --- a/prompt.sh +++ b/prompt.sh @@ -45,7 +45,7 @@ function enrich { else coloron=$on fi - if [ $use_color_off == false -a $flag == false ]; then symbol=" "; fi + if [[ $use_color_off == false && $flag == false ]]; then symbol=' '; fi if [[ $flag == true ]]; then color=$coloron; else color=$off; fi PS1="${PS1}${color}${symbol}${reset} " } @@ -59,11 +59,11 @@ function build_prompt { number_of_logs=$(git log --pretty=oneline -n1 2> /dev/null | wc -l) - if [[ $is_a_git_repo == true && $number_of_logs == 0 ]]; then just_init=true; fi - if [[ $is_a_git_repo == true && $number_of_logs -gt 0 ]]; then - current_branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null) - if [[ $current_branch == 'HEAD' ]]; then detached=true; else detached=false; fi + current_branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null) + if [[ $current_branch == 'HEAD' ]]; then detached=true; else detached=false; fi + if [[ $is_a_git_repo == true && $number_of_logs -eq 0 ]]; then just_init=true; fi + if [[ $is_a_git_repo == true && $number_of_logs -gt 0 ]]; then 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 @@ -105,7 +105,7 @@ function build_prompt { if [[ $commits_ahead -gt 0 && $commits_behind -gt 0 ]]; then has_diverged=true fi - if [[ $commits_ahead == 0 && $commits_behind -gt 0 ]]; then + if [[ $commits_ahead -eq 0 && $commits_behind -gt 0 ]]; then can_fast_forward=true fi @@ -137,9 +137,9 @@ function build_prompt { fi if [[ $detached == true ]]; then if [[ $just_init == true ]]; then - PS1="${PS1}${red}detached" + PS1="${PS1} ${red}detached" else - PS1="${PS1}${on}(${current_commit_hash:0:7})" + PS1="${PS1} ${on}(${current_commit_hash:0:7})" fi else if [[ $has_upstream == true ]]; then @@ -166,10 +166,10 @@ function build_prompt { fi if [[ $display_tag == true ]]; then - PS1="${PS1}${yellow}${is_on_a_tag_symbol}${reset}" + PS1="${PS1} ${yellow}${is_on_a_tag_symbol}${reset}" fi if [[ $display_tag_name == true && $is_on_a_tag == true ]]; then - PS1="${PS1}${yellow}[${tag_at_current_commit}]${reset}" + PS1="${PS1} ${yellow}[${tag_at_current_commit}]${reset}" fi fi From 15854f6086284f78fe010f33ee54c6c83a1dc4c1 Mon Sep 17 00:00:00 2001 From: Yehor Lvivski Date: Fri, 8 Nov 2013 01:59:55 +0200 Subject: [PATCH 09/13] new structure --- prompt.sh | 82 +++++++++++++++++++++++++++---------------------------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/prompt.sh b/prompt.sh index b9c63ff..7cb9bcb 100644 --- a/prompt.sh +++ b/prompt.sh @@ -57,64 +57,64 @@ function build_prompt { current_commit_hash=$(git rev-parse HEAD 2> /dev/null) if [[ -n $current_commit_hash ]]; then is_a_git_repo=true; else is_a_git_repo=false; fi - number_of_logs=$(git log --pretty=oneline -n1 2> /dev/null | wc -l) + if [[ $is_a_git_repo == true ]]; then + current_branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null) + if [[ $current_branch == 'HEAD' ]]; then detached=true; else detached=false; fi - current_branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null) - if [[ $current_branch == 'HEAD' ]]; then detached=true; else detached=false; fi + number_of_logs=$(git log --pretty=oneline -n1 2> /dev/null | wc -l) + if [[ $number_of_logs -eq 0 ]]; then + just_init=true + else + 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 - if [[ $is_a_git_repo == true && $number_of_logs -eq 0 ]]; then just_init=true; fi - if [[ $is_a_git_repo == true && $number_of_logs -gt 0 ]]; then - 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 + git_status=$(git status --porcelain 2> /dev/null) - git_status=$(git status --porcelain 2> /dev/null) + if [[ $git_status =~ ^.M ]]; then has_modifications=true; else has_modifications=false; fi - if [[ $git_status =~ ^.M ]]; then has_modifications=true; else has_modifications=false; fi + if [[ $git_status =~ ^M ]]; then has_modifications_cached=true; else has_modifications_cached=false; fi - if [[ $git_status =~ ^M ]]; then has_modifications_cached=true; else has_modifications_cached=false; fi + if [[ $git_status =~ ^A ]]; then has_adds=true; else has_adds=false; fi - if [[ $git_status =~ ^A ]]; then has_adds=true; else has_adds=false; fi + if [[ $git_status =~ ^.D ]]; then has_deletions=true; else has_deletions=false; fi - if [[ $git_status =~ ^.D ]]; then has_deletions=true; else has_deletions=false; fi + if [[ $git_status =~ ^D ]]; then has_deletions_cached=true; else has_deletions_cached=false; fi - if [[ $git_status =~ ^D ]]; then has_deletions_cached=true; else has_deletions_cached=false; fi + if [[ $git_status =~ ^[MAD] && ! $git_status =~ ^.[MAD\?] ]]; then ready_to_commit=true; else ready_to_commit=false; fi - if [[ $git_status =~ ^[MAD] && ! $git_status =~ ^.[MAD\?] ]]; then ready_to_commit=true; else ready_to_commit=false; fi + if [[ $git_status =~ ^\?\? ]]; then has_untracked_files=true; else has_untracked_files=false; fi - if [[ $git_status =~ ^\?\? ]]; then has_untracked_files=true; else has_untracked_files=false; fi + tag_at_current_commit=$(git describe --exact-match --tags $current_commit_hash 2> /dev/null) + if [[ -n $tag_at_current_commit ]]; then is_on_a_tag=true; else is_on_a_tag=false; fi - tag_at_current_commit=$(git describe --exact-match --tags $current_commit_hash 2> /dev/null) - if [[ -n $tag_at_current_commit ]]; then is_on_a_tag=true; else is_on_a_tag=false; fi - - has_diverged=false - can_fast_forward=false + has_diverged=false + can_fast_forward=false - commits_diff=$(git log --pretty=oneline --topo-order --left-right ${current_commit_hash}...${upstream} 2> /dev/null) + commits_diff=$(git log --pretty=oneline --topo-order --left-right ${current_commit_hash}...${upstream} 2> /dev/null) - OLD_IFS=$IFS # save ifs - IFS=$'\n' + OLD_IFS=$IFS # save ifs + IFS=$'\n' - commits_ahead=(${commits_diff//>*/}) - commits_behind=(${commits_diff//<*/}) + commits_ahead=(${commits_diff//>*/}) + commits_behind=(${commits_diff//<*/}) - IFS=$OLD_IFS # restore ifs + IFS=$OLD_IFS # restore ifs - commits_ahead=${#commits_ahead[@]} - commits_behind=${#commits_behind[@]} + commits_ahead=${#commits_ahead[@]} + commits_behind=${#commits_behind[@]} - if [[ $commits_ahead -gt 0 && $commits_behind -gt 0 ]]; then - has_diverged=true + if [[ $commits_ahead -gt 0 && $commits_behind -gt 0 ]]; then + has_diverged=true + fi + if [[ $commits_ahead -eq 0 && $commits_behind -gt 0 ]]; then + can_fast_forward=true + fi + + will_rebase=$(git config --get branch.${current_branch}.rebase 2> /dev/null) + + number_of_stashes=$(wc -l 2> /dev/null < .git/refs/stash) + if [[ $number_of_stashes -gt 0 ]]; then has_stashes=true; else has_stashes=false; fi fi - if [[ $commits_ahead -eq 0 && $commits_behind -gt 0 ]]; then - can_fast_forward=true - fi - - will_rebase=$(git config --get branch.${current_branch}.rebase 2> /dev/null) - - number_of_stashes=$(cat .git/refs/stash 2> /dev/null | wc -l) - if [[ $number_of_stashes -gt 0 ]]; then has_stashes=true; else has_stashes=false; fi - else - is_on_a_tag=false fi if [[ $is_a_git_repo == true ]]; then @@ -165,7 +165,7 @@ function build_prompt { fi fi - if [[ $display_tag == true ]]; then + if [[ $display_tag == true && $is_on_a_tag == true ]]; then PS1="${PS1} ${yellow}${is_on_a_tag_symbol}${reset}" fi if [[ $display_tag_name == true && $is_on_a_tag == true ]]; then From 6a99f86e656bf9615207d70f6a0c897d4c7368b9 Mon Sep 17 00:00:00 2001 From: Yehor Lvivski Date: Fri, 8 Nov 2013 02:06:11 +0200 Subject: [PATCH 10/13] remove magic --- prompt.sh | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/prompt.sh b/prompt.sh index 7cb9bcb..986efdb 100644 --- a/prompt.sh +++ b/prompt.sh @@ -92,16 +92,8 @@ function build_prompt { commits_diff=$(git log --pretty=oneline --topo-order --left-right ${current_commit_hash}...${upstream} 2> /dev/null) - OLD_IFS=$IFS # save ifs - IFS=$'\n' - - commits_ahead=(${commits_diff//>*/}) - commits_behind=(${commits_diff//<*/}) - - IFS=$OLD_IFS # restore ifs - - commits_ahead=${#commits_ahead[@]} - commits_behind=${#commits_behind[@]} + commits_ahead=$(grep -c '^<' <<< "$commits_diff") + commits_behind=$(grep -c '^>' <<< "$commits_diff") if [[ $commits_ahead -gt 0 && $commits_behind -gt 0 ]]; then has_diverged=true From 237100e044a46afacdc95d194d69fa7e932d8efd Mon Sep 17 00:00:00 2001 From: Yehor Lvivski Date: Fri, 8 Nov 2013 02:09:40 +0200 Subject: [PATCH 11/13] gix trailing tabs --- prompt.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prompt.sh b/prompt.sh index 986efdb..9501053 100644 --- a/prompt.sh +++ b/prompt.sh @@ -89,7 +89,7 @@ function build_prompt { has_diverged=false can_fast_forward=false - + commits_diff=$(git log --pretty=oneline --topo-order --left-right ${current_commit_hash}...${upstream} 2> /dev/null) commits_ahead=$(grep -c '^<' <<< "$commits_diff") From 0a858c855845c02a7f4c32bc2a43e68449beb69d Mon Sep 17 00:00:00 2001 From: Yehor Lvivski Date: Fri, 8 Nov 2013 02:26:26 +0200 Subject: [PATCH 12/13] remove external commands --- prompt.sh | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/prompt.sh b/prompt.sh index 9501053..589f553 100644 --- a/prompt.sh +++ b/prompt.sh @@ -91,9 +91,15 @@ function build_prompt { can_fast_forward=false commits_diff=$(git log --pretty=oneline --topo-order --left-right ${current_commit_hash}...${upstream} 2> /dev/null) + commits_ahead=0 + commits_behind=0 - commits_ahead=$(grep -c '^<' <<< "$commits_diff") - commits_behind=$(grep -c '^>' <<< "$commits_diff") + if [[ $commits_diff =~ ^\<(..) ]]; then + commits_ahead = ${#BASH_REMATCH[@]} + fi + if [[ $commits_diff =~ ^\>(..) ]]; then + commits_behind = ${#BASH_REMATCH[@]} + fi if [[ $commits_ahead -gt 0 && $commits_behind -gt 0 ]]; then has_diverged=true From 5f0a11823de8107ecb72104f974fb96882ae3249 Mon Sep 17 00:00:00 2001 From: Yehor Lvivski Date: Fri, 8 Nov 2013 02:46:53 +0200 Subject: [PATCH 13/13] fix upstream --- prompt.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prompt.sh b/prompt.sh index 589f553..384f736 100644 --- a/prompt.sh +++ b/prompt.sh @@ -66,7 +66,7 @@ function build_prompt { just_init=true else 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 + if [[ -n $upstream ]]; then has_upstream=true; else has_upstream=false; fi git_status=$(git status --porcelain 2> /dev/null)