#!/bin/bash # Prepare AUR submission archives and push updates to existing AUR bases. set -euo pipefail REPO_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd) readonly REPO_DIR UPLOAD_DIR="${AUR_UPLOAD_DIR:-${REPO_DIR}/aur-upload}" AUR_REPO_DIR="${AUR_REPO_DIR:-${REPO_DIR}/aur-repos}" # Print supported commands and options. usage() { cat << EOF Usage: $0 prepare [--force] [--output DIR] [PKGBASE...] $0 push [--all] [--repo-dir DIR] PKGBASE... prepare creates tarballs containing only PKGBUILD and .SRCINFO. push updates existing AUR repositories and pushes commits to master. Set AUR_UPLOAD_DIR or AUR_REPO_DIR to override the default directories. EOF } # Print all package bases generated in the checkout. # Globals: REPO_DIR. # Outputs: Package bases, one per line. all_package_bases() { find "${REPO_DIR}/aur-packages" \ -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | sort } # Return the source directory for a package base. # Globals: REPO_DIR. # Arguments: Package base name. # Outputs: Package directory on stdout. package_dir_for() { local package_base="$1" printf '%s\n' "${REPO_DIR}/aur-packages/${package_base}" } # Validate the package directory and its generated pkgbase. # Arguments: Package base name. # Returns: Non-zero if its metadata is missing or inconsistent. validate_package() { local package_base="$1" local package_dir [[ "$package_base" =~ ^[a-z0-9-]+$ ]] || { printf 'Invalid package base: %s\n' "$package_base" >&2 return 1 } package_dir=$(package_dir_for "$package_base") [[ -d "$package_dir" && -f "${package_dir}/PKGBUILD" && -f "${package_dir}/.SRCINFO" ]] || { printf 'Missing PKGBUILD or .SRCINFO for %s\n' "$package_base" >&2 return 1 } local actual_base actual_base=$(awk '$1 == "pkgbase" { print $3; exit }' "${package_dir}/.SRCINFO") [[ "$actual_base" == "$package_base" ]] || { printf '%s: .SRCINFO pkgbase is %s\n' "$package_base" "${actual_base:-missing}" >&2 return 1 } } # Create first-submission archives containing only AUR metadata files. # Globals: UPLOAD_DIR, REPO_DIR. # Arguments: Optional --force, --output directory, and package bases. # Outputs: Archive paths on stdout. prepare_packages() { local force=false local package_base package_dir archive temp_archive entry_count local -a package_bases=() while (($#)); do case "$1" in --force) force=true shift ;; --output) (($# >= 2)) || { usage >&2 return 2 } UPLOAD_DIR="$2" shift 2 ;; --help | -h) usage return 0 ;; --*) printf 'Unknown option: %s\n' "$1" >&2 usage >&2 return 2 ;; *) package_bases+=("$1") shift ;; esac done if ((${#package_bases[@]} == 0)); then mapfile -t package_bases < <(all_package_bases) fi mkdir -p "$UPLOAD_DIR" for package_base in "${package_bases[@]}"; do validate_package "$package_base" package_dir=$(package_dir_for "$package_base") archive="${UPLOAD_DIR}/${package_base}.tar.gz" if [[ -e "$archive" ]] && ! $force; then printf 'Upload archive already exists: %s (use --force to replace)\n' "$archive" >&2 return 1 fi temp_archive="${archive}.tmp.$$" tar -czf "$temp_archive" -C "$package_dir" PKGBUILD .SRCINFO entry_count=$(tar -tzf "$temp_archive" | wc -l) tar -tzf "$temp_archive" | grep -Fxq PKGBUILD tar -tzf "$temp_archive" | grep -Fxq .SRCINFO [[ "$entry_count" -eq 2 ]] || { rm -f "$temp_archive" printf 'Unexpected files in upload archive for %s\n' "$package_base" >&2 return 1 } mv -f -- "$temp_archive" "$archive" printf '%s\n' "$archive" done } # Update one existing AUR Git checkout and push only PKGBUILD/.SRCINFO. # Globals: AUR_REPO_DIR. # Arguments: Package base name. # Returns: makepkg/git status, or non-zero when the checkout is unsafe. push_package() { local package_base="$1" local package_dir checkout remote expected_remote actual_remote validate_package "$package_base" package_dir=$(package_dir_for "$package_base") checkout="${AUR_REPO_DIR}/${package_base}" remote="ssh://aur@aur.archlinux.org/${package_base}.git" if [[ ! -d "${checkout}/.git" ]]; then if [[ -e "$checkout" ]]; then printf 'Destination exists but is not a Git repository: %s\n' "$checkout" >&2 return 1 fi mkdir -p "$AUR_REPO_DIR" git clone "$remote" "$checkout" || { printf 'Cannot clone %s. Submit its prepare tarball first.\n' \ "$package_base" >&2 return 1 } fi expected_remote="ssh://aur@aur.archlinux.org/${package_base}.git" actual_remote=$(git -C "$checkout" remote get-url origin) case "$actual_remote" in "$expected_remote" | "aur@aur.archlinux.org:${package_base}.git") ;; *) printf 'Unexpected origin for %s: %s\n' "$package_base" "$actual_remote" >&2 return 1 ;; esac if [[ -n "$(git -C "$checkout" status --porcelain)" ]]; then printf 'Refusing to overwrite a dirty checkout: %s\n' "$checkout" >&2 return 1 fi install -m644 "${package_dir}/PKGBUILD" "${checkout}/PKGBUILD" install -m644 "${package_dir}/.SRCINFO" "${checkout}/.SRCINFO" git -C "$checkout" add -- PKGBUILD .SRCINFO if git -C "$checkout" diff --cached --quiet; then printf '%s is already up to date.\n' "$package_base" return 0 fi git -C "$checkout" commit -m "Update ${package_base}" git -C "$checkout" push origin HEAD:master } # Push selected package bases, or every generated base with --all. # Globals: AUR_REPO_DIR. # Arguments: Optional --all/--repo-dir and package bases. push_packages() { local push_all=false local package_base local -a package_bases=() while (($#)); do case "$1" in --all) push_all=true shift ;; --repo-dir) (($# >= 2)) || { usage >&2 return 2 } AUR_REPO_DIR="$2" shift 2 ;; --help | -h) usage return 0 ;; --*) printf 'Unknown option: %s\n' "$1" >&2 usage >&2 return 2 ;; *) package_bases+=("$1") shift ;; esac done if $push_all; then ((${#package_bases[@]} == 0)) || { printf 'Do not combine --all with package names.\n' >&2 return 2 } mapfile -t package_bases < <(all_package_bases) elif ((${#package_bases[@]} == 0)); then printf 'Specify package bases, or pass --all to push all packages.\n' >&2 return 2 fi for package_base in "${package_bases[@]}"; do push_package "$package_base" done } # Dispatch the requested publishing operation. main() { local command_name="${1:-}" if (($#)); then shift fi case "$command_name" in prepare) prepare_packages "$@" ;; push) push_packages "$@" ;; --help | -h | '') usage ;; *) printf 'Unknown command: %s\n' "$command_name" >&2 usage >&2 return 2 ;; esac } main "$@"