| 1 | #!/usr/bin/env bash |
| 2 | # Update an AUR package from the BlueGit source archive of this commit. |
| 3 | # AUR_PKG=bluegit (default, packaging/aur) or AUR_PKG=bluegit-runner |
| 4 | # (packaging/aur-runner). Pushes ssh://aur@aur.archlinux.org/$AUR_PKG.git |
| 5 | # master. AUR only accepts master. |
| 6 | set -euo pipefail |
| 7 | |
| 8 | ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| 9 | # shellcheck source=aur-common.sh |
| 10 | source "$(dirname "$0")/aur-common.sh" |
| 11 | |
| 12 | # pkgver_of_commit is YYYYMMDDHHMMSS in UTC, from the committer time. |
| 13 | # CI_COMMIT_TIMESTAMP is ISO 8601. Without it, git show -s --format=%cI |
| 14 | # is the same instant the self-updater uses. The job clock is not a version. |
| 15 | # BusyBox date does not parse the Z or an offset, so the offset is applied here. |
| 16 | pkgver_of_commit() { |
| 17 | local root="$1" |
| 18 | local commit="${2:-}" |
| 19 | if [[ -z "$commit" ]]; then |
| 20 | commit="$(git_safe "$root" rev-parse HEAD)" || return 1 |
| 21 | fi |
| 22 | [[ "$commit" =~ ^[0-9a-f]{40}$ ]] || { echo "CI_COMMIT_SHA or HEAD is not a full sha" >&2; return 1; } |
| 23 | local stamp version |
| 24 | if [[ -n "${CI_COMMIT_TIMESTAMP:-}" ]]; then |
| 25 | stamp="$CI_COMMIT_TIMESTAMP" |
| 26 | else |
| 27 | # Zonder || blijft stamp leeg: set -e stopt niet op een falende |
| 28 | # command substitution, en utc_digits meldt dan een lege stempel. |
| 29 | stamp="$(git_safe "$root" show -s --format=%cI "$commit")" || return 1 |
| 30 | fi |
| 31 | version="$(utc_digits "$stamp")" || return 1 |
| 32 | [[ "$version" =~ ^[0-9]{14}$ ]] || { echo "pkgver is not a timestamp: ${version:-empty} from ${stamp:-empty}" >&2; return 1; } |
| 33 | printf '%s\n' "$version" |
| 34 | } |
| 35 | |
| 36 | # utc_digits turns ISO 8601 into YYYYMMDDHHMMSS in UTC. |
| 37 | # Accepted forms: 2026-10-22T23:32:33Z, 2026-10-22T23:32:33+01:00, 2026-10-22T23:32:33+0100. |
| 38 | utc_digits() { |
| 39 | local stamp="$1" core off sign hh mm epoch adj |
| 40 | if [[ "$stamp" == *Z ]]; then |
| 41 | core="${stamp%Z}" |
| 42 | off="+00:00" |
| 43 | elif [[ "$stamp" =~ ^(.+)([+-][0-9]{2}:[0-9]{2})$ ]]; then |
| 44 | core="${BASH_REMATCH[1]}" |
| 45 | off="${BASH_REMATCH[2]}" |
| 46 | elif [[ "$stamp" =~ ^(.+)([+-][0-9]{4})$ ]]; then |
| 47 | core="${BASH_REMATCH[1]}" |
| 48 | off="${BASH_REMATCH[2]:0:3}:${BASH_REMATCH[2]:3:2}" |
| 49 | else |
| 50 | core="$stamp" |
| 51 | off="+00:00" |
| 52 | fi |
| 53 | core="${core%%.*}" |
| 54 | core="${core/T/ }" |
| 55 | [[ "$core" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}\ [0-9]{2}:[0-9]{2}:[0-9]{2}$ ]] || { |
| 56 | echo "pkgver stamp is not ISO 8601: $stamp" >&2 |
| 57 | return 1 |
| 58 | } |
| 59 | sign="${off:0:1}" |
| 60 | hh="${off:1:2}" |
| 61 | mm="${off:4:2}" |
| 62 | epoch="$(TZ=UTC date -u -d "$core" +%s)" || return 1 |
| 63 | adj=$((10#$hh * 3600 + 10#$mm * 60)) |
| 64 | if [[ "$sign" == "+" ]]; then |
| 65 | epoch=$((epoch - adj)) |
| 66 | else |
| 67 | epoch=$((epoch + adj)) |
| 68 | fi |
| 69 | TZ=UTC date -u -d "@$epoch" +%Y%m%d%H%M%S |
| 70 | } |
| 71 | |
| 72 | if [[ "${1:-}" == "--rewrite-only" ]]; then |
| 73 | rewrite_pkgbuild "$2" "$3" "$4" "$5" |
| 74 | exit 0 |
| 75 | fi |
| 76 | |
| 77 | if [[ "${1:-}" == "--print-pkgver" ]]; then |
| 78 | pkgver_of_commit "$ROOT" "${CI_COMMIT_SHA:-}" |
| 79 | exit |
| 80 | fi |
| 81 | |
| 82 | for cmd in git sed sha256sum curl; do |
| 83 | command -v "$cmd" >/dev/null || { echo "Missing command: $cmd" >&2; exit 1; } |
| 84 | done |
| 85 | |
| 86 | if [[ -n "${CI_COMMIT_SHA:-}" ]]; then |
| 87 | COMMIT="$CI_COMMIT_SHA" |
| 88 | else |
| 89 | COMMIT="$(git_safe "$ROOT" rev-parse HEAD)" || exit 1 |
| 90 | fi |
| 91 | VERSION="$(pkgver_of_commit "$ROOT" "$COMMIT")" |
| 92 | |
| 93 | PKG="${AUR_PKG:-bluegit}" |
| 94 | case "$PKG" in |
| 95 | bluegit) PKGDIR="$ROOT/packaging/aur" ;; |
| 96 | bluegit-runner) PKGDIR="$ROOT/packaging/aur-runner" ;; |
| 97 | *) echo "Unknown AUR_PKG: $PKG" >&2; exit 1 ;; |
| 98 | esac |
| 99 | INSTALL_FILE="${PKG}.install" |
| 100 | |
| 101 | TMPDIR="${TMPDIR:-/tmp}" |
| 102 | WORKDIR="$(mktemp -d "$TMPDIR/bluegit-aur-XXXXXX")" |
| 103 | cleanup() { rm -rf "$WORKDIR"; } |
| 104 | trap cleanup EXIT |
| 105 | |
| 106 | SOURCE_URL="https://git.timfalken.com/timfa/bluegit/-/archive/${COMMIT}/bluegit-${COMMIT}.tar.gz" |
| 107 | TMP_SOURCE="$WORKDIR/source.tar.gz" |
| 108 | echo "Checking source tarball: $SOURCE_URL" |
| 109 | if ! curl -fsSL "$SOURCE_URL" -o "$TMP_SOURCE"; then |
| 110 | echo "Could not download $SOURCE_URL" >&2 |
| 111 | exit 1 |
| 112 | fi |
| 113 | SOURCE_SHA="$(sha256sum "$TMP_SOURCE" | awk '{print $1}')" |
| 114 | |
| 115 | dest="$WORKDIR/$PKG" |
| 116 | clone_or_init_aur_repo "$PKG" "$dest" |
| 117 | cd "$dest" |
| 118 | git config user.name "${AUR_GIT_AUTHOR_NAME:-BlueGit release bot}" |
| 119 | git config user.email "${AUR_GIT_AUTHOR_EMAIL:-noreply@example.invalid}" |
| 120 | cp "$PKGDIR/PKGBUILD" PKGBUILD |
| 121 | cp "$PKGDIR/$INSTALL_FILE" "$INSTALL_FILE" |
| 122 | rewrite_pkgbuild "$VERSION" "$COMMIT" "$SOURCE_SHA" PKGBUILD |
| 123 | generate_srcinfo |
| 124 | |
| 125 | if git diff --quiet -- PKGBUILD .SRCINFO "$INSTALL_FILE" 2>/dev/null && git rev-parse --verify HEAD >/dev/null 2>&1; then |
| 126 | echo "AUR repo already up to date: ${PKG} ${VERSION}" |
| 127 | exit 0 |
| 128 | fi |
| 129 | |
| 130 | git add PKGBUILD .SRCINFO "$INSTALL_FILE" |
| 131 | if git rev-parse --verify HEAD >/dev/null 2>&1; then |
| 132 | git commit -m "upstream ${VERSION}" |
| 133 | else |
| 134 | git commit -m "initial import ${VERSION}" |
| 135 | fi |
| 136 | git push -u origin master |
| 137 | echo "AUR updated: ${PKG} ${VERSION}" |