#!/bin/sh
#
# git-dife - diff untracked file to a tracked version in another branch
# (default: upstream)

if command -v gstat >/dev/null 2>&1; then
  gnustat=gstat
else
  gnustat=stat
fi

file="$1"
test -e "${file}" && rooted=false || rooted=true
# shellcheck disable=SC2312
root="$(${rooted} && echo "$(gitroot)/")${file}"
test ! -e "${root}" && echo "No such file: ${file}" >&2 && exit 2
upstream="${2:-@{u}}"
# shellcheck disable=SC2250,SC2312
git show "$upstream:$($rooted || echo "./")${file}" | dif --label "$1 $(${gnustat} "$1" --format '%y')" --label "$upstream" "$root" -
