如何在不需要提供用户详细信息的情况下从特定的远程分支中提取最新信息?

How to pull latest from a specific remote branch without having need to provide user details?

要求:

使用 libgit2sharp 我想从 特定的 git 远程分支 中提取(获取 + 合并)最新信息到我当前检查的 出本地分支,而不必传递任何其他参数,例如用户凭据等。基本上我正在尝试复制git pull origin my-remote-branch

详情:

我想从 C# 中自动执行某些 Git 操作。我可以通过调用 git.exe(如果我知道路径)来简单地做我想做的事,比如 git.exe --git-dir=my-repo-directory pull origin my-remote-branch。请注意,这里我必须提供的唯一外部参数是 my-repo-directorymy-remote-branch。 Git 一切都正确,比如名称、密码、电子邮件、当前工作分支(即使它没有远程连接)和 git pull 也很简单。我不必手动传递任何这些参数。我假设 Git 从回购的当前 Git 设置中获取它们(从 %HOME% 文件夹?)。

有没有办法在 LibGit2Sharp 中模拟它?

我尝试了什么:

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
    );
}

这失败了

request failed with status code: 401

附加信息:

我不想直接调用 git.exe 因为我不能硬编码 git exe 路径。此外,由于我无法在运行时传递用户名、电子邮件等,libgit2sharp 是否可以从存储库设置中自行获取它们,就像 git.exe 那样?

I assume Git gets them from current Git settings for the repo (from %HOME% folder?).

这完全取决于遥控器 "origin" 是什么:

  • an ssh URL(在这种情况下 Git 将依赖 %HOME%\.ssh)。 ssh 对 libgit2sharp 的支持后跟 issue 7 and (rejected) PR 1072: you might have to use leobuskin/libgit2sharp-ssh
  • 一个 https URL(在这种情况下 Git 应该依赖 Git credential helper (see Git Storage), which is , as mentioned here.
    您必须自己在 CSharp 中编写该帮助程序:“Retrieve Credentials from Windows Credentials Store using C#”。
    或者,如 by Edward Thomson

    Pass a CredentialsHandler in your FetchOptions.
    Return either UsernamePasswordCredentials or DefaultCredentials from your handler, as appropriate.

See here UsernamePasswordCredentials 示例。
另见 LibGit2Sharp.Tests/TestHelpers/Constants.cs and other occurrences.


关于pull操作,涉及到一个Command Fetch, which involves a refspec. As in "Git pull/fetch with refspec differences",你可以通过source:destination分支名称进行pull(即使没有tracking信息)。

这就是 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);