Files
dotfiles/chezmoi/.chezmoiscripts/run_before_10-migrate-gitconfig.ps1
T
Dmitry Dementiev 9b66fc70fc feat(windows): добавлены proxy-хелперы и упрощен git baseline
- Зачем:
  - нужно перенести привычные proxy_on/proxy_off/myip из bash в pwsh и убрать устаревший codevpn.
  - нужно сделать Windows Git baseline проще и предсказуемее, чтобы effective значения и проверки не путались.
- Что:
  - в managed profile PowerShell 7 добавлены `proxy_on`, `proxy_off`, `myip` и удалён `codevpn`.
  - Git baseline перенесён в `~/.gitconfig`, а `~/.gitconfig.local` оставлен только для локальных и персональных override-настроек.
  - README уточнён: legacy-статус Windows PowerShell 5.1, новые helper-функции и корректная команда проверки Git-конфига.
- Проверка:
  - `chezmoi -S "$HOME\dotfiles\chezmoi" apply --force`
  - `pwsh -NoLogo -Command ''Get-Command proxy_on,proxy_off,myip,Reload-Profile,wt-here; Test-Path function:codevpn''`
  - `git config --global --includes --list --show-origin`
2026-03-22 15:58:10 +03:00

99 lines
2.2 KiB
PowerShell

if (-not $IsWindows) {
return
}
$GlobalGitConfig = Join-Path $HOME '.gitconfig'
$LocalGitConfig = Join-Path $HOME '.gitconfig.local'
$BackupPath = '{0}.pre-chezmoi.bak-{1}' -f $GlobalGitConfig, (Get-Date -Format 'yyyyMMdd-HHmmss')
$ManagedStubs = @(
@'
[core]
autocrlf = false
eol = lf
quotepath = false
longpaths = true
[fetch]
prune = true
[pull]
ff = only
[init]
defaultBranch = main
[include]
path = ~/.gitconfig.local
'@,
@'
[include]
path = ~/.gitconfig.local
'@,
@'
[include]
path = ~/.config/git/config
[include]
path = ~/.gitconfig.local
'@
)
$ManagedKeys = @(
'core.autocrlf',
'core.eol',
'core.quotepath',
'core.longpaths',
'fetch.prune',
'pull.ff',
'init.defaultBranch'
)
function Test-ManagedGitStub {
param(
[Parameter(Mandatory = $true)]
[string]$Content
)
$NormalizedContent = ($Content -replace "`r", '').Trim()
foreach ($ManagedStub in $ManagedStubs) {
$NormalizedStub = ($ManagedStub -replace "`r", '').Trim()
if ($NormalizedContent -eq $NormalizedStub) {
return $true
}
}
return $false
}
if (-not (Test-Path -LiteralPath $GlobalGitConfig)) {
if (-not (Test-Path -LiteralPath $LocalGitConfig)) {
New-Item -ItemType File -Path $LocalGitConfig -Force | Out-Null
}
return
}
$CurrentContent = Get-Content -LiteralPath $GlobalGitConfig -Raw
if (Test-ManagedGitStub -Content $CurrentContent) {
if (-not (Test-Path -LiteralPath $LocalGitConfig)) {
New-Item -ItemType File -Path $LocalGitConfig -Force | Out-Null
}
return
}
if (-not (Test-Path -LiteralPath $LocalGitConfig)) {
Copy-Item -LiteralPath $GlobalGitConfig -Destination $BackupPath -Force
Copy-Item -LiteralPath $GlobalGitConfig -Destination $LocalGitConfig -Force
foreach ($Key in $ManagedKeys) {
git config --file $LocalGitConfig --unset-all $Key 2>$null
}
Write-Host "Migrated existing ~/.gitconfig to ~/.gitconfig.local"
Write-Host "Backup saved to: $BackupPath"
return
}
Copy-Item -LiteralPath $GlobalGitConfig -Destination $BackupPath -Force
Write-Warning "~/.gitconfig.local already exists; current ~/.gitconfig was backed up before managed stub overwrite."
Write-Host "Backup saved to: $BackupPath"