All posts by Ruturaj Vartak

Git Bisect Tutorial

If you’re reading this, you’re an avid user of git, have made tons of commits and now you’re trying to find..

where the hell did the bug get introduced ?

git-bisect is a perfect tool to find that. To explain this awesome tool, I’ve created a small demo-repository which has only one file text.txt. In this file, I’ve incrementally added a, b, c, d and so on in each commit.

This is how my file text.txt looks like

[rutu@ruturaj-vartak git-bisect-demo]$ cat text.txt 
a
z
c
d
e
f
g
h
i
j
k
l
m
n

My git graph/log looks as below

If you are wondering what is “git g” its just an alias, following is my ~/.gitconfig

[rutu@ruturaj-vartak git-bisect-demo]$ cat ~/.gitconfig 
[alias]
	ci = commit
	st = status
	di = diff
	co = checkout
	k = !gitk --all
	g = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --all
	d = difftool
[color]
	ui = true
[user]
	name = Ruturaj K. Vartak
	email = ruturaj@localhost
[diff]
	tool = vimdiff

To simulate a bug, somewhere in my code, I’ve replaced b with z. Now the with the current state of my code, I’ve got to find where and who introduced z in my code ?. Lets begin

git-bisect uses BST style division and search mechanism. So lets start.

  1. Start with a command,
    $ git bisect start
  2. Now its necessary to tell git, that where was the code good and bad. Obviously my code was good at revision b162893 and the HEAD or 20bacecis now bad. So ..
    1. $ git bisect good b162893
    2. $ git bisect bad 20bacec

    Now if you check the git graph…


    Notice how git has divided this history into 2 parts, and the HEAD now points to the partitioning commit fa70446.

    We can now cat the file and see if the bug was introduced.

    [rutu@ruturaj-vartak git-bisect-demo]$ cat text.txt 
    a
    b
    c
    d
    e
    f
    g
    h

    As you see, the bug still has not been introduced, so lets tell git that this commit is GOOD

  3. $ git bisect good
    Now lets look how the graph looks


    The head is now d126a8f, the half between 20bacec and fa70446 and lets cat our file, text.txt

    [rutu@ruturaj-vartak git-bisect-demo]$ cat text.txt 
    a
    z
    c
    d
    e
    f
    g
    h
    i
    j
    k

    Wohoo ! our bug is existing here, but we don’t know if it was added in b38e6d7, df4f3bc or the HEAD – d126a8f

  4. So we now tell git, that this is BAD commit
    $ git bisect bad
    Now lets look at our graph,


    It has made another division, HEAD now pointing to df4f3bc
  5. Lets see if this HEAD is GOOD or BAD,
    [rutu@ruturaj-vartak git-bisect-demo]$ cat text.txt 
    a
    b
    c
    d
    e
    f
    g
    h
    i
    j

    Hey !!! The bug is non-existent over here. Lets tell git the good news

    $ git bisect good
    And this is what it returns us back

    [rutu@ruturaj-vartak git-bisect-demo]$ git bisect good
    d126a8fbd75cbf5e015228f0411d205745a77e05 is the first bad commit
    commit d126a8fbd75cbf5e015228f0411d205745a77e05
    Author: Ruturaj K. Vartak 
    Date:   Sat Jul 7 12:32:04 2012 +0530
    
        added k
    
    :100644 100644 92dfa216416a1ac944633ab674568f8bae139d95 261ccd63acaba8ae42219a40579a9acec61f05d0 M	text.txt

    Since there were no commits between our last found BAD commit – d126a8f and our current HEAD – df4f3bc, git has determined the bug was introduced in d126a8f. It throws a complete info stack of what it has for that commit.

    This is how our final graph looks like


    Note: there is nothing left to check between d126a8f(BAD) and df4f3bc(GOOD)

  6. The last thing to do, is reset everything with
    git bisect reset
    Which leaves a clean state

    [rutu@ruturaj-vartak git-bisect-demo]$ git bisect reset
    Previous HEAD position was df4f3bc... added j
    Switched to branch 'master'
    [rutu@ruturaj-vartak git-bisect-demo]$ git g
    * 20bacec - (HEAD, master) added n (3 hours ago) 
    * c7edac3 - added m (3 hours ago) 
    * 83a34bc - added l (3 hours ago) 
    * d126a8f - added k (3 hours ago) 
    * df4f3bc - added j (3 hours ago) 
    * b38e6d7 - added i (3 hours ago) 
    * fa70446 - added h (3 hours ago) 
    * e4efb05 - added g (3 hours ago) 
    * 8fe6f2a - added f (3 hours ago) 
    * 96381ba - added e (3 hours ago) 
    * a07ce18 - added d (3 hours ago) 
    * 0b824e0 - added c (3 hours ago) 
    * b162893 - added b (3 hours ago) 
    * 7a60f8d - added a (3 hours ago)

Automating git bisect

Even though the above method looks easy, it could be painful if you’re going through 100s of commits. git has an automated way as well, using the git bisect run. Lets look how we can search our above code for bugs using the automated way

For git bisect run cmd, It needs an argument of a command that’ll do the search of bug in the script and return exit code 0 for GOOD or 1 for BAD.

Lets create a simple bug-finder.sh script that will grep for b, if grep finds, it’ll give exit status as 0, or else 1. We’ll capture the exit status of grep and throw as exit status of our script. We’ll place our bug-finder.sh in root folder of our repo.

Here is the script, Make sure you chmod +x bug-finder.sh

[rutu@ruturaj-vartak git-bisect-demo]$ cat bug-finder.sh 
#!/bin/bash
grep "b" text.txt
exitCode=$?
if [ $exitCode -eq 0 ]; then
	exit 0;
else
	exit 1;
fi

Lets begin, steps stay the same…

  1. $ git bisect start
  2. $ git bisect good b162893
  3. $ git bisect bad 20bacec
  4. Now, the magic –
    $ git bisect run ./bug-finder.sh
    The output is as follows – git successfully finding the bug
[rutu@ruturaj-vartak git-bisect-demo]$ git bisect run ./bug-finder.sh 
running ./bug-finder.sh
b
Bisecting: 2 revisions left to test after this (roughly 2 steps)
[d126a8fbd75cbf5e015228f0411d205745a77e05] added k
running ./bug-finder.sh
Bisecting: 0 revisions left to test after this (roughly 1 step)
[df4f3bc1e079bc75afd922726c719e16d1e4efdf] added j
running ./bug-finder.sh
b
d126a8fbd75cbf5e015228f0411d205745a77e05 is the first bad commit
commit d126a8fbd75cbf5e015228f0411d205745a77e05
Author: Ruturaj K. Vartak 
Date:   Sat Jul 7 12:32:04 2012 +0530

    added k

:100644 100644 92dfa216416a1ac944633ab674568f8bae139d95 261ccd63acaba8ae42219a40579a9acec61f05d0 M	text.txt
bisect run success

There it is, simply and cool. Ofcourse in this case a lot depends how and what you’ve coded in ur bug-finder.sh. A bug in this script will just fool git.

Update: You can clone the git-bisect-demo repository

Head to head vs Goal Difference

Russia was knocked off the Euro 2012 by Greece by their achievement of scoring more goals than Greece.

Their sin – they lost to Greece. I wonder if the strategy to give heads-up to a team only ’cause their one-on-one record is better than a goal difference is a good one!

So when it ended, stats were ..

  Russia Greece
Points 4 4
Goals For 5 3
Goals Against 3 3
Goal Diff 2 0
Yellow Cards 6 9
Red Cards 0 1

I wonder why other statistics too were ignored? Is head-to-head a better criteria to move a team up?

Guitar Chords – O Re Chiraiya – Satyamev Jayte show

This entry is part 30 of 41 in the series Guitar

At the end of the touching episode on female foeticide, Swanand Kirkire with his self composed song O re Chiraiya sung a heart melting song with Ram Sampath.

Key – G

Those who want a cleaner chord, could use C instead of C9 – C9 is just easier to play, but Cmajor sounds better.

O Ri Chiraiya, Nanhi si chidiya
G       C9   G            C9   G
Angana mein phir aaja re
            D         G
O Ri Chiraiya, Nanhi si chidiya
G       C9   G            C9   G
Angana mein phir aaja re
            D         G

Andhiyara.. hai ghana
Em                 G
Aur lahu se sana
      C       D

Kirno ke tinke, ambar se chunn ke
G         C9  G             C9   G
Angana mein phir aaja re
             D         G
Humne tujhpe hazaaro sitham hai kiye
G             C9        D          G
Humne tujhpe jahaa bhar ke zulm kiye
G             C9        D          G
Humne socha nahi, Tu jo udh jaayegi
G             C         D          G
Ye zameen tere bin sooni reh jaayegi
G             C         D          G
Kiske dum pe sajega mera angana
G       C9     D      C      D

O Ri Chiraiya, Meri chiraiya
G       C9   G            C9   G
Angana mein phir aaja re
             D        G

Tere pankho mein saare sitare jadhu
G             C9        D          G
Teri chunar thanak setha satrangi bhunu
G             C9        D          G
Tere kaajal mein main, kaali raina bharu
G             C         D          G
Teri mehandi mein main, kachi dhoopa malu
G             C              D          G
Tere naino sajaa doon naya sapna
G           D          C       D

O Ri Chiraiya, Meri chiraiya
G       C9   G            C9   G
Angana mein phir aaja re
             D        G
O Ri Chiraiya, Nanhi si chidiya
G       C9   G            C9   G
Angana mein phir aaja re
            D         G

O Ri Chiraiya
G          C9

Chords: Hai apna dil toh awara

This entry is part 29 of 41 in the series Guitar

A cousin gifted me a harmonica, and my mama/uncle played an old song hai apna dil toh awara. And I was caught – how could’ve I missed playing this song on a guitar, besides trying to play it on harmonica / mouth organ.

This sweet song just rolls in the key of G, with simple 3 major chords in it, Gmaj, Dmaj and Cmaj

G                          D
Hai apanaa dil, to aawaaraa,
          C             G
Naa jaane kis pe aayegaa.
G                          D
Hai apanaa dil, to aawaaraa,
          C             G
Naa jaane kis pe aayegaa.

Verse 1:
G
Haseenon ne bulayaa, gale se bhee lagaya,
D                 C               G
Bahut samajhaya, yahi naa samajhaa,
G
Haseenon ne bulayaa, gale se bhee lagaya,
D                 C               G
Bahut samajhaya, yahi naa samajhaa,

G                          D
Bahut bholaa hain bechaaraa,
         C             G
Na jaane kis pe aayegaa.
G                          D
Hai apanaa dil, to aawaaraa,
          C             G
Naa jaane kis pe aayegaa.

Verse 2:
G
Ajab hay deewana, na dwar na thikana,
D                 C             G
Zameen se begana, falak se judaa.
G
Ajab hay deewana, na dwar na thikana,
D                 C             G
Zameen se begana, falak se judaa.
G                   D
Ye ek toota hua tara,
         C             G
Na jaane kis pe aayegaa.
G                          D
Hai apanaa dil, to aawaaraa,
          C             G
Naa jaane kis pe aayegaa.

Verse 3:
G
Zamana dekhaa sara, hain sab kaa sahara,
D                 C             G
Ye dil hi humara, hua na kise ka,
G
Zamana dekhaa sara, hain sab kaa sahara,
D                 C             G
Ye dil hi humara, hua na kise ka,
G                         D
Safar mein hain ye banjara,
          C             G
Naa jaane kis pe aayegaa.
G                          D
Hai apanaa dil, to aawaaraa,
          C             G
Naa jaane kis pe aayegaa.

Verse 4:
G
Hua jo kabhi raazi, to mila naheen kaazi,
D                     C             G
Janhaa pe lagi baazi, waheen pe hara,
G
Hua jo kabhi raazi, to mila naheen kaazi,
D                     C             G
Janhaa pe lagi baazi, waheen pe hara,
G                      D
Zaamane bhar ka naakara,
          C             G
Naa jaane kis pe aayegaa.
G                          D
Hai apanaa dil, to aawaaraa,
          C             G
Naa jaane kis pe aayegaa.

Strumming pattern

The pattern is Down, Up, Down, Down, Down. In the verse and in the chords, the last chord – G is just struck once (down) to end the pattern of G D C G.

Search targeting might take a hit with Firefox’s search over SSL

Adnetworks like Chitika who rely a lot on search engines and search queries to target their ads will have to rethink on their strategies.

With the merge of the current Aurora branch of Firefox into release, all search traffic to Google would go via SSL. Which means adnetworks doing search targeting could lose about 20% of their search traffic, as Google might stop sending search referrer URLs.

Enabling HTTPS for these searches shields our users from network infrastructure that may be gathering data about the users or modifying/censoring their search results. Additionally, using HTTPS helps providers like Google remove information from the referrer string. While Google users may expect Google to know what they are searching for, Firefox users may not be aware these search terms are often transmitted to sites they visit when they click on items in the search results; enabling HTTPS search helps sites like Google strip this infortmation from the HTTP referrer string, putting the user better in control of when and to whom their interests are shared.

 

via Rolling Out HTTPS Google search | Mozilla Privacy Blog.

 

Guhagar, Velneshwar Bike Ride

Guhagar and Velneshwar was “pending” since our last Malvan-Goa Bike ride. Many events had passed by which halted our progress towards it. But finally on 27th Apr 2012 was engraved for the start of the tour.

I messaged Manas @4.30 just to check if he is awake, and he called back, “जलदी निकल सकते हैं क्या ?” And we pre-poned our start-time to 0500 hrs instead of 0530. Ride to Guhagar was kind of disappointing with Kashedi Ghat “closed” for construction. 3 days of maintenance work. It had to align with our riding date !!? We took a big de-tour through unknown roads and relatively bad roads if compared to the winding corners of Kashedi ghat


View Larger Map

After being back on NH17, we were quick to reach Velneshwar via Chiplun-naka – Modka Agar – Velneshwar by about 1215. The road from Modka Agar to Velneshwar was horrible to say the least, 18km of ride out of which 15km was under construction with pebbles, gravel, tar – but no road and the rest with pot-hole laden hell.

Velneshwar beach, however was a paradise, similar to Ganpatipule’s decent from the top of the hill, for Velneshwar, we start climbing another of “n” hills after Palshet village, and then come down to Velneshwar. It has just about 2kms of beach, but it was breezy and reminded us straight of Agonda beach in Goa.

The next day we spent our time roaming north of Velneshwar, Gopalgadh, Dabhol power project, Guhagar beach. Due to our lazy-planning we had to cover the 18km worth of hell-road times! we could’ve checked out of Velneshwer the 2nd morning, and then stayed somewhere in Guhagar.

On our return trip, we didn’t want “no-Kashedi” experience, but also wanted turning, climbing, descending roads. Manas suggested we take a huge de-tour to Mumbai via Satara! Which would then cover the awesome Kumbharli ghat.

Our final bike route looking something like this –


View Larger Map

Photos

Welneshwar Guhaghar Bike Ride Apr 2012

Things to see and visit

We both were not of the religious type, but we did visit the Velneshwar temple in the night where there wasn’t a single soul, tranquil hear the crickets, the westerly cool breeze coming from the sea and us

  • Baman ghal (must see), a narrow gorge in the rocks through which the sea has cut a section almost 15feet deep and a 3 feet wide
  • Gopalgad
  • Guhagar beach
  • .. and try taking a de-tour through Koynanagar, and visit the Kumbharli ghat !

Baman Ghal

Web beacons with node.js

Node.js is also used as an extremely efficient Webserver, so why not leverage the efficiency for a typical adserving web-beacon logging.

The node service has to do the following

  • Data Logging
  • Serving a 1×1 pixel image

Data Logging

Select through a list of available connection providers available for node.js through npm

Image Serving

Here you have 2 options, either read a file and output the buffer like below

...
var img = fs.readFileSync('./log.gif');
res.writeHead(200, {'Content-Type': 'image/gif' });
res.end(img, 'binary');
...

or you can read the content of the small file into a buffer, convert to a hexadecimal string, and save hexadecimal string as a javascript variable, and then at runtime convert the hex to a buffer and flush out the buffer as image/[type].

This shall greatly help remove all the file IO while serving the images.

to read the image as a buffer,

fs = require('fs');
imgBuffer = fs.readFileSync('1x1.gif');

// Convert the buffer to hex
imgHex = imgBuffer.toString('hex');

Following sample code can then be added to the beacon script

//The hex string can now be directly added to the beacon script, simply as ..
var imgHex = '47494638396101000100800000dbdfef00000021f90401000000002c00000000010001000002024401003b';

// Then at runtime convert the hex back to a buffer
imgBuffer = new Buffer(imgHex, 'hex');

// This buffer can directly sent on the HTTP response stream
response.end(imgBuffer, 'binary');

The following gist is a complete integration for serving with the HTTP Server

References