-
Notifications
You must be signed in to change notification settings - Fork 896
/
Copy pathSubmodule.cs
181 lines (155 loc) · 6.83 KB
/
Submodule.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
using System;
using System.Diagnostics;
using System.Globalization;
using LibGit2Sharp.Core;
namespace LibGit2Sharp
{
/// <summary>
/// A Submodule.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class Submodule : IEquatable<Submodule>, IBelongToARepository
{
private static readonly LambdaEqualityHelper<Submodule> equalityHelper =
new LambdaEqualityHelper<Submodule>(x => x.Name, x => x.HeadCommitId);
private readonly Repository repo;
private readonly string name;
private readonly string path;
private readonly string url;
private string branch;
private readonly ILazy<ObjectId> headCommitId;
private readonly ILazy<ObjectId> indexCommitId;
private readonly ILazy<ObjectId> workdirCommitId;
private readonly ILazy<SubmoduleRecurse> fetchRecurseSubmodulesRule;
private readonly ILazy<SubmoduleIgnore> ignoreRule;
private readonly ILazy<SubmoduleUpdate> updateRule;
/// <summary>
/// Needed for mocking purposes.
/// </summary>
protected Submodule()
{ }
internal Submodule(Repository repo, string name, string path, string url, string branch)
{
this.repo = repo;
this.name = name;
this.path = path;
this.url = url;
this.branch = branch;
var commitIds = new SubmoduleLazyGroup(repo, name);
headCommitId = commitIds.AddLazy(Proxy.git_submodule_head_id);
indexCommitId = commitIds.AddLazy(Proxy.git_submodule_index_id);
workdirCommitId = commitIds.AddLazy(Proxy.git_submodule_wd_id);
var rules = new SubmoduleLazyGroup(repo, name);
fetchRecurseSubmodulesRule = rules.AddLazy(Proxy.git_submodule_fetch_recurse_submodules);
ignoreRule = rules.AddLazy(Proxy.git_submodule_ignore);
updateRule = rules.AddLazy(Proxy.git_submodule_update_strategy);
}
/// <summary>
/// The name of the submodule.
/// </summary>
public virtual string Name { get { return name; } }
/// <summary>
/// The path of the submodule.
/// </summary>
public virtual string Path { get { return path; } }
/// <summary>
/// The URL of the submodule.
/// </summary>
public virtual string Url { get { return url; } }
/// <summary>
/// The name of the remote branch
/// </summary>
public virtual string Branch { get { return branch; } set { SetRemoteBranch(value); } }
/// <summary>
/// The commit ID for this submodule in the current HEAD tree.
/// </summary>
public virtual ObjectId HeadCommitId { get { return headCommitId.Value; } }
/// <summary>
/// The commit ID for this submodule in the index.
/// </summary>
public virtual ObjectId IndexCommitId { get { return indexCommitId.Value; } }
/// <summary>
/// The commit ID for this submodule in the current working directory.
/// </summary>
public virtual ObjectId WorkDirCommitId { get { return workdirCommitId.Value; } }
/// <summary>
/// The fetchRecurseSubmodules rule for the submodule.
///
/// Note that at this time, LibGit2Sharp does not honor this setting and the
/// fetch functionality current ignores submodules.
/// </summary>
public virtual SubmoduleRecurse FetchRecurseSubmodulesRule { get { return fetchRecurseSubmodulesRule.Value; } }
/// <summary>
/// The ignore rule of the submodule.
/// </summary>
public virtual SubmoduleIgnore IgnoreRule { get { return ignoreRule.Value; } }
/// <summary>
/// The update rule of the submodule.
/// </summary>
public virtual SubmoduleUpdate UpdateRule { get { return updateRule.Value; } }
/// <summary>
/// Retrieves the state of this submodule in the working directory compared to the staging area and the latest commit.
/// </summary>
/// <returns>The <see cref="SubmoduleStatus"/> of this submodule.</returns>
public virtual SubmoduleStatus RetrieveStatus()
{
return Proxy.git_submodule_status(repo.Handle, Name);
}
/// <summary>
/// Determines whether the specified <see cref="Object"/> is equal to the current <see cref="Submodule"/>.
/// </summary>
/// <param name="obj">The <see cref="Object"/> to compare with the current <see cref="Submodule"/>.</param>
/// <returns>True if the specified <see cref="Object"/> is equal to the current <see cref="Submodule"/>; otherwise, false.</returns>
public override bool Equals(object obj)
{
return Equals(obj as Submodule);
}
/// <summary>
/// Determines whether the specified <see cref="Submodule"/> is equal to the current <see cref="Submodule"/>.
/// </summary>
/// <param name="other">The <see cref="Submodule"/> to compare with the current <see cref="Submodule"/>.</param>
/// <returns>True if the specified <see cref="Submodule"/> is equal to the current <see cref="Submodule"/>; otherwise, false.</returns>
public bool Equals(Submodule other)
{
return equalityHelper.Equals(this, other);
}
/// <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);
}
/// <summary>
/// Returns the <see cref="Name"/>, a <see cref="String"/> representation of the current <see cref="Submodule"/>.
/// </summary>
/// <returns>The <see cref="Name"/> that represents the current <see cref="Submodule"/>.</returns>
public override string ToString()
{
return Name;
}
private string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture, "{0} => {1}", Name, Url);
}
}
private void SetRemoteBranch(string branch)
{
using (var handle = Proxy.git_submodule_lookup(repo.Handle, name))
{
if (handle == null)
{
throw new NotFoundException("Submodule lookup failed for '{0}'.",
name);
}
Proxy.git_submodule_set_branch(repo.Handle, name, branch);
Proxy.git_submodule_reload(handle);
this.branch = Proxy.git_submodule_branch(handle);
}
}
IRepository IBelongToARepository.Repository { get { return repo; } }
}
}