4caedb3074
Set-GitRemotes переименует origin по хосту; SSH config для .ru через LAN IP.
116 lines
3.1 KiB
PowerShell
116 lines
3.1 KiB
PowerShell
#Requires -Version 5.1
|
|
<#
|
|
.SYNOPSIS
|
|
Стандартные git remote: home (papatramp), kalinamall, github (PTah).
|
|
|
|
.EXAMPLE
|
|
cd C:\Users\papat\Projects\MyRepo
|
|
& "$HOME\CursorRules\scripts\Set-GitRemotes.ps1"
|
|
#>
|
|
[CmdletBinding()]
|
|
param(
|
|
[string]$Owner = 'PapaTramp',
|
|
[string]$GithubOrg = 'PTah',
|
|
[string]$Repo = '',
|
|
[switch]$SkipKalinamall,
|
|
[switch]$WhatIf
|
|
)
|
|
|
|
Set-StrictMode -Version Latest
|
|
$ErrorActionPreference = 'Stop'
|
|
|
|
function Write-Step([string]$Message) { Write-Host "[*] $Message" -ForegroundColor Cyan }
|
|
function Write-Ok([string]$Message) { Write-Host "[+] $Message" -ForegroundColor Green }
|
|
|
|
function Test-GitRepo {
|
|
git rev-parse --is-inside-work-tree 2>$null | Out-Null
|
|
if ($LASTEXITCODE -ne 0) {
|
|
throw 'Run inside a git repository or pass -Repo.'
|
|
}
|
|
}
|
|
|
|
function Get-RepoName {
|
|
param([string]$Name)
|
|
if ($Name) { return $Name }
|
|
return (Split-Path -Leaf (git rev-parse --show-toplevel))
|
|
}
|
|
|
|
function Get-OriginRemoteTarget {
|
|
$url = (git remote get-url origin 2>$null)
|
|
if (-not $url) { return $null }
|
|
$lower = $url.ToLower()
|
|
if ($lower -match 'kalinamall') { return 'kalinamall' }
|
|
if ($lower -match 'papatramp') { return 'home' }
|
|
if ($lower -match 'github\.com') { return 'github' }
|
|
return $null
|
|
}
|
|
|
|
function Set-Remote {
|
|
param(
|
|
[string]$Name,
|
|
[string]$Url
|
|
)
|
|
|
|
$existing = @(git remote 2>$null)
|
|
$has = $existing -contains $Name
|
|
|
|
if ($WhatIf) {
|
|
Write-Step "WhatIf: remote $Name -> $Url"
|
|
return
|
|
}
|
|
|
|
if ($has) {
|
|
$current = (git remote get-url $Name 2>$null).Trim()
|
|
if ($current -eq $Url) {
|
|
Write-Ok "remote $Name OK"
|
|
return
|
|
}
|
|
git remote set-url $Name $Url
|
|
Write-Ok "remote $Name updated -> $Url"
|
|
return
|
|
}
|
|
|
|
if ($existing -contains 'origin') {
|
|
$originTarget = Get-OriginRemoteTarget
|
|
if ($originTarget -eq $Name) {
|
|
git remote rename origin $Name
|
|
git remote set-url $Name $Url
|
|
Write-Ok "remote origin renamed to $Name -> $Url"
|
|
return
|
|
}
|
|
}
|
|
|
|
$legacy = @{
|
|
home = @('gitea', 'papatramp', 'origin-home')
|
|
github = @()
|
|
kalinamall = @('gitea-kalinamall', 'kalina')
|
|
}
|
|
foreach ($old in $legacy[$Name]) {
|
|
if ($existing -contains $old) {
|
|
git remote rename $old $Name
|
|
git remote set-url $Name $Url
|
|
Write-Ok "remote $old renamed to $Name -> $Url"
|
|
return
|
|
}
|
|
}
|
|
|
|
git remote add $Name $Url
|
|
Write-Ok "remote $Name added -> $Url"
|
|
}
|
|
|
|
Test-GitRepo
|
|
$repoName = Get-RepoName -Name $Repo
|
|
|
|
$homeUrl = "ssh://git@git.papatramp.ru:2222/${Owner}/${repoName}.git"
|
|
$kalinamallUrl = "ssh://git@git.kalinamall.ru:2222/${Owner}/${repoName}.git"
|
|
$githubUrl = "git@github.com:${GithubOrg}/${repoName}.git"
|
|
|
|
Write-Step "Repo: $repoName"
|
|
Set-Remote -Name 'home' -Url $homeUrl
|
|
Set-Remote -Name 'kalinamall' -Url $kalinamallUrl
|
|
Set-Remote -Name 'github' -Url $githubUrl
|
|
|
|
Write-Host ''
|
|
Write-Host 'Remotes:' -ForegroundColor Magenta
|
|
git remote -v
|