Jan 29 2010

sprunge.us shell helper

When I quickly need to post the output of a command to a pastebin, I use the sprunge.us service. And because I usually don't remember the exact curl command to upload the output to sprunge.us, I added this little snippet to my ~/.bashrc:

sprunge () {
    if [ "$*" ]; then
        local prompt="$(PS1="$PS1" bash -i <<<$'\nexit' 2>&1 | head -n1)"
        ( echo "$(sed 's/\o033\[[0-9]*;[0-9]*m//g'  <<<"$prompt")$@"; exec $@; )
    else
        cat
    fi | curl -F 'sprunge=<-' http://sprunge.us
}

I can use it either to pipe the output into it, in which case it will simply upload its stdin to sprunge.us:

[ susie ] ~ $ echo "test" | sprunge

Or I can prepend it to a command and it will also upload my $PS1 as well as the command I was trying to run to sprunge.us:

[ susie ] git (master)+1-7 - / $ sprunge git show

# This will create a new pastebin with the following text in it:
[ susie ] git (master)+1-7 - / $ git show
commit b7f79f25f515f5a3f20f1f0fc1d336f436946669
Author: Tomas Carnecky <tom@dbservice.com>
Date:   Wed Jul 22 18:37:21 2009 +0200

...

Sep 13 2009

Remove commit from history

# Let's say <sha> is the commit you want to remove
# from history, then you can use git rebase -i to
# get rid of it. This snippet uses the shell expansion
# so you don't have to type the <sha> twice. However,
# and as always with rebase, this only works if the
# history between <sha>^ and HEAD is linear.
 
git rebase --onto <sha>{^,}

Feb 21 2009

photo

git-prompt.png

The prompt can get a bit long, but that's a small price I pay for having the information right where I need it.


Feb 18 2009

Bash prompt with colored git branch

There already is solid support for adding the current git branch to the bash prompt (using __git_ps1). And many have experimented how to also display the working tree state (clean / dirty). But that usually is by appending yet another character or string to the prompt, so the bash prompt grows larger and larger. Since I don't live in a one-bit-color world anymore and all my terminals support 16 or even 256 colors, I'd prefer to actually make use of the colors.

The small code snippet below adds the current git branch into your prompt and colors it depending on whether the working tree is dirty (red) or clean (green). Please note that it can take a few seconds for the prompt to appear when you cd the first time into a large project. This is due to git-diff-index scanning the whole working tree for changes. However once the cache is hot you shouldn't see a big difference.

# <<< completion
. /usr/share/bash-completion/git

__git_ps1_color () {
    if [ -z "$(git rev-parse --git-dir 2>/dev/null)" ]; then
        return
    fi

    if test -z "$(git diff-index --name-only HEAD --)"; then
        printf "\033[01;32m"
    else
        printf "\033[01;31m"
    fi
}

PS1="\${PWD##*/}\$(__git_ps1_color)\$(__git_ps1) \$ "