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

Posted on 23.02.2015

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 b365530bb6341cad..b85656f28575fd 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 b365530bb6..b85656f28575