Search code examples
c#libgit2sharp

LibGit2Sharp Getting the last version of a remote repository


I want to track a project that uses git in my winforms project. I don't want to clone the full repository and the full history, I just want the latest version, and I want to be able to update to new revisions from the remote project.

I have tried this

co.CredentialsProvider = (_url, _user, _cred) => new UsernamePasswordCredentials { Username = userName, Password = passWord };

        Repository.Clone("Git/repo", @tmpRepoFolder, co);

, but this creates a copy of the entire repository (huge file size), and tracking changes makes the disk space even bigger (100mb of files now takes up over 2gb).

I don't need the history and i don't need the tags. I just want the latest version .


Solution

  • Basically you want a shallow clone (the equivalent of the git clone --depth command) which is not actually supported, there is an open issue for that

    As an alternative you can launch a Process which do what you want using the git application.

    Here an example:

    using(System.Diagnostics.Process p = new Process())
    {
        p.StartInfo = new ProcessStartInfo()
        {
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            UseShellExecute = false,
            FileName = @"C:\Program Files\Git\bin\git.exe",
            Arguments = "clone http://username:password@path/to/repo.git"  + " --depth 1"                
        };
    
        p.Start();
        string output = p.StandardOutput.ReadToEnd();
        p.WaitForExit();
    }