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