Skip to content

Commit 26fa4e0

Browse files
committed
refactor(downGithub.ps1): 重构仓库更新逻辑并添加OnlyBackup参数
将仓库更新逻辑提取到独立的updateRepo函数中,以提高代码复用性和可维护性。同时添加OnlyBackup参数以支持纯备份场景。
1 parent aae3114 commit 26fa4e0

1 file changed

Lines changed: 32 additions & 10 deletions

File tree

downGithub.ps1

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ param (
3131
[string]
3232
$Path = '.',
3333
[switch]
34-
$WithDate
34+
$WithDate,
35+
# 纯备份场景不需要工作区
36+
[switch]
37+
$OnlyBackup
3538
)
3639

3740
# $repos = Invoke-RestMethod -Uri "https://api.github.com/users/$UserName/repos" -Headers @{ "User-Agent" = "Mozilla/5.0" }
@@ -125,7 +128,32 @@ $repoParentPath = Join-Path $Path $folderMame
125128
if (-not (Test-Path $repoParentPath)) {
126129
New-Item -ItemType Directory -Force -Path $repoParentPath
127130
}
128-
131+
function updateRepo {
132+
params(
133+
[string]$Path
134+
)
135+
# 检查是否为 Git 仓库
136+
if (-not (Test-Path (Join-Path $Path ".git"))) {
137+
Write-Error "目录 '$Path' 不是 Git 仓库!"
138+
return
139+
}
140+
try {
141+
# 使用 -C 直接指定路径,避免切换目录
142+
git -C $Path fetch --all --prune
143+
Write-Host "仓库 '$Path' 更新成功。" -ForegroundColor Green
144+
}
145+
catch {
146+
Write-Error "更新仓库 '$Path' 时出错: $_"
147+
}
148+
# Push-Location $Path
149+
# try {
150+
# git fetch --all
151+
# git remote update
152+
# }
153+
# finally {
154+
# Pop-Location
155+
# }
156+
}
129157

130158
foreach ($repo in $finalRepos) {
131159
$repoName = $repo.name
@@ -135,17 +163,11 @@ foreach ($repo in $finalRepos) {
135163
# 检查是否已经存在该目录
136164
if (-not (Test-Path -Path $repoPath)) {
137165
gh repo clone $repoUrl $repoPath
166+
updateRepo -Path $repoPath
138167
}
139168
else {
140169
Write-Output "Repository '$repoName' already exists, updating..."
141-
Push-Location $repoPath
142-
try {
143-
git fetch --all
144-
git remote update
145-
}
146-
finally {
147-
Pop-Location
148-
}
170+
updateRepo -Path $repoPath
149171
}
150172
}
151173

0 commit comments

Comments
 (0)