Last active
December 17, 2015 03:19
-
-
Save pbkhrv/5542785 to your computer and use it in GitHub Desktop.
Bash prompt script that reflects the state of the git repository in the current directory by showing current branch name and several indicators. Add it to your .profile
This file contains hidden or 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
# * uncommitted changes | |
# ^ stashed changes | |
# > ahead of origin | |
# < behind origin | |
# ↕ diverged from origin | |
function parse_git_dirty { | |
[[ $(git status 2> /dev/null | tail -n1) != "nothing to commit (working directory clean)" ]] && echo "*" | |
} | |
function parse_git_stash { | |
[[ $(git stash list 2> /dev/null | tail -n1) != "" ]] && echo "^" | |
} | |
function parse_git_ahead { | |
[[ -n $(git status 2> /dev/null | grep 'Your branch is ahead of') ]] && echo ">" | |
} | |
function parse_git_behind { | |
[[ -n $(git status 2> /dev/null | grep 'Your branch is behind') ]] && echo "<" | |
} | |
function parse_git_diverge { | |
[[ -n $(git status 2> /dev/null | grep 'have diverged') ]] && echo "↕" | |
} | |
function current_git_branch { | |
git rev-parse --abbrev-ref HEAD 2> /dev/null | |
} | |
function current_git_branch_with_markers { | |
git rev-parse --abbrev-ref HEAD 2> /dev/null | sed -e "s/\(.*\)/\1$(parse_git_stash)$(parse_git_ahead)$(parse_git_behind)$(parse_git_diverge)$(parse_git_dirty)/" | |
} | |
function set_prompt { | |
local GREEN='\[\e[0;32m\]' | |
local RED='\[\e[0;31m\]' | |
local NORMAL='\[\e[0m\]' | |
PS1="$GREEN\w $RED(\$(current_git_branch_with_markers))$NORMAL $ " | |
} | |
set_prompt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment