vbcc/sync-sources.sh
2026-07-29 16:10:22 +03:00

105 lines
3.1 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
readonly PROJECT='vbcc'
readonly SOURCE_URL='http://www.ibaug.de/vbcc/vbcc.tar.gz'
readonly SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")" && pwd -P)"
readonly SCRIPT_NAME="$(basename -- "$0")"
readonly TMP_DIR="$(mktemp -d "${TMPDIR:-/tmp}/${PROJECT}-mirror.XXXXXXXX")"
readonly EXTRACT_DIR="${TMP_DIR}/sources"
cleanup() {
rm -rf -- "$TMP_DIR"
}
trap cleanup EXIT HUP INT TERM
for command in awk curl git rsync tar; do
if ! command -v "$command" >/dev/null 2>&1; then
printf 'error: required command not found: %s\n' "$command" >&2
exit 1
fi
done
git_root="$(git -C "$SCRIPT_DIR" rev-parse --show-toplevel 2>/dev/null || true)"
if [[ -z "$git_root" ]] || \
[[ "$(CDPATH= cd -- "$git_root" && pwd -P)" != "$SCRIPT_DIR" ]]; then
printf 'error: %s is not the root of a Git repository\n' "$SCRIPT_DIR" >&2
exit 1
fi
mkdir -p -- "$EXTRACT_DIR"
archive="${TMP_DIR}/${PROJECT}.tar.gz"
printf 'Downloading %s from %s\n' "$PROJECT" "$SOURCE_URL"
curl --location --fail --show-error --silent \
--retry 3 --retry-delay 2 --output "$archive" "$SOURCE_URL"
if ! tar -tzf "$archive" | awk -v root="$PROJECT" '
$0 == root || $0 == root "/" { next }
index($0, root "/") != 1 || $0 ~ /(^|\/)\.\.(\/|$)/ { bad = 1 }
END { exit bad }
'; then
printf 'error: archive contains an unexpected path\n' >&2
exit 1
fi
tar -xzf "$archive" --no-same-owner --directory "$EXTRACT_DIR"
source_root="${EXTRACT_DIR}/${PROJECT}"
if [[ ! -d "$source_root" ]]; then
printf 'error: %s/ is missing from the archive\n' "$PROJECT" >&2
exit 1
fi
version="$(awk '
/^char \*copyright="vbcc V/ {
sub(/^.*"vbcc V/, "")
sub(/ \(c\).*$/, "")
print
exit
}
' "${source_root}/vars.c")"
if [[ -z "$version" ]]; then
printf 'error: could not extract the %s version\n' "$PROJECT" >&2
exit 1
fi
tag="v${version// /-}"
if ! git check-ref-format "refs/tags/${tag}" >/dev/null; then
printf 'error: extracted version produces an invalid tag: %s\n' "$tag" >&2
exit 1
fi
printf 'Synchronizing %s %s into %s\n' "$PROJECT" "$version" "$SCRIPT_DIR"
rsync -av --delete \
--exclude='/.git/' --exclude='/.github/' --exclude='/README.md' \
--exclude='/vbcc.rb' --exclude="/${SCRIPT_NAME}" \
"${source_root}/" "${SCRIPT_DIR}/"
if [[ -z "$(git -C "$SCRIPT_DIR" status --porcelain)" ]]; then
printf '%s is already up to date; nothing to commit.\n' "$PROJECT"
exit 0
fi
printf '\nPending changes for %s %s (tag %s):\n' "$PROJECT" "$version" "$tag"
git -C "$SCRIPT_DIR" status --short
git -C "$SCRIPT_DIR" diff --stat
if git -C "$SCRIPT_DIR" show-ref --verify --quiet "refs/tags/${tag}"; then
printf 'error: tag %s already exists; commit was not created\n' "$tag" >&2
exit 1
fi
answer=''
printf '\nRun git add -A, commit, and create tag %s? Type uppercase Y to confirm: ' "$tag"
IFS= read -r answer || true
if [[ "$answer" != 'Y' ]]; then
printf 'Not confirmed; changes remain uncommitted.\n'
exit 0
fi
git -C "$SCRIPT_DIR" add -A
git -C "$SCRIPT_DIR" commit -m "Import ${PROJECT} ${version}"
git -C "$SCRIPT_DIR" tag -a "$tag" -m "${PROJECT} ${version}"
printf 'Created commit %s and tag %s.\n' \
"$(git -C "$SCRIPT_DIR" rev-parse --short HEAD)" "$tag"