Skip to content

Instantly share code, notes, and snippets.

@pedramamini
Last active August 29, 2015 14:06
Show Gist options
  • Save pedramamini/6b701190c98d14a1f5d3 to your computer and use it in GitHub Desktop.
Save pedramamini/6b701190c98d14a1f5d3 to your computer and use it in GitHub Desktop.
Git aware prompt that shows dirty state and unsynced commit state.
function parse_git_dirty() {
[[ $(git status 2> /dev/null | tail -n1) != *"working directory clean"* ]] && echo "*"
}
function parse_git_branch() {
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/\1$(parse_git_dirty)/"
}
# requires the following in .gitconfig under [alias] section:
# unpushed = !GIT_CURRENT_BRANCH=$(git name-rev --name-only HEAD) && git log origin/$GIT_CURRENT_BRANCH..$GIT_CURRENT_BRANCH --oneline
function parse_git_unpushed() {
[[ -n $(git branch 2> /dev/null) ]] && \
[[ -n $(git unpushed 2> /dev/null) ]] && \
echo -n ' (' && \
(git unpushed | wc -l | tr -d ' \n') && \
echo -n ')'
}
export PS1="\[${BOLD}${MAGENTA}\]\u \[$WHITE\]at \[$ORANGE\]\h \[$WHITE\]in \[$GREEN\]\w\[$WHITE\]\$([[ -n \$(git branch 2> /dev/null) ]] && echo \" on \")\[$PURPLE\]\$(parse_git_branch)\$(parse_git_unpushed)\[$WHITE\]\n\$ \[$RESET\]"
export PS2="\[$ORANGE\] \[$RESET\]"
export PROMPT_COMMAND="history -a"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment