Files
Git-Skill/.cursor/skills/git-init/scripts/store-profile.ps1
T
2026-08-20 23:42:02 +08:00

107 lines
3.7 KiB
PowerShell

# Interactive: write local author profile (not a secret).
# Run in the user's own terminal, not in an agent session.
param(
[string]$UserName,
[string]$UserEmail,
[string]$GitUsername,
[string]$SshKeyPath
)
$ErrorActionPreference = 'Stop'
. (Join-Path $PSScriptRoot 'common.ps1')
$defaults = Get-GitSkillsDefaults
function Read-DefaultPrompt {
param(
[string]$Label,
[string]$Default
)
if ([string]::IsNullOrWhiteSpace($Default)) {
$value = Read-Host $Label
return $value.Trim()
}
$value = Read-Host "$Label [$Default]"
if ([string]::IsNullOrWhiteSpace($value)) {
return $Default
}
return $value.Trim()
}
$existing = Get-GitSkillsProfile
# Do not assign to $profile - that is a PowerShell automatic variable.
if (-not $PSBoundParameters.ContainsKey('UserName') -or [string]::IsNullOrWhiteSpace($UserName)) {
$fallback = if ($existing) { $existing.userName } else { $defaults.userName }
$UserName = Read-DefaultPrompt -Label 'Git user.name' -Default $fallback
}
if (-not $PSBoundParameters.ContainsKey('UserEmail') -or [string]::IsNullOrWhiteSpace($UserEmail)) {
$fallback = if ($existing) { $existing.userEmail } else { $defaults.userEmail }
$UserEmail = Read-DefaultPrompt -Label 'Git user.email' -Default $fallback
}
if (-not $PSBoundParameters.ContainsKey('GitUsername') -or [string]::IsNullOrWhiteSpace($GitUsername)) {
$fallback = if ($existing) { $existing.gitUsername } else { '' }
$GitUsername = Read-DefaultPrompt -Label 'Gitea username (HTTPS login name, not the token)' -Default $fallback
}
if (-not $PSBoundParameters.ContainsKey('SshKeyPath') -or [string]::IsNullOrWhiteSpace($SshKeyPath)) {
$fallback = if ($existing -and $existing.sshKeyPath) {
$existing.sshKeyPath
} else {
Join-Path $env:USERPROFILE '.ssh\id_ed25519'
}
$SshKeyPath = Read-DefaultPrompt -Label 'SSH private key path' -Default $fallback
}
$coAuthors = @()
if ($existing -and $existing.PSObject.Properties.Name -contains 'coAuthors' -and $null -ne $existing.coAuthors) {
$coAuthors = @($existing.coAuthors)
}
$extraCo = ''
if (-not [Console]::IsInputRedirected) {
$extraCo = Read-DefaultPrompt -Label 'Extra Co-authored-by as Name <email> (empty keeps current list)' -Default ''
}
if ($extraCo -match '^\s*(.+?)\s*<([^>]+)>\s*$') {
$entry = [pscustomobject]@{
name = $Matches[1].Trim()
email = $Matches[2].Trim()
}
$dup = $false
foreach ($c in $coAuthors) {
if (([string]$c.name) -eq $entry.name -and ([string]$c.email) -eq $entry.email) {
$dup = $true
break
}
}
if (-not $dup) {
$coAuthors += $entry
}
}
if ([string]::IsNullOrWhiteSpace($UserName)) { throw 'user.name is required.' }
if ([string]::IsNullOrWhiteSpace($UserEmail)) { throw 'user.email is required.' }
if ([string]::IsNullOrWhiteSpace($GitUsername)) { throw 'HTTPS Git username is required.' }
$profileObj = [pscustomobject]@{
userName = $UserName
userEmail = $UserEmail
gitUsername = $GitUsername
sshKeyPath = $SshKeyPath
coAuthors = @($coAuthors)
updatedAtUtc = [DateTime]::UtcNow.ToString('o')
}
Save-GitSkillsProfile -Profile $profileObj
Write-StatusLine -Key 'status' -Value 'saved'
Write-StatusLine -Key 'path' -Value $script:ProfilePath
Write-StatusLine -Key 'userName' -Value $UserName
Write-StatusLine -Key 'userEmail' -Value $UserEmail
Write-StatusLine -Key 'gitUsername' -Value $GitUsername
Write-StatusLine -Key 'sshKeyPath' -Value $SshKeyPath
$trailers = Get-CoAuthorTrailerLines -GitProfile $profileObj
Write-StatusLine -Key 'co_authored_by_count' -Value "$($trailers.Count)"
foreach ($line in $trailers) {
Write-StatusLine -Key 'co_authored_by' -Value $line
}