#!/bin/bash
# reset environment variables that could interfere with normal usage
export GREP_OPTIONS=
# put all utility functions here

# make a temporary file
git_extra_mktemp() {
    mktemp -t "$(basename "$0")".XXXXXXX
}

git_extra_default_branch() {
    local default_branch
    default_branch=$(git config --get git-extras.default-branch)
    if [ -z "$default_branch" ]; then
        echo "master"
    else
        echo "$default_branch"
    fi
}
#
# check whether current directory is inside a git repository
#

is_git_repo() {
  git rev-parse --show-toplevel > /dev/null 2>&1
  result=$?
  if test $result != 0; then
    >&2 echo 'Not a git repo!'
    exit $result
  fi
}

is_git_repo
#
# Change files modification time to their last commit date
#
if [ "$1" = --touch ]; then
  # Internal use option only just to parallelize things.
  shift
  for f; do
    iso_t=$(git --no-pager log --no-renames --pretty=format:%cI -1 @ -- "$f" 2>/dev/null)
    if [ -n "$iso_t" ]; then
      bsd=$(date -j > /dev/null 2>&1 && echo 'true')
      if [ "$bsd" == "true" ]; then
        t=$(date -j -f %Y-%m-%dT%H:%M:%S "$iso_t" +%Y%m%d%H%M.%S)
      else
        t=$(date -d"$iso_t" +%Y%m%d%H%M.%S)
      fi
      echo "+ touch -t $t $f" >&2
      touch -t "$t" "$f"
    fi
  done
else
  opt_r=$(xargs -r false < /dev/null > /dev/null 2>&1 && echo '-r')

  # `-n` should be limited or parallelization will not give effect,
  # because all args will go into single worker.
  NPROC=$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || getconf _NPROCESSORS_ONLN 2>/dev/null)
  # shellcheck disable=SC2086
  git ls-tree -z -r -t --name-only @ \
  | xargs -0 -P${NPROC:-1} -n${NPROC:-1} $opt_r git utimes --touch
fi
