为什么要这个功能
近期我滴好朋友们的Github账号被莫名其妙送上ban位,虽然笔者代码没有什么高科技含量,但毕竟也是一下一下码出来的,所以笔者配合AI写了个同步脚本。废话不多说上脚本!
脚本教程
前置工作
本脚本支持同步自己的仓库和starred仓库,使用前请确保你的电脑已经安装了git,并且能够正常的拉取自己仓库中的代码。
Token获取方式
- 跳转到 https://github.com/settings/tokens
- 点击
Generate new token
- 建议给全部权限,不然可能会出现同步不全的情况
- 令牌失效时间拉满,防止经常维护
powershell脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
$token = "<your_token>"
$headers = @{ "Authorization" = "token $token" }
Write-Host "开始同步自己的仓库"
$repoList = @() $page = 1 $perPage = 100
do { $apiUrl = "https://api.github.com/user/repos?visibility=all&page=$page&per_page=$perPage" $response = Invoke-RestMethod -Uri $apiUrl -Method Get -Headers $headers $repoList += $response $page++ } while ($response.Length -eq $perPage)
$cloneDirectory = "F:\Github本地缓存" Set-Location -Path $cloneDirectory
foreach ($repo in $repoList) { $cloneUrl = $repo.clone_url $repoName = $repo.name
if (Test-Path $repoName) { Write-Host "Repository $repoName already exists. Updating..." Push-Location -Path $repoName & git pull Pop-Location } else { Write-Host "Cloning $repoName..." & git clone $cloneUrl } }
Write-Host "开始同步starred仓库"
$filePath = "starred.txt"
if (Test-Path $filePath) { Remove-Item $filePath }
$starredRepos = @() $page = 1 $perPage = 100
do { $response = Invoke-RestMethod -Uri "https://api.github.com/user/starred?page=$page&per_page=$perPage" -Method Get -Headers $headers $starredRepos += $response $page++ Start-Sleep -Seconds 2 } while ($response.Length -eq $perPage)
foreach ($repo in $starredRepos) { Write-Host $repo.html_url $repo.html_url | Out-File -FilePath $filePath -Append -Encoding utf8 }
|
建议
可以在Windows计划任务中设置开机自启,这样就可以实现开机自动同步了。