Skip to content

Instantly share code, notes, and snippets.

@igorescobar
Last active April 21, 2020 07:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save igorescobar/3753797 to your computer and use it in GitHub Desktop.
Save igorescobar/3753797 to your computer and use it in GitHub Desktop.
My Personal PS1 customization to work with git repositories.
# This bash script will update your PS1 configuration to inform you about:
# - current hour
# - current logged user
# - machine name
# - current path
# - status of current branch:
# - untracked files
# - uncommited changes
# - when nothing is changed on current branch
# - inform you when the current user.name and user.email is different of the global settings
function git_author {
GIT_AUTHOR_NAME=`git config --get user.name`
GLOBAL_GIT_AUTHOR_NAME=`git config --global --get user.name`
GIT_AUTHOR_EMAIL=`git config --get user.email`
GLOBAL_GIT_AUTHOR_EMAIL=`git config --global --get user.email`
if [[ ! -z "$GIT_AUTHOR_EMAIL" ]]; then
if [[ "$GIT_AUTHOR_EMAIL" == "$GLOBAL_GIT_AUTHOR_EMAIL" ]]; then
GIT_AUTHOR_EMAIL=""
fi
else
GIT_AUTHOR_EMAIL=""
fi
if [[ ! -z "$GIT_AUTHOR_NAME" ]]; then
if [[ "$GIT_AUTHOR_NAME" == "$GLOBAL_GIT_AUTHOR_NAME" ]]; then
GIT_AUTHOR_NAME=""
fi
else
GIT_AUTHOR_NAME=""
fi
GIT_AUTHOR=$GIT_AUTHOR_NAME
if [[ ! -z "$GIT_AUTHOR_EMAIL" ]]; then
if [[ ! -z "$GIT_AUTHOR" ]]; then
GIT_AUTHOR=$GIT_AUTHOR', '
fi
GIT_AUTHOR=$GIT_AUTHOR$GIT_AUTHOR_EMAIL
fi
if [[ ! -z "$GIT_AUTHOR" ]]; then
echo "[$GIT_AUTHOR]"
fi
}
function _git_status() {
local git_status="`git status -unormal 2>&1`"
if ! [[ "$git_status" =~ Not\ a\ git\ repo ]]; then
if [[ "$git_status" =~ nothing\ to\ commit ]]; then
local color=32
elif [[ "$git_status" =~ nothing\ added\ to\ commit\ but\ untracked\ files\ present ]]; then
local color=33
else
local color=35
fi
echo -n '\[\e[0;'"$color"';1m\]★\[\e[0m\] '
fi
}
function _git_current_branch_branch() {
local git_status="`git status -unormal 2>&1`"
if ! [[ "$git_status" =~ Not\ a\ git\ repo ]]; then
if [[ "$git_status" =~ On\ branch\ ([^[:space:]]+) ]]; then
branch=${BASH_REMATCH[1]}
test "$branch" != master || branch=''
else
# Detached HEAD. (branch=HEAD is a faster alternative.)
branch="(`git describe --all --contains --abbrev=4 HEAD 2> /dev/null ||
echo HEAD`)"
fi
if [[ "$branch" != "" ]]; then
branch=" $branch "
fi
echo -n '\[\e[0;37;41;1m\]'"$branch"'\[\e[0m\] '
fi
}
function _prompt_command() {
PS1=" \t \[\033[0;32m\]\u@\h \[\e[1;33m\]\w \[\e[36m\]`git_author` `_git_current_branch_branch`\n `_git_status`\[\033[0m\]"
}
PROMPT_COMMAND=_prompt_command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment