Requirement:
Using libgit2sharp I want to pull (fetch + merge) latest from a specific git remote branch to my currently checked out local branch, without having to pass any other argument, like user credentials etc. Basically I am trying to replicate git pull origin my-remote-branch
Details:
I want to automate certain Git operations from C#. I can simply do what I want by invoking git.exe
(if I know the path), like git.exe --git-dir=my-repo-directory pull origin my-remote-branch
. Notice that here the only external parameters I have to supply are my-repo-directory
and my-remote-branch
. Git gets everything right, like the name, password, email, current working branch (even if it doesnt have remote attached) and git pull simply works. I dont have to pass any of those parameters manually. I assume Git gets them from current Git settings for the repo (from %HOME% folder?).
Is there a way to simulate that 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
);
}
Which fails since it says there is no tracking branch
. I dont necessarily need a tracking remote branch. I just want to fetch latest from a specific random remote repo and perform automerge if possible.
Just to see if it works I tried:
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
);
}
This fails with
request failed with status code: 401
Additional info:
I dont want to invoke git.exe directly because I cant hardcode the git exe path. Also, since I cant pass username, email etc at runtime, is there a way libgit2sharp get them by itself from the repository settings, like how git.exe does?