#!/bin/bash
# chmod +x and copy to /usr/local/bin/p4merge
# then
# git config --global merge.tool p4merge
# git config --global mergetool.keepTemporaries false
# git config --global mergetool.prompt false

if [ -n "$GIT_DIR" ]; then
  if $(git submodule | grep -q $2); then
    tmp=$(mktemp -t $2) && trap 'rm -rf $tmp' EXIT HUP INT QUIT TERM
    sha=$(cd $2 > /dev/null && git rev-parse HEAD)
    echo "Subproject commit $sha" > $tmp
    set -- $1 $tmp
  fi
fi

for arg; do [[ $arg = /* ]] || arg=$PWD/$arg; absargs+=("$arg"); done;
/Applications/p4merge.app/Contents/Resources/launchp4merge "${absargs[@]}"
