Skip to content

Instantly share code, notes, and snippets.

@brendonrapp
Created January 11, 2012 19:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brendonrapp/1596290 to your computer and use it in GitHub Desktop.
Save brendonrapp/1596290 to your computer and use it in GitHub Desktop.
LegionSB's git prompt
##################
# ~/.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'
###############
# ~/.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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment