]> git.pld-linux.org Git - packages/rpm-build-tools.git/blobdiff - bash-prompt.sh
bash-prompt: rewritten not to rely on git status output text
[packages/rpm-build-tools.git] / bash-prompt.sh
index 739e950b06588645599a14967922bc37eb5806bf..467e4b372fac0e9dcb21da1860da98a595f7b749 100644 (file)
@@ -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,37 @@ __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
 
-       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"
-
-       if [[ ! ${git_status} =~ "working directory clean" ]]; then
-               state="${RED}⚡"
-       fi
+       # without branch, nothing is shown; don't bother further
+       branch=$(git symbolic-ref --short HEAD 2>/dev/null) || return
 
-       # 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
-                       remote="${YELLOW}↑"
-               else
-                       remote="${YELLOW}↓"
-               fi
+       if [ -n "$(git status -s 2> /dev/null)" ]; then
+               state="${RED}★"
        fi
 
-       if [[ ${git_status} =~ ${diverge_pattern} ]]; then
+       # http://stackoverflow.com/a/3278427
+       local=$(git rev-parse @)
+       remote=$(git rev-parse '@{u}')
+       base=$(git merge-base @ '@{u}')
+
+       if [ $local = $remote ]; then
+               remote=""
+       elif [ $local = $base ]; then
+               remote="${YELLOW}↓"
+       elif [ $remote = $base ]; then
+               remote="${YELLOW}↑"
+       else
                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]}
@@ -107,7 +106,7 @@ __package_update_rpmversion() {
 
        # give only first version (ignore subpackages)
        version=$(rpm --define "_specdir $PWD" --specfile $specfile -q --qf '%{VERSION}\n' | head -n1)
-       __package_update_rpmversion_cache[$PWD]="$specfile ${version:-?} $mtime"
+       __package_update_rpmversion_cache[$PWD]="$specfile ${version:-ERR} $mtime"
 }
 
 __package_rpmversion() {
This page took 0.039792 seconds and 4 git commands to generate.