summaryrefslogtreecommitdiff
path: root/bash-git-prompt.in
blob: 14640b9eb2fb59221c1613fe7353fb458c739806 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/bash

# bash-git-prompt version #VERSION#

tput colors &> /dev/null && mitFarbe=true || mitFarbe=false

if [ -z "$PS1Start" ] && [ -z "$PS1Ende" ] && [ -n "$PS1" ]
then
  export PS1Start="$(echo "$PS1" | sed 's@\(\\\[\\033\[[0-9]\+m\\]\|\s\)*\(\\\?\S\)\?\(\\\[\\033\[[0-9]\+m\\]\|\s\)*$@@')"
  export PS1Ende="${PS1:${#PS1Start}}"
fi

PS1="$PS1Start"

. #ETCDIR#/bash-git-prompt.conf

if currentContext=$(kubectl config current-context 2>/dev/null) \
&& [ -n "${currentContext}" ]; then
  if $mitFarbe; then
    if toPrint=$(echo "${currentContext}" | grep -o -- "${kubectxRegExProd}"); then
      PS1="$PS1\[\033[01;31m\]"
    elif toPrint=$(echo "${currentContext}" | grep -o -- "${kubectxRegExDev}"); then
      PS1="$PS1\[\033[01;34m\]"
    else
      toPrint="${currentContext}"
    fi
  fi
  PS1="$PS1 ${toPrint}"
  if $mitFarbe; then
    PS1="$PS1\[\033[00m\]"
  fi
fi

skip=false
if [ -n "${skipDirRegEx}" ] && pwd | grep -q "${skipDirRegEx}" &> /dev/null
then
  skip=true
fi

if ! ${skip} && status=$(git status --porcelain 2> /dev/null)
then
  [ -n "${PS1}" ] && PS1="$PS1 "
  branch="$((git symbolic-ref HEAD 2> /dev/null || git describe --always) | sed "s|^.*/||")"
  gitDir="$(git rev-parse --show-toplevel)/$(git rev-parse --git-dir)"
  if [ -e ${gitDir}/rebase-merge ] || [ -e ${gitDir}/rebase-apply ]
  then
    branch="($branch)"
  fi
  echo "$status" | grep -q "^.\S" && wtClean=false || wtClean=true
  echo "$status" | grep -q "^\S" && idxClean=false || idxClean=true

  [ $(git show-ref "${branch}" --head | \
        grep -vF ' refs/original/refs/' | \
        cut -d' ' -f1 | \
        uniq | \
        wc -l
    ) -eq 1 ] && \
    remClean=true || \
    remClean=false

  if $mitFarbe
  then
    if ! $wtClean
    then
      PS1="$PS1\[\033[01;31m\]"
    elif ! $idxClean
    then
      PS1="$PS1\[\033[01;33m\]"
    elif ! $remClean
    then
      PS1="$PS1\[\033[01;34m\]"
    else
      PS1="$PS1\[\033[01;32m\]"
    fi
  fi
  PS1="$PS1$branch"
  if $mitFarbe
  then
    PS1="$PS1\[\033[00m\]"
  else
    if ! $wtClean
    then
      PS1="$PS1!"
    elif ! $idxClean
    then
      PS1="$PS1+"
    elif ! $remClean
    then
      PS1="$PS1^"
    fi
  fi
fi

PS1="$PS1$PS1Ende"