Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BranchTrackingDetails.cs « LibGit2Sharp - github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c2e51cdbc8508c8d30973e04f199e4ccb941842a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Compat;

namespace LibGit2Sharp
{
    /// <summary>
    ///   Tracking information for a <see cref="Branch" />
    /// </summary>
    public class BranchTrackingDetails
    {
        private readonly Repository repo;
        private readonly Branch branch;
        private readonly Lazy<Tuple<int?, int?>> aheadBehind;
        private readonly Lazy<Commit> commonAncestor;

        /// <summary>
        ///   Needed for mocking purposes.
        /// </summary>
        protected BranchTrackingDetails()
        { }

        internal BranchTrackingDetails(Repository repo, Branch branch)
        {
            this.repo = repo;
            this.branch = branch;

            aheadBehind = new Lazy<Tuple<int?, int?>>(ResolveAheadBehind);
            commonAncestor = new Lazy<Commit>(ResolveCommonAncestor);
        }

        /// <summary>
        ///   Gets the number of commits that exist in this local branch but don't exist in the tracked one.
        ///   <para>
        ///     This property will return null if there is no tracked branch linked to this local branch.
        ///   </para>
        /// </summary>
        public virtual int? AheadBy
        {
            get { return aheadBehind.Value.Item1; }
        }

        /// <summary>
        ///   Gets the number of commits that exist in the tracked branch but don't exist in this local one.
        ///   <para>
        ///     This property will return null if there is no tracked branch linked to this local branch.
        ///   </para>
        /// </summary>
        public virtual int? BehindBy
        {
            get { return aheadBehind.Value.Item2; }
        }

        /// <summary>
        ///   Gets the common ancestor of the local branch and its tracked remote branch.
        ///   <para>
        ///     This property will return null if there is no tracked branch linked to this local branch,
        ///     or if either branch is an orphan.
        ///   </para>
        /// </summary>
        public virtual Commit CommonAncestor
        {
            get { return commonAncestor.Value; }
        }

        private Tuple<int?, int?> ResolveAheadBehind()
        {
            return branch.IsTracking
                       ? Proxy.git_graph_ahead_behind(repo.Handle, branch.TrackedBranch.Tip, branch.Tip)
                       : new Tuple<int?, int?>(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);
        }
    }
}