From ea04673ad1b27223b14483e578f0cafc1a792b0f Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Mon, 7 Apr 2025 18:56:49 +0200 Subject: [PATCH] refactor(hooks): simplify script fetching logic and error handling --- hooks/dstr.sh | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/hooks/dstr.sh b/hooks/dstr.sh index bbc4c95..e396c46 100644 --- a/hooks/dstr.sh +++ b/hooks/dstr.sh @@ -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 $?