]>
Commit | Line | Data |
---|---|---|
9d0d2184 | 1 | # NOTE: |
893f1bfe | 2 | # This code is known to work with bash |
9d0d2184 | 3 | |
5b76c9e3 | 4 | # the code below requires bash 4.x, skip if earlier |
1a003a0e | 5 | test ${BASH_VERSION%%.*} -lt 4 && return 1 |
5b76c9e3 | 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 | |
893f1bfe | 12 | # - shows current branch |
9d0d2184 ER |
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 | |
893f1bfe | 46 | git rev-parse --git-dir &> /dev/null || return |
9d0d2184 | 47 | |
9d0d2184 ER |
48 | local state remote branch |
49 | ||
893f1bfe ER |
50 | # without branch, nothing is shown; don't bother further |
51 | branch=$(git symbolic-ref --short HEAD 2>/dev/null) || return | |
9d0d2184 | 52 | |
893f1bfe | 53 | if [ -n "$(git status -s 2> /dev/null)" ]; then |
bcc4434f | 54 | state="${RED}★" |
9d0d2184 ER |
55 | fi |
56 | ||
893f1bfe ER |
57 | # http://stackoverflow.com/a/3278427 |
58 | local=$(git rev-parse @) | |
59 | remote=$(git rev-parse '@{u}') | |
60 | base=$(git merge-base @ '@{u}') | |
9d0d2184 | 61 | |
893f1bfe ER |
62 | if [ $local = $remote ]; then |
63 | remote="" | |
64 | elif [ $local = $base ]; then | |
65 | remote="${YELLOW}↓" | |
66 | elif [ $remote = $base ]; then | |
67 | remote="${YELLOW}↑" | |
68 | else | |
9d0d2184 ER |
69 | remote="${YELLOW}↕" |
70 | fi | |
71 | ||
893f1bfe | 72 | echo " (${branch})${remote}${state}" |
9d0d2184 ER |
73 | } |
74 | ||
75 | # cache requires bash 4.x | |
8eb455ec | 76 | declare -A __package_update_rpmversion_cache=() |
9d0d2184 ER |
77 | __package_update_rpmversion() { |
78 | # extract vars from cache | |
79 | set -- ${__package_update_rpmversion_cache[$PWD]} | |
80 | local specfile=$1 version=$2 mtime=$3 | |
81 | ||
82 | # invalidate cache | |
83 | if [ -f "$specfile" ]; then | |
84 | local stat | |
85 | stat=$(stat -c %Y $specfile) | |
86 | if [ $mtime ] && [ $stat -gt $mtime ]; then | |
87 | unset version | |
88 | fi | |
89 | mtime=$stat | |
90 | else | |
91 | # reset cache, .spec may be renamed | |
92 | unset version specfile | |
93 | fi | |
94 | ||
95 | # we have cached version | |
96 | test -n "$version" && return | |
97 | ||
98 | # needs to be one file | |
99 | specfile=${specfile:-$(\ls *.spec 2>/dev/null)} | |
100 | if [ ! -f "$specfile" ]; then | |
101 | unset __package_update_rpmversion_cache[$PWD] | |
102 | return | |
103 | fi | |
104 | ||
105 | mtime=${mtime:-$(stat -c %Y $specfile)} | |
106 | ||
107 | # give only first version (ignore subpackages) | |
108 | version=$(rpm --define "_specdir $PWD" --specfile $specfile -q --qf '%{VERSION}\n' | head -n1) | |
e4fc5755 | 109 | __package_update_rpmversion_cache[$PWD]="$specfile ${version:-ERR} $mtime" |
9d0d2184 ER |
110 | } |
111 | ||
112 | __package_rpmversion() { | |
113 | # extract vars from cache | |
114 | set -- ${__package_update_rpmversion_cache[$PWD]} | |
115 | # print version | |
116 | echo $2 | |
117 | } |