Skip to content

Instantly share code, notes, and snippets.

@dgmike
Forked from igorescobar/.bashrc_ps1.sh
Created September 21, 2012 16:09
Show Gist options
  • Save dgmike/3762365 to your computer and use it in GitHub Desktop.
Save dgmike/3762365 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_another_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_prompt() {
local git_status="`git status -unormal 2>&1`"
if ! [[ "$git_status" =~ Not\ a\ git\ repo ]]; then
if [[ "$git_status" =~ nothing\ to\ commit ]]; then
local ansi=42
elif [[ "$git_status" =~ nothing\ added\ to\ commit\ but\ untracked\ files\ present ]]; then
local ansi=43
else
local ansi=45
fi
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
echo -n '\[\e[0;37;'"$ansi"';1m\]'"$branch"'\[\e[0m\] '
fi
}
function _prompt_command() {
PS1=" \t \[\033[0;32m\]\u@\h \[\e[1;33m\]\w \[\e[36m\]`git_another_author``_git_prompt` \[\033[0m\]\n "
}
PROMPT_COMMAND=_prompt_command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment