Files
dotfiles/chezmoi/.chezmoiscripts/run_before_10-migrate-gitconfig.ps1
T
Dmitry Dementiev f9083af8b2 fix(windows): исправлены LF и совместимость chezmoi с PS 5.1
- Зачем:
  - нужно убрать line-ending noise в репозитории и сделать Windows-слой безопаснее для запуска без установленного pwsh.
- Что:
  - добавлена `.gitattributes` с LF-базой для текста и CRLF-исключениями для `.bat` и `.cmd`.
  - `README.md` нормализован в LF и quick start дополнен явной установкой PowerShell 7.
  - `chezmoi`-скрипты переведены на guard, совместимый с PowerShell 5.1, а запись `settings.json` переведена на `utf8BOM`.
- Проверка:
  - `git diff --check`.
  - `powershell.exe -NoProfile -ExecutionPolicy Bypass -File chezmoi\\.chezmoiscripts\\run_before_10-migrate-gitconfig.ps1`.
  - `powershell.exe -NoProfile -ExecutionPolicy Bypass -File chezmoi\\.chezmoiscripts\\run_20-configure-windows-terminal.ps1`.
  - `chezmoi -S C:\\Users\\dmitry\\dotfiles\\chezmoi apply --dry-run --verbose`.
2026-03-22 17:12:59 +03:00

99 lines
2.3 KiB
PowerShell

if (-not ($IsWindows -or $PSVersionTable.PSEdition -eq 'Desktop')) {
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"