gitpages

A collection of scripts to securely webhost and publish git repositories.
git clone https://scm.kuandu.systems/git-raw/gitpages.git
Log | Files | Refs | README | LICENSE

commit a8f12757d2d4f6170653ff80489013ac96d0c987
parent 12363605cdb955059c3d41eea7ca058a161407f1
Author: Fred Großkopf <fred@kuandu.systems>
Date:   Tue, 28 Apr 2026 22:28:35 +0200

WIP: updates gitpages-init.sh

Diffstat:
Mgitpages-init.sh | 72+++++++++++++++++++++++++++++++++++-------------------------------------
Atest-gitpages-init.sh | 49+++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 84 insertions(+), 37 deletions(-)

diff --git a/gitpages-init.sh b/gitpages-init.sh @@ -2,52 +2,50 @@ set -eu -usage() { - cat << EOF >&2 -usage: $(basename "$0") SRC RAW_DST HTML_DST [ASSETS_DIR] - -Requires: mirror-git.sh gitpages.sh git stagit stagit-index - -One-command git hosting setup: - 1. Mirror repos: SRC → RAW_DST - 2. Generate HTML: RAW_DST → HTML_DST - -Examples: - $(basename "$0") /home/user/repos /var/www/git-raw /var/www/git -EOF - exit 1 -} +# Ensure a stable environment +PATH=/usr/local/bin:/usr/bin:/bin:$PATH error() { printf 'error: %s\n' "$1" >&2 exit 1 } -check_deps() { - for cmd in mirror-git.sh gitpages.sh git stagit stagit-index; do - command -v "$cmd" > /dev/null 2>&1 || error "$cmd not found (install required dependencies)" - done -} - -check_dir_writable() { - [ -d "$1" ] || error "$1 not a directory" - [ -w "$1" ] || error "$1 not writable" +# Consistent awk-based config retrieval +get_config_val() { + awk -F '=' -v key="$1" '$1 == key {gsub(/[[:space:]]/, "", $2); print $2}' "$2" | tail -n 1 } main() { - [ $# -lt 3 ] || [ $# -gt 4 ] || usage - - SRC="$1" RAW_DST="$2" HTML_DST="$3" ASSETS_DIR="${4:-}" - - check_deps - check_dir_writable "$RAW_DST" - check_dir_writable "$HTML_DST" - - printf 'info: 1/2 Mirror repos %s → %s\n' "$SRC" "$RAW_DST" - mirror-git.sh "$SRC" "$RAW_DST" - - printf 'info: 2/2 Generate HTML %s → %s\n' "$RAW_DST" "$HTML_DST" - gitpages.sh "$RAW_DST" "$HTML_DST" "$ASSETS_DIR" + CONFIG_PATH="/etc/gitpages.conf" + + # Optional -c argument parsing + if [ "${1:-}" = "-c" ]; then + [ $# -lt 2 ] && { printf "usage: $(basename "$0") [-c config]\n"; exit 1; } + CONFIG_PATH="$2" + shift 2 + fi + + [ -f "$CONFIG_PATH" ] || error "Config file $CONFIG_PATH not found" + + # Retrieve values + GIT_SRC=$(get_config_val "GIT_SRC" "$CONFIG_PATH") + GIT_RAW=$(get_config_val "GIT_RAW" "$CONFIG_PATH") + + [ -z "$GIT_SRC" ] || [ -z "$GIT_RAW" ] && error "GIT_SRC or GIT_RAW missing in config" + [ -d "$GIT_SRC" ] || error "GIT_SRC directory $GIT_SRC does not exist" + [ -d "$GIT_RAW" ] || error "GIT_RAW directory $GIT_RAW does not exist" + + printf 'info: 1/2 Mirroring repos %s → %s\n' "$GIT_SRC" "$GIT_RAW" + mirror-git.sh "$GIT_SRC" "$GIT_RAW" || error "Mirroring failed" + + printf 'info: 2/2 Generating HTML for all repos in %s\n' "$GIT_RAW" + + # Iterate over each .git directory and call gitpages.sh + for repo in "$GIT_RAW"/*.git; do + [ -e "$repo" ] || continue + printf ' Processing: %s\n' "$(basename "$repo")" + gitpages.sh -c "$CONFIG_PATH" "$repo" || error "HTML generation failed for $repo" + done } main "$@" diff --git a/test-gitpages-init.sh b/test-gitpages-init.sh @@ -0,0 +1,49 @@ +#!/bin/sh +set -e + +# Configuration +TEST_DIR=$(realpath "$(mktemp -d)") +CONFIG_FILE="$TEST_DIR/gitpages.conf" +GIT_SRC="$TEST_DIR/src" +GIT_RAW="$TEST_DIR/raw" +ASSETS_DIR="$TEST_DIR/assets" + +# Mock dependencies so we don't need real binaries +mkdir -p "$TEST_DIR/bin" +export PATH="$TEST_DIR/bin:$PATH" + +# The mock script correctly uses the filename as the identifier +mock_bin() { + cat << EOF > "$TEST_DIR/bin/$1" +#!/bin/sh +echo "MOCK: $1 called with \$*" +exit 0 +EOF + chmod +x "$TEST_DIR/bin/$1" +} + +# Generate mocks +mock_bin mirror-git.sh +mock_bin gitpages.sh +mock_bin git +mock_bin stagit +mock_bin stagit-index + +# Setup test environment +mkdir -p "$GIT_SRC" "$GIT_RAW" "$ASSETS_DIR" + +# Create at least one dummy repo file so the loop in gitpages-init.sh triggers +touch "$GIT_RAW/myrepo.git" + +# Create test config +cat <<EOF > "$CONFIG_FILE" +GIT_SRC=$GIT_SRC +GIT_RAW=$GIT_RAW +EOF + +echo "Running test on gitpages-init.sh..." +./gitpages-init.sh -c "$CONFIG_FILE" + +# Clean up +rm -rf "$TEST_DIR" +echo "PASS: Init script executed successfully."