#!/usr/bin/bash

if [ $# -lt 3 ]; then
	exit 1
fi

RUN="$1"
shift
PATCH="$1"
shift

SHA=`sed -ne '/^Git-commit/ { s@^Git-commit: \([a-f0-9]*\)@\1@p ; q }' "$PATCH"`
OPATCH=`grep -rl "$SHA" $@`

case "$RUN" in
	vim)
		exec vim "$OPATCH" "$PATCH" +'set ft=diff'
		;;
	colordiff)
		exec colordiff -u "$OPATCH" "$PATCH" | less
		;;
	*)
		echo "$OPATCH"
		echo "$PATCH"
		;;
esac
