#!/usr/bin/env bash # sync_upstream_knoe_db.sh — Pull upstream knoe-db changes into a review branch. # # Usage: # ./scripts/sync_upstream_knoe_db.sh [--dry-run] # # What it does: # 1. Ensures the 'upstream-knoe-db' remote points to git@git.knoe.dev:knoe-dev/knoe-db.git # 2. Fetches the upstream default branch # 3. Creates a timestamped branch upstream/knoe-db/YYYYMMDD from the fetch # 4. Prints next steps for review and merge # # Run from the knoe-db/ subdirectory or the repo root (script auto-detects). set -euo pipefail UPSTREAM_REMOTE="upstream-knoe-db" UPSTREAM_URL="git@git.knoe.dev:knoe-dev/knoe-db.git" UPSTREAM_BRANCH="main" # adjust if upstream uses a different default branch DATE_TAG="$(date +%Y%m%d)" REVIEW_BRANCH="upstream/knoe-db/${DATE_TAG}" DRY_RUN=false # ── argument parsing ────────────────────────────────────────────────────────── for arg in "$@"; do case "$arg" in --dry-run) DRY_RUN=true ;; *) echo "Unknown argument: $arg" >&2; exit 1 ;; esac done run() { if $DRY_RUN; then echo "[dry-run] $*" else "$@" fi } # ── locate the knoe-db git worktree ─────────────────────────────────────────── SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)" KNOE_DB_DIR="${REPO_ROOT}/knoe-db" if [ ! -d "${KNOE_DB_DIR}/.git" ] && [ ! -f "${KNOE_DB_DIR}/.git" ]; then # knoe-db may be a plain directory inside a mono-repo; operate from repo root GIT_DIR="${REPO_ROOT}" else GIT_DIR="${KNOE_DB_DIR}" fi echo "==> Working in git repo: ${GIT_DIR}" cd "${GIT_DIR}" # ── ensure we are on a clean working tree ──────────────────────────────────── if ! $DRY_RUN; then if ! git diff --quiet || ! git diff --cached --quiet; then echo "ERROR: Working tree has uncommitted changes. Stash or commit before syncing." >&2 exit 1 fi fi # ── add / verify upstream remote ───────────────────────────────────────────── if git remote get-url "${UPSTREAM_REMOTE}" &>/dev/null; then CURRENT_URL="$(git remote get-url "${UPSTREAM_REMOTE}")" if [ "${CURRENT_URL}" != "${UPSTREAM_URL}" ]; then echo "==> Updating remote '${UPSTREAM_REMOTE}' URL to ${UPSTREAM_URL}" run git remote set-url "${UPSTREAM_REMOTE}" "${UPSTREAM_URL}" else echo "==> Remote '${UPSTREAM_REMOTE}' already set to ${UPSTREAM_URL}" fi else echo "==> Adding remote '${UPSTREAM_REMOTE}' → ${UPSTREAM_URL}" run git remote add "${UPSTREAM_REMOTE}" "${UPSTREAM_URL}" fi # ── fetch upstream ──────────────────────────────────────────────────────────── echo "==> Fetching ${UPSTREAM_REMOTE}/${UPSTREAM_BRANCH} …" run git fetch "${UPSTREAM_REMOTE}" "${UPSTREAM_BRANCH}" # ── create review branch ────────────────────────────────────────────────────── if git show-ref --verify --quiet "refs/heads/${REVIEW_BRANCH}"; then echo "==> Branch '${REVIEW_BRANCH}' already exists — skipping creation." echo " Delete it first if you want a fresh sync: git branch -D ${REVIEW_BRANCH}" else echo "==> Creating review branch '${REVIEW_BRANCH}' from ${UPSTREAM_REMOTE}/${UPSTREAM_BRANCH}" run git checkout -b "${REVIEW_BRANCH}" "${UPSTREAM_REMOTE}/${UPSTREAM_BRANCH}" run git checkout - # return to previous branch fi # ── summary ─────────────────────────────────────────────────────────────────── echo "" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo " Upstream sync complete." echo "" echo " Review branch : ${REVIEW_BRANCH}" echo " Upstream : ${UPSTREAM_URL} (${UPSTREAM_BRANCH})" echo "" echo " Next steps:" echo " 1. Inspect the incoming changes:" echo " git log main..${REVIEW_BRANCH} --oneline" echo " git diff main...${REVIEW_BRANCH} -- knoe-db/" echo "" echo " 2. Check out the review branch and resolve any conflicts:" echo " git checkout ${REVIEW_BRANCH}" echo "" echo " 3. Merge (or cherry-pick) into main when satisfied:" echo " git checkout main" echo " git merge --no-ff ${REVIEW_BRANCH} -m 'chore(knoe-db): merge upstream ${DATE_TAG}'" echo "" echo " 4. Push and open a PR / MR for team review." echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"