如何使用libgit2sharp从Git下载文件/子文件夹

时间:2014-08-11 20:10:53

标签: libgit2sharp

从ASP.NET应用程序我想使用LibGit2Sharp下载Git远程存储库中的特定文件和子文件夹。

例如:

主远程存储库网址:https://github.com/macmillanhighered/TestApplication

子文件夹网址:https://github.com/macmillanhighered/TestApplication/tree/develop/Scripts/PRODUCTS

我只想使用libgit2sharp API获取所有文件/文件夹而不是获取特定文件夹。

我使用下面的示例代码来执行此操作,但它不起作用。请尽快帮助我

// Url of the remote repository to clone
        string url = "https://github.com/macmillanhighered/TestApplication/tree/develop/Scripts/PRODUCTS";

        // Location on the disk where the local repository should be cloned
        string workingDirectory = "C:\\TestGit";

        var credential = new UsernamePasswordCredentials() { Username = "nitin", Password = "test" };

        CloneOptions cloneOptions = new CloneOptions();
        cloneOptions.Credentials = credential;

        // Perform the initial clone
        string repoPath = Repository.Clone(url, workingDirectory, cloneOptions);


        using (var repo = new Repository(repoPath))
        {
             // "origin" is the default name given by a Clone operation
            // to the created remote
            var remote = repo.Network.Remotes["origin"];

            // Retrieve the changes from the remote repository
            // (eg. new commits that have been pushed by other contributors)
            Signature sign = new Signature("Test", "", DateTime.Now);

            FetchOptions fetchOptions = new FetchOptions();
            fetchOptions.Credentials = credential;

            MergeOptions mergeOptions = new MergeOptions();

            PullOptions pullOptions = new PullOptions();
            pullOptions.FetchOptions = fetchOptions;
            pullOptions.MergeOptions = mergeOptions;

            repo.Network.Pull(sign, pullOptions);
        }

我收到以下错误

请求失败,状态代码为:404

1 个答案:

答案 0 :(得分:1)

  

我收到以下错误   请求失败,状态代码为:404

Clone()期望一个url指向一个git存储库,而不是一个文件或文件夹。

这样的事情会更好:string url = "https://github.com/macmillanhighered/TestApplication"

  

我只想使用libgit2sharp API获取所有文件/文件夹而不是获取特定文件夹。

Git协议并不像这样工作。您应该获取整个存储库,然后在本地使用您感兴趣的所选文件。