#!/bin/sh
#
# Copyright (c) Josef "Jeff" Sipek, 2007
#

USAGE="[<patchname>]"
. `dirname $0`/guilt

if [ $# -gt 1 ]; then
	usage
fi

patchname="$1"

bottom=`head -1 < "$applied" | cut -d: -f1`
base=`git-rev-parse $bottom^`

if [ -z "$patchname" ]; then
	top=`git-rev-parse HEAD`
else
	top=`grep "^[0-9a-f]\{40\}:$patchname" "$applied" | cut -d: -f1`
	if [ -z "$top" ]; then
		die "Cannot find patch '$patchname'. Is it applied?"
	fi
fi

getfiles()
{
	git-diff-tree -r "$1^" "$1" | tr '\t' ' ' | cut -d' ' -f6
}

cache="$GUILT_DIR/$branch/.graphcache.$$"
mkdir "$cache"
trap "rm -rf \"$cache\"" 0

echo "digraph G {"

current="$top"

while [ "$current" != "$base" ]; do
	echo "# checking rev $current"

	# new "hash table"
	rm -f "$cache/dep"
	touch "$cache/dep"

	getfiles $current | while read f; do
		# hash the filename
		fh=`echo "$f" | sha1sum | cut -d' ' -f1`
		if [ -e "$cache/$fh" ]; then
			# ok, something already touched this file before us
			cat "$cache/$fh" >> "$cache/dep"
		fi
		echo "$current" > "$cache/$fh"
	done

	sort -u "$cache/dep" | while read h; do
		echo "	\"${h:0:8}\" -> \"${current:0:8}\"; // ?"
	done

	current=`git-rev-parse $current^`
done

echo "}"
