How to Relay your Git Commit Messages to Twitter in 16 Easy Steps

Posted on April 2, 2013 by David Hilowitz

A while back I started using Twitter as a micro-journaling platform. I created a private Twitter account, and every few hours as I worked on code, I would jot down a private tweet to myself about what it was that I was doing. This private Twitter stream was then archived using Momento App for iPhone. After a while, it occurred to me that another stream that I would love to have archived in Momento is my Git commits as it often says more about what I’m up to than it would occur to me to write in a journal.

I set up a Git post-commit hook that posts to my private Twitter account each time I make a local commit on my development repository. Here’s what I did:

  1. Prerequisite: Create a Twitter account. It doesn’t have to be private, but it can be.
  2. Register an application with Twitter. Here’s how you do that:
    1. Go to http://dev.twitter.com. You may need to sign in again.Screen Shot 2013-04-02 at 5.12.18 PM
    2. Hover over your avatar in the top-right, and choose “My Applications” from the menu that appears.Screen Shot 2013-04-02 at 5.22.26 PM
    3. Click on the “Create New Application” Button.Screen Shot 2013-04-02 at 5.23.14 PM
    4. Fill out the next however you’d like. The website address can be anything. The name of the app can be anything. Leave Callback URL blank. Agree to the agreement, enter the CAPTCHA and you’re good to go.Screen Shot 2013-04-02 at 5.36.56 PM
    5. Next, you’ll be taken to a screen with a bunch of keys. Copy all of those down into a text editor.
    6. Click the “Settings” tab. Change to “Read and Write” for Application Type. Click Update.Screen Shot 2013-04-02 at 5.36.42 PM
    7. Go back to the Details tab. Scroll down to the bottom and click “Create my OAuth Access Token.”
    8. Wait a few seconds, refresh the page, wait some more. Eventually, at the bottom of the page a section should appear that says “Your access token.” Copy these codes down.
    9. That’s it for your Twitter App setup.
  3. Open up a shell on the machine you are planning to be commit to (and tweet from).
  4. Install http_post. You will have to compile this from source. (make and make install). Make sure it’s accessible from your PATH.
  5. Install oauth_sign. You will also have to compile this form source. (make and make install) Make sure it’s accessible from your PATH.
  6. Finally, save the following script into .git/hooks/post-commit in your Git repository.
    #!/bin/sh
    
    # PATH modification needed for http_post and oauth_sign
    export PATH=$PATH:/usr/local/bin
    
    toplevel_path=`git rev-parse --show-toplevel`
    toplevel_dir=`basename "$toplevel_path"`
    
    branch=`git rev-parse --abbrev-ref HEAD`
    subject=`git log --pretty=format:%s -n1`
    hashtags="#code #$toplevel_dir"
    tweet=$hashtags' ['$branch']: "'$subject'"'
    
    # truncate tweets that are longer than 140 characters
    if [ ${#tweet} -gt 140 ]
        then
            tweet_trunc=$(echo $tweet | cut -c1-137)
            tweet=${tweet_trunc}...
    fi
    
    consumer_key="<Put your computer key here>"
    consumer_secret="<Put your consumer secret here>"
    access_token="<Put your access token here>"
    access_secret="<Put your access token secret here>"
    url="https:api.twitter.com/1.1/statuses/update.json"
    
    http_post -h Authorization "$(oauth_sign \
    $consumer_key $consumer_secret \
    $access_token $access_secret \
    POST "$url" status="$tweet")" \
         "$url" status="$tweet"
  7. Make sure that you make the file executable. (chmod a+x .git/hooks/post-commit)
  8. That’s it! If you want to have this automatically added to any new repositories you make, modify the git-core templates. You’ll have to figure out where those are (it’s different for every set up). For me, they’re located here: /opt/local/share/git-core/templates/hooks/post-commit.

This is all based heavily on scripts at the following two links:

Good luck!

–David

 

1 Comment »

  1. I didn't even notice the water. Unless needed to process the rest of the foods, why add water?I noticed this in a chocolate/zucchini dehydrator cookie recipe that I made. I left out the water, and they came out great.

    Comment by http://www./ — October 22, 2016 @ 11:08 am

Leave a comment