Last active
February 13, 2017 17:09
-
-
Save davidkuster/8e242676073507c74009 to your computer and use it in GitHub Desktop.
bash script to include Git and Mercurial branch and status in command prompt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# DESCRIPTION: | |
# | |
# Set the bash prompt according to: | |
# * the branch/status of the current git repository | |
# * the branch of the current subversion repository | |
# * the return value of the previous command | |
# | |
# USAGE: | |
# | |
# 1. Save this file as ~/.git_svn_bash_prompt | |
# 2. Add the following line to the end of your ~/.profile or ~/.bash_profile: | |
# . ~/.git_svn_bash_prompt | |
# | |
# AUTHOR: | |
# | |
# Scott Woods <scott@westarete.com> | |
# West Arete Computing | |
# | |
# Based on work by halbtuerke and lakiolen. | |
# | |
# http://gist.github.com/31967 | |
# The various escape codes that we can use to color our prompt. | |
RED="\[\033[0;31m\]" | |
YELLOW="\[\033[0;33m\]" | |
CYAN="\[\033[0;36m\]" | |
BLUE="\[\033[0;34m\]" | |
GREEN="\[\033[0;32m\]" | |
LIGHT_RED="\[\033[1;31m\]" | |
LIGHT_GREEN="\[\033[1;32m\]" | |
LIGHT_YELLOW="\[\033[1;33m\]" | |
LIGHT_BLUE="\[\033[1;34m\]" | |
WHITE="\[\033[1;37m\]" | |
LIGHT_GRAY="\[\033[0;37m\]" | |
COLOR_NONE="\[\e[0m\]" | |
NORMAL="\[\033[0m\]" | |
# Detect whether the current directory is a git repository. | |
function is_git_repository { | |
# this only checks if the current directory is a git repo | |
#test -d .git | |
# this checks if the current or parent dir is a git repo | |
git status > /dev/null 2>&1 | |
} | |
# Detect whether the current directory is a hg repository. | |
function is_hg_repository { | |
test -d .hg | |
} | |
# Determine the branch/state information for this git repository. | |
function set_git_branch { | |
# Capture the output of the "git status" command. | |
git_status="$(git status 2> /dev/null)" | |
# Set color based on clean/staged/dirty. | |
if [[ ${git_status} =~ "Untracked files" ]]; then | |
remote="" | |
state="${RED}" | |
elif [[ ${git_status} =~ "not staged" ]]; then | |
remote="" | |
state="${RED}" | |
elif [[ ${git_status} =~ "Changes to be committed" ]]; then | |
remote="" | |
state="${YELLOW}" | |
# Set arrow icon based on status against remote. | |
elif [[ ${git_status} =~ "Your branch is ahead" ]]; then | |
remote="↑" | |
state="${GREEN}" | |
elif [[ ${git_status} =~ "Your branch is behind" ]]; then | |
remote="↓" | |
state="${RED}" | |
elif [[ ${git_status} =~ "Your branch and (.*) have diverged" ]]; then | |
remote="↕" | |
state="${YELLOW}" | |
else | |
remote="" | |
state="${CYAN}" | |
fi | |
# Get the name of the branch. | |
branch_pattern="On branch ([^${IFS}]*)" | |
if [[ ${git_status} =~ ${branch_pattern} ]]; then | |
branch=${BASH_REMATCH[1]} | |
fi | |
# Set the final branch string. | |
BRANCH="${state}(${branch})${remote}${COLOR_NONE} " | |
} | |
# Determine the branch information for this hg repository. | |
function set_hg_branch { | |
# Capture the output of the "hg summary" command. | |
hg_status="$(hg summary 2> /dev/null)" | |
localstate="${GREEN}" | |
#remote="" | |
#remotestate="" | |
remote_out="↑" | |
remote_in="↓" | |
remotestate_out="" | |
remotestate_in="" | |
# Set color based on clean/staged/dirty. | |
if [[ ${hg_status} =~ "added" ]]; then | |
localstate="${YELLOW}" | |
elif [[ ${hg_status} =~ "modified" ]]; then | |
localstate="${LIGHT_YELLOW}" | |
elif [[ ${hg_status} =~ "unknown" ]]; then | |
localstate="${LIGHT_BLUE}" | |
fi | |
if [[ ${check_threshold} -lt 1 ]] || [[ ${check_threshold} -gt 10 ]]; then | |
hg_status_remote="$(hg summary --remote 2> /dev/null)" | |
check_threshold=1 | |
else | |
check_threshold=$((check_threshold+1)) | |
fi | |
#if [[ ${hg_status_remote} =~ "outgoing" ]] && [[ ${hg_status_remote} =~ "incoming" ]]; then | |
# remote="↕" | |
# remotestate="${YELLOW}" | |
#elif [[ ${hg_status_remote} =~ "incoming" ]]; then | |
# remote="↓" | |
# remotestate="${RED}" | |
#elif [[ ${hg_status_remote} =~ "outgoing" ]]; then | |
# remote="↑" | |
# remotestate="${GREEN}" | |
#else | |
# remotestate="${CYAN}" | |
#fi | |
# Set arrow icon based on status against remote. | |
if [[ ${hg_status_remote} =~ "incoming" ]]; then | |
remotestate_in="${RED}" | |
else | |
remotestate_in="${GREEN}" | |
fi | |
if [[ ${hg_status_remote} =~ "outgoing" ]]; then | |
remotestate_out="${RED}" | |
else | |
remotestate_out="${GREEN}" | |
fi | |
# Get the name of the branch. | |
branch=$(hg branch 2> /dev/null) | |
# Set the final branch string. | |
#BRANCH="${localstate}(${branch})${remotestate} ${remote}${COLOR_NONE} " | |
BRANCH="${localstate}(${branch}) ${remotestate_out}${remote_out}${remotestate_in}${remote_in}${COLOR_NONE} " | |
} | |
# Return the prompt symbol to use, colorized based on the return value of the | |
# previous command. | |
function set_prompt_symbol () { | |
if test $1 -eq 0 ; then | |
PROMPT_SYMBOL="\$" | |
else | |
PROMPT_SYMBOL="${RED}\$${COLOR_NONE}" | |
fi | |
} | |
# Set the full bash prompt. | |
function set_bash_prompt () { | |
# Set the PROMPT_SYMBOL variable. We do this first so we don't lose the | |
# return value of the last command. | |
set_prompt_symbol $? | |
# Set the BRANCH variable. | |
if is_git_repository ; then | |
set_git_branch | |
elif is_hg_repository ; then | |
set_hg_branch | |
else | |
BRANCH='' | |
fi | |
# Set the bash prompt variable. | |
#PS1="[ ${MAGENTA}\u@\h ${LIGHT_GREEN}\w ${BRANCH}${NORMAL}]\n${BLUE}\$${NORMAL} " | |
PS1="\w ${BRANCH}${NORMAL}\n\u@\h \$ " | |
# in iTerm2 windows, set tab title to the current directory | |
if [ $ITERM_SESSION_ID ]; then | |
echo -ne "\033];${PWD##*/}\007"; | |
fi | |
} | |
# Tell bash to execute this function just before displaying its prompt. | |
PROMPT_COMMAND=set_bash_prompt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment