如何使用 Powershell 中的 LibGit2Sharp 进行克隆?

How to clone using LibGit2Sharp from Powershell?

从 devops 的角度来看,我尝试使用 PowerShell 和 库克隆 git 存储库(来自 gitlab 并进行身份验证)。

所以我安装了 Desktop for trying to use this Library this way (git-clone method)。这是我的代码的实际状态,带有'???'。

$mylib = [Reflection.Assembly]::LoadFile("C:\Users\Administrator\AppData\Local\GitHub\PortableGit_284a859b0e6deba86edc624fef1e4db2aa8241a9\usr\share\git-tfs\LibGit2Sharp.dll")
$url="https://github.com/libgit2/libgit2sharp.git"
$dir="C:\Windows\Temp"

$upc = new-object LibGit2Sharp.UsernamePasswordCredentials
$upc.Username = ""
$upc.Password = ""

$co = new-object LibGit2Sharp.CloneOptions
$co.BranchName = "master"
$co.CredentialsProvider = $upc

[LibGit2Sharp.Repository].Clone($url,$dir,$co)

我不是 .NET 专家,我不明白为什么它不起作用。感谢您的帮助。

像这样的事情应该让你开始。

function Clone($url, $path, $login, $password) {

  $authCallback = {
    Param (
      [String] $url,
      [String] $usernameFromUrl,
      [LibGit2Sharp.SupportedCredentialTypes] $types
    )

    Write-Host -ForegroundColor "Green" "Authenticating login '$login'..."

    $creds = New-Object LibGit2Sharp.UsernamePasswordCredentials
    $creds.Username = $login
    $creds.Password = $password

    return [LibGit2Sharp.Credentials]$creds
  }

  $transferCallback = {
    Param (
      [LibGit2Sharp.TransferProgress] $progress
    )

    $ratio = [Int32](100 * $progress.IndexedObjects / $progress.TotalObjects)

    Write-Progress -Activity "Transfering..." -status "$ratio% Complete" -PercentComplete $ratio

    return [bool]$true
  }

  $options = New-Object LibGit2Sharp.CloneOptions

  $options.CredentialsProvider = $authCallback -as [LibGit2Sharp.Handlers.CredentialsHandler]
  $options.OnTransferProgress = $transferCallback -as [LibGit2Sharp.Handlers.TransferProgressHandler]

  Write-Host -ForegroundColor "Green" "Cloning $url in `"$path`"..."

  [LibGit2Sharp.Repository]::Clone($url, $path, $options) | Out-Null

  Write-Progress -Activity "Transfering..." -Completed

  Write-Host "Done."
}