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

Branch.cs « LibGit2Sharp - github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8344c998c15562b541a62286cb905703a69b17db (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System;
using System.Globalization;
using LibGit2Sharp.Core;

namespace LibGit2Sharp
{
    /// <summary>
    ///   A branch is a special kind of reference
    /// </summary>
    public class Branch : IEquatable<Branch>
    {
        private static readonly LambdaEqualityHelper<Branch> equalityHelper =
            new LambdaEqualityHelper<Branch>(new Func<Branch, object>[] {x => x.CanonicalName, x => x.Tip});

        private readonly Repository repo;

        /// <summary>
        ///   Initializes a new instance of the <see cref = "Branch" /> class.
        /// </summary>
        /// <param name = "tip">The commit which is pointed at by this Branch</param>
        /// <param name = "repo">The repo.</param>
        /// <param name = "canonicalName">The full name of the reference</param>
        internal Branch(string canonicalName, Commit tip, Repository repo)
        {
            this.repo = repo;
            CanonicalName = canonicalName;
            Tip = tip;
        }

        /// <summary>
        ///   Gets the full name of this branch.
        /// </summary>
        public string CanonicalName { get; private set; }

        /// <summary>
        ///   Gets the name of this branch.
        /// </summary>
        public string Name
        {
            get { return ShortenName(CanonicalName); }
        }

        /// <summary>
        ///   Gets a value indicating whether this instance is a remote.
        /// </summary>
        /// <value>
        ///   <c>true</c> if this instance is remote; otherwise, <c>false</c>.
        /// </value>
        public bool IsRemote
        {
            get { return IsRemoteBranch(CanonicalName); }
        }

        /// <summary>
        ///   Gets a value indicating whether this instance is current branch (HEAD) in the repository.
        /// </summary>
        /// <value>
        ///   <c>true</c> if this instance is current branch; otherwise, <c>false</c>.
        /// </value>
        public bool IsCurrentRepositoryHead
        {
            get { return repo.Refs[CanonicalName].ResolveToDirectReference() == repo.Refs["HEAD"].ResolveToDirectReference(); }
        }

        /// <summary>
        ///   Gets the commit id that this branch points to.
        /// </summary>
        public Commit Tip { get; private set; }

        /// <summary>
        ///   Gets the commits on this branch. (Starts walking from the References's target).
        /// </summary>
        public ICommitCollection Commits
        {
            get { return repo.Commits.QueryBy(new Filter{Since = this}); }
        }

        #region IEquatable<Branch> Members

        /// <summary>
        ///   Determines whether the specified <see cref = "Branch" /> is equal to the current <see cref = "Branch" />.
        /// </summary>
        /// <param name = "other">The <see cref = "Branch" /> to compare with the current <see cref = "Branch" />.</param>
        /// <returns>True if the specified <see cref = "Branch" /> is equal to the current <see cref = "Branch" />; otherwise, false.</returns>
        public bool Equals(Branch other)
        {
            return equalityHelper.Equals(this, other);
        }

        #endregion

        /// <summary>
        ///   Determines whether the specified <see cref = "Object" /> is equal to the current <see cref = "Branch" />.
        /// </summary>
        /// <param name = "obj">The <see cref = "Object" /> to compare with the current <see cref = "Branch" />.</param>
        /// <returns>True if the specified <see cref = "Object" /> is equal to the current <see cref = "Branch" />; otherwise, false.</returns>
        public override bool Equals(object obj)
        {
            return Equals(obj as Branch);
        }

        /// <summary>
        ///   Returns the hash code for this instance.
        /// </summary>
        /// <returns>A 32-bit signed integer hash code.</returns>
        public override int GetHashCode()
        {
            return equalityHelper.GetHashCode(this);
        }

        private static bool IsRemoteBranch(string canonicalName)
        {
            return canonicalName.StartsWith("refs/remotes/", StringComparison.Ordinal);
        }

        private static string ShortenName(string branchName)
        {
            if (branchName.StartsWith("refs/heads/", StringComparison.Ordinal))
            {
                return branchName.Substring("refs/heads/".Length);
            }

            if (branchName.StartsWith("refs/remotes/", StringComparison.Ordinal))
            {
                return branchName.Substring("refs/remotes/".Length);
            }

            throw new ArgumentException(string.Format(CultureInfo.InvariantCulture,"'{0}' does not look like a valid branch name.", branchName));
        }

        /// <summary>
        ///   Tests if two <see cref = "Branch" /> are equal.
        /// </summary>
        /// <param name = "left">First <see cref = "Branch" /> to compare.</param>
        /// <param name = "right">Second <see cref = "Branch" /> to compare.</param>
        /// <returns>True if the two objects are equal; false otherwise.</returns>
        public static bool operator ==(Branch left, Branch right)
        {
            return Equals(left, right);
        }

        /// <summary>
        ///   Tests if two <see cref = "Branch" /> are different.
        /// </summary>
        /// <param name = "left">First <see cref = "Branch" /> to compare.</param>
        /// <param name = "right">Second <see cref = "Branch" /> to compare.</param>
        /// <returns>True if the two objects are different; false otherwise.</returns>
        public static bool operator !=(Branch left, Branch right)
        {
            return !Equals(left, right);
        }

        /// <summary>
        ///  Returns the <see cref="CanonicalName"/>, a <see cref="String"/> representation of the current <see cref="Branch"/>.
        /// </summary>
        /// <returns>The <see cref="CanonicalName"/> that represents the current <see cref="Branch"/>.</returns>
        public override string ToString()
        {
            return CanonicalName;
        }
    }
}