Skip to content

Instantly share code, notes, and snippets.

@lewinski
Created August 26, 2015 15:18
Show Gist options
  • Save lewinski/d1471058fe2320ada435 to your computer and use it in GitHub Desktop.
Save lewinski/d1471058fe2320ada435 to your computer and use it in GitHub Desktop.
COLOR_NONE="\[\e[0m\]"
BLACK_BOLD="\[\e[30;1m\]"
RED_BOLD="\[\e[31;1m\]"
GREEN_BOLD="\[\e[32;1m\]"
BROWN_BOLD="\[\e[33;1m\]"
BLUE_BOLD="\[\e[34;1m\]"
PINK_BOLD="\[\e[35;1m\]"
CYAN_BOLD="\[\e[36;1m\]"
WHITE_BOLD="\[\e[37;1m\]"
LIGHTNING_BOLT="⚡"
UP_ARROW="↑"
DOWN_ARROW="↓"
UD_ARROW="↕"
FF_ARROW="→"
MIDDOT="•"
function parse_git_branch {
branch_pattern="^On branch ([^${IFS}]*)"
remote_pattern_ahead="Your branch is ahead of"
remote_pattern_behind="Your branch is behind"
remote_pattern_ff="Your branch (.*) can be fast-forwarded."
diverge_pattern="Your branch and (.*) have diverged"
git_status="$(git status 2> /dev/null)"
if [[ ! ${git_status} =~ ${branch_pattern} ]]; then
# Rebasing?
toplevel=$(git rev-parse --show-toplevel 2> /dev/null)
[[ -z "$toplevel" ]] && return
[[ -d "$toplevel/.git/rebase-merge" || -d "$toplevel/.git/rebase-apply" ]] && {
sha_file="$toplevel/.git/rebase-merge/stopped-sha"
[[ -e "$sha_file" ]] && {
sha=`cat "${sha_file}"`
}
echo "${PINK_BOLD}(rebase in progress)${COLOR_NONE} ${sha}"
}
return
fi
branch=${BASH_REMATCH[1]}
# Dirty?
if [[ ! ${git_status} =~ "working directory clean" ]]; then
[[ ${git_status} =~ "modified:" ]] && {
git_is_dirty="${RED_BOLD}${LIGHTNING_BOLT}"
}
[[ ${git_status} =~ "Untracked files" ]] && {
git_is_dirty="${git_is_dirty}${BLACK_BOLD}${MIDDOT}"
}
[[ ${git_status} =~ "new file:" ]] && {
git_is_dirty="${git_is_dirty}${GREEN_BOLD}+"
}
[[ ${git_status} =~ "deleted:" ]] && {
git_is_dirty="${git_is_dirty}${RED_BOLD}-"
}
[[ ${git_status} =~ "renamed:" ]] && {
git_is_dirty="${git_is_dirty}${BROWN_BOLD}→"
}
fi
# Are we ahead of, behind, or diverged from the remote?
if [[ ${git_status} =~ ${remote_pattern_ahead} ]]; then
remote="${BLACK_BOLD}${UP_ARROW}"
elif [[ ${git_status} =~ ${remote_pattern_behind} ]]; then
remote="${BLACK_BOLD}${DOWN_ARROW}"
elif [[ ${git_status} =~ ${diverge_pattern} ]]; then
remote="${BLACK_BOLD}${UD_ARROW}"
fi
if [[ ${git_status} =~ ${remote_pattern_ff} ]]; then
remote_ff="${BLACK_BOLD}${FF_ARROW}"
fi
echo "${remote}${remote_ff}${BROWN_BOLD}(${branch})${COLOR_NONE}${git_is_dirty}${COLOR_NONE}"
}
function simple_git_branch {
branch=$(git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/')
echo "${BROWN_BOLD}${branch}"
}
function set_prompt {
PS1="${GREEN_BOLD}\\u"
PS1="${PS1}${BLACK_BOLD}@"
PS1="${PS1}${CYAN_BOLD}\\h"
PS1="${PS1}${BLACK_BOLD}:"
PS1="${PS1}${BLUE_BOLD}\\w"
PS1="${PS1}$(parse_git_branch)"
PS1="${PS1}${COLOR_NONE} \\$ "
}
PROMPT_COMMAND=set_prompt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment