167 lines
5.3 KiB
PowerShell
167 lines
5.3 KiB
PowerShell
# ============================================
|
|
# Git 批量管理脚本 - PowerShell 版本
|
|
# 适用于 Windows PowerShell 和 PowerShell Core
|
|
# ============================================
|
|
|
|
# -------------------- 配置区 --------------------
|
|
# 父目录路径(支持相对路径和绝对路径)
|
|
# 示例:
|
|
# ".." -> 上级目录
|
|
# "." -> 当前目录
|
|
# ".\projects" -> 当前目录下的 projects 文件夹
|
|
# "..\projects" -> 上级目录下的 projects 文件夹
|
|
# "C:\projects" -> 绝对路径
|
|
# "$env:USERPROFILE\projects" -> 用户主目录下的 projects
|
|
$ParentDir = ".."
|
|
|
|
# Git 远程仓库用户名
|
|
$GitUsername = "e2hang"
|
|
|
|
# Git 远程仓库基础 URL
|
|
$GitBaseUrl = "https://huajishe.fun/git"
|
|
|
|
# 默认分支名
|
|
$DefaultBranch = "main"
|
|
|
|
# -------------------- 脚本主体 --------------------
|
|
|
|
# 将相对路径转换为绝对路径
|
|
$ParentDir = Resolve-Path -Path $ParentDir -ErrorAction SilentlyContinue
|
|
|
|
# 检查父目录是否存在
|
|
if (-not $ParentDir -or -not (Test-Path -Path $ParentDir -PathType Container)) {
|
|
Write-Host "错误: 父目录不存在或无法访问" -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
|
|
Write-Host "========================================" -ForegroundColor Cyan
|
|
Write-Host "开始处理目录: $ParentDir" -ForegroundColor Cyan
|
|
Write-Host "========================================" -ForegroundColor Cyan
|
|
Write-Host ""
|
|
|
|
# 统计变量
|
|
$total = 0
|
|
$success = 0
|
|
$skipped = 0
|
|
$failed = 0
|
|
|
|
# 获取所有一级子文件夹
|
|
$projects = Get-ChildItem -Path $ParentDir -Directory
|
|
|
|
foreach ($project in $projects) {
|
|
$total++
|
|
$projectPath = $project.FullName
|
|
$projectName = $project.Name
|
|
|
|
Write-Host "[$total] 处理项目: $projectName" -ForegroundColor Cyan
|
|
|
|
# 进入项目目录
|
|
try {
|
|
Push-Location -Path $projectPath -ErrorAction Stop
|
|
}
|
|
catch {
|
|
Write-Host " ✗ 无法进入目录" -ForegroundColor Red
|
|
$failed++
|
|
continue
|
|
}
|
|
|
|
# 检查是否为 Git 仓库
|
|
if (-not (Test-Path -Path ".git" -PathType Container)) {
|
|
Write-Host " → 初始化 Git 仓库..." -ForegroundColor Yellow
|
|
|
|
# 初始化 Git 仓库
|
|
try {
|
|
git init -b $DefaultBranch 2>&1 | Out-Null
|
|
}
|
|
catch {
|
|
# 兼容旧版本 Git
|
|
git init 2>&1 | Out-Null
|
|
git checkout -b $DefaultBranch 2>&1 | Out-Null
|
|
}
|
|
|
|
# 设置远程仓库
|
|
$remoteUrl = "$GitBaseUrl/$GitUsername/$projectName.git"
|
|
git remote add origin $remoteUrl 2>&1 | Out-Null
|
|
Write-Host " ✓ 已初始化并设置远程仓库: $remoteUrl" -ForegroundColor Green
|
|
}
|
|
|
|
# 检查是否有改动
|
|
$diffResult = git diff --quiet 2>&1
|
|
$diffCachedResult = git diff --cached --quiet 2>&1
|
|
$untrackedFiles = git ls-files --others --exclude-standard 2>&1
|
|
|
|
if ($LASTEXITCODE -eq 0 -and $diffCachedResult -eq $null -and [string]::IsNullOrWhiteSpace($untrackedFiles)) {
|
|
Write-Host " ○ 无改动,跳过" -ForegroundColor Yellow
|
|
$skipped++
|
|
Pop-Location
|
|
Write-Host ""
|
|
continue
|
|
}
|
|
|
|
# 添加所有文件
|
|
Write-Host " → 添加文件..." -ForegroundColor Yellow
|
|
git add . 2>&1 | Out-Null
|
|
|
|
# 再次检查是否有需要提交的内容
|
|
git diff --cached --quiet 2>&1
|
|
if ($LASTEXITCODE -eq 0) {
|
|
Write-Host " ○ 无需提交的改动,跳过" -ForegroundColor Yellow
|
|
$skipped++
|
|
Pop-Location
|
|
Write-Host ""
|
|
continue
|
|
}
|
|
|
|
# 生成提交信息
|
|
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
|
$commitMsg = "Auto commit at $timestamp"
|
|
|
|
# 提交
|
|
Write-Host " → 提交更改..." -ForegroundColor Yellow
|
|
$commitResult = git commit -m $commitMsg 2>&1
|
|
|
|
if ($LASTEXITCODE -eq 0) {
|
|
Write-Host " ✓ 提交成功" -ForegroundColor Green
|
|
|
|
# 推送到远程仓库
|
|
Write-Host " → 推送到远程仓库..." -ForegroundColor Yellow
|
|
|
|
# 尝试推送
|
|
$pushResult = git push -u origin $DefaultBranch 2>&1
|
|
|
|
if ($LASTEXITCODE -eq 0 -or $pushResult -match "Everything up-to-date|Successfully") {
|
|
Write-Host " ✓ 推送成功" -ForegroundColor Green
|
|
$success++
|
|
}
|
|
else {
|
|
# 尝试普通推送
|
|
$pushResult = git push 2>&1
|
|
if ($LASTEXITCODE -eq 0 -or $pushResult -match "Everything up-to-date|Successfully") {
|
|
Write-Host " ✓ 推送成功" -ForegroundColor Green
|
|
$success++
|
|
}
|
|
else {
|
|
Write-Host " ✗ 推送失败(可能需要先 pull 或检查远程仓库)" -ForegroundColor Red
|
|
$failed++
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
Write-Host " ✗ 提交失败" -ForegroundColor Red
|
|
$failed++
|
|
}
|
|
|
|
# 返回上级目录
|
|
Pop-Location
|
|
Write-Host ""
|
|
}
|
|
|
|
# 输出统计信息
|
|
Write-Host "========================================" -ForegroundColor Cyan
|
|
Write-Host "处理完成!" -ForegroundColor Cyan
|
|
Write-Host "========================================" -ForegroundColor Cyan
|
|
Write-Host "总计: $total 个项目"
|
|
Write-Host "成功: $success" -ForegroundColor Green
|
|
Write-Host "跳过: $skipped" -ForegroundColor Yellow
|
|
Write-Host "失败: $failed" -ForegroundColor Red
|
|
Write-Host "" |