#!/bin/bash
#
# Copyright (C) 2007,
#   Geoff Buchan	<geoffrey.buchan@gmail.com>
# Based on the script cvsvimdiff, written by
#   Stefano Zacchiroli	<zack@cs.unibo.it>
#   Enrico Tassi	<tassi@cs.unibo.it>
#
# This is free software, you can redistribute it and/or modify it under the
# terms of the GNU General Public License version 2 as published by the Free
# Software Foundation.
#

vimdiff="vimdiff"
suffix="vimgitdiff"
if [[ $1 == "-g" ]] ; then
  vimdiff="gvimdiff -f"
  shift 1
fi

if [[ $# < 0 || $1 == "--help" || $1 == "-h" ]] ; then
    echo "gitvimdiff - script to show git diff in vimdiff format"
    echo ""
    echo "gitvimdiff [options] file"
    echo ""
    echo "Option:"
    echo "-g    Use gvimdiff (graphical mode) instead of vimdiff"
    echo "All other options are passed to git diff"
    echo ""
    echo "If file is omitted it will cycle through all changed files in"
    echo "the current directory."
    exit 1
fi

# Assume the last argument is the filename.
# Save everything to pass to svn diff
if (( $# > 0 )) ; then
   shift_args=$(($# - 1))
else
   shift_args=$#
fi
args=$*
shift $shift_args
files="$1"
patch=`tempfile -p $suffix`
orig=`tempfile -p $suffix`
trap "rm -f $patch $orig" EXIT
if [ -z $files ] || ! [ -f $files ] ; then
    # No file given, so loop over all files svn st says have changed
    files=$(git status 2> /dev/null | grep -e "modified:" | cut -c 15-)
    for f in $files; do
      if ! [ -f $f ]; then break; fi
      filename=`basename $f`
      ending=${filename/*./}
      orig=$orig"."$ending 
      cp "$f" $orig
      git diff $args $f > $patch
      if ! [ $? -eq 0 ]; then break; fi
      patch -R -p0 $orig $patch
      $vimdiff $orig $f
    done
else
    # file given, so just work with that one
    filename=`basename $files`
    ending=${filename/*./}
    orig=$orig"."$ending
    cp $files $orig
    git diff $args > $patch
    if ! [ $? -eq 0 ]; then break; fi
    patch -R -p0 $orig $patch
    $vimdiff $orig $files
fi
