gpt4 book ai didi

windows - 如何使用 PowerShell 下载完整的存储库?

转载 作者:可可西里 更新时间:2023-11-01 10:02:51 25 4
gpt4 key购买 nike

我使用这个 power shell 脚本从 Git Hub 存储库下载文件

$url = "https://gist.github.com/ . . ./test.jpg"
$output = "C:\Users\admin\Desktop\test.jpg"
$start_time = Get-Date

$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, $output)
#OR
(New-Object System.Net.WebClient).DownloadFile($url, $output)

Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"

它适用于一个文件。如何使用 PowerShell 下载完整的存储库?我不能使用 git pull

编辑这是我在真实存储库上尝试过的。

存储库:https://github.com/githubtraining/hellogitworld/archive/master.zip

这是一个测试代码,但它并没有完全下载存储库

$url = "https://github.com/githubtraining/hellogitworld/archive/master.zip"
$output = "C:\Users\mycompi\Desktop\test\master.zip"
$start_time = Get-Date

$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, $output)

Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"

最佳答案

您可以从以下 URL 下载当前 HEAD 分支的压缩副本:

https://github.com/[owner]/[repository]/archive/[branch].zip

例如,要在 GitHub 上下载 PowerShell 存储库的当前 master 分支,您可以这样做:

$url = "https://github.com/PowerShell/PowerShell/archive/master.zip"
$output = "C:\Users\admin\Desktop\master.zip"
$start_time = Get-Date

$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, $output)

Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"

您可以轻松地将它变成一个可重用的函数:

function Save-GitHubRepository
{
param(
[Parameter(Mandatory)]
[string]$Owner,

[Parameter(Mandatory)]
[string]$Project,

[Parameter()]
[string]$Branch = 'master'
)

$url = "https://github.com/$Owner/$Project/archive/$Branch.zip"
$output = Join-Path $HOME "Desktop\${Project}-${Branch}_($(Get-Date -Format yyyyMMddHHmm)).zip"
$start_time = Get-Date

$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, $output)

Write-Host "Time taken: $((Get-Date).Subtract($start_time).TotalSeconds) second(s)"
}

然后像这样使用它:

PS C:\> Save-GitHubRepository PowerShell PowerShell

关于windows - 如何使用 PowerShell 下载完整的存储库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48547612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com