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"