commit 764401c7ea747b9cb8ae6b223b7cd88090797b2d parent a8f12757d2d4f6170653ff80489013ac96d0c987 Author: Fred Großkopf <fred@kuandu.systems> Date: Tue, 28 Apr 2026 23:55:33 +0200 WIP: adds gitpages-update.sh Diffstat:
| A | gitpages-update.sh | | | 77 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | test-gitpages-update.sh | | | 51 | +++++++++++++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 128 insertions(+), 0 deletions(-)
diff --git a/gitpages-update.sh b/gitpages-update.sh @@ -0,0 +1,77 @@ +#!/bin/sh +# Asynchronous Gitpages update orchestrator. + +set -eu +PATH=/usr/local/bin:/usr/bin:/bin:$PATH + +# --- Configuration --- +LOCK_DIR="${GITPAGES_LOCK_DIR:-/tmp/gitpages-update.lock}" +QUEUE_FILE="${GITPAGES_QUEUE_FILE:-/var/spool/gitpages-update-queue.txt}" +LOG_FILE="${GITPAGES_LOG_FILE:-/var/log/gitpages/update.log}" +DEFAULT_CONFIG="/etc/gitpages.conf" + +# --- Functions --- +error() { printf "[%s] ERROR: %s\n" "$(date)" "$1" >&2; } +log() { printf "[%s] %s\n" "$(date)" "$1"; } + +get_config_val() { + [ -f "$1" ] && awk -F '=' -v key="$2" '$1 == key {gsub(/[[:space:]]/, "", $2); print $2}' "$1" | tail -n 1 +} + +process_repo() { + repo_path=$1 + config_file=$3 + + repo_name=$(basename "$repo_path") + GIT_RAW=$(get_config_val "$config_file" "GIT_RAW") + + if ! mirror-git.sh "$repo_path" "$GIT_RAW/$repo_name"; then + error "Failed to mirror $repo_name" + return 1 + fi + + # Use explicit arguments to avoid word splitting/shellcheck issues + if ! gitpages.sh -c "$config_file" "$GIT_RAW/$repo_name"; then + error "Failed to generate pages for $repo_name" + return 1 + fi +} + +main() { + CONFIG_FILE="$DEFAULT_CONFIG" + + if [ "${1:-}" = "-c" ]; then + [ $# -lt 2 ] && { + echo "Usage: $0 [-c config]" + exit 1 + } + CONFIG_FILE="$2" + shift 2 + fi + + if ! mkdir "$LOCK_DIR" 2> /dev/null; then + log "Update already in progress. Exiting." + exit 0 + fi + trap 'rmdir "$LOCK_DIR"' EXIT + + if [ -f "$QUEUE_FILE" ] && [ -s "$QUEUE_FILE" ]; then + paths=$(sort -u "$QUEUE_FILE") + : > "$QUEUE_FILE" + + log "Starting update for: $paths" + for repo_path in $paths; do + if [ -d "$repo_path" ]; then + process_repo "$repo_path" "" "$CONFIG_FILE" + else + error "Repository directory $repo_path not found" + fi + done + log "Update completed." + fi +} + +mkdir -p "$(dirname "$LOG_FILE")" +touch "$LOG_FILE" 2> /dev/null +exec >> "$LOG_FILE" 2>&1 +main "$@" diff --git a/test-gitpages-update.sh b/test-gitpages-update.sh @@ -0,0 +1,51 @@ +#!/bin/sh +set -e + +# Configuration +TEST_DIR=$(realpath "$(mktemp -d)") +export GITPAGES_LOCK_DIR="$TEST_DIR/lock" +export GITPAGES_QUEUE_FILE="$TEST_DIR/queue.txt" +export GITPAGES_LOG_FILE="$TEST_DIR/log.txt" +CONFIG_FILE="$TEST_DIR/gitpages.conf" +GIT_RAW="$TEST_DIR/raw" +REPO_PATH="$GIT_RAW/myrepo.git" + +# Mock dependencies +mkdir -p "$TEST_DIR/bin" +export PATH="$TEST_DIR/bin:$PATH" + +mock_bin() { + cat << EOF > "$TEST_DIR/bin/$1" +#!/bin/sh +echo "MOCK: $1 called with \$*" +exit 0 +EOF + chmod +x "$TEST_DIR/bin/$1" +} + +mock_bin mirror-git.sh +mock_bin gitpages.sh + +# Setup test environment +mkdir -p "$GIT_RAW" +touch "$REPO_PATH" +echo "$REPO_PATH" > "$GITPAGES_QUEUE_FILE" + +# Create test config +cat << EOF > "$CONFIG_FILE" +GIT_RAW=$GIT_RAW +EOF + +echo "Running test on gitpages-update.sh..." +./gitpages-update.sh -c "$CONFIG_FILE" + +# Verify +if [ ! -s "$GITPAGES_QUEUE_FILE" ]; then + echo "PASS: Queue was cleared." +else + echo "FAIL: Queue was not cleared." + exit 1 +fi + +rm -rf "$TEST_DIR" +echo "PASS: Update script executed successfully."