diff --git a/git.ps1 b/git.ps1 new file mode 100644 index 0000000..81e783b --- /dev/null +++ b/git.ps1 @@ -0,0 +1,87 @@ +# === Конфигурация === +$GiteaUrl = "https://git.urbny.dev" +$Username = "urbnywrt" +$Token = "3c54642a513557062f1230fce0090ea75701f8f4" + +# === Кодировка UTF-8 для PowerShell === +chcp 65001 +[Console]::OutputEncoding = [System.Text.Encoding]::UTF8 + +# === Определяем имя репозитория по имени папки === +$RepoPath = Get-Location +$RepoName = Split-Path -Leaf $RepoPath +$ScriptName = "git.ps1" +$gitignore = "$RepoPath\.gitignore" + +# === Добавляем git.ps1 в .gitignore, если его там нет === +if (-not (Test-Path $gitignore)) { + New-Item -ItemType File -Path $gitignore | Out-Null +} +if (-not (Select-String -Path $gitignore -Pattern [regex]::Escape($ScriptName) -Quiet)) { + Add-Content -Path $gitignore -Value $ScriptName +} + +# === Проверяем наличие репозитория на Gitea === +try { + $response = Invoke-RestMethod -Uri "$GiteaUrl/api/v1/repos/$Username/$RepoName" ` + -Headers @{Authorization = "token $Token"} ` + -Method GET + $exists = $true +} +catch { + if ($_.Exception.Response.StatusCode.value__ -eq 404) { + $exists = $false + } + else { + throw $_ + } +} + +# === Добавляем текущую папку в safe.directory === +git config --global --add safe.directory "$RepoPath" + +# === Формируем remote URL с токеном для HTTPS === +$remoteUrl = "https://${Username}:${Token}@git.urbny.dev/${Username}/${RepoName}.git" + +# === Если репозитория нет — создаём === +if (-not $exists) { + Write-Host "📦 Репозиторий $RepoName не найден, создаём..." + + # Создаём репозиторий на Gitea + Invoke-RestMethod -Uri "$GiteaUrl/api/v1/user/repos" ` + -Headers @{Authorization = "token $Token"} ` + -Method POST -ContentType "application/json" ` + -Body (@{name=$RepoName} | ConvertTo-Json) + + git init + git remote add origin $remoteUrl + git add . + + # Не включаем git.ps1 в первый коммит + git reset -- $ScriptName + + git commit -m "Initial commit" + git branch -M main + git push -u origin main +} +else { + Write-Host "✅ Репозиторий $RepoName найден, пушим изменения..." + + git add . + + # Исключаем git.ps1 из staged, если случайно добавился + if (git ls-files --error-unmatch $ScriptName > $null 2>&1) { + git reset -- $ScriptName + } + + # Проверяем, есть ли staged изменения + $changes = git diff --cached --name-only + if (-not [string]::IsNullOrWhiteSpace($changes)) { + $commitMsg = "Update " + (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + git commit -m $commitMsg + git push origin main + } + else { + Write-Host "Нет изменений для коммита." + } +}