|
|
@@ -1,233 +1,71 @@
|
|
|
-# Git 凭证配置脚本
|
|
|
-# 功能:修改 Git 用户名、邮箱和密码凭证
|
|
|
+# Git Credential Configuration Script
|
|
|
|
|
|
param(
|
|
|
[string]$Username = "",
|
|
|
[string]$Email = "",
|
|
|
- [string]$Password = "",
|
|
|
- [string]$RemoteUrl = "",
|
|
|
- [switch]$Global = $false,
|
|
|
- [switch]$Help = $false
|
|
|
+ [SecureString]$Password = $null,
|
|
|
+ [switch]$Global = $false
|
|
|
)
|
|
|
|
|
|
-function Show-Help {
|
|
|
- Write-Host @"
|
|
|
-Git 凭证配置脚本
|
|
|
-
|
|
|
-用法:
|
|
|
- .\git-credential-config.ps1 [选项]
|
|
|
-
|
|
|
-选项:
|
|
|
- -Username <用户名> Git 用户名
|
|
|
- -Email <邮箱> Git 邮箱
|
|
|
- -Password <密码> Git 密码或访问令牌
|
|
|
- -RemoteUrl <URL> 远程仓库 URL(用于清除旧凭证)
|
|
|
- -Global 应用到全局配置(默认只修改当前仓库)
|
|
|
- -Help 显示此帮助信息
|
|
|
-
|
|
|
-示例:
|
|
|
- # 交互式配置(推荐)
|
|
|
- .\git-credential-config.ps1
|
|
|
-
|
|
|
- # 直接指定参数
|
|
|
- .\git-credential-config.ps1 -Username "yourname" -Email "your@email.com" -Password "yourpassword"
|
|
|
-
|
|
|
- # 配置全局设置
|
|
|
- .\git-credential-config.ps1 -Username "yourname" -Email "your@email.com" -Global
|
|
|
-
|
|
|
- # 清除特定远程仓库的凭证
|
|
|
- .\git-credential-config.ps1 -RemoteUrl "https://example.com/repo.git"
|
|
|
-"@ -ForegroundColor Cyan
|
|
|
-}
|
|
|
-
|
|
|
-function Get-CurrentGitConfig {
|
|
|
- Write-Host "`n当前 Git 配置:" -ForegroundColor Yellow
|
|
|
- $currentName = git config --local user.name 2>$null
|
|
|
- $currentEmail = git config --local user.email 2>$null
|
|
|
- if (-not $currentName) {
|
|
|
- $currentName = git config --global user.name 2>$null
|
|
|
- $currentEmail = git config --global user.email 2>$null
|
|
|
- Write-Host " (全局配置)" -ForegroundColor Gray
|
|
|
- }
|
|
|
- Write-Host " 用户名: $currentName" -ForegroundColor White
|
|
|
- Write-Host " 邮箱: $currentEmail" -ForegroundColor White
|
|
|
-}
|
|
|
-
|
|
|
-function Set-GitCredentials {
|
|
|
- param(
|
|
|
- [string]$Username,
|
|
|
- [string]$Email,
|
|
|
- [string]$Password,
|
|
|
- [string]$RemoteUrl,
|
|
|
- [bool]$IsGlobal
|
|
|
- )
|
|
|
-
|
|
|
- $scope = if ($IsGlobal) { "--global" } else { "--local" }
|
|
|
- $scopeName = if ($IsGlobal) { "全局" } else { "本地" }
|
|
|
-
|
|
|
- # 设置用户名
|
|
|
- if ($Username) {
|
|
|
- git config $scope user.name $Username
|
|
|
- if ($LASTEXITCODE -eq 0) {
|
|
|
- Write-Host "[OK] 已设置${scopeName}用户名: $Username" -ForegroundColor Green
|
|
|
- } else {
|
|
|
- Write-Host "[X] 设置用户名失败" -ForegroundColor Red
|
|
|
- return $false
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- # 设置邮箱
|
|
|
- if ($Email) {
|
|
|
- git config $scope user.email $Email
|
|
|
- if ($LASTEXITCODE -eq 0) {
|
|
|
- Write-Host "[OK] 已设置${scopeName}邮箱: $Email" -ForegroundColor Green
|
|
|
- } else {
|
|
|
- Write-Host "[X] 设置邮箱失败" -ForegroundColor Red
|
|
|
- return $false
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- # 清除旧凭证
|
|
|
- if ($RemoteUrl) {
|
|
|
- Clear-OldCredentials -RemoteUrl $RemoteUrl
|
|
|
- }
|
|
|
-
|
|
|
- # 设置新凭证(如果有密码)
|
|
|
- if ($Password -and $RemoteUrl) {
|
|
|
- Set-NewCredentials -RemoteUrl $RemoteUrl -Username $Username -Password $Password
|
|
|
- }
|
|
|
-
|
|
|
- return $true
|
|
|
+# ========== Configure your username, email and password here ==========
|
|
|
+$defaultUsername = "yichael" # Change to your Git username
|
|
|
+$defaultEmail = "2812319400@qq.com" # Change to your Git email
|
|
|
+$defaultPassword = "Qwerty123" # Change to your Git password or access token
|
|
|
+# ======================================================================
|
|
|
+
|
|
|
+# Use default values if parameters not provided
|
|
|
+if (-not $Username) { $Username = $defaultUsername }
|
|
|
+if (-not $Email) { $Email = $defaultEmail }
|
|
|
+if (-not $Password) {
|
|
|
+ $Password = ConvertTo-SecureString $defaultPassword -AsPlainText -Force
|
|
|
}
|
|
|
|
|
|
-function Clear-OldCredentials {
|
|
|
- param([string]$RemoteUrl)
|
|
|
-
|
|
|
- Write-Host "`n清除旧凭证..." -ForegroundColor Yellow
|
|
|
-
|
|
|
- # 从 URL 提取域名
|
|
|
- if ($RemoteUrl -match "https?://([^/]+)") {
|
|
|
- $domain = $Matches[1]
|
|
|
-
|
|
|
- # 清除 Windows 凭证管理器中的旧凭证
|
|
|
- $credentialTargets = @(
|
|
|
- "LegacyGeneric:target=git:https://$domain",
|
|
|
- "LegacyGeneric:target=git:$domain",
|
|
|
- "LegacyGeneric:target=$domain"
|
|
|
- )
|
|
|
+# Get remote repository URL
|
|
|
+$RemoteUrl = git config --get remote.origin.url 2>$null
|
|
|
|
|
|
- foreach ($target in $credentialTargets) {
|
|
|
- $result = cmdkey /list 2>$null | Select-String -Pattern $target.Replace("LegacyGeneric:target=", "")
|
|
|
- if ($result) {
|
|
|
- cmdkey /delete:$target 2>$null | Out-Null
|
|
|
- Write-Host " [OK] 已清除凭证: $target" -ForegroundColor Green
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- # 清除 Git credential cache
|
|
|
- git credential-cache exit 2>$null | Out-Null
|
|
|
- Write-Host " [OK] 已清除 Git credential cache" -ForegroundColor Green
|
|
|
+# Convert SecureString to plain string (only when needed)
|
|
|
+$passwordPlain = $null
|
|
|
+if ($Password) {
|
|
|
+ $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password)
|
|
|
+ $passwordPlain = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
|
|
|
}
|
|
|
|
|
|
-function Set-NewCredentials {
|
|
|
- param(
|
|
|
- [string]$RemoteUrl,
|
|
|
- [string]$Username,
|
|
|
- [string]$Password
|
|
|
- )
|
|
|
-
|
|
|
- Write-Host "`n设置新凭证..." -ForegroundColor Yellow
|
|
|
+# Set Git configuration
|
|
|
+$scope = if ($Global) { "--global" } else { "--local" }
|
|
|
+$success = $true
|
|
|
|
|
|
- if ($RemoteUrl -match "https?://([^/]+)") {
|
|
|
- $domain = $Matches[1]
|
|
|
-
|
|
|
- # 使用 Git credential helper 存储凭证
|
|
|
- $credentialInput = "protocol=https`nhost=$domain`nusername=$Username`npassword=$Password`n`n"
|
|
|
-
|
|
|
- $credentialInput | git credential approve 2>$null
|
|
|
- if ($LASTEXITCODE -eq 0) {
|
|
|
- Write-Host " [OK] 凭证已保存到 Windows Credential Manager" -ForegroundColor Green
|
|
|
- } else {
|
|
|
- Write-Host " [WARN] 自动保存凭证失败,下次 push 时会提示输入" -ForegroundColor Yellow
|
|
|
- }
|
|
|
- }
|
|
|
+if ($Username) {
|
|
|
+ git config $scope user.name $Username 2>$null
|
|
|
+ if ($LASTEXITCODE -ne 0) { $success = $false }
|
|
|
}
|
|
|
|
|
|
-# 主程序
|
|
|
-if ($Help) {
|
|
|
- Show-Help
|
|
|
- exit 0
|
|
|
+if ($Email) {
|
|
|
+ git config $scope user.email $Email 2>$null
|
|
|
+ if ($LASTEXITCODE -ne 0) { $success = $false }
|
|
|
}
|
|
|
|
|
|
-Write-Host "`n=== Git 凭证配置脚本 ===" -ForegroundColor Cyan
|
|
|
-Write-Host ""
|
|
|
-
|
|
|
-# 显示当前配置
|
|
|
-Get-CurrentGitConfig
|
|
|
-
|
|
|
-# 交互式输入(如果未提供参数)
|
|
|
-if (-not $Username -and -not $Email -and -not $Password) {
|
|
|
- Write-Host "`n请输入配置信息(直接回车跳过该项):" -ForegroundColor Yellow
|
|
|
-
|
|
|
- $inputUsername = Read-Host "用户名"
|
|
|
- $inputEmail = Read-Host "邮箱"
|
|
|
- $inputPassword = Read-Host "密码/访问令牌" -AsSecureString
|
|
|
-
|
|
|
- if ($inputUsername) { $Username = $inputUsername }
|
|
|
- if ($inputEmail) { $Email = $inputEmail }
|
|
|
- if ($inputPassword) {
|
|
|
- $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($inputPassword)
|
|
|
- $Password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
|
|
|
- }
|
|
|
-
|
|
|
- # 询问是否清除远程凭证
|
|
|
- $clearCreds = Read-Host "`n是否清除远程仓库的旧凭证? (y/n)"
|
|
|
- if ($clearCreds -eq "y" -or $clearCreds -eq "Y") {
|
|
|
- if (-not $RemoteUrl) {
|
|
|
- $RemoteUrl = Read-Host "请输入远程仓库 URL(如: https://example.com/repo.git)"
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- # 询问应用范围
|
|
|
- $useGlobal = Read-Host "应用到全局配置? (y/n,默认 n)"
|
|
|
- if ($useGlobal -eq "y" -or $useGlobal -eq "Y") {
|
|
|
- $Global = $true
|
|
|
- }
|
|
|
+# Clear old credentials
|
|
|
+if ($RemoteUrl -and $RemoteUrl -match "https?://([^/]+)") {
|
|
|
+ $domain = $Matches[1]
|
|
|
+ cmdkey /delete:"LegacyGeneric:target=git:https://$domain" 2>$null | Out-Null
|
|
|
+ git credential-cache exit 2>$null | Out-Null
|
|
|
}
|
|
|
|
|
|
-# 如果没有提供 RemoteUrl,尝试从当前仓库获取
|
|
|
-if (-not $RemoteUrl) {
|
|
|
- $remoteUrl = git config --get remote.origin.url 2>$null
|
|
|
- if ($remoteUrl) {
|
|
|
- $RemoteUrl = $remoteUrl
|
|
|
- Write-Host "`n检测到远程仓库: $RemoteUrl" -ForegroundColor Gray
|
|
|
- }
|
|
|
+# Set new credentials
|
|
|
+if ($passwordPlain -and $RemoteUrl -and $RemoteUrl -match "https?://([^/]+)") {
|
|
|
+ $domain = $Matches[1]
|
|
|
+ $credentialInput = "protocol=https`nhost=$domain`nusername=$Username`npassword=$passwordPlain`n`n"
|
|
|
+ $credentialInput | git credential approve 2>$null
|
|
|
+ if ($LASTEXITCODE -ne 0) { $success = $false }
|
|
|
+ # Clear plain password from memory
|
|
|
+ [System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($BSTR)
|
|
|
+ $passwordPlain = $null
|
|
|
}
|
|
|
|
|
|
-# 执行配置
|
|
|
-if ($Username -or $Email) {
|
|
|
- if (Set-GitCredentials -Username $Username -Email $Email -Password $Password -RemoteUrl $RemoteUrl -IsGlobal $Global) {
|
|
|
- Write-Host "`n=== 配置完成 ===" -ForegroundColor Green
|
|
|
-
|
|
|
- # 显示新配置
|
|
|
- Write-Host "`n新的 Git 配置:" -ForegroundColor Yellow
|
|
|
- $scope = if ($Global) { "--global" } else { "--local" }
|
|
|
- $newName = git config $scope user.name 2>$null
|
|
|
- $newEmail = git config $scope user.email 2>$null
|
|
|
- Write-Host " 用户名: $newName" -ForegroundColor White
|
|
|
- Write-Host " 邮箱: $newEmail" -ForegroundColor White
|
|
|
-
|
|
|
- if ($Password -and $RemoteUrl) {
|
|
|
- Write-Host "`n提示: 下次 push 时,系统会自动使用保存的凭证" -ForegroundColor Cyan
|
|
|
- Write-Host " 如果凭证无效,请重新运行此脚本更新" -ForegroundColor Cyan
|
|
|
- }
|
|
|
- } else {
|
|
|
- Write-Host "`n配置失败,请检查错误信息" -ForegroundColor Red
|
|
|
- exit 1
|
|
|
- }
|
|
|
+# Show result
|
|
|
+if ($success) {
|
|
|
+ Write-Host "[OK] Configuration successful" -ForegroundColor Green
|
|
|
} else {
|
|
|
- Write-Host "`n未提供任何配置信息" -ForegroundColor Yellow
|
|
|
- Show-Help
|
|
|
- exit 0
|
|
|
+ Write-Host "[X] Configuration failed" -ForegroundColor Red
|
|
|
+ exit 1
|
|
|
}
|