LibGit2Sharp检测本地存储库是否位于远程后面

时间:2017-09-09 11:04:32

标签: libgit2sharp

我是LibGit2Sharp的新手,需要一些帮助

我试图检测到,本地仓库是远程仓库后面的 - 相当于git status我的测试用例返回:

On branch master
Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
  (use "git pull" to update your local branch)
nothing to commit, working tree clean

aka:gut pull需要......

真正丑陋的解决方案就是写下这样的事情:

var workingTreeBeindOrigin = Process
        .Start("git status")
        .GetStdOutAsString() // not real code but u get the idea ...
        .Contains("Your branch is behind")

但那只是#$%^ ...

我已经知道的事情:

using (var repo = new Repository(localRepositoryPath)) {

    // this loads working tree changes - aka commit required
    var status = repo.RetrieveStatus();

    // this fetches data from remote but i dont what data or how to check remote status
    string logMessage = "";
    foreach (Remote remote in repo.Network.Remotes) {
        IEnumerable<string> refSpecs = remote.FetchRefSpecs.Select(x => x.Specification);
        Commands.Fetch(repo, remote.Name, refSpecs, null, logMessage);
    }

    // repo not showing any changes in watch window (mostly shows 'all threads needs to run' instead)
}

感谢任何正确的方向......

2 个答案:

答案 0 :(得分:1)

回购并非严格地说是远程回购的前方或后方。但是,分支可以位于其远程跟踪分支的后面和/或之前。

Branch类具有TrackingDetails属性,您可以使用该属性访问本地分支和跟踪分支之间的AheadBy / BehindBy提交计数。

例如,您可以查看this unit test来查找更多信息。

答案 1 :(得分:0)

您可以使用以下方法进行检查:

            repo.Head.TrackingDetails
            repo.Head.TrackingDetails.AheadBy
            repo.Head.TrackingDetails.BehindBy
            repo.Head.TrackingDetails.CommonAncestor

如果 repo 没有变化,AheadBy 和 BeyondBy 为空。