CzBuCHi
CzBuCHi

Reputation: 101

LibGit2Sharp detect if local repository is behind remote

im total newbie to LibGit2Sharp and need some help

im trying to detect, that local repo is behind remote repo - equivalent of git status with for my test case returns this:

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 required ...

really ugly solution would be write somethind like this:

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

but thats just #$%^ ...

what i already know:

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

thanks for any kick in right direction ...

Upvotes: 0

Views: 1774

Answers (2)

Gauravsa
Gauravsa

Reputation: 6528

You can use the following to check:

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

If the repo has no changes, AheadBy and BehindBy are null.

Upvotes: 0

yorah
yorah

Reputation: 2673

A repo is not strictly speaking ahead or behind a remote repo. A branch, however, can be behind and/or ahead of its remote tracked branch.

The Branch class has a TrackingDetails property, which you can use to access the AheadBy/BehindBy commit count between the local branch and the tracked branch.

You can find more info by looking at this unit test for instance.

Upvotes: 2

Related Questions