【问题标题】:C# Download complete directory structure with subdirectories and files from GitHub (Octokit.Net)C# 从 GitHub (Octokit.Net) 下载包含子目录和文件的完整目录结构
【发布时间】:2017-03-25 13:27:52
【问题描述】:

我正在尝试弄清楚(在 C# 中)如何通过 API 在我的 GitHub 存储库中下载包含内容(文件和文件夹)的目录。有些文章提到了 Octokit.Net,所以我下载了这个并写了以下几行:

var github = new GitHubClient(new ProductHeaderValue("PROJECT"), new InMemoryCredentialStore(new Credentials("xxxtokenxxx")));

var repositories =  github.Repository.GetAllForCurrent().Result;

var repository = repositories.Single(x => x.Name == "MyRepo");

然后我得到了存储库,它可以工作,但我不确定从这里去哪里?

如何将包含所有文件的 Folder1(如下所示)和带有结构文件的 Folder2 下载到我的本地硬盘?

https://github.com/PROJECT/MyRepo/tree/2016-1/Folder1/Folder2

任何人都可以帮助我朝着正确的方向前进吗?非常感谢您的帮助。谢谢

【问题讨论】:

    标签: c# github download octokit.net


    【解决方案1】:

    根据 Octokit 上的 Issue #1950 download whole folder/directory from repository,鉴于 github API 的限制,这是不可能的。你能做的最好的就是下载整个 repo 并自己解析文件:

    var archiveBytes = await client.Repository.Content.GetArchive("octokit", "octokit.net", ArchiveFormat.Zipball);
    

    【讨论】:

      【解决方案2】:

      为什么不使用 GIT 通过进程运行?像

      Process proc = new Process();
      proc.StartInfo.FileName = @"git";
      proc.StartInfo.Arguments = string.Format(@"clone ""{0}"" ""{1}""", repository_address, target_directory);
      proc.StartInfo.UseShellExecute = false;
      proc.StartInfo.CreateNoWindow = true;
      proc.Start();
      proc.WaitForInputIdle();
      

      我认为这应该可行。但我也可能是错的。 :)

      编辑:设置用户凭据

      // Create process
      Process proc = new Process();
      proc.StartInfo.FileName = @"*your_git_location*\\git-bash.exe";
      proc.StartInfo.RedirectStandardInput = true;
      proc.StartInfo.UseShellExecute = false;
      proc.StartInfo.CreateNoWindow = true;
      proc.Start();
      // Wait for it to run
      proc.WaitForInputIdle();
      // Set up user name
      proc.StandardInput.WriteLine("git config user.name ""your_user_name""");
      proc.WaitForInputIdle();
      // Set up user email
      proc.StandardInput.WriteLine("git config user.email ""your_user_email""");
      proc.WaitForInputIdle();
      // Request clone of repository
      proc.StandardInput.WriteLine(string.Format(@"git clone ""{0}"" ""{1}""", repository_address, target_directory););
      proc.WaitForInputIdle();
      // Now git should ask for your password
      proc.StandardInput.WriteLine("your_password");
      proc.WaitForInputIdle();
      // Now clone should be complete
      proc.Dispose();
      

      这个应该可以工作,我没有测试过,可能还有一些语法错误,但我相信你会弄清楚的。关于 GIT 中的身份验证,可以使用名为 credential helper 的功能以某种方式存储凭据,但我不确定如何设置它。我认为这个问题是一个好的开始,尝试从那里研究:

      Is there a way to skip password typing when using https:// on GitHub?

      【讨论】:

      • 谢谢,但我不确定使用这种方法将我的凭据(登录信息)放在哪里。
      • 是的。对。我没有想到。我将用可能的解决方案写出新的答案。太长了,不适合评论。 :)
      • 这提供了命令行注入。如果您的输入是由不受信任的攻击者提交的,我敢打赌他们会很开心。
      • 也许吧。该过程作为 git bash 运行,您可以在其中主要执行与 git 相关的操作。但是,我不确定调用“退出”时会发生什么。我的猜测是该过程将停止,因为“退出”将关闭 bash。另一方面,它还不如转到 Windows 命令行,那将是一些严重的麻烦。正如我所写,它没有经过测试,只是一个想法。 :)
      猜你喜欢
      • 1970-01-01
      • 2015-07-16
      • 1970-01-01
      • 1970-01-01
      • 2020-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-17
      相关资源
      最近更新 更多