]> git.pld-linux.org Git - packages/rpm-build-tools.git/blame - bash-prompt.sh
pclean: cleaning git diffs
[packages/rpm-build-tools.git] / bash-prompt.sh
CommitLineData
9d0d2184
ER
1# NOTE:
2# This code works known to work for bash
3
5b76c9e3
ER
4# the code below requires bash 4.x, skip if earlier
5test ${BASH_VERSION%%.*} -lt 4 && return
6
9d0d2184
ER
7# To use it, source this file and set $PROMPT_COMMAND env var:
8# PROMPT_COMMAND=__bash_prompt_command
9
10#
11# A colorized bash prompt
12# - shows curret branch
13# - shows if branch is up to date/ahead/behind
14# - shows if last command exited with error (red)
15#
16__bash_prompt_command() {
17 local previous_return_value=$?
18
19 local RED="\[\033[0;31m\]"
20 local YELLOW="\[\033[0;33m\]"
21 local GREEN="\[\033[0;32m\]"
22 local BLUE="\[\033[0;34m\]"
23 local LIGHT_RED="\[\033[1;31m\]"
24 local LIGHT_GREEN="\[\033[1;32m\]"
25 local WHITE="\[\033[1;37m\]"
26 local LIGHT_GRAY="\[\033[0;37m\]"
27 local COLOR_NONE="\[\e[0m\]"
28
29 # if we are in rpm subdir and have exactly one .spec in the dir, include package version
30 __package_update_rpmversion
31 local rpmver=$(__package_rpmversion)
32
33 local prompt="${BLUE}[${RED}\w${GREEN}${rpmver:+($rpmver)}$(__bash_parse_git_branch)${BLUE}]${COLOR_NONE} "
34 if [ $previous_return_value -eq 0 ]; then
35 PS1="${prompt}➔ "
36 else
37 PS1="${prompt}${RED}➔${COLOR_NONE} "
38 fi
39}
40
41# helper for __bash_prompt_command
42# command line (git) coloring
43# note we use "\" here to avoid any "git" previous alias/func
44__bash_parse_git_branch() {
45 # not in git dir. return early
46 \git rev-parse --git-dir &> /dev/null || return
47
48 local git_status branch_pattern remote_pattern diverge_pattern
49 local state remote branch
50
51 git_status=$(\git -c color.ui=no status 2> /dev/null)
52 branch_pattern="^On branch ([^${IFS}]*)"
53 remote_pattern="Your branch is (behind|ahead) "
54 diverge_pattern="Your branch and (.*) have diverged"
55
56 if [[ ! ${git_status} =~ "working directory clean" ]]; then
57 state="${RED}⚡"
58 fi
59
60 # add an else if or two here if you want to get more specific
61 if [[ ${git_status} =~ ${remote_pattern} ]]; then
62 if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then
63 remote="${YELLOW}↑"
64 else
65 remote="${YELLOW}↓"
66 fi
67 fi
68
69 if [[ ${git_status} =~ ${diverge_pattern} ]]; then
70 remote="${YELLOW}↕"
71 fi
72
73 if [[ ${git_status} =~ ${branch_pattern} ]]; then
74 branch=${BASH_REMATCH[1]}
75 echo " (${branch})${remote}${state}"
76 fi
77}
78
79# cache requires bash 4.x
8eb455ec 80declare -A __package_update_rpmversion_cache=()
9d0d2184
ER
81__package_update_rpmversion() {
82 # extract vars from cache
83 set -- ${__package_update_rpmversion_cache[$PWD]}
84 local specfile=$1 version=$2 mtime=$3
85
86 # invalidate cache
87 if [ -f "$specfile" ]; then
88 local stat
89 stat=$(stat -c %Y $specfile)
90 if [ $mtime ] && [ $stat -gt $mtime ]; then
91 unset version
92 fi
93 mtime=$stat
94 else
95 # reset cache, .spec may be renamed
96 unset version specfile
97 fi
98
99 # we have cached version
100 test -n "$version" && return
101
102 # needs to be one file
103 specfile=${specfile:-$(\ls *.spec 2>/dev/null)}
104 if [ ! -f "$specfile" ]; then
105 unset __package_update_rpmversion_cache[$PWD]
106 return
107 fi
108
109 mtime=${mtime:-$(stat -c %Y $specfile)}
110
111 # give only first version (ignore subpackages)
112 version=$(rpm --define "_specdir $PWD" --specfile $specfile -q --qf '%{VERSION}\n' | head -n1)
e4fc5755 113 __package_update_rpmversion_cache[$PWD]="$specfile ${version:-ERR} $mtime"
9d0d2184
ER
114}
115
116__package_rpmversion() {
117 # extract vars from cache
118 set -- ${__package_update_rpmversion_cache[$PWD]}
119 # print version
120 echo $2
121}
This page took 0.077037 seconds and 4 git commands to generate.