Robot Has No Heart

Xavier Shay blogs here

A robot that does not have a heart

Code to test ratio per commit with git

I came across a post titled visualizing commits with bubble charts

That seems pretty neat. I don’t have the visualization yet, but I put together a script to pull the required data from a git repository:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/bin/bash
# usage: gitstats HEAD~5..

revs=`git log --format="%H" $1`

for rev in $revs; do
  author=`git log --format="%an" -n 1 $rev`
  date=`git log --format="%at" -n 1 $rev`

  git show --stat $rev |
    sed '$d' |
    egrep "(lib|spec)" |
    awk -v author="$author" -v rev="$rev" -v date="$date" '{
      split($1,a,"/"); sum[a[1]] += $3
    } END {
      if (sum["lib"]) print rev "," date "," author "," (sum["spec"] + sum["lib"]) "," (sum["spec"]/sum["lib"])
    } '
done

Would be nice not to shell out to git log three times, if anyone has any suggestions. This gives you one line per commit with the ref, timestamp, author, lines changed, code:test ratio, for example:

1
e10db7972b236c9b5e3eddc13e879f120cc4a82f,1333223104,Xavier Shay,42,1.33333

Storing build time in git notes with zsh

Playing around with git notes, having seen them on the github blog. I needed to update to git 1.7.2 (homebrew has it). The following shell command stores the run time of your specs inside a note on the latest commit:

1
{time rake spec} 2> >(tail -n 1 | cut -f 10 -d ' ' - |  git notes --ref=buildtime add -F - -f )

Breaking down the tricky bits:

{time rake spec} Honestly, I cargo culted the curly braces, and can’t find a good description of exactly what they do in this instance. It’s some sort of grouping thing: I found without them time didn’t apply properly.

2> time prints its output to STDERR, 2> redirects STDERR to the next argument. It is kind of like |, but for STDERR rather than STDOUT.

1
{time sleep 0.1} 2> /tmp/time.log

>( ... ) Rather than redirecting STDERR to a file, this allows us to pipe it in to more commands.

tail -n 1 rake spec also prints to STDERR, so pipe through tail to grab only the last line (which will be from time)

cut -f 10 -d ' ' - Split the line on a space character, choose the tenth column of the output from time, which is the total time taken. The trailing - says “read from STDIN”.

git notes --ref=buildtime add -F - -f Add a note to the latest commit (HEAD is default) in the buildtime namespace. -F - reads the note content from STDIN, which by now is only the final time taken for the spec run, and -f forces an update of the note if it already exists.

A pretty flower Another pretty flower