1

My existing Fetch method looks like this.

public void Fetch(string remote) => CommandLine.Run($"git fetch {remote}", _repoFolder);

I would like to implement the same feature using libgit2sharp.

This is what I have came up with:

public void Fetch(string remote)
{
    var repo = new Repository(_folder);
    var options = new FetchOptions();
    options.Prune = true;
    options.TagFetchMode = TagFetchMode.Auto;
    var refSpecs = $"+refs/heads/*:refs/remotes/{remote}/*";
    Commands.Fetch(repo, remote, new [] {refSpecs}, options, "Fetching remote");
}

However this fails with the following error message: System.InvalidOperationException: 'authentication cancelled'

I have also tried with the libgit2sharp-ssh package, where the result is error code 401 (unathorized client).

I presume the git command line tool works because it knows how to authorize the access (since there is already a remote). How can I achieve the same using libgit2sharp?

1 Answer 1

3

Have you tried something like

using(var repo = new Repository(_folder))
{
    var options = new FetchOptions();
    options.Prune = true;
    options.TagFetchMode = TagFetchMode.Auto;
    options.CredentialsProvider = new CredentialsHandler(
        (url, usernameFromUrl, types) =>
            new UsernamePasswordCredentials()
            {
                Username = "username",
                Password = "password"
            });

    var remote = repo.Network.Remotes["origin"];
    var msg = "Fetching remote";
    var refSpecs = remote.FetchRefSpecs.Select(x => x.Specification);
    Commands.Fetch(repo, remote.Name, refSpecs, options, msg);                
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.