TIP: How to Play a Sound Whenever You Commit to Git

Posted on June 12, 2013 by David Hilowitz

Writing code alone at home can be an isolating experience. There you are, day in day out, quietly making magic with your mind (sarcasm, obv.) only to silently commit the fruits of your labor into the void of your source control repository, appreciated by no one. If only a crowd of children could be retained for the sole purpose of cheering you on every time you complete something.

Amazingly, Brandon Keepers over at Collective Idea had the same exact same thought (almost; he was substantially less melodramatic in his blog post about it).  Anyway, here is what my version of his script looks like:

#!/bin/sh

toplevel_path=`git rev-parse --show-toplevel`
afplay -v 0.1 $toplevel_path/.git/hooks/happykids.wav > /dev/null 2>&1 &

I put this in a file called .git/hooks/post-commit.playsound. I then trigger this from the main .git/hooks/post-commit script as follows:

#!/bin/sh

toplevel_path=`git rev-parse --show-toplevel`
$toplevel_path/.git/hooks/post-commit.tweet
$toplevel_path/.git/hooks/post-commit.playsound

Where the post-commit.tweet script is the script from this blog post. If you aren’t also tweeting your commit posts, you’ll want to delete that line.

If you want this to work for every single Git repository from now on, add these scripts to your git-core templates. You’ll have to figure out where these are (it’s different for every setup). For my Mac, they’re located here: /opt/local/share/git-core/templates/hooks/post-commit.

–David

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

 

TIP: How to do disk image backups in Ubuntu Linux 10.04

Posted on November 28, 2011 by David Hilowitz

So, I just installed Ubuntu Linux 10.04 on my trusty Toshiba Portege 3480CT8. (Yes, it works beautifully as a lightweight LAMP server.)

One of the first things I set out to do was make a disk image of my new server, so that if something goes awry, I can restore things later on.

Here’s what I did:

First, I plugged my external drive into the USB port. This got assigned to /dev/sdb.   I mounted the drive.

$ sudo mkdir /media/backup_04 
$ sudo mount /dev/sdb1 /media/backup_04 -t ntfs 
$ sudo mkdir /media/backup_04/portege-linux-backups

Next,  I installed dcfldd. This is just like dd, which comes with most Linux distributions, except that it can output time remaining (among other things).

$ apt-get install dcfldd

Finally, I started the backup:

$ dcfldd if=/dev/sda of=/media/backup_04/portege-linux-backups/2011_11_28.img sizeprobe=if

This produces an output that looks like this:

$ [16% of 28615Mb] 155136 blocks (4848Mb) written. 01:17:37 remaining.

–David

GO PROGRAMMING TIP: Where to put the ‘go.pbfilespec’ and ‘go.xclangspec’ files.

Posted on November 16, 2009 by David Hilowitz

So I was trying to get XCode syntax highlighting support for Google’s new Go language. I had located the two XCode files in the Go SRC package (these were in $GOROOT/misc/xcode), but for the longest time I couldn’t figure out where to put them. I finally stumbled on the answer (thanks to Talamathi for putting me on the right track): The files go in /Developer/Library/PrivateFrameworks/XcodeEdit.framework/Versions/A/Resources/

In short, if you want syntax highlighting support for the Go language in XCode, the following should get you there:
cp $GOROOT/misc/xcode/* /Developer/Library/PrivateFrameworks/XcodeEdit.framework/Versions/A/Resources/