1

我试图弄清楚(在 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

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

4

2 回答 2

0

根据问题 #1950从 Octokit 的存储库下载整个文件夹/目录,鉴于 github API 的限制,这是不可能的。你能做的最好的就是下载整个 repo 并自己解析文件:

var archiveBytes = await client.Repository.Content.GetArchive("octokit", "octokit.net", ArchiveFormat.Zipball);
于 2020-07-12T18:48:34.333 回答
-1

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

在 GitHub 上使用 https:// 时,有没有办法跳过密码输入?

于 2017-03-25T13:37:53.480 回答