213 lines
8.1 KiB
Bash
Executable File
213 lines
8.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# gitea-setup.sh — Initialize a local folder as a git repo and push to Gitea.
|
|
#
|
|
# Handles two cases:
|
|
# A) Folder is already a git repo → just create Gitea repo and push
|
|
# B) Folder is not yet a git repo → git init, stage all, commit, then push
|
|
#
|
|
# Usage:
|
|
# ./gitea-setup.sh [options] [<repo-path>]
|
|
#
|
|
# Options:
|
|
# -n, --name NAME Gitea repo name (default: directory name)
|
|
# -d, --desc DESC Repository description (default: empty)
|
|
# -o, --org ORG Create under organization instead of personal account
|
|
# -p, --private Create as private repo
|
|
# -l, --login LOGIN tea login profile (default: active login)
|
|
# --auto-push Enable Gitea auto-push after sync-push.sh
|
|
# --no-auto-push Disable Gitea auto-push (default)
|
|
# -h, --help Show this help
|
|
#
|
|
# Prerequisites:
|
|
# - Gitea CLI (tea) installed and at least one login configured
|
|
# (run 'tea login add' if not done yet)
|
|
|
|
set -euo pipefail
|
|
|
|
# ── Defaults ──────────────────────────────────────────────────────────────────
|
|
REPO_PATH="."
|
|
REPO_NAME=""
|
|
DESCRIPTION=""
|
|
ORG=""
|
|
PRIVATE=false
|
|
TEA_LOGIN_FLAG=""
|
|
AUTO_PUSH=false
|
|
AUTO_PUSH_SET=false
|
|
|
|
# ── Argument parsing ──────────────────────────────────────────────────────────
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
-n|--name) REPO_NAME="$2"; shift 2 ;;
|
|
-d|--desc) DESCRIPTION="$2"; shift 2 ;;
|
|
-o|--org) ORG="$2"; shift 2 ;;
|
|
-p|--private) PRIVATE=true; shift ;;
|
|
-l|--login) TEA_LOGIN_FLAG="--login $2"; shift 2 ;;
|
|
--auto-push) AUTO_PUSH=true; AUTO_PUSH_SET=true; shift ;;
|
|
--no-auto-push) AUTO_PUSH=false; AUTO_PUSH_SET=true; shift ;;
|
|
-h|--help)
|
|
sed -n '2,/^set /p' "$0" | grep '^#' | sed 's/^# \{0,1\}//'
|
|
exit 0 ;;
|
|
-*) echo "Unknown option: $1" >&2; exit 1 ;;
|
|
*) REPO_PATH="$1"; shift ;;
|
|
esac
|
|
done
|
|
|
|
# ── Sanity checks ─────────────────────────────────────────────────────────────
|
|
if ! command -v tea &>/dev/null; then
|
|
echo "ERROR: 'tea' (Gitea CLI) is not installed or not in PATH." >&2
|
|
echo " Install it from https://gitea.com/gitea/tea/releases" >&2
|
|
exit 1
|
|
fi
|
|
|
|
if ! tea login list &>/dev/null || [ "$(tea login list --output csv 2>/dev/null | wc -l)" -lt 2 ]; then
|
|
echo "ERROR: No Gitea login configured. Run: tea login add" >&2
|
|
exit 1
|
|
fi
|
|
|
|
# Resolve absolute path (directory may not exist yet — only warn)
|
|
REPO_PATH="$(realpath -m "$REPO_PATH")"
|
|
if [ ! -d "$REPO_PATH" ]; then
|
|
echo "Directory does not exist, creating: $REPO_PATH"
|
|
mkdir -p "$REPO_PATH"
|
|
fi
|
|
|
|
[ -z "$REPO_NAME" ] && REPO_NAME="$(basename "$REPO_PATH")"
|
|
|
|
# ── Detect active tea login info ──────────────────────────────────────────────
|
|
# CSV columns: NAME,URL,SSH-URL,USER,ACTIVE
|
|
ACTIVE_LOGIN_LINE=$(tea login list --output csv 2>/dev/null \
|
|
| awk -F',' 'NR>1 && $5=="true" {print; exit}')
|
|
|
|
if [ -z "$ACTIVE_LOGIN_LINE" ]; then
|
|
# Fallback: take first non-header line
|
|
ACTIVE_LOGIN_LINE=$(tea login list --output csv 2>/dev/null \
|
|
| awk -F',' 'NR==2 {print; exit}')
|
|
fi
|
|
|
|
if [ -z "$ACTIVE_LOGIN_LINE" ]; then
|
|
echo "ERROR: Could not determine active Gitea login. Run: tea login list" >&2
|
|
exit 1
|
|
fi
|
|
|
|
GITEA_URL=$(echo "$ACTIVE_LOGIN_LINE" | cut -d',' -f2 | tr -d '"' | sed 's|/$||')
|
|
GITEA_USER=$(echo "$ACTIVE_LOGIN_LINE" | cut -d',' -f4 | tr -d '"')
|
|
|
|
echo "Gitea instance : $GITEA_URL"
|
|
echo "Gitea user : $GITEA_USER"
|
|
echo "Repo name : $REPO_NAME"
|
|
echo "Path : $REPO_PATH"
|
|
[ -n "$ORG" ] && echo "Organization : $ORG"
|
|
$PRIVATE && echo "Visibility : private" || echo "Visibility : public"
|
|
echo ""
|
|
|
|
# ── Step 1: Ensure it is a git repo ───────────────────────────────────────────
|
|
cd "$REPO_PATH"
|
|
|
|
if git rev-parse --git-dir &>/dev/null; then
|
|
echo "[1/4] Already a git repository — skipping init."
|
|
IS_NEW_REPO=false
|
|
else
|
|
echo "[1/4] Initializing git repository..."
|
|
git init
|
|
git checkout -b main 2>/dev/null || true
|
|
|
|
# Stage everything
|
|
git add .
|
|
STAGED=$(git diff --cached --name-only | wc -l)
|
|
if [ "$STAGED" -gt 0 ]; then
|
|
echo " Staging $STAGED file(s)..."
|
|
read -rp " Commit message [Initial commit]: " MSG
|
|
MSG="${MSG:-Initial commit}"
|
|
git commit -m "$MSG"
|
|
else
|
|
echo " Directory is empty — creating an empty initial commit."
|
|
git commit --allow-empty -m "Initial commit"
|
|
fi
|
|
IS_NEW_REPO=true
|
|
fi
|
|
|
|
# ── Step 2: Create repo on Gitea ───────────────────────────────────────────────
|
|
echo ""
|
|
echo "[2/4] Creating Gitea repository '$REPO_NAME'..."
|
|
|
|
PRIVATE_FLAG=""
|
|
$PRIVATE && PRIVATE_FLAG="--private"
|
|
|
|
ORG_FLAG=""
|
|
[ -n "$ORG" ] && ORG_FLAG="--owner $ORG"
|
|
|
|
# shellcheck disable=SC2086
|
|
if ! tea repo create \
|
|
--name "$REPO_NAME" \
|
|
--description "$DESCRIPTION" \
|
|
$PRIVATE_FLAG \
|
|
$ORG_FLAG \
|
|
$TEA_LOGIN_FLAG; then
|
|
echo ""
|
|
echo "ERROR: Failed to create Gitea repository." >&2
|
|
echo " The repo may already exist. To push to an existing repo," >&2
|
|
echo " add the remote manually and push:" >&2
|
|
if [ -n "$ORG" ]; then
|
|
echo " git remote add gitea ${GITEA_URL}/${ORG}/${REPO_NAME}.git" >&2
|
|
else
|
|
echo " git remote add gitea ${GITEA_URL}/${GITEA_USER}/${REPO_NAME}.git" >&2
|
|
fi
|
|
exit 1
|
|
fi
|
|
|
|
# Construct remote URL
|
|
if [ -n "$ORG" ]; then
|
|
REMOTE_URL="${GITEA_URL}/${ORG}/${REPO_NAME}.git"
|
|
else
|
|
REMOTE_URL="${GITEA_URL}/${GITEA_USER}/${REPO_NAME}.git"
|
|
fi
|
|
|
|
# ── Step 3: Add remote and push ────────────────────────────────────────────────
|
|
echo ""
|
|
echo "[3/4] Adding remote 'gitea' → $REMOTE_URL"
|
|
|
|
if git remote get-url gitea &>/dev/null; then
|
|
echo " Remote 'gitea' already exists — updating URL."
|
|
git remote set-url gitea "$REMOTE_URL"
|
|
else
|
|
git remote add gitea "$REMOTE_URL"
|
|
fi
|
|
|
|
echo " Pushing all branches and tags..."
|
|
git push gitea --all
|
|
git push gitea --tags
|
|
|
|
# ── Step 4: Configure auto-push ───────────────────────────────────────────────
|
|
echo ""
|
|
echo "[4/4] Configuring sync settings..."
|
|
git config sync.gitea.remote "gitea"
|
|
|
|
if $AUTO_PUSH_SET; then
|
|
git config sync.gitea.autopush "$AUTO_PUSH"
|
|
$AUTO_PUSH \
|
|
&& echo " Auto-push to Gitea after sync-push.sh: ENABLED" \
|
|
|| echo " Auto-push to Gitea after sync-push.sh: DISABLED"
|
|
else
|
|
# Ask interactively
|
|
read -rp " Enable automatic Gitea push after sync-push.sh? [y/N] " ans
|
|
ans="${ans:-N}"
|
|
if [[ "$ans" =~ ^[Yy] ]]; then
|
|
git config sync.gitea.autopush "true"
|
|
echo " Auto-push enabled. Toggle later with:"
|
|
echo " git config sync.gitea.autopush false"
|
|
else
|
|
git config sync.gitea.autopush "false"
|
|
echo " Auto-push disabled. Enable later with:"
|
|
echo " git config sync.gitea.autopush true"
|
|
fi
|
|
fi
|
|
|
|
# ── Done ───────────────────────────────────────────────────────────────────────
|
|
echo ""
|
|
echo "Done. Repository is live at:"
|
|
echo " $REMOTE_URL"
|
|
echo ""
|
|
echo "Next steps:"
|
|
echo " - Run sync-push.sh to export commits to the shared folder"
|
|
echo " - Run sync-init-export.sh to prepare a bootstrap bundle for the other machine"
|