使用 libgit2sharp 从分支中获取最新信息
using libgit2sharp to pull latest from a branch
我在 c# 解决方案中使用 libgit2sharp 来切换到分支并引入最新的更改。这是我正在使用的代码:
public void FetchAll()
{
using (var repo = new Repository(_LocalGitPath))
{
foreach (Remote remote in repo.Network.Remotes)
{
FetchOptions options = new FetchOptions();
options.CredentialsProvider = new CredentialsHandler((url, usernameFromUrl, types) => new UsernamePasswordCredentials()
{
Username = _UserName,
Password = _Password
});
repo.Network.Fetch(remote, options);
}
}
}
public string CheckoutBranch(string branchName)
{
using (var repo = new Repository(_LocalGitPath))
{
var trackingBranch = repo.Branches[branchName];
if (trackingBranch.IsRemote)
{
branchName = branchName.Replace("origin/", string.Empty);
var branch = repo.CreateBranch(branchName, trackingBranch.Tip);
repo.Branches.Update(branch, b => b.TrackedBranch = trackingBranch.CanonicalName);
Commands.Checkout(repo, branch, new CheckoutOptions { CheckoutModifiers = CheckoutModifiers.Force });
}
else
{
Commands.Checkout(repo, trackingBranch, new CheckoutOptions { CheckoutModifiers = CheckoutModifiers.Force });
}
return branchName;
}
}
public void PullBranch(string branchName)
{
using (var repo = new Repository(_LocalGitPath))
{
PullOptions options = new PullOptions();
options.MergeOptions = new MergeOptions();
options.MergeOptions.FailOnConflict = true;
options.FetchOptions = new FetchOptions();
options.FetchOptions.CredentialsProvider = new CredentialsHandler((url, usernameFromUrl, types) => new UsernamePasswordCredentials()
{
Username = _UserName,
Password = _Password
});
repo.Network.Pull(new Signature(_UserName, _Password, new DateTimeOffset(DateTime.Now)), options);
}
}
我可以轻松获取和签出分支。当我尝试拉最新时,我收到一条错误消息,'There is no tracking information for the current branch.' 我相信这意味着本地分支不知道要从中拉取更改的正确远程存储库是什么,但我无法做到弄清楚如何告诉 libgit2sharp 远程回购路径是什么。有人有什么建议吗?
在研究这个问题时,我发现了这个:https://github.com/libgit2/libgit2sharp/issues/1235。本质上,libgit2sharp dev 描述了我所看到的确切问题,但没有提供任何修复代码。
附加说明:我永远不会合并或推回此存储库中的任何更改。我将其提取用于自动构建,因此我们可以忽略或覆盖任何本地更改。我只需要获取最新的代码。
解决方案:
我已经用我开始工作的解决方案更新了上面的代码。您需要非常小心,以确保当您签出一个分支时,您检查了您正在签出的分支上的 isRemote 标志是否设置为 true。如果您签出一个不是远程的分支,它会将远程设置为“。”在你的 git 配置文件中,你需要手动修复它。如果没有有效的遥控器,您将无法提取分支更改。
不要使用 libgit2sharp wiki 上的代码示例,除非他们添加了此签入。
您可以使用 Refspec 在本地分支上设置跟踪分支信息:
using (var repo = new Repository("/Users/sushi/code/redux/mono"))
{
var trackingBranch = repo.Branches["remotes/origin/work-btls"];
if (trackingBranch.IsRemote)
{
var branch = repo.CreateBranch("SomeLocalBranchName", trackingBranch.Tip);
repo.Branches.Update(branch, b => b.TrackedBranch = trackingBranch.CanonicalName);
repo.Checkout(branch, new CheckoutOptions { CheckoutModifiers = CheckoutModifiers.Force });
}
}
您可以使用 git
来验证 SomeLocalBranchName
现在正在跟踪 remotes/origin/work-btls
:
>>>git for-each-ref --format='%(refname:short) <- %(upstream:short)' refs/heads
SomeLocalBranchName <- remotes/origin/work-btls
master <- origin/master
>>>git status
On branch SomeLocalBranchName
Your branch is up-to-date with 'remotes/origin/work-btls'.
我在 c# 解决方案中使用 libgit2sharp 来切换到分支并引入最新的更改。这是我正在使用的代码:
public void FetchAll()
{
using (var repo = new Repository(_LocalGitPath))
{
foreach (Remote remote in repo.Network.Remotes)
{
FetchOptions options = new FetchOptions();
options.CredentialsProvider = new CredentialsHandler((url, usernameFromUrl, types) => new UsernamePasswordCredentials()
{
Username = _UserName,
Password = _Password
});
repo.Network.Fetch(remote, options);
}
}
}
public string CheckoutBranch(string branchName)
{
using (var repo = new Repository(_LocalGitPath))
{
var trackingBranch = repo.Branches[branchName];
if (trackingBranch.IsRemote)
{
branchName = branchName.Replace("origin/", string.Empty);
var branch = repo.CreateBranch(branchName, trackingBranch.Tip);
repo.Branches.Update(branch, b => b.TrackedBranch = trackingBranch.CanonicalName);
Commands.Checkout(repo, branch, new CheckoutOptions { CheckoutModifiers = CheckoutModifiers.Force });
}
else
{
Commands.Checkout(repo, trackingBranch, new CheckoutOptions { CheckoutModifiers = CheckoutModifiers.Force });
}
return branchName;
}
}
public void PullBranch(string branchName)
{
using (var repo = new Repository(_LocalGitPath))
{
PullOptions options = new PullOptions();
options.MergeOptions = new MergeOptions();
options.MergeOptions.FailOnConflict = true;
options.FetchOptions = new FetchOptions();
options.FetchOptions.CredentialsProvider = new CredentialsHandler((url, usernameFromUrl, types) => new UsernamePasswordCredentials()
{
Username = _UserName,
Password = _Password
});
repo.Network.Pull(new Signature(_UserName, _Password, new DateTimeOffset(DateTime.Now)), options);
}
}
我可以轻松获取和签出分支。当我尝试拉最新时,我收到一条错误消息,'There is no tracking information for the current branch.' 我相信这意味着本地分支不知道要从中拉取更改的正确远程存储库是什么,但我无法做到弄清楚如何告诉 libgit2sharp 远程回购路径是什么。有人有什么建议吗?
在研究这个问题时,我发现了这个:https://github.com/libgit2/libgit2sharp/issues/1235。本质上,libgit2sharp dev 描述了我所看到的确切问题,但没有提供任何修复代码。
附加说明:我永远不会合并或推回此存储库中的任何更改。我将其提取用于自动构建,因此我们可以忽略或覆盖任何本地更改。我只需要获取最新的代码。
解决方案: 我已经用我开始工作的解决方案更新了上面的代码。您需要非常小心,以确保当您签出一个分支时,您检查了您正在签出的分支上的 isRemote 标志是否设置为 true。如果您签出一个不是远程的分支,它会将远程设置为“。”在你的 git 配置文件中,你需要手动修复它。如果没有有效的遥控器,您将无法提取分支更改。
不要使用 libgit2sharp wiki 上的代码示例,除非他们添加了此签入。
您可以使用 Refspec 在本地分支上设置跟踪分支信息:
using (var repo = new Repository("/Users/sushi/code/redux/mono"))
{
var trackingBranch = repo.Branches["remotes/origin/work-btls"];
if (trackingBranch.IsRemote)
{
var branch = repo.CreateBranch("SomeLocalBranchName", trackingBranch.Tip);
repo.Branches.Update(branch, b => b.TrackedBranch = trackingBranch.CanonicalName);
repo.Checkout(branch, new CheckoutOptions { CheckoutModifiers = CheckoutModifiers.Force });
}
}
您可以使用 git
来验证 SomeLocalBranchName
现在正在跟踪 remotes/origin/work-btls
:
>>>git for-each-ref --format='%(refname:short) <- %(upstream:short)' refs/heads
SomeLocalBranchName <- remotes/origin/work-btls
master <- origin/master
>>>git status
On branch SomeLocalBranchName
Your branch is up-to-date with 'remotes/origin/work-btls'.