X-Git-Url: http://git.pld-linux.org/?p=packages%2Frpm-build-tools.git;a=blobdiff_plain;f=bash-prompt.sh;h=5eec27643d2dc9ce6900c8fd2b289e975a7c8aec;hp=b893fd9d3efefe8a0c96620eda5a2e7cc97bb581;hb=947500436acb28d51898eff42b7eb0a6b0159faa;hpb=e4fc575551feb774003cc18480e2876cd239e5a0 diff --git a/bash-prompt.sh b/bash-prompt.sh index b893fd9..5eec276 100644 --- a/bash-prompt.sh +++ b/bash-prompt.sh @@ -1,12 +1,15 @@ # NOTE: -# This code works known to work for bash +# This code is known to work with bash + +# the code below requires bash 4.x, skip if earlier +test ${BASH_VERSION%%.*} -lt 4 && return 1 # To use it, source this file and set $PROMPT_COMMAND env var: # PROMPT_COMMAND=__bash_prompt_command # # A colorized bash prompt -# - shows curret branch +# - shows current branch # - shows if branch is up to date/ahead/behind # - shows if last command exited with error (red) # @@ -40,41 +43,41 @@ __bash_prompt_command() { # note we use "\" here to avoid any "git" previous alias/func __bash_parse_git_branch() { # not in git dir. return early - \git rev-parse --git-dir &> /dev/null || return + git rev-parse --git-dir &> /dev/null || return - local git_status branch_pattern remote_pattern diverge_pattern - local state remote branch + local state remote branch base local - git_status=$(\git -c color.ui=no status 2> /dev/null) - branch_pattern="^On branch ([^${IFS}]*)" - remote_pattern="Your branch is (behind|ahead) " - diverge_pattern="Your branch and (.*) have diverged" + # without branch, nothing is shown; don't bother further + branch=$(git symbolic-ref --short HEAD 2>/dev/null) || return - if [[ ! ${git_status} =~ "working directory clean" ]]; then - state="${RED}⚡" + if [ -n "$(git status -s 2> /dev/null)" ]; then + state="${RED}★" fi - # add an else if or two here if you want to get more specific - if [[ ${git_status} =~ ${remote_pattern} ]]; then - if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then + # http://stackoverflow.com/a/3278427 + remote=$(git rev-parse '@{u}' 2>/dev/null) + base=$(git merge-base @ '@{u}' 2>/dev/null) + if [[ -n "$remote" && -n "$base" ]]; then + local=$(git rev-parse @) + + if [[ $local = $remote ]]; then + remote="" + elif [[ $local = $base ]]; then + remote="${YELLOW}↓" + elif [[ $remote = $base ]]; then remote="${YELLOW}↑" else - remote="${YELLOW}↓" + remote="${YELLOW}↕" fi + else + remote="" fi - if [[ ${git_status} =~ ${diverge_pattern} ]]; then - remote="${YELLOW}↕" - fi - - if [[ ${git_status} =~ ${branch_pattern} ]]; then - branch=${BASH_REMATCH[1]} - echo " (${branch})${remote}${state}" - fi + echo " (${branch})${remote}${state}" } # cache requires bash 4.x -declare -A __package_update_rpmversion_cache +declare -A __package_update_rpmversion_cache=() __package_update_rpmversion() { # extract vars from cache set -- ${__package_update_rpmversion_cache[$PWD]}