如何使用libgit2连接到GitHub仓库? [英] How to connect to a GitHub repo using libgit2?

查看:287
本文介绍了如何使用libgit2连接到GitHub仓库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用(var Git = new Repository(
Repository .Clone(https://github.com/wikimedia/mediawiki-core,tmp)
)){
foreach(Git.Commits中的Commit)
{
MessageBox.Show(Commit.Author.Name);
}
}

连接正常(如果我更改了URL我得到预期的异常),但没有显示 MessageBox es - 为什么?这应该很简单。

解决方案




  • 不能连接到远程存储库并通过git协议动态检索一个文件。通常,它需要检索一个本地副本(通过 Repository.Clone()),然后对本地存储库执行一些工作。

  • Mediawiki-core 是一个相当庞大的存储库。它包含超过10000个提交。因此,克隆可能需要相当长的一段时间。通过提供带有传输进度处理程序的 Clone()方法,可以获得有关克隆进度的一些信息。

  • @CarlosMartínNieto是对的。如果您只需要一个文件(而不是整个存储库的历史记录),请依靠 GitHub API 确实会更有效率。请注意,此API的使用受限于 费率限制 ,您可能需要根据预见的使用情况,可以考虑使用它。下面的代码(极大地启发了你自己的代码)克隆了一个远程仓库,输出了当前的克隆进程到控制台,并枚举可从 HEAD 访问的提交。



    已成功通过LibGit2Sharp v0.14.1 NuGet软件包进行测试。

      public void CloneAndEnumerateCommitsFromHead()
    {
    var tmp = Path.Combine(Path.GetTempPath(),Guid.NewGuid()。ToString());

    string path = Repository.Clone(
    https://github.com/nulltoken/TestGitRepository,
    tmp,
    onTransferProgress:ProgressHandler);

    使用(var Git = new Repository(path))
    {
    foreach(Git.Commits中的Commit)
    {
    Console.WriteLine {0} by {1},
    Commit.Id.ToString(7),
    Commit.Author.Name);



    $ b private int ProgressHandler(TransferProgress progress)
    {
    Console.WriteLine({0} / {1 },progress.IndexedObjects,progress.TotalObjects);
    返回0;
    }

    运行时,它会输出以下内容:

      0/70 
    1/70
    2/70
    2/70
    ...为了简洁...
    68/70
    69/70
    70/70
    70/70
    49322bb由AU Thor
    d0114ab由AU Thor
    f73b956由AU Thor
    6e14752由AU Thor
    1203b03由AU Thor
    bab66b4由AU Thor
    83834a7由AU Thor
    6462e7d由AU Thor
    42e4e7c由AU Thor
    7f82283由AU Thor
    59706a1由AU Thor
    c070ad8由AU Thor
    d31f5a6由AU Thor
    83d2f04由AU Thor
    6db9c2e由AU Thor
    d86a2aa由AU Thor
    0966a43由AU Thor
    2c34933由AU Thor
    ac7e7e4由AU Thor
    58be465由AU Thor
    6c8b137由AU Thor


    I'm trying to connect to a repo:

    using(var Git = new Repository(
        Repository.Clone("https://github.com/wikimedia/mediawiki-core", "tmp")
    )){
        foreach(var Commit in Git.Commits)
        {
            MessageBox.Show(Commit.Author.Name);
        }
    }
    

    It connects ok (as in, if I change the URL I get the expected exception), but no MessageBoxes are shown - why? This should be simple.

    解决方案

    Few things to consider regarding your question:

    • One cannot "connect" to a remote repository and dynamically retrieve one file through the git protocols. Usually, it is expected to retrieve a local copy (through Repository.Clone()) then to perform some work against the local repository.
    • Mediawiki-core is quite a huge repository. It contains more than 10000 commits. As such, it can take quite some time to be cloned. One can get some insight regarding the progress of the cloning by providing the Clone() method with a transfer progress handler.
    • @CarlosMartínNieto is right. If all you need is one single file (rather than the whole history of the repository), relying on the GitHub API would indeed be more efficient. Note that usage of this API is however governed through some rate limitations you may want to consider, depending on the foreseen usage.

    The code below (greatly inspired from your own code) clones a remote repository, outputting the current clone progress to the console, and enumerates the commits reachable from HEAD.

    It's been successfully tested against LibGit2Sharp v0.14.1 NuGet package.

    public void CloneAndEnumerateCommitsFromHead()
    {
        var tmp = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
    
        string path = Repository.Clone(
            "https://github.com/nulltoken/TestGitRepository", 
            tmp,
            onTransferProgress: ProgressHandler);
    
        using (var Git = new Repository(path))
        {
            foreach (var Commit in Git.Commits)
            {
                Console.WriteLine("{0} by {1}",
                    Commit.Id.ToString(7),
                    Commit.Author.Name);
            }
        }
    }
    
    private int ProgressHandler(TransferProgress progress)
    {
        Console.WriteLine("{0}/{1}", progress.IndexedObjects, progress.TotalObjects);
        return 0;
    }
    

    When being run, it outputs the following

    0/70
    1/70
    2/70
    2/70
    ...snipped for brevity...
    68/70
    69/70
    70/70
    70/70
    49322bb by A U Thor
    d0114ab by A U Thor
    f73b956 by A U Thor
    6e14752 by A U Thor
    1203b03 by A U Thor
    bab66b4 by A U Thor
    83834a7 by A U Thor
    6462e7d by A U Thor
    42e4e7c by A U Thor
    7f82283 by A U Thor
    59706a1 by A U Thor
    c070ad8 by A U Thor
    d31f5a6 by A U Thor
    83d2f04 by A U Thor
    6db9c2e by A U Thor
    d86a2aa by A U Thor
    0966a43 by A U Thor
    2c34933 by A U Thor
    ac7e7e4 by A U Thor
    58be465 by A U Thor
    6c8b137 by A U Thor
    

    这篇关于如何使用libgit2连接到GitHub仓库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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