2 # This code works known to work for bash
4 # To use it, source this file and set $PROMPT_COMMAND env var:
5 # PROMPT_COMMAND=__bash_prompt_command
8 # A colorized bash prompt
9 # - shows curret branch
10 # - shows if branch is up to date/ahead/behind
11 # - shows if last command exited with error (red)
13 __bash_prompt_command() {
14 local previous_return_value=$?
16 local RED="\[\033[0;31m\]"
17 local YELLOW="\[\033[0;33m\]"
18 local GREEN="\[\033[0;32m\]"
19 local BLUE="\[\033[0;34m\]"
20 local LIGHT_RED="\[\033[1;31m\]"
21 local LIGHT_GREEN="\[\033[1;32m\]"
22 local WHITE="\[\033[1;37m\]"
23 local LIGHT_GRAY="\[\033[0;37m\]"
24 local COLOR_NONE="\[\e[0m\]"
26 # if we are in rpm subdir and have exactly one .spec in the dir, include package version
27 __package_update_rpmversion
28 local rpmver=$(__package_rpmversion)
30 local prompt="${BLUE}[${RED}\w${GREEN}${rpmver:+($rpmver)}$(__bash_parse_git_branch)${BLUE}]${COLOR_NONE} "
31 if [ $previous_return_value -eq 0 ]; then
34 PS1="${prompt}${RED}➔${COLOR_NONE} "
38 # helper for __bash_prompt_command
39 # command line (git) coloring
40 # note we use "\" here to avoid any "git" previous alias/func
41 __bash_parse_git_branch() {
42 # not in git dir. return early
43 \git rev-parse --git-dir &> /dev/null || return
45 local git_status branch_pattern remote_pattern diverge_pattern
46 local state remote branch
48 git_status=$(\git -c color.ui=no status 2> /dev/null)
49 branch_pattern="^On branch ([^${IFS}]*)"
50 remote_pattern="Your branch is (behind|ahead) "
51 diverge_pattern="Your branch and (.*) have diverged"
53 if [[ ! ${git_status} =~ "working directory clean" ]]; then
57 # add an else if or two here if you want to get more specific
58 if [[ ${git_status} =~ ${remote_pattern} ]]; then
59 if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then
66 if [[ ${git_status} =~ ${diverge_pattern} ]]; then
70 if [[ ${git_status} =~ ${branch_pattern} ]]; then
71 branch=${BASH_REMATCH[1]}
72 echo " (${branch})${remote}${state}"
76 # cache requires bash 4.x
77 declare -A __package_update_rpmversion_cache
78 __package_update_rpmversion() {
79 # extract vars from cache
80 set -- ${__package_update_rpmversion_cache[$PWD]}
81 local specfile=$1 version=$2 mtime=$3
84 if [ -f "$specfile" ]; then
86 stat=$(stat -c %Y $specfile)
87 if [ $mtime ] && [ $stat -gt $mtime ]; then
92 # reset cache, .spec may be renamed
93 unset version specfile
96 # we have cached version
97 test -n "$version" && return
99 # needs to be one file
100 specfile=${specfile:-$(\ls *.spec 2>/dev/null)}
101 if [ ! -f "$specfile" ]; then
102 unset __package_update_rpmversion_cache[$PWD]
106 mtime=${mtime:-$(stat -c %Y $specfile)}
108 # give only first version (ignore subpackages)
109 version=$(rpm --define "_specdir $PWD" --specfile $specfile -q --qf '%{VERSION}\n' | head -n1)
110 __package_update_rpmversion_cache[$PWD]="$specfile ${version:-?} $mtime"
113 __package_rpmversion() {
114 # extract vars from cache
115 set -- ${__package_update_rpmversion_cache[$PWD]}