Git + Twitter = Friedcode I've now written my first Git hook. For the people who don't know what Git is you have either lived under a rock for the past few years or your not into computer programming at all.

The hook is a post-commit hook and what it does is that it sends the last commit message up to a twitter account I called "friedcode". I guess it's not entirely useful but for you who want to be loud about your work and the progress you make I guess it can make sense. Or if you're a team and you want to get a brief overview of what your team mates are up to. For me, it was mostly an experiment to try Git hooks and pytwitter. Here's how I did it:

Go into the .git directory and edit the file 'post-commit':


$ cd myproject
$ cd .git/hooks
$ jed post-commit

Here's the script I wrote which contains some horrible python one-liners simply because my sed/awk-fu isn't good enough:


#: Nothing
last_message=`git log --pretty=oneline -n1`
last_message=`echo $last_message |  python -c "import sys;sys.stdout.write(\
' '.join(sys.stdin.read().strip().split()[1:]))"`
repo_name=`git info | head -n1`
repo_name=`echo $repo_name | python -c "import \ 
 sys;sys.stdout.write(sys.stdin.read().strip().split('/')[-1])"`
echo "($repo_name) $last_message" | Update_friedcode.py

To enable the hook what you have to do is simply make it executable and you're done:


$ chmod +x post-commit

Then I needed the pytwitter script called Update_friedcode.py which I've put in '~/bin':


#!/usr/bin/env python
import sys
"""To use:
$ echo "I ate too much" | ./Update_friedcode.py
"""
U = 'friedcode'
P = <something something>

import pytwitter
client = pytwitter.pytwitter(username=U, password=P)
status_update = sys.stdin.read().strip()[:140]
client.statuses_update(status=status_update)

Comments

Post your own comment
Alex

Nice hack!
Unluckily the link under the image is broken

Peter Bengtsson

Not any more :)

James Rowe

Pretty cool idea, and definitely simpler than writing code to spit out HTML/Atom feeds for your co-workers. Couple of comments...

The first one-liner can be replaced with:

echo last_message | sed 's,^[^ ]* ,,'

or if you /bin/sh is always bash:

sed 's,^[^ ]* ,,' <<< $last_message

And the second one-liner is just basename, so os.path.basename if you
really want to use Python or simply the basename command if you don't. Again, if your /bin/sh is bash/ksh you can use ${last_message##*/}

And a question; What version of git has an info command by the way? Don't have it on my systems.

Peter Bengtsson

Great! Thank you for that sed command.

About the second one-liner, it's the git info returns this:
"== Remote URL: origin ssh://root@git.fry-it.com/var/cache/git/waf"

So basename wouldn't work on that.

I'm using 1.5.6.3 from Ubuntu to get git info.

James Rowe

Python's os.path.basename, basename from coreutils and busybox's basename all do the "right thing" with your example string.

On my systems, running 1.6.2.3, I'd use `git config --get remote.origin.url' to get for example "git@github.com:JNRowe/misc-overlay.git"

Peter Bengtsson

I think I know what you mean but the whole string starts with "== Remote URL: " which means that basename fails on it.

$ repo_name=`git info | head -n1`
$ basename $repo_name
basename: extra operand `URL:'
Try `basename --help' for more information.

Anonymous

My fault, I should have used an example.

basename "$repo_name"

it is the quoting that is important, it makes basename treat the string as an ugly space and symbol including filename

James Rowe

Bah, I meant to say you could use the following instead of the first one-liner if you're sure you using bash/ksh:

echo ${last_message#* }

Lawrence D'Oliveiro

I have tried 3 different versions of Git—1.4.4.4, 1.5.6,5 and 1.6.2.1—and none of them has the “git info” command.

Peter Bengtsson

Strange. I have 1.6.0.4 and it has git info.

Perhaps you can get the same info from:
$ git remote show origin

Your email will never ever be published.

Related posts

Go to top of the page