[OT] git and hg in prompt (was: My first ever Python program, comments welcome)

Tim Delaney timothy.c.delaney at gmail.com
Tue Jul 24 23:56:34 CEST 2012


On 24 July 2012 21:34, Lipska the Kat <lipskathekat at yahoo.co.uk> wrote:

> On 24/07/12 06:13, rusi wrote:
>
>> On Jul 22, 10:23 pm, Lipska the Kat<lip... at lipskathekat.com>  wrote:
>>
>>  Heh heh, Nothing to do with Eclipse, just another thing to get my head
>>> around. For work and Java IMHO you can't beat eclipse...
>>> at the moment I'm getting my head around git,
>>>
>>
>> Bumped into this yesterday. Seems like a good aid to git-comprehension
>> https://github.com/git/git/**blob/master/contrib/**
>> completion/git-prompt.sh<https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh>
>>
>
> eek ... now that's a shell script to be proud of isn't it .. and it works
> [lipska at ubuntu fileio (master)]$ impressive. Good find, thanks.


OT, but I have the following in my prompt covering both git and hg - much
simpler, but gives similar information. Extending should be fairly easy.

function hg_ps1() {
    # requires http://stevelosh.com/projects/hg-prompt/
    #hg prompt '[{update}{{tags|:}:}{{bookmark}:}{branch}:{rev}] ' 2>
/dev/null
    hg prompt '[{update}{branch}:{rev}] ' 2> /dev/null
}

function git_ps1() {
    local head="`git rev-list -n 1 --abbrev-commit HEAD 2> /dev/null`"

    if [ "${head}" != "" ] ; then
        local branch="`git branch --no-color 2> /dev/null | sed -e
'/^[^*]/d' -e 's/* \(.*\)/\1/'`"

        # Puts a ^ in the prompt if this revision is not FETCH_HEAD
        local uptodate="`git log --no-color -n 1 --pretty=format:^
HEAD..FETCH_HEAD 2> /dev/null`"

        # Puts a comparison with the remote tracking branch in the prompt:
+ (ahead), - (behind) or * (both - diverged).
        local tracking="`git branch -avv --no-color 2> /dev/null | sed -e
'/^[^*]/d' -e 's/  */ /g' -e 's/* \(.*\)/\1/' -e
's/^[^[]*\[\([^]]*\)\].*$/\1/' -e 's/^.*ahead [0-9][0-9]*/+/' -e
's/[^+].*behind [0-9][0-9]*.*$/-/' -e '/^[^+-]/d' -e 's/+-/*/'`"

        echo "[${tracking}${uptodate}${branch}:${head}] "
        return 0
    fi

    return 1
}

function git_hg_ps1() {
    git_ps1

    if [ $? -eq 0 ] ; then
        return 0
    fi

    hg_ps1
    return $?
}

export PS1='$(git_hg_ps1)\[\033[1;30m\]${USERNAME}@${HOSTNAME}:\[\033[0m\]\[\033[1;30m\]${PWD%%${PWD##$HOME}}\[\033[0m\]${PWD##$HOME}>
'

It's designed to call as few external processes as possible (esp. when not
in a git repository) since it's used on Windows as well (msys, should work
in cygwin) and spawning on Windows is slow.

Tim Delaney
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120725/4f65ce69/attachment.html>


More information about the Python-list mailing list