C#从GitHub(Octokit.Net)下载包含子目录和文件的完整目录结构

时间:2017-03-25 13:27:53

标签: c# github download octokit.net

我试图找出(在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

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

2 个答案:

答案 0 :(得分:0)

根据Octokit上的问题#1950 download whole folder/directory from repository,鉴于github API的限制,这是不可能的。最好的办法是下载整个存储库,然后自己解析文件:

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

答案 1 :(得分:-1)

为什么不通过Process使用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中的身份验证,可以使用名为凭据帮助程序的功能以某种方式存储凭据。我不确定如何设置它。我认为这个问题是一个好的开始,试着从那里开始研究:

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