#!/usr/bin/env bash # Shared functions for retention-cleanup.sh # Can be sourced by tests for unit testing age_days_from_stat() { local dir="$1" epoch_dir now epoch_dir=$(stat -c %Y "$dir" 2>/dev/null || stat -f %m "$dir" 2>/dev/null || echo 0) [ "$epoch_dir" -eq 0 ] && echo 99999 && return now=$(date +%s) echo $(( (now - epoch_dir) / 86400 )) } read_rule() { local config="$1" branch="$2" key="$3" default="$4" v=$(jq -r --arg b "$branch" --arg k "$key" '.branches[$b][$k] // empty' "$config") [ -n "$v" ] && echo "$v" || echo "$default" } branch_exists() { local owner="$1" repo="$2" branch="$3" local branch_urlenc code [ -z "$GITEA_API_URL" ] && return 0 [ -z "$GITEA_TOKEN" ] && return 0 branch_urlenc=$(echo "$branch" | sed 's/\//%2F/g') code=$(curl -s -o /dev/null -w "%{http_code}" \ -H "Authorization: token $GITEA_TOKEN" \ "$GITEA_API_URL/api/v1/repos/$owner/$repo/branches/$branch_urlenc" 2>/dev/null || echo "000") [ "$code" = "200" ] }