1

I'm trying to figure out (in C#) how to download a directory with content (files and folders) in my GitHub repository through the API. Some of the articles mention Octokit.Net so i downloaded this and wrote the following lines:

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");

Well i then get the repository and it works but i'm not sure where to go from here?

How can i download Folder1 (look below) containing all files and Folder2 with files in a structure to my local hard disk?

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

Can anyone help me in the right direction? Your help is greatly appreciated. Thanks

NorthRebel
  • 11
  • 2

2 Answers2

0

According to Issue #1950 download whole folder/directory from repository on Octokit, this isn't possible given the limitations of the github API. The best you can do is download the entire repo and parse through the files yourself:

var archiveBytes = await client.Repository.Content.GetArchive("octokit", "octokit.net", ArchiveFormat.Zipball);
KyleMit
  • 30,350
  • 66
  • 462
  • 664
-1

Why not use GIT run via Process? Like

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();

I think this should work. But I might as well be wrong. :)

EDIT: with setting user credentials

// 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();

This SHOULD work, I have not tested it and there might also be some syntax errors, but I believe you will figure those out. Regarding authentication in GIT, it is possible to somehow store credentials using feature called credential helper tho I am not really sure how to set this up. I think this question is good start, try researching from there:

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

Community
  • 1
  • 1
RhodryCZ
  • 129
  • 1
  • 8
  • Thank you but i'm not sure where i put my credentials (login info) by using this approach. – NorthRebel Mar 26 '17 at 20:06
  • Yea. Right. I didn't think of that. I will write new answer with possible solution. It is too long to fit comment. :) – RhodryCZ Mar 28 '17 at 18:37
  • This provides for command line injection. If your inputs are submitted by an untrusted attacker, I bet they would have fun. – Mitch Mar 28 '17 at 19:04
  • Maybe. The process is run as git bash, where you can do mostly just git related stuff. I am not sure however what would happen when "exit" is called. My guess is that the process would stop, because "exit" will shut down the bash. On the other hand it might as well go to windows command line and that would be some serious trouble. As I wrote, it is not tested, just an idea. :) – RhodryCZ Mar 29 '17 at 07:44