Update 2025-04-13_15:43:33

This commit is contained in:
2025-04-13 15:43:33 +02:00
parent 0a1a209dac
commit b35de1ecc4

View File

@ -5,9 +5,9 @@
# Set variables # Set variables
# ======== # ========
# Try to extract GITEA_API_TOKEN from ~/.netrc if present # Try to extract GITEA_API_TOKEN from ~/.gitea_token if not set
if [ -z "$GITEA_API_TOKEN" ] && grep -q '^GITEA_API_TOKEN=' ~/.netrc 2>/dev/null; then if [ -z "$GITEA_API_TOKEN" ] && [ -f "$HOME/.gitea_token" ]; then
GITEA_API_TOKEN=$(grep '^GITEA_API_TOKEN=' ~/.netrc | head -n1 | cut -d= -f2 | xargs) GITEA_API_TOKEN=$(<"$HOME/.gitea_token")
export GITEA_API_TOKEN export GITEA_API_TOKEN
fi fi
@ -26,13 +26,13 @@ GITEA_API_URL="$GITEA_URL/api/v1"
PRIVATE=false PRIVATE=false
DEBUG=false DEBUG=false
COMMIT_MESSAGE="Update $(date +%F_%T)" COMMIT_MESSAGE="Update $(date +"%F_%T")"
# Logging function # Logging function
# ======== # ========
log() { log() {
local level="$1"; shift 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_reset="$(tput sgr0)"
local color="" local color=""
case "$level" in case "$level" in
@ -57,6 +57,7 @@ create_repo() {
if echo "$RESPONSE" | grep -q '"clone_url"'; then if echo "$RESPONSE" | grep -q '"clone_url"'; then
log INFO "Remote repository created successfully." log INFO "Remote repository created successfully."
HTTP_STATUS=200
else else
log ERROR "Failed to create remote repository: $RESPONSE" log ERROR "Failed to create remote repository: $RESPONSE"
exit 1 exit 1
@ -65,12 +66,14 @@ create_repo() {
prepare_commit() { prepare_commit() {
git add . 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" log INFO "Creating initial commit"
git commit -m "$COMMIT_MESSAGE" git commit -m "$COMMIT_MESSAGE"
else elif ! git diff --quiet HEAD; then
log INFO "Committing changes" 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 fi
} }
@ -104,9 +107,12 @@ if [ $# -eq 0 ]; then
echo " cd /etc && $0 server" echo " cd /etc && $0 server"
echo echo
echo "Authentication:" echo "Authentication:"
echo " Git uses ~/.netrc for authentication. You can create it like this:" echo " Git operations (clone, push, pull) use ~/.netrc with your Git password:"
echo " echo \"machine \$(echo \"$GITEA_URL\" | sed 's|https\\?://||') login $GITEA_USER password \"<password>\"\" > ~/.netrc" echo " machine \$(echo \"$GITEA_URL\" | sed 's|https\?://||') login $GITEA_USER password \"<your Git password>\""
echo " chmod 600 ~/.netrc" echo " chmod 600 ~/.netrc"
echo
echo " API operations (e.g. creating repos) use a Personal Access Token stored in ~/.gitea_token"
echo " echo \"<your_token>\" > ~/.gitea_token && chmod 600 ~/.gitea_token"
exit 0 exit 0
fi fi
@ -136,26 +142,28 @@ done
set -- "${POSITIONAL_ARGS[@]}" set -- "${POSITIONAL_ARGS[@]}"
if [ $# -ne 1 ]; then if [[ $# -ne 1 ]]; then
echo "Usage: $0 [--private] [--debug] [--message \"your commit message\"] <host_group>" echo "Usage: $0 [--private] [--debug] [--message \"your commit message\"] <host_group>"
exit 1 exit 1
fi fi
HOST_GROUP=$(echo "$1" | tr '[:upper:]' '[:lower:]' | tr -cd 'a-z0-9-') HOST_GROUP=$(echo "$1" | tr '[:upper:]' '[:lower:]' | tr -cd 'a-z0-9-')
HOST_NAME=$(hostname -s | 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" REPO_PATH="$PWD"
REMOTE_PATH="$FOLDER_NAME" REMOTE_PATH="$FOLDER_NAME"
GIT_REMOTE="$GITEA_URL/$GITEA_USER/$FOLDER_NAME.git" GIT_REMOTE="$GITEA_URL/$GITEA_USER/$FOLDER_NAME.git"
# Git authentication hint # Git authentication hint
log DEBUG "Ensure ~/.netrc has: machine <host> login $GITEA_USER password <personal access token>" log DEBUG "Ensure ~/.netrc has: machine <host> login $GITEA_USER password <your Git password>"
# Check if GITEA_API_TOKEN is set # Check or create remote repo
check_or_create_repo() {
if [ -z "$GITEA_API_TOKEN" ]; then if [ -z "$GITEA_API_TOKEN" ]; then
log WARNING "GITEA_API_TOKEN is not set. Skipping API repo creation." log WARNING "GITEA_API_TOKEN is not set. Skipping API repo creation."
else return
# Check if remote repo exists fi
HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" \ HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" \
-H "Authorization: token $GITEA_API_TOKEN" \ -H "Authorization: token $GITEA_API_TOKEN" \
"$GITEA_API_URL/repos/$GITEA_USER/$FOLDER_NAME") "$GITEA_API_URL/repos/$GITEA_USER/$FOLDER_NAME")
@ -165,7 +173,10 @@ else
else else
log INFO "Remote repository already exists." log INFO "Remote repository already exists."
fi fi
fi }
check_or_create_repo
# Main Process # Main Process
# ======== # ========