Files
dotfiles/chezmoi/.chezmoiscripts/run_before_10-migrate-gitconfig.ps1
T
Dmitry Dementiev 8fd5b9c630 feat(windows): добавлен chezmoi-слой для pwsh, terminal и git
- Зачем:
  - нужно перенести стабильную Windows-конфигурацию в dotfiles без поломки существующего stow/install.sh workflow для Linux и WSL.
  - нужно убрать типичные проблемы Git на Windows, включая line endings, из ручной настройки машины.
- Что:
  - добавлен отдельный source dir `chezmoi/` с tiny stub для PowerShell 7 и managed profile в `~/.config/powershell`.
  - добавлены идемпотентная настройка Windows Terminal и managed Git baseline с миграцией локальных настроек в `~/.gitconfig.local`.
  - обновлён README: описан Windows workflow, helper-функции pwsh и состав Git baseline без персональных данных.
- Проверка:
  - `chezmoi -S "$HOME\dotfiles\chezmoi" apply`
  - `pwsh -NoLogo -Command ''$PSVersionTable.PSVersion.ToString(); chcp; [Console]::InputEncoding.WebName; [Console]::OutputEncoding.WebName''`
  - `git config --global --includes --list --show-origin`
2026-03-22 15:45:08 +03:00

68 lines
1.9 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')
$ManagedStub = @'
[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()
$NormalizedStub = ($ManagedStub -replace "`r", '').Trim()
return $NormalizedContent -eq $NormalizedStub
}
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"