Skip to content

Instantly share code, notes, and snippets.

@nrichards
Last active August 29, 2015 14:27
Show Gist options
  • Save nrichards/6efed68ba9786fe9d00d to your computer and use it in GitHub Desktop.
Save nrichards/6efed68ba9786fe9d00d to your computer and use it in GitHub Desktop.
SCM status Bash prompt supporting both hg and git
  1. Clone Git Prompt support https://github.com/jimeh/git-aware-prompt to ~/.bash/ (~/.bash/git-aware-prompt/)
  2. Add enclosed Mercurial Prompt support to ~/.bash/ (~/.bash/hg_prompt.sh)
  3. Integrate both into your ~/.bash_profile with:
# git prompt

export GITAWAREPROMPT=~/.bash/git-aware-prompt
source "${GITAWAREPROMPT}/main.sh"

# mercurial prompt

source ~/.bash/hg_prompt.sh

# combined prompt

scm_branch() {
    if [ -z "$git_branch" ]
    then
        local _hg_branch=$(hg_branch)
        if [ -z "$_hg_branch" ]
        then
            return
        else
            echo -n "("$_hg_branch")"
        fi
    else
        echo -n $git_branch
    fi
}

scm_dirty() {
    if [ -z "$git_dirty" ]
    then
        local _hg_dirty=$(hg_dirty)
        if [ -z "$_hg_dirty" ]
        then
            return
        else
            echo -n "$_hg_dirty"
        fi
    else
        echo -n "$git_dirty"
    fi
}

# install prompt

export PS1="\u@\h \w \[$txtcyn\]\$(scm_branch)\[$txtred\]\$(scm_dirty)\[$txtrst\]\$ "

Then enjoy seeing directory sensitive prompts when CD'ing into git and hg repos such as: nr@Computer ~/proj/slash (features/main)*$ and nr@Computer ~/proj/editor (cloud/time)!$. Note the hg support currently feels 'slow'.

# adapted from http://stevelosh.com/blog/2009/03/mercurial-bash-prompts/
hg_in_repo() {
if [ -d ".hg" ]
then
return 1
else
hg root &> /dev/null # sets $?
if [[ $? -eq 0 ]]
then
return 1
fi
fi
return 0
}
hg_branch() {
hg_in_repo # sets $?
if [ $? -eq 1 ]
then
hg branch 2> /dev/null
fi
}
hg_dirty() {
hg_in_repo # sets $?
if [ $? -eq 1 ]
then
# FIXME optimize me, takes almost 0.1s
hg status --no-color 2> /dev/null \
| awk '$1 == "?" { print "?" } $1 != "?" { print "!" }' \
| sort | uniq | head -c1
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment