#!/usr/bin/env sh
#
# git-diff-all - show colored output for changed + untracked files

set -e

file="$1"
output=$(git diff --color=always "${file}" 2>/dev/null | delta)

if [ -n "${output}" ]; then
  echo "${output}"
elif [ -n "${file}" ]; then
  git diff --color=always --no-index /dev/null "${file}" | delta
elif [ -z "${file}" ]; then
  # remove any old files
  # its not easy to git diff a deleted file, as it doesnt have to be in-tree.
  # thats why we copy the last revision (HEAD) to our XDG runtime dir
  # so we can compare it with /dev/null.
  tmpfile=$(mktemp "${TMPDIR:-/tmp}"/"$(basename "$0")") || return
  rm -- "${tmpfile}"
  git show HEAD:./"${file}" > "${tmpfile}"
  git diff --color=always --no-index "${tmpfile}" /dev/null | delta
  trap 'command rm -f -- "$tmpfile"' EXIT INT TERM HUP
fi
