Skip to content

Instantly share code, notes, and snippets.

@Anthchirp
Created February 7, 2019 09:18
Show Gist options
  • Save Anthchirp/e1317e0c19e2cb5ca4aa25a5d2d74d8f to your computer and use it in GitHub Desktop.
Save Anthchirp/e1317e0c19e2cb5ca4aa25a5d2d74d8f to your computer and use it in GitHub Desktop.
GIT prompt
function __ps1_git () {
# preserve exit status
local exit=$?
local repo_info
repo_info="$(git rev-parse --git-dir --is-inside-git-dir --is-inside-work-tree 2>/dev/null)"
if [ -z "$repo_info" ]; then
return $exit
fi
local inside_worktree="${repo_info##*$'\n'}"
repo_info="${repo_info%$'\n'*}"
local inside_gitdir="${repo_info##*$'\n'}"
local gitdir="${repo_info%$'\n'*}"
echo -en "\001\033[1;33m\002["
if [ "true" = "$inside_gitdir" ]; then
echo -n ".git"
elif [ "true" = "$inside_worktree" ]; then
if [[ -z $(git status --porcelain) ]]; then
echo -ne "\001\033[0;32m\002"
else
echo -ne "\001\033[1;31m\002"
fi
BRANCH=`git branch 2>/dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'`
echo -n $BRANCH
REMOTE=`git for-each-ref --format='%(upstream:short)' $(git symbolic-ref -q HEAD)`
if [ "$REMOTE" != "" ]; then
AHEAD=`git rev-list $REMOTE..HEAD | wc -l` # when git 1.8 becomes available: --count instead of wc
BEHIND=`git rev-list HEAD..$REMOTE | wc -l`
if [ "$BEHIND" != "0" ]; then echo -ne "\001\033[1;31m\002-$BEHIND"; fi
if [ "$AHEAD" != "0" ]; then echo -ne "\001\033[1;32m\002+$AHEAD"; fi
fi
local extmode=""
if [ -d "$gitdir/rebase-merge" ]; then
extmode="rebase"
elif [ -d "$gitdir/rebase-apply" ]; then
extmode="rebase"
elif [ -f "$gitdir/MERGE_HEAD" ]; then
extmode="merge"
elif [ -f "$gitdir/CHERRY_PICK_HEAD" ]; then
extmode="cherry"
elif [ -f "$gitdir/REVERT_HEAD" ]; then
extmode="revert"
elif [ -f "$gitdir/BISECT_LOG" ]; then
extmode="bisect"
fi
if [ ! -z "$extmode" ]; then
echo -en "\001\033[1;33m\002|$extmode"
fi
if [ "bisect" = "$extmode" ]; then
echo -en "\001\033[1;34m\002:`git bisect visualize --oneline 2>/dev/null | wc -l`"
fi
fi
echo -en "\001\033[1;33m\002] "
}
PS1="\[\033[1;31m\]\h \[\033[1;34m\]\W \`if [ \$? = 0 ]; then echo '\[\033[1;32m\]:)'; else echo '\[\033[1;31m\]:('; fi\` \`__ps1_git\`\[\033[0m\]\$ "
function gitoff () {
PS1="\[\033[1;31m\]\h \[\033[1;34m\]\W \`if [ \$? = 0 ]; then echo '\[\033[1;32m\]:)'; else echo '\[\033[1;31m\]:('; fi\` \[\033[0m\]\$ "
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment