From a5bed544ab2d63492baa07af56176adb195565ab Mon Sep 17 00:00:00 2001 From: Malfurious Date: Tue, 31 Oct 2023 14:40:01 -0400 Subject: acid: source: Return null hash on error This seems like a more consistent behavior. Also, I would like to embed the git hash into the build log record somehow. Returning all zeroes in an error case allows the main script to simply pass along the result instead of worrying about an edge case. Signed-off-by: Malfurious --- acid/acid | 1 + acid/acid-source | 17 +++++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/acid/acid b/acid/acid index 4f4da5a..d75e475 100755 --- a/acid/acid +++ b/acid/acid @@ -7,6 +7,7 @@ CYCHE_SERVICE_DIR="/data/services" CYCHE_LOG_DIR="/data/logs" CYCHE_STATUS_FILE="/data/status" +NULL_HASH="0000000000000000000000000000000000000000" source config.sh now() { diff --git a/acid/acid-source b/acid/acid-source index 29b1516..351d3d0 100755 --- a/acid/acid-source +++ b/acid/acid-source @@ -12,8 +12,8 @@ # The caller should cache this hash value upon successful deployment. # # If a new version of the file tree is checked out, this file exits with code 1, -# otherwise a stale success exits with 0. On any error, no hash is printed, and -# we exit with 2. +# otherwise a stale success exits with 0. On any error, the null hash is +# printed, and we exit with 2. gethash() { git for-each-ref \ @@ -23,16 +23,21 @@ gethash() { "$1" # pattern } +fail() { + echo "$NULL_HASH" + exit 2 +} + cd "$CYCHE_SERVICE_DIR" # For , limit to a-z, 0-9, -, _ if ! echo "$1" | grep -Eq '^[-_a-z0-9]*$'; then echo "Bad service name: $1" >&2 - exit 2 + fail fi if ! [ -d "$1" ]; then - git clone "$2" "$1" >&2 || exit 2 + git clone "$2" "$1" >&2 || fail touch "$1/.git/previous_hash" touch "$1/.git/previous_slug" fi @@ -40,14 +45,14 @@ fi cd "$1" git remote set-url origin "$2" >&2 # pick up url changes -git fetch --all --prune >&2 || exit 2 +git fetch --all --prune >&2 || fail prev=$(cat '.git/previous_hash') next=$(gethash "$3") if [ -z "$next" ]; then echo "No refs match $3" >&2 - exit 2 + fail fi echo "$next" -- cgit v1.2.3