public
Created

LegionSB's git prompt

  • Download Gist
colors
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
##################
# ~/.bash/colors #
##################
 
#!/bin/bash
# vim:set syntax=bash
 
## Colors
# Normal
BLACK='\033[0;30m'
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
WHITE='\033[0;37m'
 
# Bold
BLACK_BOLD='\033[1;30m'
RED_BOLD='\033[1;31m'
GREEN_BOLD='\033[1;32m'
YELLOW_BOLD='\033[1;33m'
BLUE_BOLD='\033[1;34m'
PURPLE_BOLD='\033[1;35m'
CYAN_BOLD='\033[1;36m'
WHITE_BOLD='\033[1;37m'
 
# Underline
BLACK_UNDERLINE='\033[4;30m'
RED_UNDERLINE='\033[4;31m'
GREEN_UNDERLINE='\033[4;32m'
YELLOW_UNDERLINE='\033[4;33m'
BLUE_UNDERLINE='\033[4;34m'
PURPLE_UNDERLINE='\033[4;35m'
CYAN_UNDERLINE='\033[4;36m'
WHITE_UNDERLINE='\033[4;37m'
 
# Background
BLACK_BG='\033[40m'
RED_BG='\033[41m'
GREEN_BG='\033[42m'
YELLOW_BG='\033[43m'
BLUE_BG='\033[44m'
PURPLE_BG='\033[45m'
CYAN_BG='\033[46m'
WHITE_BG='\033[47m'
 
# Reset colors back to terminal default
RESET='\033[m'
vcs
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
###############
# ~/.bash/vcs #
###############
 
#!/bin/bash
# vim:set syntax=bash
 
# Help and inspiration taken from:
# http://jeditoolkit.com/2010/09/04/git-status-in-bash-prompt.html
# http://asimilatorul.com/index.php/2010/04/13/bash-prompt-with-git-branch-and-status/
# http://gwolf.org/blog/my-git-tips
 
source ~/.bash/colors
 
# Git functions
function parse_git_dirty {
[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "true"
}
function parse_git_branch {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/\1/"
}
function parse_git_commit_hash {
git log -n 1 --format="%h" 2> /dev/null
}
 
function git_output {
DIRTY=$(parse_git_dirty)
BRANCH=$(parse_git_branch)
HASH=$(parse_git_commit_hash)
if [ -n "$BRANCH" ]; then
 
# Branch color - green for master, yellow for development, cyan for other
BRANCH_COLOR=${WHITE_BOLD}
if [ "$BRANCH" = "master" ]; then
BRANCH_COLOR=${BRANCH_COLOR}${RED_BOLD}
elif [ "$BRANCH" = "development" ]; then
BRANCH_COLOR=${BRANCH_COLOR}${YELLOW_BOLD}
else
BRANCH_COLOR=${BRANCH_COLOR}${CYAN_BOLD}
fi
if [ -n "$DIRTY" ]; then
status=$(git status --porcelain 2> /dev/null)
mod=$(echo "$status" | grep '^[A-Z ]M ' | wc -l | tr -s ' ' | sed 's/^[ ]//g' 2> /dev/null)
stg=$(echo "$status" | grep '^[A-Z]' | wc -l | tr -s ' ' | sed 's/^[ ]//g' 2> /dev/null)
del=$(echo "$status" | grep '^[A-Z ]D' | wc -l | tr -s ' ' | sed 's/^[ ]//g' 2> /dev/null)
unt=$(echo "$status" | grep '?? ' | wc -l | tr -s ' ' | sed 's/^[ ]//g' 2> /dev/null)
if [ $mod != 0 ]; then MODIFIED="${MODIFIED} ${YELLOW}${mod}M${RESET}"; fi
if [ $del != 0 ]; then MODIFIED="${MODIFIED} ${RED}${del}D${RESET}"; fi
if [ $unt != 0 ]; then MODIFIED="${MODIFIED} ${RED}${unt}U${RESET}"; fi
if [ $stg != 0 ]; then MODIFIED="${MODIFIED} ${GREEN}${stg}S${RESET}"; fi
fi
echo -e "\n${RESET}[git: (${HASH}) ${BRANCH_COLOR}${BRANCH}${RESET}${MODIFIED}] "
elif [ x`git config core.bare`x = "xtruex" ]; then
echo -e "\n${RESET}[git: ${CYAN_BOLD}bare repo${RESET}] "
fi
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.