How to pull the last of a specific remote branch without having to provide user data?

Requirements:

Using libgit2sharp I want to pull (fetch + merge) the last from the remote git branch to my verified local branch , without having to pass any other arguments like user credentials, etc. I'm mainly trying to replicategit pull origin my-remote-branch

More details:

I want to automate some git operations with C #. I just can do what I want by calling git.exe(if I know the path), for example git.exe --git-dir=my-repo-directory pull origin my-remote-branch. Please note that here the only external parameters that I have to provide are my-repo-directoryand my-remote-branch. git gets everything right, such as name, password, email, the current working branch (even if it does not have a remote connection), and git pull just works. I do not need to pass any of these parameters manually. I assume that git gets them from the current git parameters for the repo (from the% HOME% folder?).

Is there any way to simulate this in LibGit2Sharp?

What I tried:

using (var repo = new Repository("my-repo-directory"))
{
    PullOptions pullOptions = new PullOptions()
    {
        MergeOptions = new MergeOptions()
        {
            FastForwardStrategy = FastForwardStrategy.Default
        }
    };

    MergeResult mergeResult = Commands.Pull(
        repo,
        new Signature("my name", "my email", DateTimeOffset.Now), // I dont want to provide these
        pullOptions
    );
}

, there is no tracking branch. . , , .

, , :

using (var repo = new Repository("my-repo-directory"))
{
    var trackingBranch = repo.Branches["remotes/origin/my-remote-branch"];

    if (trackingBranch.IsRemote) // even though I dont want to set tracking branch like this
    {
        var branch = repo.Head;
        repo.Branches.Update(branch, b => b.TrackedBranch = trackingBranch.CanonicalName);
    }

    PullOptions pullOptions = new PullOptions()
    {
        MergeOptions = new MergeOptions()
        {
            FastForwardStrategy = FastForwardStrategy.Default
        }
    };

    MergeResult mergeResult = Commands.Pull(
        repo,
        new Signature("my name", "my email", DateTimeOffset.Now),
        pullOptions
    );
}

: 401

:

git.exe , hardcode git exe. , , .. , libgit2sharp , , git.exe?

+4
1

, Git Git ( %HOME%?).

, "":

. UsernamePasswordCredentials.
. LibGit2Sharp.Tests/TestHelpers/Constants.cs .


pull, Command Fetch, refspec. Git pull/fetch refspec, source:destination ( ).

, LibGit2Sharp.Tests/FetchFixture.cs.

string refSpec = string.Format("refs/heads/{2}:refs/remotes/{0}/{1}", remoteName, localBranchName, remoteBranchName);
Commands.Fetch(repo, remoteName, new string[] { refSpec }, new FetchOptions {
                TagFetchMode = TagFetchMode.None,
                OnUpdateTips = expectedFetchState.RemoteUpdateTipsHandler
}, null);
+4

Source: https://habr.com/ru/post/1687814/


All Articles