#!/usr/bin/env zsh
# Max seconds to wait for the remote before giving up
FETCH_TIMEOUT=${PROJECT_GIT_FETCH_TIMEOUT:-3}

# Is this directory inside a git repo?
if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
  # Are we at the repo root? (prefix empty = root)
  if [ -z "$(git rev-parse --show-prefix)" ]; then
    echo "Fetching latest changes from remote..."
    # Never prompt for credentials, fail fast on unreachable SSH hosts
    GIT_TERMINAL_PROMPT=0 \
    GIT_SSH_COMMAND="ssh -o ConnectTimeout=$FETCH_TIMEOUT -o BatchMode=yes" \
      timeout -k 1 "$FETCH_TIMEOUT" git fetch --quiet 2>/dev/null
    case $? in
      0) ;;
      124|137) echo "⚠ git fetch abandoned: remote unreachable after ${FETCH_TIMEOUT}s" ;;
      *) echo "⚠ git fetch failed (remote unreachable or auth error)" ;;
    esac
  fi
fi
