/ linux

Random Hack: Identify the mid-point between two git commits

Francis asked on irc://irc.freenode.net/#coreboot how to find the mid-point between two commits, like what git bisect is doing – just without git bisect.

So here's a bash function that does this:

git_midpoint() {
	local NUM
    NUM=$(($(git log --pretty=%h $1 |wc -l) / 2))
    if [ $NUM -gt 0 ]; then
    	git show $(git log --pretty=%h -n $NUM $1 |tail -1)
    fi
}

Running git_midpoint b365530bb6341cad601532e43fc899f56ba57acb..b85656f28575fdf3dbb6925aa047ea8db64d0495 on the coreboot repository works as expected. The reverse order returns nothing, as is typical for git commands.

And integrating into git is also possible (and it also uses more basic shell commands for better compatibility):

$ git config --global alias.midpoint '!sh -c '"'"'NUM=`git log --pretty=%h $1 |wc -l`; NUM=`expr $NUM / 2`; if [ $NUM -gt 0 ]; then git show `git log --pretty=%h -n $NUM $1 |tail -1`; fi'"'"' - '

With this, it's

$ git midpoint b365530bb6341cad601532e43fc899f56ba57acb..b85656f28575fdf3dbb6925aa047ea8db64d0495
Patrick Georgi

Patrick Georgi

Von der Stadt aufs Land. Freund des retro computings und des inneren Exils. Die Gedanken sind frei. Dieses Recht findet seine Schranken in den Vorschriften der allgemeinen Gesetze.

Read More