algo/scripts/create_and_push_gitea.sh
Nicolas Cantu 109160de66 Initial commit
**Motivations:**
- Initial push of project to Gitea

**Evolutions:**
- Add v0 content (plan, chapters, analysis, references)

**Pages affectées:**
- v0/*.md, scripts, .gitignore
2026-02-13 16:17:01 +01:00

73 lines
2.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# Create private repo on Gitea (git.4nkweb.com) and push this project to default branch.
# Requires: GITEA_TOKEN (Settings -> Applications -> Generate New Token on Gitea).
# Usage: GITEA_TOKEN=xxx ./scripts/create_and_push_gitea.sh
set -e
REPO_NAME="algo"
GITEA_URL="${GITEA_URL:-https://git.4nkweb.com}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
if [ -z "${GITEA_TOKEN}" ]; then
echo "GITEA_TOKEN is not set. Create a token on ${GITEA_URL}: Settings -> Applications -> Generate New Token."
exit 1
fi
echo "Fetching current user from Gitea..."
USER_JSON=$(curl -s -H "Authorization: token ${GITEA_TOKEN}" "${GITEA_URL}/api/v1/user")
LOGIN=$(echo "$USER_JSON" | grep -o '"login":"[^"]*"' | head -1 | cut -d'"' -f4)
if [ -z "$LOGIN" ]; then
echo "Failed to get user (invalid token or API error). Response: $USER_JSON"
exit 1
fi
echo "User: $LOGIN"
echo "Creating repository ${REPO_NAME} (private)..."
CREATE_RESP=$(curl -s -w "\n%{http_code}" -X POST \
-H "Authorization: token ${GITEA_TOKEN}" \
-H "Content-Type: application/json" \
-d "{\"name\":\"${REPO_NAME}\",\"private\":true}" \
"${GITEA_URL}/api/v1/user/repos")
HTTP_BODY=$(echo "$CREATE_RESP" | head -n -1)
HTTP_CODE=$(echo "$CREATE_RESP" | tail -n 1)
if [ "$HTTP_CODE" = "201" ]; then
echo "Repository created."
elif [ "$HTTP_CODE" = "409" ] || echo "$HTTP_BODY" | grep -q "already exists"; then
echo "Repository already exists, continuing."
else
echo "Create repo failed (HTTP $HTTP_CODE). Response: $HTTP_BODY"
exit 1
fi
cd "$PROJECT_ROOT"
if [ ! -d .git ]; then
git init
git branch -M main
fi
# Initial commit when no commit exists yet (HEAD missing)
if ! git rev-parse --verify HEAD 1>/dev/null 2>&1; then
git add -A
git commit -m "Initial commit
**Motivations:**
- Initial push of project to Gitea
**Evolutions:**
- Add v0 content (plan, chapters, analysis, references)
**Pages affectées:**
- v0/*.md, scripts, .gitignore"
fi
if ! git remote get-url origin 2>/dev/null; then
git remote add origin "https://${LOGIN}:${GITEA_TOKEN}@${GITEA_URL#https://}/${LOGIN}/${REPO_NAME}.git"
else
git remote set-url origin "https://${LOGIN}:${GITEA_TOKEN}@${GITEA_URL#https://}/${LOGIN}/${REPO_NAME}.git"
fi
git branch -M main
echo "Pushing to origin main..."
git push -u origin main
echo "Done. Repository: ${GITEA_URL}/${LOGIN}/${REPO_NAME} (private)."