Created
August 19, 2014 19:29
-
-
Save karol-blaszczyk/6568aa096c083c8e6ed4 to your computer and use it in GitHub Desktop.
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
# git repo status in prompt functions | |
# ripped from <https://github.com/twolfson/sexy-bash-prompt/blob/master/.bash_prompt> | |
# with small changes | |
# | |
function _get_git_branch() { | |
# On branches, this will return the branch name | |
# On non-branches, (no branch) | |
_REF="$(git symbolic-ref HEAD 2> /dev/null | sed -e 's/refs\/heads\///')" | |
if [[ $_REF != "" ]]; then | |
echo $_REF | |
else | |
echo "(no branch)" | |
fi | |
} | |
function _is_branch1_behind_branch2 () { | |
# $ git log origin/master..master -1 | |
# commit 4a633f715caf26f6e9495198f89bba20f3402a32 | |
# Author: Todd Wolfson <[email protected]> | |
# Date: Sun Jul 7 22:12:17 2013 -0700 | |
# | |
# Unsynced commit | |
# Find the first log (if any) that is in branch1 but not branch2 | |
_FIRST_LOG="$(git log $1..$2 -1 2> /dev/null)" | |
# Exit with 0 if there is a first log, 1 if there is not | |
[[ -n $_FIRST_LOG ]] | |
} | |
function _branch_exists () { | |
# List remote branches | # Find our branch and exit with 0 or 1 if found/not found | |
git branch --remote 2> /dev/null | grep --quiet "$1" | |
} | |
function _parse_git_ahead () { | |
# Grab the local and remote branch | |
_BRANCH="$(_get_git_branch)" | |
_REMOTE_BRANCH=origin/"$_BRANCH" | |
# $ git log origin/master..master | |
# commit 4a633f715caf26f6e9495198f89bba20f3402a32 | |
# Author: Todd Wolfson <[email protected]> | |
# Date: Sun Jul 7 22:12:17 2013 -0700 | |
# | |
# Unsynced commit | |
# If the remote branch is behind the local branch | |
# or it has not been merged into origin (remote branch doesn't exist) | |
if (_is_branch1_behind_branch2 "$_REMOTE_BRANCH" "$_BRANCH" || | |
! _branch_exists "$_REMOTE_BRANCH"); then | |
# echo our character | |
echo 1 | |
fi | |
} | |
function _parse_git_behind () { | |
# Grab the branch | |
_BRANCH="$(_get_git_branch)" | |
_REMOTE_BRANCH=origin/"$_BRANCH" | |
# $ git log master..origin/master | |
# commit 4a633f715caf26f6e9495198f89bba20f3402a32 | |
# Author: Todd Wolfson <[email protected]> | |
# Date: Sun Jul 7 22:12:17 2013 -0700 | |
# | |
# Unsynced commit | |
# If the local branch is behind the remote branch | |
if _is_branch1_behind_branch2 "$_BRANCH" "$_REMOTE_BRANCH"; then | |
# echo our character | |
echo 1 | |
fi | |
} | |
function _parse_git_dirty () { | |
# ?? file.txt # Unstaged new files | |
# A file.txt # Staged new files | |
# M file.txt # Unstaged modified files | |
# M file.txt # Staged modified files | |
# D file.txt # Unstaged deleted files | |
# D file.txt # Staged deleted files | |
# If the git status has *any* changes (i.e. dirty) | |
if [[ -n "$(git status --porcelain 2> /dev/null)" ]]; then | |
# echo our character | |
echo 1 | |
fi | |
} | |
function _is_on_git() { | |
git rev-parse 2> /dev/null | |
} | |
function _get_git_status() { | |
# Grab the git dirty and git behind | |
_DIRTY_BRANCH="$(_parse_git_dirty)" | |
_BRANCH_AHEAD="$(_parse_git_ahead)" | |
_BRANCH_BEHIND="$(_parse_git_behind)" | |
# Iterate through all the cases and if it matches, then echo | |
if [[ $_DIRTY_BRANCH == 1 && $_BRANCH_AHEAD == 1 && $_BRANCH_BEHIND == 1 ]]; then | |
echo "⬢ " | |
elif [[ $_DIRTY_BRANCH == 1 && $_BRANCH_AHEAD == 1 ]]; then | |
echo "▲ " | |
elif [[ $_DIRTY_BRANCH == 1 && $_BRANCH_BEHIND == 1 ]]; then | |
echo "▼ " | |
elif [[ $_BRANCH_AHEAD == 1 && $_BRANCH_BEHIND == 1 ]]; then | |
echo "⬡ " | |
elif [[ $_BRANCH_AHEAD == 1 ]]; then | |
echo "△ " | |
elif [[ $_BRANCH_BEHIND == 1 ]]; then | |
echo "▽ " | |
elif [[ $_DIRTY_BRANCH == 1 ]]; then | |
echo "* " | |
fi | |
} | |
function _get_git_info () { | |
# Grab the branch | |
_BRANCH="$(_get_git_branch)" | |
_REPO_NAME="$(basename `git rev-parse --show-toplevel`)" | |
# If there are any branches | |
if [[ $_BRANCH != "" ]]; then | |
# Echo the branch | |
_OUTPUT=$_BRANCH | |
# Add on the git status | |
_OUTPUT="$(_get_git_status)"$_OUTPUT | |
# Echo our output | |
echo -en $"$_YELLOW| $_BLUE$_REPO_NAME$_YELLOW [$_GREEN$_OUTPUT$_YELLOW]$_NO_COLOR " | |
fi | |
} | |
# all those pretty colours | |
# | |
PS1=$"${_PURPLE}\n ʎ \$(_is_on_git && \ | |
echo -n \"\$(_get_git_info)\" && \ | |
echo -n \"\" )${_NO_COLOR}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment