Git + Twitter = Friedcode

22 April 2009   10 comments   Python, Linux

Powered by Fusion×

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([1:]))"`
repo_name=`git info | head -n1`
repo_name=`echo $repo_name | python -c "import \ 
echo "($repo_name) $last_message" |

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 which I've put in '~/bin':

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

import pytwitter
client = pytwitter.pytwitter(username=U, password=P)
status_update =[:140]


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://"

So basename wouldn't work on that.

I'm using 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, I'd use `git config --get remote.origin.url' to get for example ""
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.
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.5.6,5 and—and none of them has the “git info” command.
Peter Bengtsson
Strange. I have 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

Lesson learned: Unicodifying request variables in Zope 16 April 2009
Tux - lterally in Linux code 24 April 2009
Related by keywords:
Registration and sign-in by email verification 29 April 2013
Integrate BrowserID in a Tornado web app 22 November 2011
Beach volleyball bums 02 August 2012
Bash tip of the day: ff 25 March 2011
Too Cool For Me? 25 September 2011
Bookmarklet to replace the current domain with localhost:8000 17 January 2010
iPhone push notifications for Twitter with Prowl 25 October 2009
My first Twitter app - 22 September 2009
gg - wrapping git-grep 11 August 2009