Files
testarena_backend/gitea_repo_controller.sh
2026-01-07 12:32:31 +01:00

122 lines
2.9 KiB
Bash
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
set -e
# ----------------------------------------
# 1. Configuration (UPDATE IF NEEDED)
# ----------------------------------------
GIT_USERNAME="asfautomation"
GIT_PASSWORD="asfautomation"
REPO_HOST="gitea.nabd-co.com"
REPO_PATH="ASF-Nabd/ASF-SH"
TARGET_DIR="TPF/Sensor_hub_repo"
# ----------------------------------------
# 2. URL Encoding Function
# ----------------------------------------
urlencode() {
perl -pe 's/([^a-zA-Z0-9_.-])/sprintf("%%%02x", ord($1))/ge'
}
ENCODED_USERNAME=$(printf '%s' "${GIT_USERNAME}" | urlencode)
ENCODED_PASSWORD=$(printf '%s' "${GIT_PASSWORD}" | urlencode)
AUTH_URL="https://${ENCODED_USERNAME}:${ENCODED_PASSWORD}@${REPO_HOST}/${REPO_PATH}.git"
# ----------------------------------------
# 3. Command & Arguments
# ----------------------------------------
COMMAND="$1"
BRANCH_NAME="$2"
# ----------------------------------------
# 4. Functions
# ----------------------------------------
clone_repo() {
if [ -d "${TARGET_DIR}" ]; then
echo " Repository already exists. Skipping clone."
return 0
fi
echo "📥 Cloning repository..."
git clone "${AUTH_URL}" "${TARGET_DIR}"
echo "✅ Clone completed."
}
checkout_branch() {
if [ -z "${BRANCH_NAME}" ]; then
echo "❌ Branch name is required for checkout."
exit 1
fi
if [ ! -d "${TARGET_DIR}" ]; then
echo "❌ Repository not found. Run clone first."
exit 1
fi
cd "${TARGET_DIR}"
echo "📦 Stashing local changes (including untracked)..."
git stash push -u -m "automation-stash-before-checkout" || true
echo "🔄 Fetching latest changes..."
git fetch origin
echo "🌿 Checking out main branch..."
git checkout main
echo "⬇️ Pulling latest main..."
git pull "${AUTH_URL}" main
echo "🌿 Checking out target branch: ${BRANCH_NAME}"
git checkout "${BRANCH_NAME}"
echo "⬆️ Rebasing '${BRANCH_NAME}' onto latest main..."
git rebase main
cd - >/dev/null
echo "✅ Checkout and rebase completed successfully."
}
delete_repo() {
if [ -d "${TARGET_DIR}" ]; then
echo "🗑️ Deleting repository directory..."
rm -rf "${TARGET_DIR}"
echo "✅ Repository deleted."
else
echo " Repository directory does not exist."
fi
}
# ----------------------------------------
# 5. Main Execution
# ----------------------------------------
case "${COMMAND}" in
clone)
clone_repo
;;
checkout)
checkout_branch
;;
delete)
delete_repo
;;
*)
echo "❌ Invalid command."
echo "Usage:"
echo " $0 clone"
echo " $0 checkout <branch>"
echo " $0 delete"
exit 1
;;
esac
echo "----------------------------------------"
echo "✔ Automation script finished successfully"
echo "----------------------------------------"