I was thinking about how to synchronize my home dirs and ended up having them now under revision control with git.
The blog post Don’t Reinvent the Wheel describes how to make use of the git provided script git-sh-prompt
and replace the default PS1
with a PROMPT_COMMAND
.
Cleaned and updated the default Debian .bashrc
to use PROMPT_COMMAND
when git-sh-prompt
is installed or fall back to the default Debian PS1
.
I don’t set the environment variable GIT_PS1_SHOWUNTRACKEDFILES=1
for $HOME
as there are many untracked files but i wanted it for other repositories.
Added a function gituntracked that sets GIT_PS1_SHOWUNTRACKEDFILES=1
based on current directory and added the function to PROMPT_COMMAND
.
On OmniOS the git pkg doesn’t install git-sh-prompt
, i copied it from some other machine to /opt/gitprompt/git-sh-prompt
.
The path to git-sh-prompt
is found and stored in the variable __gitprompt
which then is sourced.
# check for git-sh-prompt. Doesn't get installed with the OmniOS git package, copied to # /opt/gitprompt if [ -f /usr/lib/git-core/git-sh-prompt ]; then __gitprompt="/usr/lib/git-core/git-sh-prompt" elif [ -f /opt/gitprompt/git-sh-prompt ]; then __gitprompt="/opt/gitprompt/git-sh-prompt" else __gitprompt="" fi # enable git prompt when git-sh-prompt is found or fallback to default PS1 if [[ -f "${__gitprompt}" ]]; then source "${__gitprompt}" # home dir function, change path for $HOME to ~; used to set terminal titlebar function get_dir { printf "%s" $(pwd | sed "s:$HOME:~:") } # set terminal window titlebar function set_titlebar { case "$TERM" in xterm*|rxvt*) printf "\033]0;%s\007" "$*" ;; esac } ## git variables for use with git-sh-prompt # show git untracked files for repos outside $HOME function gituntracked { if ! [[ $(pwd) =~ ${HOME} ]]; then export GIT_PS1_SHOWUNTRACKEDFILES=1 else unset GIT_PS1_SHOWUNTRACKEDFILES fi } # show git repo status in prompt export GIT_PS1_SHOWDIRTYSTATE=1 export GIT_PS1_SHOWCOLORHINTS=1 export GIT_PS1_SHOWSTASHSTATE=1 export GIT_PS1_SHOWUPSTREAM="verbose git" # set PROMPT_COMMAND instead of PS1 so git can use colors in prompt PROMPT_COMMAND='gituntracked; \ __git_ps1 "\[\033[01;33m\]\u@\h\[\033[00m\]:\[\033[01;37m\]\w\[\033[00m\]" "\\\$ "; \ set_titlebar "$USER@${HOSTNAME%%.*}: $(get_dir)"' else # when no git installed set prompt and window title PS1='\[\033[01;33m\]\u@\h\[\033[00m\]:\[\033[01;37m\]\w\[\033[00m\]\$ ' # If this is an xterm set the title to user@host:dir case "$TERM" in xterm*|rxvt*) PS1="\[\e]0;\u@\h: \w\a\]$PS1" ;; *) ;; esac fi # export git commiter email address with plused hostname detail. # ${HOSTNAME} is already set on Linux and OmniOS export GIT_COMMITTER_EMAIL="username+${HOSTNAME}@domain.tld"
[update 2016-10-08]
– add function to show untracked files in repositories outside $HOME
directory
– make git-sh-prompt
work on OmniOS where it’s not installed with the package