using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Compat;
namespace LibGit2Sharp
{
///
/// Tracking information for a
///
public class BranchTrackingDetails
{
private readonly Repository repo;
private readonly Branch branch;
private readonly Lazy> aheadBehind;
private readonly Lazy commonAncestor;
///
/// Needed for mocking purposes.
///
protected BranchTrackingDetails()
{ }
internal BranchTrackingDetails(Repository repo, Branch branch)
{
this.repo = repo;
this.branch = branch;
aheadBehind = new Lazy>(ResolveAheadBehind);
commonAncestor = new Lazy(ResolveCommonAncestor);
}
///
/// Gets the number of commits that exist in this local branch but don't exist in the tracked one.
///
/// This property will return null if there is no tracked branch linked to this local branch.
///
///
public virtual int? AheadBy
{
get { return aheadBehind.Value.Item1; }
}
///
/// Gets the number of commits that exist in the tracked branch but don't exist in this local one.
///
/// This property will return null if there is no tracked branch linked to this local branch.
///
///
public virtual int? BehindBy
{
get { return aheadBehind.Value.Item2; }
}
///
/// Gets the common ancestor of the local branch and its tracked remote branch.
///
/// This property will return null if there is no tracked branch linked to this local branch,
/// or if either branch is an orphan.
///
///
public virtual Commit CommonAncestor
{
get { return commonAncestor.Value; }
}
private Tuple ResolveAheadBehind()
{
return branch.IsTracking
? Proxy.git_graph_ahead_behind(repo.Handle, branch.TrackedBranch.Tip, branch.Tip)
: new Tuple(null, null);
}
private Commit ResolveCommonAncestor()
{
if (!branch.IsTracking)
{
return null;
}
if (branch.Tip == null || branch.TrackedBranch.Tip == null)
{
return null;
}
return repo.Commits.FindCommonAncestor(branch.Tip, branch.TrackedBranch.Tip);
}
}
}