#Requires -Version 5.1 <# .SYNOPSIS Rewrite cross-repo git host URLs in tracked text files for the target remote. .PARAMETER Target kalinamall | home | papatramp | github #> param( [Parameter(Mandatory = $true)] [ValidateSet('github', 'kalinamall', 'home', 'papatramp')] [string]$Target, [string]$Root = (Get-Location).Path ) $ErrorActionPreference = 'Stop' $Root = (Resolve-Path $Root).Path Set-Location $Root if ($Target -eq 'home') { $Target = 'papatramp' } switch ($Target) { 'github' { $Base = 'https://github.com/PTah' $BlobSuffix = '/blob/main' } 'kalinamall' { $Base = 'https://git.kalinamall.ru/PapaTramp' $BlobSuffix = '/src/branch/main' } 'papatramp' { $Base = 'https://git.papatramp.ru/PapaTramp' $BlobSuffix = '/src/branch/main' } } $BaseHost = $Base -replace '^https://', '' $sshTo = switch ($Target) { 'github' { 'https://git.papatramp.ru/PapaTramp/' } 'kalinamall' { 'ssh://git@git.papatramp.ru:2222/PapaTramp/' } default { 'ssh://git@git.papatramp.ru:2222/PapaTramp/' } } $patterns = @( @{ From = 'https://github\.com/PTah/([^)/''"\s]+)/blob/main/'; To = "$Base/`${1}$BlobSuffix/" } @{ From = 'https://github\.com/PTah/([^)/''"\s]+)/src/branch/main/'; To = "$Base/`${1}$BlobSuffix/" } @{ From = 'https://git\.kalinamall\.ru/PapaTramp/([^)/''"\s]+)/blob/main/'; To = "$Base/`${1}$BlobSuffix/" } @{ From = 'https://git\.kalinamall\.ru/PapaTramp/([^)/''"\s]+)/src/branch/main/'; To = "$Base/`${1}$BlobSuffix/" } @{ From = 'https://git\.papatramp\.ru/PapaTramp/([^)/''"\s]+)/src/branch/main/'; To = "$Base/`${1}$BlobSuffix/" } @{ From = 'https://git\.papatramp\.ru/PTah/([^)/''"\s]+)/src/branch/main/'; To = "$Base/`${1}$BlobSuffix/" } @{ From = 'https://github\.com/PTah/'; To = "$Base/" } @{ From = 'https://git\.kalinamall\.ru/PapaTramp/'; To = "$Base/" } @{ From = 'https://git\.papatramp\.ru/PapaTramp/'; To = "$Base/" } @{ From = 'https://git\.papatramp\.ru/PTah/'; To = "$Base/" } @{ From = 'ssh://git@git\.kalinamall\.ru:2222/PapaTramp/'; To = $sshTo } @{ From = 'ssh://git@git\.papatramp\.ru:2222/PapaTramp/'; To = $sshTo } @{ From = 'github\.com/PTah/'; To = "$BaseHost/" } @{ From = 'git\.kalinamall\.ru/PapaTramp/'; To = "$BaseHost/" } @{ From = 'git\.papatramp\.ru/PapaTramp/'; To = "$BaseHost/" } @{ From = 'git\.papatramp\.ru/PTah/'; To = "$BaseHost/" } ) $extensions = @('*.md', '*.json', '*.service', '*.example', '*.sh', '*.ps1', '*.yml', '*.yaml', '*.txt', '*.kt', '*.kts', '*.xml', '*.html', '*.css', '*.ts', '*.vue', '*.py') $files = @() if (Test-Path (Join-Path $Root '.git')) { $files = @(git -C $Root ls-files $extensions 2>$null | Where-Object { $_ -and (Test-Path (Join-Path $Root $_)) }) } if ($files.Count -eq 0) { $files = @(Get-ChildItem -Path $Root -Recurse -File -Include $extensions | Where-Object { $_.FullName -notmatch '\\(\.git|node_modules|\.venv|venv|dist|build)\\' } | ForEach-Object { $_.FullName.Substring($Root.Length).TrimStart('\', '/') }) } $n = 0 foreach ($file in $files) { $path = Join-Path $Root $file $content = [System.IO.File]::ReadAllText($path) $updated = $content foreach ($p in $patterns) { $updated = [regex]::Replace($updated, $p.From, $p.To) } if ($updated -ne $content) { # UTF-8 BOM for .ps1 (Windows PS 5.1); otherwise UTF-8 no BOM $bom = ($path -match '\.(ps1|psm1)$') [System.IO.File]::WriteAllText($path, $updated, [System.Text.UTF8Encoding]::new($bom)) Write-Output "updated: $file" $n++ } } Write-Output "Rewrite-GitHostUrls: target=$Target base=$Base files_changed=$n"