如何使用 PowerShell 下载完整的存储库? [英] How to download full repository using PowerShell?

查看:31
本文介绍了如何使用 PowerShell 下载完整的存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use this power shell script to download files from Git Hub repository

$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.

It works well for one file. How can I download the full repository using PowerShell? I cannot use git pull.

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

EDIT Here is what I tried on a real repository.

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

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

Here is a test code, but it doesn't download the repository completely

$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 的分支的压缩副本:

You can download a zipped copy of a branch at it's current HEAD from the following URL:

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

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

So to donwload the current master branch of the PowerShell repository on GitHub for example, you'd do:

$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)" 

您可以轻松地将其转换为可重用的功能:

You could easily turn this into a reusable function:

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

这篇关于如何使用 PowerShell 下载完整的存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆