Bash git

BASH Detected Guest 193 Views Size: 4.04 KB Posted on: Dec 6, 25 @ 8:30 PM
  1. function parse_git_branch {
  2.   if [ ! -d . ]; then
  3.     return '';
  4.   fi
  5.             # We pass in the magic token bash makes from \[ and \] (^A and ^B probably)
  6.             # so we can use them in our echo statement.
  7.  
  8.       #Bash 3.2 does not understand our \[ syntax for prompt escaping.. so
  9.       #sadl, no colors on it... but do colors on 4.x
  10.       if [ ${BASH_VERSINFO[0]} -ge 4 ]; then
  11.           gitcolorprompt=yes
  12.       fi
  13.      
  14.       if [ $gitcolorprompt-xx == 'yes-xx' ]; then
  15.                 RED="$1\e[0;31m$2"
  16.              YELLOW="$1\e[33m$2"
  17.               GREEN="$1\e[32m$2"
  18.                BLUE="$1\e[34m$2"
  19.           LIGHT_RED="$1\e[31m$2"
  20.                CYAN="$1\e[36m$2"
  21.          LIGHT_CYAN="$1\e[1;36m$2"
  22.         LIGHT_GREEN="$1\e[1;32m$2"
  23.               WHITE="$1\e[1;37m$2"
  24.          LIGHT_GRAY="$1\e[0;37m$2"
  25.          COLOR_NONE="$1\e[0m$2"
  26.  
  27.      fi
  28.  
  29.  if git rev-parse --git-dir &> /dev/null; then
  30.       git_status="$(git status 2> /dev/null)"
  31.       branch_pattern="^#? ?On branch ([^${IFS}]*)"
  32.       detached_branch_pattern="Not currently on any branch"
  33.       remote_pattern="Your branch is (.*) of"
  34.       diverge_pattern="Your branch and (.*) have diverged"
  35.       changed_pattern="Changes not staged for commit"
  36.       untracked_pattern="Untracked files:"
  37.       staged_pattern="Changes to be committed"
  38.       if [[ ${git_status} =~ "Changed but not updated" ]]; then
  39.          #state="${RED}⚡"
  40.          state="⚡"
  41.       fi
  42.       if [[ ${git_status} =~ ${changed_pattern} ]]; then
  43.           state="${state}Ⓜ "
  44.       fi
  45.       if [[ ${git_status} =~ ${untracked_pattern} ]]; then
  46.           state="${state}Ⓤ "
  47.       fi
  48.       if [[ ${git_status} =~ ${staged_pattern} ]]; then
  49.           #state="${state}Ⓢ "
  50.           state="${state}✇"
  51.       fi
  52.       # add an else if or two here if you want to get more specific
  53.       if [[ ${git_status} =~ ${remote_pattern} ]]; then
  54.           if [[ ${BASH_REMATCH[1]} == "ahead" ]]; then
  55.               #remote="${YELLOW}↑"
  56.               remote="↑"
  57.           else
  58.               #remote="${YELLOW}↓"
  59.               remote="↓"
  60.           fi
  61.       fi
  62.       if [[ ${git_status} =~ ${diverge_pattern} ]]; then
  63.         #remote="${YELLOW}↕"
  64.         remote="↕"
  65.       fi
  66.       if [[ ${git_status} =~ ${branch_pattern} ]]; then
  67.           branch=${BASH_REMATCH[1]}
  68.       elif [[ ${git_status} =~ ${detached_branch_pattern} ]]; then
  69.           branch="NO BRANCH"
  70.           #branch="${YELLOW}NO BRANCH"
  71.       fi
  72.  
  73.     if [[ ${#state} -gt "0" || ${#remote} -gt "0" ]]; then
  74.         s=" "
  75.     fi
  76.     echo -e "${CYAN}[${LIGHT_GRAY}${branch}${s}${remote}${state}${changed}${CYAN}] "
  77.     #echo -e "[${branch}${s}${remote}${state}] "
  78.   fi
  79.   PROMPT_RECURSE=0
  80. }
  81.  
  82. if [ "$PS1" ]; then
  83.    if [ $UID -eq "0" ]; then
  84.       case "$TERM" in
  85.       xterm*|rxvt*|screen)
  86.         PS1='\n\[\e]2;\u@\h:\w\a\e[1m\e[31m\]\[\e[31m\]\u\[\e[37m\]@\[\e[35m\]\h\[\e[37m\]:\[\e[32m\]\w\[\e[33m\]\[\e[31m\] #\[\e[0m\] '
  87.         ;;
  88.       *)
  89.       PS1='[\!]\u@\h \w# '
  90.       ;;
  91.       esac
  92.    else
  93.       case "$TERM" in
  94.       xterm*|rxvt*|screen)
  95.         #strings like \342\224\224 define UTF multi-byte chars. but we also have to wrap all but one piece
  96.         # of multi-byte chars in \[ non-printing \]  or line-wrapping messes up.
  97.         #PS1='\n[\!]\[\e]2;\u@\h:\w\a\e[1m\e[33m\]\[\e[36m\]\u\[\e[37m\]@\[\e[35m\]\h\[\e[37m\]:\[\e[32m\]\w\[\e[33m\]\[\e[33m\]>\[\e[0m\] \[\e[37m\]$(parse_git_branch "\[" "\]" 2>/dev/null)\[\e[0m\]'
  98.         sadness='$([[ $? != 0 ]] && echo "[\[\033[0;31m\]\[\342\234\]\227\[\033[0;37m\]]")'
  99.         tube1="\[\342\224\]\214\[\342\224\]\200"
  100.         tube2="\[\e[37m\]\[\342\224\]\224\[\342\224\]\200\[\342\224\]\200\[\342\225\]\274"
  101.         settitle="\[\e]2;\u@\h:\w\a\e[1m\e[37m\]"
  102.         PS1="$settitle\n$tube1\[\033[0;37m\]$sadness[\[\e[0;33m\]\u\[\e[37m\]@\[\e[36m\]\h\[\e[37m\]][\[\e[0;32m\]\[\e[32m\]\w\[\e[33m\]\[\e[0;37m\]]\[\e[0m\] \[\e[37m\]\$(parse_git_branch \"\[\" \"\]\" 2>/dev/null)\n$tube2 \[\e[0m\]"
  103.         ;;
  104.       *)
  105.       PS1='[\!]\u@\h \w> '
  106.       ;;
  107.       esac
  108.    fi
  109. fi
  110.  
  111. export PS1

Raw Paste

Comments 0
Login to post a comment.
  • No comments yet. Be the first.
Login to post a comment. Login or Register
We use cookies. To comply with GDPR in the EU and the UK we have to show you these.

We use cookies and similar technologies to keep this website functional (including spam protection via Google reCAPTCHA or Cloudflare Turnstile), and — with your consent — to measure usage and show ads. See Privacy.