#!/usr/bin/env bash
# nerf-git-pull -- Pull the current branch from a remote
# Generated from git manifest. Do not edit directly.
# nerf:threat:read=remote
# nerf:threat:write=workspace

set -euo pipefail

_NERF_DRY_RUN=""

usage() {
  cat >&2 <<'EOF'
Usage: nerf-git-pull <remote>

Arguments:
  <remote> (required)
      Remote name (e.g. origin)
      Must match: ^[a-z0-9_-]+$

Maps to: git pull <remote>

Pull the current branch from a remote.
EOF
  exit 1
}

while [[ $# -gt 0 ]]; do
  case "$1" in
    --nerf-dry-run) _NERF_DRY_RUN="true"; shift 1 ;;
    -h|--help) usage ;;
    --) shift; break ;;
    *) break ;;
  esac
done

REMOTE="${1:-}"
shift 2>/dev/null || true

if [[ -n "${REMOTE}" ]] && [[ "${REMOTE}" == -* ]]; then
  echo "error: nerf-git-pull: <remote> cannot start with '-'" >&2
  echo "  hint: use -- before positional arguments if needed" >&2
  exit 1
fi

if [[ -z "${REMOTE}" ]]; then
  echo "error: nerf-git-pull: missing required argument <remote>" >&2
  echo "  hint: provide a value for <remote>" >&2
  usage
fi

_NERF_PATTERN='^[a-z0-9_-]+$'
if [[ -n "${REMOTE}" ]] && ! [[ "${REMOTE}" =~ $_NERF_PATTERN ]]; then
  echo "error: nerf-git-pull: argument <remote> does not match required pattern" >&2
  echo "  value:   \"${REMOTE}\"" >&2
  echo "  pattern: ^[a-z0-9_-]+$" >&2
  echo "  hint: value must match ^[a-z0-9_-]+$" >&2
  exit 1
fi

if [[ "$_NERF_DRY_RUN" == "true" ]]; then
  echo "dry-run: git pull "${REMOTE}""
  exit 0
fi

exec git pull "${REMOTE}"
