Groesbeek, view of the 'National Liberation Museum 1944-1945' in Groesbeek. © Ton Kersten
Fork me on GitHub
Posts tagged as git

git status in the prompt

2012-07-23 (124) by Ton Kersten, tagged as code, git, linux, sysadm

Working with git a lot I decided I needed some git status in my prompt.

I searched the web and some solutions where almost what I wanted and this one by Sebastian Celis came very close.

But it didn't work with my version of zsh, because that didn't seem to understand the =~ operator.

I also think Sebastian makes things over complicated and so I changed some things aroud.

This is what I came up with:

First make sure this code is included in your ~/.zshenv file

prompt_git_info()
{
    unset __GIT_BRANCH
    unset __GIT_BRANCH_STATUS
    unset __GIT_BRANCH_DIRTY

    local st="$(git status 2>/dev/null)"
    if [[ -n "$st" ]]; then
        local -a arr
        arr=(${(f)st})

        if [[ $arr[1] = *Not\ currently\ on\ any\ branch.* ]]
        then
            __GIT_BRANCH='no-branch'
        else
            __GIT_BRANCH="${arr[1][(w)4]}"
        fi

        if [[ $arr[2] = *Your\ branch\ is* ]]
        then
            if [[ $arr[2] = *ahead* ]]
            then
                __GIT_BRANCH_STATUS='ahead'
            elif [[ $arr[2] = *diverged* ]]
            then
                __GIT_BRANCH_STATUS='diverged'
            else
                __GIT_BRANCH_STATUS='behind'
            fi
        fi

        if [[ $st = *nothing\ to\ commit* ]]
        then
            __GIT_BRANCH_DIRTY='0'
        else
            __GIT_BRANCH_DIRTY='1'
        fi
    fi

    if [[ -n "$__GIT_BRANCH" ]]
    then
        local s="("
        s+="$__GIT_BRANCH"
        case "$__GIT_BRANCH_STATUS"
        in
            ahead)      s+="↑"  ;;
            diverged)   s+="↕"  ;;
            behind)     s+="↓"  ;;
        esac
        if [[ "$__GIT_BRANCH_DIRTY" = "1" ]]
        then
            s+="⚡"
        fi
        s+=")"

        printf " %s%s" "%{${fg[yellow]}%}" $s
    fi
}

and set your prompt to something like this

PS1=$'$C_CYAN%n@%m$(prompt_git_info) $C_WHITE%2~$ $C_OFF'

When I now switch to a directory that is under control of git I get gt status messages in my prompt, like

tonk@mach (master) ~/dir$ git commit -a
[master fca5ac3] Nice, new stuff.
 6 files changed, 88 insertions(+), 12 deletions(-)
tonk@mach (master) ~/.dir$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
nothing to commit (working directory clean)
tonk@mach (master) ~/.dir$

New version of git.vi

2010-12-20 (84) by Ton Kersten, tagged as git

I was working on a customers project and I had to change a lot of files. This could easily be done with the sed and find commands, but I wanted these changes to be checked in into git as well. And I also wanted the git keywords expanded.

There was always the trick to edit all the files with sed, then edit them again with git.vi and just press ZZ for all files. This would be tedious, I know.

So I decided to take the Geeks Shortcut and recode git.vi so everything can be be done automatic.

Read more »

New git.vi

2010-11-09 (77) by Ton Kersten, tagged as git

It took some time, but I did manage to make a new version of git.vi. It has some errors corrected and some extra features.

And now it comes with its own, shiny, new man page. (git.vi.1).

Come and g[ei]t it. It's in the files section or on github.

Or clone it with:

git clone https://github.com/tonk/git.vi.git

I'm on Github

2010-10-04 (72) by Ton Kersten, tagged as git

As I develop some scripts and other things for fun I decided to make some of these more public.

The following projects can be found on Github, with this URL.

  • Header
  • MySQL backup
  • git.vi

Have fun ans let me know what you think.

No more git RPM's

2010-08-30 (69) by Ton Kersten, tagged as git

I, for some time now, created RPM's for git. I will not do that anymore, because Dag Wieers RPMForge now has them and even up-to-date ones as well.

I also removed the git archive RPM's.

New Git RPM set

2010-07-20 (53) by Ton Kersten, tagged as git

There is a complete new set of Git RPMs put online. This is version 1.7.1.1 and they are compiled for CentOS 5.5 on the i386 architecture.

You can find them in the Downloads section.

Those busy git developers

2010-03-25 (47) by Ton Kersten, tagged as git

Last time I blogged that the developers of git are a really busy bunch.

Well, they really are. I found out I almost missed a complete release, so with this set I'm catching up.

Today I build version 1.7.0.3.

By now, you know where to find them.

The next entry will be more interresting, not only git building.

Those busy git developers

2010-03-13 (46) by Ton Kersten, tagged as git

The developers of git are a really busy bunch. Today a new git (1.7.0.2) arrived, again, so I did build the RPM's.

By now, you know where to find them.