Skip to content

Instantly share code, notes, and snippets.

@EECOLOR
Created March 26, 2023 08:04
Show Gist options
  • Save EECOLOR/4bef52f98f50e6d75b79cf8c1f6927ba to your computer and use it in GitHub Desktop.
Save EECOLOR/4bef52f98f50e6d75b79cf8c1f6927ba to your computer and use it in GitHub Desktop.
Git Situational Awareness Bash
#!/usr/bin/env bash
PROMPT_GREEN='\001'`tput setaf 2`'\002'
PROMPT_PINK='\001'`tput setaf 5`'\002'
PROMPT_PLAIN='\001'`tput op`'\002'
function in_git_repo {
[ -d .git ] || git rev-parse --git-dir > /dev/null 2>&1
}
function git_changed {
if in_git_repo; then
local result=""
local status="$(git status --porcelain)"
if [ "$(echo "$status" | grep ^M)" ]; then
result="$result+"
fi
if [ "$(echo "$status" | grep ^.M)" ]; then
result="$result*"
fi
if [ "$(echo "$status" | grep ^\?\?)" ]; then
result="$result~"
fi
echo "$result"
fi
}
function git_branch {
if in_git_repo; then
branch=$(git branch | grep "*" | cut -d ' ' -f 2-3)
if [ "$branch" = "" ]; then
branch=$(git status | grep "# On branch" | cut -d ' ' -f 4-5)
fi
echo $branch
fi
}
function git_branch_separator {
if in_git_repo; then
echo ":"
fi
}
function git_stash_height {
if in_git_repo; then
stash_height="$(git stash list | wc -l | awk '{ print $1 }')"
if [ $stash_height = "0" ]; then
stash_height=""
fi
echo "$stash_height"
fi
}
function git_stash_height_separator {
if [ "$(git_stash_height)" ]; then
echo ":"
fi
}
function base_prompt {
local base='${debian_chroot:+($debian_chroot)}\001\033[01;32m\002\u'
if [ "$SSH_CONNECTION" ]; then
base="$base@\h"
fi
echo "$base\001\033[00m\002:"
}
export HAVE_GIT_SIT_AWARENESS=1
# tweak this as you see fit
export PS1="$(base_prompt)\001\e[1;34m\002\w\001\e[0m\002\$(git_branch_separator)${PROMPT_PINK}\$(git_changed)${PROMPT_GREEN}\$(git_branch)${PROMPT_PLAIN}\$(git_stash_height_separator)${PROMPT_PINK}\$(git_stash_height)${PROMPT_PLAIN}\$ "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment