summaryrefslogtreecommitdiff
path: root/bash-git-prompt
blob: 85d1bd0789cdb9a10db0dd9139698f8b73feb2e0 (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
#!/bin/bash

set -o pipefail

[[ "$TERM" = *color ]] && mitFarbe=true || mitFarbe=false

[ -z "$PS1Start" ] && [ -z "$PS1Ende" ] &&
  if echo "$PS1" | grep -q "\S"
  then
    PS1Ende="$(echo "$PS1" | sed 's|^.*[^\]\(\\\?[$#>]\s*\([]\\[0-9m]\)*\s*\)$|\1|')"
    PS1Start="$(echo "$PS1" | sed 's|\\\?[$#>]\s*\([]\\[0-9m]\)*\s*$||')"
  else
    PS1Ende=""
    PS1Start="$PS1"
  fi

PS1="$PS1Start"

if branches="$(git branch -a --no-color 2>/dev/null | sed 's|^\s*||')" && status=$(git status --porcelain 2> /dev/null)
then
  PS1="$PS1 "
  branch="$(
    echo "${branches}" | \
      grep '^\*' | \
      sed 's|^\*\s*||'
  )"
  echo "$status" | grep -q "^.\S" && wtClean=false || wtClean=true
  echo "$status" | grep -q "^\S" && idxClean=false || idxClean=true
  remBranch="$(
    echo "${branches}" | \
      grep '^\s*remotes/[^/]\+/'"${branch}" | \
      sed 's|^\s*\(remotes/[^/]\+/'"${branch}"'\)/.*$|\1|'
  )"
  [ -n "${remBranch}" ] && \
    [ "$(git describe --always --abbrev=0 "HEAD")" == "$(git describe --always --abbrev=0 "${remBranch}")" ] && \
    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"