forked from libgit2/libgit2sharp
/
Branch.cs
193 lines (169 loc) · 6.73 KB
/
Branch.cs
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System;
using System.Globalization;
using System.Linq;
using LibGit2Sharp.Core.Compat;
namespace LibGit2Sharp
{
/// <summary>
/// A branch is a special kind of reference
/// </summary>
public class Branch : ReferenceWrapper<Commit>
{
private readonly Lazy<Branch> trackedBranch;
/// <summary>
/// Needed for mocking purposes.
/// </summary>
protected Branch()
{ }
/// <summary>
/// Initializes a new instance of the <see cref = "Branch" /> class.
/// </summary>
/// <param name = "repo">The repo.</param>
/// <param name = "reference">The reference.</param>
/// <param name = "canonicalName">The full name of the reference</param>
internal Branch(Repository repo, Reference reference, string canonicalName)
: this(repo, reference, _ => canonicalName)
{
}
/// <summary>
/// Initializes a new instance of an orphaned <see cref = "Branch" /> class.
/// <para>
/// This <see cref = "Branch" /> instance will point to no commit.
/// </para>
/// </summary>
/// <param name = "repo">The repo.</param>
/// <param name = "reference">The reference.</param>
internal Branch(Repository repo, Reference reference)
: this(repo, reference, r => r.TargetIdentifier)
{
}
private Branch(Repository repo, Reference reference, Func<Reference, string> canonicalNameSelector)
: base(repo, reference, canonicalNameSelector)
{
trackedBranch = new Lazy<Branch>(ResolveTrackedBranch);
}
/// <summary>
/// Gets the <see cref = "TreeEntry" /> pointed at by the <paramref name = "relativePath" /> in the <see cref = "Tip" />.
/// </summary>
/// <param name = "relativePath">The relative path to the <see cref = "TreeEntry" /> from the <see cref = "Tip" /> working directory.</param>
/// <returns><c>null</c> if nothing has been found, the <see cref = "TreeEntry" /> otherwise.</returns>
public virtual TreeEntry this[string relativePath]
{
get
{
if (Tip == null)
{
return null;
}
return Tip[relativePath];
}
}
/// <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 virtual bool IsRemote
{
get { return IsRemoteBranch(CanonicalName); }
}
/// <summary>
/// Gets the remote branch which is connected to this local one.
/// </summary>
public virtual Branch TrackedBranch
{
get { return trackedBranch.Value; }
}
/// <summary>
/// Determines if this local branch is connected to a remote one.
/// </summary>
public virtual bool IsTracking
{
get { return TrackedBranch != null; }
}
/// <summary>
/// Gets the number of commits, starting from the <see cref="Tip"/>, that have been performed on this local branch and aren't known from the remote one.
/// </summary>
public virtual int AheadBy
{
get { return IsTracking ? repo.Commits.QueryBy(new Filter { Since = Tip, Until = TrackedBranch }).Count() : 0; }
}
/// <summary>
/// Gets the number of commits that exist in the remote branch, on top of <see cref="Tip"/>, and aren't known from the local one.
/// </summary>
public virtual int BehindBy
{
get { return IsTracking ? repo.Commits.QueryBy(new Filter { Since = TrackedBranch, Until = Tip }).Count() : 0; }
}
/// <summary>
/// Gets a value indicating whether this instance is current branch (HEAD) in the repository.
/// </summary>
/// <value>
/// <c>true</c> if this instance is the current branch; otherwise, <c>false</c>.
/// </value>
public virtual bool IsCurrentRepositoryHead
{
get { return repo.Head == this; }
}
/// <summary>
/// Gets the <see cref="Commit"/> that this branch points to.
/// </summary>
public virtual Commit Tip
{
get { return TargetObject; }
}
/// <summary>
/// Gets the commits on this branch. (Starts walking from the References's target).
/// </summary>
public virtual ICommitLog Commits
{
get { return repo.Commits.QueryBy(new Filter { Since = this }); }
}
private Branch ResolveTrackedBranch()
{
var trackedRemote = repo.Config.Get<string>("branch", Name, "remote", null);
if (trackedRemote == null)
{
return null;
}
var trackedRefName = repo.Config.Get<string>("branch", Name, "merge", null);
if (trackedRefName == null)
{
return null;
}
var remoteRefName = ResolveTrackedReference(trackedRemote, trackedRefName);
return repo.Branches[remoteRefName];
}
private static string ResolveTrackedReference(string trackedRemote, string trackedRefName)
{
if (trackedRemote == ".")
{
return trackedRefName;
}
//TODO: To be replaced by native libgit2 git_branch_tracked_reference() when available.
return trackedRefName.Replace("refs/heads/", string.Concat("refs/remotes/", trackedRemote, "/"));
}
private static bool IsRemoteBranch(string canonicalName)
{
return canonicalName.StartsWith("refs/remotes/", StringComparison.Ordinal);
}
/// <summary>
/// Returns the friendly shortened name from a canonical name.
/// </summary>
/// <param name="canonicalName">The canonical name to shorten.</param>
/// <returns></returns>
protected override string Shorten(string canonicalName)
{
if (canonicalName.StartsWith("refs/heads/", StringComparison.Ordinal))
{
return canonicalName.Substring("refs/heads/".Length);
}
if (canonicalName.StartsWith("refs/remotes/", StringComparison.Ordinal))
{
return canonicalName.Substring("refs/remotes/".Length);
}
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "'{0}' does not look like a valid branch name.", canonicalName));
}
}
}