unos-repository

APK repository for unos
git clone git://git.finwo.net/misc/unos-repository
Log | Files | Refs | README

check-revision.sh (3894B)


      1 #!/bin/sh
      2 # mk/check-revision.sh - refuse to build if version-rrev already published.
      3 #
      4 #   ./mk/check-revision.sh <pkg> [arch] [--repo <repo-url>]
      5 #
      6 # Checks whether <pkg>-<version>-r<revision>.apk already exists in the
      7 # published repo. If it does, the template must bump revision before it can
      8 # be built/published. This enforces the "revision bump required" policy.
      9 #
     10 # Repo resolution: --repo flag, else local build/repo/<arch>, else
     11 # https://repo.unos.finwo.net/<arch> (if reachable).
     12 #
     13 # Used by mk/build.sh (gate) and by the conductor (pre-dispatch check).
     14 set -eu
     15 
     16 HERE=$(cd "$(dirname "$0")" && pwd)
     17 ROOT=$(cd "${HERE}/.." && pwd)
     18 
     19 PKG=""
     20 ARCH="x86_64"
     21 REPO=""
     22 
     23 while [ $# -gt 0 ]; do
     24   case "$1" in
     25     --arch) ARCH="$2"; shift 2 ;;
     26     --arch=*) ARCH="${1#--arch=}"; shift ;;
     27     --repo) REPO="$2"; shift 2 ;;
     28     --repo=*) REPO="${1#--repo=}"; shift ;;
     29     -h|--help) sed -n '1,25p' "$0"; exit 0 ;;
     30     -*) echo "check-revision.sh: unknown option: $1" >&2; exit 1 ;;
     31     *) if [ -z "${PKG}" ]; then PKG="$1"; else
     32          case "$1" in x86_64|aarch64|arm64) ARCH="$1";; *) REPO="$1";; esac
     33        fi; shift ;;
     34   esac
     35 done
     36 
     37 [ -n "${PKG}" ] || { echo "usage: check-revision.sh <pkg> [arch] [--repo <url>]" >&2; exit 1; }
     38 
     39 case "${ARCH}" in arm64) ARCH=aarch64 ;; esac
     40 case "${ARCH}" in x86_64|aarch64) ;; *) echo "check-revision.sh: unsupported ARCH ${ARCH}" >&2; exit 1 ;; esac
     41 
     42 TEMPLATE="${ROOT}/packages/${PKG}/template"
     43 [ -f "${TEMPLATE}" ] || { echo "check-revision.sh: no template: ${TEMPLATE}" >&2; exit 1; }
     44 
     45 # Read version/revision from template (same subshell pattern as deps.sh)
     46 eval "$( (
     47   version=; revision=0
     48   SYSROOT=; DESTDIR=; WRKSRC=; FILESDIR=; WORK=; SRCDEST=; UNOS_MKDIR=
     49   die() { :; } ; msg() { :; } ; vinstall() { :; } ; vmkdir() { :; }
     50   . "${TEMPLATE}" >/dev/null 2>&1 || true
     51   printf 'VERSION=%s\nREVISION=%s\n' "${version}" "${revision}"
     52 ) )"
     53 
     54 [ -n "${VERSION}" ] || { echo "check-revision.sh: template sets no version" >&2; exit 1; }
     55 
     56 PKGVER="${VERSION}-r${REVISION}"
     57 APK_NAME="${PKG}-${PKGVER}.apk"
     58 
     59 # Check local repo first
     60 if [ -z "${REPO}" ]; then
     61   if [ -f "${ROOT}/build/repo/${ARCH}/${APK_NAME}" ]; then
     62     echo "check-revision.sh: ${APK_NAME} already exists in build/repo/${ARCH}/" >&2
     63     echo "  bump revision in packages/${PKG}/template before rebuilding" >&2
     64     exit 1
     65   fi
     66   # Also check published repo via local path if it exists
     67   REPO="${ROOT}/build/repo/${ARCH}"
     68 fi
     69 
     70 # If REPO is a URL, use curl HEAD; if local dir, check file existence.
     71 case "${REPO}" in
     72   http://*|https://*)
     73     url="${REPO%/}/${APK_NAME}"
     74     # Normalize // in path (BUCKET_PUBLIC_URL may have trailing slash).
     75     url=$(printf "%s" "${url}" | sed 's#://#___PROTO___#; s#//*#/#g; s#___PROTO___#://#')
     76 
     77     if ! command -v curl >/dev/null 2>&1 && command -v apt-get >/dev/null 2>&1; then
     78       apt-get update -qq >/dev/null 2>&1 || true
     79       apt-get install -y -qq --no-install-recommends curl ca-certificates >/dev/null 2>&1 || true
     80     fi
     81     command -v curl >/dev/null 2>&1 || {
     82       echo "check-revision.sh: curl unavailable; cannot determine if ${APK_NAME} is published" >&2
     83       exit 2
     84     }
     85 
     86     code=$(curl -sSL -o /dev/null -w '%{http_code}' --head "${url}" 2>/dev/null) || code=000
     87     case "${code}" in
     88       2*)
     89         echo "check-revision.sh: ${APK_NAME} already published at ${url}" >&2
     90         echo "  bump revision in packages/${PKG}/template" >&2
     91         exit 1
     92         ;;
     93       404) ;;
     94       *)
     95         echo "check-revision.sh: cannot determine if ${APK_NAME} is published (HTTP ${code} for ${url})" >&2
     96         exit 2
     97         ;;
     98     esac
     99     ;;
    100   *)
    101     if [ -f "${REPO}/${APK_NAME}" ]; then
    102       echo "check-revision.sh: ${APK_NAME} already exists in ${REPO}/" >&2
    103       echo "  bump revision in packages/${PKG}/template before rebuilding" >&2
    104       exit 1
    105     fi
    106     ;;
    107 esac
    108 
    109 echo "check-revision.sh: OK ${APK_NAME} not yet published"