refactor(hooks): simplify script fetching logic and error handling

This commit is contained in:
Jan K9f 2025-04-07 18:56:49 +02:00
parent 451e41552c
commit ea04673ad1
Signed by: jank
GPG key ID: B9F475106B20F144

View file

@ -1,25 +1,20 @@
#!/bin/bash
# Configuration - where to fetch the full script from
SCRIPT_REPO="https://git.kjan.de/jank/scripts.git"
SCRIPT_PATH="hooks/pre-recieve.sh"
BRANCH="main"
# URL to the raw script file
SCRIPT_URL="https://git.kjan.de/jank/scripts/raw/branch/main/hooks/pre-recieve.sh"
# Temporary directory
TEMP_DIR=$(mktemp -d)
trap 'rm -rf "$TEMP_DIR"' EXIT
# Clone only the needed branch and specific file for efficiency
git clone --depth 1 --branch "$BRANCH" --single-branch "$SCRIPT_REPO" "$TEMP_DIR" >/dev/null 2>&1
if [ ! -f "$TEMP_DIR/$SCRIPT_PATH" ]; then
echo "Error: Could not fetch validation script from repository"
exit 1
# Try curl first, then fall back to wget if curl is not available
if command -v curl &>/dev/null; then
# Pass standard input to the downloaded script and preserve exit code
EXITCODE=0
# Read all input into a variable so we can pass it to the downloaded script
INPUT=$(cat)
echo "$INPUT" | (curl -s "$SCRIPT_URL" | bash) || EXITCODE=$?
exit $EXITCODE
else
# Fall back to wget if curl is not available
EXITCODE=0
INPUT=$(cat)
echo "$INPUT" | (wget -q -O - "$SCRIPT_URL" | bash) || EXITCODE=$?
exit $EXITCODE
fi
# Make the script executable
chmod +x "$TEMP_DIR/$SCRIPT_PATH"
# Pass all input to the downloaded script and preserve its exit code
cat | "$TEMP_DIR/$SCRIPT_PATH"
exit $?