# Push project to Gitea # Usage: # $env:GITEA_URL = "http://your-gitea.example.com" # $env:GITEA_TOKEN = "your_access_token" # $env:GITEA_OWNER = "your_username" # optional, defaults from API /user # powershell -ExecutionPolicy Bypass -File scripts/push-to-gitea.ps1 # # Or create .env.gitea in project root: # GITEA_URL=https://git.xdjchuanhui.com # GITEA_TOKEN=xxxxxxxx # GITEA_OWNER=admin param( [string]$RepoName = "ai-fresh-delivery", [string]$Description = "AI生鲜配送系统 - 智能生鲜供应链管理平台" ) $ErrorActionPreference = "Stop" $root = Split-Path -Parent $PSScriptRoot Set-Location $root function Find-Git { $cmd = Get-Command git -ErrorAction SilentlyContinue if ($cmd) { return $cmd.Source } $candidates = @( "C:\Program Files\Git\bin\git.exe", "C:\Program Files (x86)\Git\bin\git.exe", "$root\.tools\PortableGit\bin\git.exe" ) foreach ($p in $candidates) { if (Test-Path $p) { return $p } } return $null } $git = Find-Git if (-not $git) { Write-Error @" Git is not installed. Install Git for Windows first: winget install Git.Git Then re-run this script. "@ } $envFile = Join-Path $root ".env.gitea" if (Test-Path $envFile) { Get-Content $envFile | ForEach-Object { if ($_ -match '^\s*#' -or $_ -match '^\s*$') { return } $parts = $_ -split '=', 2 if ($parts.Length -eq 2) { $name = $parts[0].Trim() $value = $parts[1].Trim() Set-Item -Path "Env:$name" -Value $value } } } $giteaUrl = $env:GITEA_URL $giteaToken = $env:GITEA_TOKEN $giteaOwner = $env:GITEA_OWNER if (-not $giteaUrl -or -not $giteaToken) { Write-Error @" Missing Gitea config. Set environment variables or create .env.gitea: GITEA_URL=http://your-gitea-host:3000 GITEA_TOKEN=your_personal_access_token GITEA_OWNER=your_username "@ } $giteaUrl = $giteaUrl.TrimEnd('/') Write-Host "Checking Gitea connection..." -ForegroundColor Cyan $userResp = Invoke-RestMethod -Uri "$giteaUrl/api/v1/user" -Headers @{ Authorization = "token $giteaToken" } if (-not $giteaOwner) { $giteaOwner = $userResp.username } Write-Host "Gitea user: $giteaOwner" -ForegroundColor Green Write-Host "Creating repository '$RepoName'..." -ForegroundColor Cyan $createBody = @{ name = $RepoName description = $Description private = $false auto_init = $false } | ConvertTo-Json -Compress try { Invoke-RestMethod -Method POST -Uri "$giteaUrl/api/v1/user/repos" ` -Headers @{ Authorization = "token $giteaToken"; "Content-Type" = "application/json" } ` -Body $createBody | Out-Null Write-Host "Repository created." -ForegroundColor Green } catch { $msg = $_.Exception.Message if ($msg -match '409|already exists|exist') { Write-Host "Repository already exists, will push to it." -ForegroundColor Yellow } else { throw } } $repoPath = "$giteaOwner/$RepoName" $cloneUrl = "$giteaUrl/$repoPath.git" $uri = [Uri]$giteaUrl $hostWithPort = if ($uri.IsDefaultPort) { $uri.Host } else { "$($uri.Host):$($uri.Port)" } $authRemote = "$($uri.Scheme)://oauth2:$giteaToken@$hostWithPort/$repoPath.git" if (-not (Test-Path (Join-Path $root ".git"))) { & $git init & $git branch -M main } & $git add -A $status = & $git status --porcelain if ($status) { & $git -c user.email="dev@local" -c user.name="AI生鲜配送系统" commit -m "$( @' Initial commit: AI生鲜配送系统 Express + PostgreSQL local stack for AI fresh food delivery management. '@ )" } else { Write-Host "No changes to commit." -ForegroundColor Yellow } & $git remote remove origin 2>$null & $git remote add origin $authRemote Write-Host "Pushing to $cloneUrl ..." -ForegroundColor Cyan & $git push -u origin main & $git remote set-url origin $cloneUrl Write-Host "" Write-Host "Done!" -ForegroundColor Green Write-Host "Repository: $cloneUrl" -ForegroundColor White