#!/usr/bin/env bash set -Eeuo pipefail ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" cd "$ROOT_DIR" branch="${1:-${DEPLOY_BRANCH:-main}}" remote="${2:-${DEPLOY_REMOTE:-origin}}" log() { printf '\n[%s] %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$1" } if ! command -v git >/dev/null 2>&1; then printf 'Missing required command: git\n' >&2 exit 1 fi if [[ ! -d .git ]]; then printf 'No .git directory found in %s\n' "$ROOT_DIR" >&2 printf 'Clone the repository on the server with git before using this script.\n' >&2 exit 1 fi if ! git diff --quiet || ! git diff --cached --quiet; then printf 'Tracked local changes detected. Commit or stash them before updating from GitHub.\n' >&2 exit 1 fi log "Fetching updates from ${remote}" git fetch --prune "$remote" if git show-ref --verify --quiet "refs/heads/$branch"; then log "Switching to branch ${branch}" git checkout "$branch" else log "Creating local branch ${branch} tracking ${remote}/${branch}" git checkout -b "$branch" --track "${remote}/${branch}" fi log "Pulling the latest changes for ${branch}" git pull --ff-only "$remote" "$branch" log "Running deployment script" bash "$ROOT_DIR/scripts/deploy.sh"