From 965d48935c4983a4ca980aad4e97bd41a7cca710 Mon Sep 17 00:00:00 2001 From: oib Date: Sun, 13 Apr 2025 15:59:25 +0200 Subject: [PATCH] Update 2025-04-13_15:59:24 --- gitea_push.sh | 53 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/gitea_push.sh b/gitea_push.sh index b512472..0bca28e 100755 --- a/gitea_push.sh +++ b/gitea_push.sh @@ -1,13 +1,13 @@ #!/bin/zsh -# Script Version: 1.4 +# Script Version: 1.5 # Description: Pushes the current folder (e.g. /etc) to a nested Gitea repo using provided nesting arguments. Auto-creates the remote repo via Gitea API if missing. # Set variables # ======== -# Try to extract GITEA_API_TOKEN from ~/.netrc if present -if [ -z "$GITEA_API_TOKEN" ] && grep -q '^GITEA_API_TOKEN=' ~/.netrc 2>/dev/null; then - GITEA_API_TOKEN=$(grep '^GITEA_API_TOKEN=' ~/.netrc | head -n1 | cut -d= -f2 | xargs) +# Try to extract GITEA_API_TOKEN from ~/.gitea_token if not set +if [ -z "$GITEA_API_TOKEN" ] && [ -f "$HOME/.gitea_token" ]; then + GITEA_API_TOKEN=$(<"$HOME/.gitea_token") export GITEA_API_TOKEN fi @@ -26,13 +26,13 @@ GITEA_API_URL="$GITEA_URL/api/v1" PRIVATE=false DEBUG=false -COMMIT_MESSAGE="Update $(date +%F_%T)" +COMMIT_MESSAGE="Update $(date +"%F_%T")" # Logging function # ======== log() { local level="$1"; shift - if [ "$level" = "DEBUG" ] && [ "$DEBUG" != true ]; then return; fi + if [[ "$level" == "DEBUG" && "$DEBUG" != true ]]; then return; fi local color_reset="$(tput sgr0)" local color="" case "$level" in @@ -57,6 +57,7 @@ create_repo() { if echo "$RESPONSE" | grep -q '"clone_url"'; then log INFO "Remote repository created successfully." + HTTP_STATUS=200 else log ERROR "Failed to create remote repository: $RESPONSE" exit 1 @@ -65,12 +66,14 @@ create_repo() { prepare_commit() { git add . - if ! git rev-parse --verify HEAD >/dev/null 2>&1; then + if git diff --quiet HEAD && ! git rev-parse --verify HEAD >/dev/null 2>&1; then log INFO "Creating initial commit" git commit -m "$COMMIT_MESSAGE" - else + elif ! git diff --quiet HEAD; then log INFO "Committing changes" - git commit -m "$COMMIT_MESSAGE" || log INFO "Nothing to commit" + git commit -m "$COMMIT_MESSAGE" + else + log INFO "Nothing to commit" fi } @@ -104,9 +107,12 @@ if [ $# -eq 0 ]; then echo " cd /etc && $0 server" echo echo "Authentication:" - echo " Git uses ~/.netrc for authentication. You can create it like this:" - echo " echo \"machine \$(echo \"$GITEA_URL\" | sed 's|https\\?://||') login $GITEA_USER password \"\"\" > ~/.netrc" - echo " chmod 600 ~/.netrc" + echo " Git operations (clone, push, pull) use ~/.netrc with your Git password:" + echo " machine \$(echo \"$GITEA_URL\" | sed 's|https\?://||') login $GITEA_USER password \"\"" + echo " chmod 600 ~/.netrc" + echo + echo " API operations (e.g. creating repos) use a Personal Access Token stored in ~/.gitea_token" + echo " echo \"\" > ~/.gitea_token && chmod 600 ~/.gitea_token" exit 0 fi @@ -136,26 +142,28 @@ done set -- "${POSITIONAL_ARGS[@]}" -if [ $# -ne 1 ]; then +if [[ $# -ne 1 ]]; then echo "Usage: $0 [--private] [--debug] [--message \"your commit message\"] " exit 1 fi HOST_GROUP=$(echo "$1" | tr '[:upper:]' '[:lower:]' | tr -cd 'a-z0-9-') HOST_NAME=$(hostname -s | tr '[:upper:]' '[:lower:]' | tr -cd 'a-z0-9-') -FOLDER_NAME="${HOST_NAME}-$(basename "$PWD")" +FOLDER_NAME="${HOST_NAME}-${HOST_GROUP}-$(basename "$PWD")" REPO_PATH="$PWD" REMOTE_PATH="$FOLDER_NAME" GIT_REMOTE="$GITEA_URL/$GITEA_USER/$FOLDER_NAME.git" # Git authentication hint -log DEBUG "Ensure ~/.netrc has: machine login $GITEA_USER password " +log DEBUG "Ensure ~/.netrc has: machine login $GITEA_USER password " + +# Check or create remote repo +check_or_create_repo() { + if [ -z "$GITEA_API_TOKEN" ]; then + log WARNING "GITEA_API_TOKEN is not set. Skipping API repo creation." + return + fi -# Check if GITEA_API_TOKEN is set -if [ -z "$GITEA_API_TOKEN" ]; then - log WARNING "GITEA_API_TOKEN is not set. Skipping API repo creation." -else - # Check if remote repo exists HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" \ -H "Authorization: token $GITEA_API_TOKEN" \ "$GITEA_API_URL/repos/$GITEA_USER/$FOLDER_NAME") @@ -165,7 +173,10 @@ else else log INFO "Remote repository already exists." fi -fi +} + + +check_or_create_repo # Main Process # ========