#!/bin/sh

TMP=$(mktemp -t git-overview.XXXXXXXXXXX)
trap 'rm -f "$TMP"' 0 1 2 3 15

git log --reverse --pretty=short --decorate --pretty=format:%H%d $@ |\
    awk 'BEGIN {FS="[ ,()]*"} NF>=2 {print $1}' | \
    while read hash ; do
    # Independent tags/branches parents:
    # Notes:
    #   * -n 1000 to limit the search, ideally "git log" could stop
    #     traversing the history when hits a tag/branch tip
    #   * head -n 25 because "git show-branch --independent" has this limit
    ancestors=$(git log -n 1000 --pretty=short --decorate --pretty=format:%H%d $hash^@ |\
    awk 'BEGIN {FS="[ ,()]*"} NF>=2 {print $1}' | head -n 25)
    if [ -n "$ancestors" ] ; then
	echo $hash $(git show-branch --independent $ancestors)
    else
	echo $hash
    fi
done > $TMP
GIT_GRAFT_FILE=$TMP gitk "$@" -d
