Просмотр исходного кода

娣诲姞 Git 鍑瘉閰嶇疆宸ュ叿鑴氭湰

yichael 4 недель назад
Родитель
Сommit
5f4066804b
2 измененных файлов с 262 добавлено и 0 удалено
  1. 29 0
      bat-tool/git-credential-config.bat
  2. 233 0
      bat-tool/git-credential-config.ps1

+ 29 - 0
bat-tool/git-credential-config.bat

@@ -0,0 +1,29 @@
+@echo off
+chcp 65001 >nul
+title Git 凭证配置工具
+
+REM 获取脚本所在目录
+set "SCRIPT_DIR=%~dp0"
+set "PS_SCRIPT=%SCRIPT_DIR%git-credential-config.ps1"
+
+REM 检查 PowerShell 脚本是否存在
+if not exist "%PS_SCRIPT%" (
+    echo [错误] 找不到 PowerShell 脚本: %PS_SCRIPT%
+    echo.
+    pause
+    exit /b 1
+)
+
+REM 切换到项目根目录(bat-tool 的父目录)
+cd /d "%~dp0\.."
+
+REM 执行 PowerShell 脚本
+powershell.exe -ExecutionPolicy Bypass -NoProfile -File "%PS_SCRIPT%" %*
+
+REM 如果出错,暂停以便查看错误信息
+if errorlevel 1 (
+    echo.
+    echo [错误] 脚本执行失败
+    echo.
+    pause
+)

+ 233 - 0
bat-tool/git-credential-config.ps1

@@ -0,0 +1,233 @@
+# Git 凭证配置脚本
+# 功能:修改 Git 用户名、邮箱和密码凭证
+
+param(
+    [string]$Username = "",
+    [string]$Email = "",
+    [string]$Password = "",
+    [string]$RemoteUrl = "",
+    [switch]$Global = $false,
+    [switch]$Help = $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
+}
+
+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"
+        )
+
+        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
+}
+
+function Set-NewCredentials {
+    param(
+        [string]$RemoteUrl,
+        [string]$Username,
+        [string]$Password
+    )
+
+    Write-Host "`n设置新凭证..." -ForegroundColor Yellow
+
+    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 ($Help) {
+    Show-Help
+    exit 0
+}
+
+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
+    }
+}
+
+# 如果没有提供 RemoteUrl,尝试从当前仓库获取
+if (-not $RemoteUrl) {
+    $remoteUrl = git config --get remote.origin.url 2>$null
+    if ($remoteUrl) {
+        $RemoteUrl = $remoteUrl
+        Write-Host "`n检测到远程仓库: $RemoteUrl" -ForegroundColor Gray
+    }
+}
+
+# 执行配置
+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
+    }
+} else {
+    Write-Host "`n未提供任何配置信息" -ForegroundColor Yellow
+    Show-Help
+    exit 0
+}