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

时间:2016-10-01 18:52:55

标签: powershell libgit2sharp

在devops透视图中,我尝试使用PowerShell和库克隆git存储库(来自gitlab with authentification)。

因此我试图以这种方式使用此库()。git-clone method桌面。这是我的代码与somme' ???'。

的实际状态
$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专家,我不明白为什么它不起作用。谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

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

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."
}