Is it possible to download a single file from GitLab using libgit2sharp?

93 views Asked by At

It is possible to clone the repository using 'libgit2sharp'. But I need, only some specific files from the repository. Like search with a filename and then download/save. I tried like below, but failed to find the searched item.

foreach (var item in repository.Commits)
{
    var blob = repository.Lookup<Blob>(item.Sha + ":" + fileName);

    if (blob == null)
    {
        continue;
    }
    else
    {
        // How to save?
    }
}

Also, it is unknown to save file from a 'blob' type. It is just for a download. Not needed for a pull, commit etc in the workflow. Also, a search in the local folder after clone is not a right option in my case. Please advise.

Note: My files are model files(.mdl and .slx).

1

There are 1 answers

1
Baran Akçakaya On

You can look at the documantation.

var fileName = "fileOne.txt"; 
var commitSha = "807736c691865a8f03c6f433d90db16d2ac7a005"; // For example
var repoPath = @"path/to/repo";

using (var repo = new Repository(repoPath))
{
    var blob = repo.Lookup<Blob>(commitSha + ":" + fileName);

    using (var content = new StreamReader(blob.GetContentStream(), Encoding.UTF8))
    {
        var fileContent = content.ReadToEnd();
        Console.WriteLine(fileContent);
    }
}