使用libgit2sharp从分支中获取最新信息 [英] using libgit2sharp to pull latest from a branch

查看:179
本文介绍了使用libgit2sharp从分支中获取最新信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c#解决方案中使用libgit2sharp切换到分支并引入最新更改。这里是我使用的代码:

  public void FetchAll()
{
using(var repo = new Repository(_LocalGitPath))
{
foreach(远程远程在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);


$ b 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(分支,b => b.T​​rackedBranch = 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选项=新的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(新签名(_UserName,_Password,new DateTimeOffset(DateTime.Now)),options);


$ / code>

我没有任何问题提取,并检出分支。当我试图最新的时候,我得到一个错误,说'没有当前分支的跟踪信息'。我相信这意味着本地分支不知道正确的远程存储库是从哪个变更中提取的,但我一直无法弄清楚如何告诉libgit2sharp远程回购路径是什么。任何人有任何建议?



在研究这个问题时,我发现这个: https://github.com/libgit2/libgit2sharp/issues/1235 。从本质上讲,libgit2sharp开发描述了我所看到的确切问题,但不提供任何修补程序代码。



另外一个注意事项:我永远不会合并或推送从此存储库返回的任何更改。我拉它来自动构建,所以我们可以忽略或覆盖任何本地更改。我只需要获得最新的代码。

解决方案:
我已经使用解决方案更新了上面的代码。您需要非常小心,以确保在检出分支时检查分支上的isRemote标志是否设置为true。如果你签出一个不是远程的分支,它会将你的远程设置为'。',并且你需要手动修复它。如果没有有效的远程站点,您将无法撤消分支更改。



不要使用libgit2sharp wiki上的代码示例,除非他们添加此检查。

解决方案

您可以使用Refspec在本地分支上设置跟踪brach信息:

  using(var repo = new Repository(/ Users / sushi / code / redux / mono))
{
var trackingBranch = repo.Branches [ 遥控/原点/工作BTLS];
if(trackingBranch.IsRemote)
{
var branch = repo.CreateBranch(SomeLocalBranchName,trackingBranch.Tip);
repo.Branches.Update(分支,b => b.T​​rackedBranch = trackingBranch.CanonicalName);
repo.Checkout(branch,new CheckoutOptions {CheckoutModifiers = CheckoutModifiers.Force});你可以使用 git
$ b

/ code>来验证 SomeLocalBranchName 现在正在跟踪远程/原产地/工作地点

 >>>> git for-each-ref --format ='%(refname:short)<  - % :short)'refs / heads 

SomeLocalBranchName< - 遥控器/ origin / work-btls
master< - origin / master

>>> ; git status

在分支上SomeLocalBranchName
你的分支是'remotes / origin / work-btls'的最新版本。


I am using libgit2sharp in a c# solution to switch to a branch and pull in the latest changes. Here is the code I am using:

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

I have no problem fetching, and checking out a branch. When I try to pull latest, I get an error saying, 'There is no tracking information for the current branch.' I believe that this means that the local branch doesn't know what the correct remote repository is to pull changes from, but I haven't been able to figure out how to tell libgit2sharp what the remote repo path is. Anyone have any suggestions?

While doing research on this problem I found this: https://github.com/libgit2/libgit2sharp/issues/1235. Essentially, a libgit2sharp dev describes the exact problem I am seeing, but doesn't provide any code for the fix.

One additional note: I will never be merging or pushing any changes back from this repository. I am pulling it for an automated build, so we can ignore or overwrite any local changes. I just need to get the latest code.

SOLUTION: I have updated the code above with the solution that I got working. You need to be really careful to make sure that when you checkout a branch, you check the isRemote flag on the branch you are checking out is set to true. If you checkout a branch that isn't a remote it will set the remote to '.' in your git config file, and you need to manually fix it. If there isn't a valid remote you will not be able to pull the branch changes.

Do not use the code sample on the libgit2sharp wiki unless they add this check in.

解决方案

You can setup the tracking brach information on the local branch by using the 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 });
    }
}

You can use git to verify that SomeLocalBranchName is now tracking 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'.

这篇关于使用libgit2sharp从分支中获取最新信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆