Skip to content

Commit 4d90f85

Browse files
committed
Use LazyGroup
1 parent 716190a commit 4d90f85

File tree

3 files changed

+51
-39
lines changed

3 files changed

+51
-39
lines changed

LibGit2Sharp/Core/SubmoduleLazyGroup.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Globalization;
23
using LibGit2Sharp.Core.Handles;
34

45
namespace LibGit2Sharp.Core
@@ -17,6 +18,13 @@ protected override void EvaluateInternal(Action<SubmoduleSafeHandle> evaluator)
1718
{
1819
using (var handle = Proxy.git_submodule_lookup(repo.Handle, name))
1920
{
21+
if (handle == null)
22+
{
23+
throw new LibGit2SharpException(string.Format(
24+
CultureInfo.InvariantCulture,
25+
"Submodule lookup failed for '{0}'.", name));
26+
}
27+
2028
evaluator(handle);
2129
}
2230
}

LibGit2Sharp/Submodule.cs

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
using System.Diagnostics;
33
using System.Globalization;
44
using LibGit2Sharp.Core;
5-
using LibGit2Sharp.Core.Compat;
6-
using LibGit2Sharp.Core.Handles;
75

86
namespace LibGit2Sharp
97
{
@@ -16,22 +14,39 @@ public class Submodule : IEquatable<Submodule>
1614
private static readonly LambdaEqualityHelper<Submodule> equalityHelper =
1715
new LambdaEqualityHelper<Submodule>(x => x.Name, x => x.HeadCommitId);
1816

19-
private readonly SubmoduleSafeHandle handle;
17+
private readonly Repository repo;
2018
private readonly string name;
21-
private readonly Lazy<string> lazyPath;
19+
private readonly string path;
20+
private readonly string url;
21+
private readonly ILazy<ObjectId> indexCommitId;
22+
private readonly ILazy<ObjectId> headCommitId;
23+
private readonly ILazy<ObjectId> workdirCommitId;
24+
private readonly ILazy<bool> fetchRecurseSubmodulesRule;
25+
private readonly ILazy<SubmoduleIgnore> ignoreRule;
26+
private readonly ILazy<SubmoduleUpdate> updateRule;
2227

2328
/// <summary>
2429
/// Needed for mocking purposes.
2530
/// </summary>
2631
protected Submodule()
2732
{ }
2833

29-
private Submodule(SubmoduleSafeHandle handle, string name)
34+
internal Submodule(Repository repo, string name, string path, string url)
3035
{
31-
this.handle = handle;
36+
this.repo = repo;
3237
this.name = name;
33-
34-
lazyPath = new Lazy<string>(() => Proxy.git_submodule_path(handle));
38+
this.path = path;
39+
this.url = url;
40+
41+
var commitIds = new SubmoduleLazyGroup(repo, name);
42+
indexCommitId = commitIds.AddLazy(Proxy.git_submodule_index_id);
43+
headCommitId = commitIds.AddLazy(Proxy.git_submodule_head_id);
44+
workdirCommitId = commitIds.AddLazy(Proxy.git_submodule_wd_id);
45+
46+
var rules = new SubmoduleLazyGroup(repo, name);
47+
fetchRecurseSubmodulesRule = rules.AddLazy(Proxy.git_submodule_fetch_recurse_submodules);
48+
ignoreRule = rules.AddLazy(Proxy.git_submodule_ignore);
49+
updateRule = rules.AddLazy(Proxy.git_submodule_update);
3550
}
3651

3752
/// <summary>
@@ -42,65 +57,56 @@ private Submodule(SubmoduleSafeHandle handle, string name)
4257
/// <summary>
4358
/// The path of the submodule.
4459
/// </summary>
45-
public virtual string Path { get { return lazyPath.Value; } }
60+
public virtual string Path { get { return path; } }
4661

4762
/// <summary>
48-
/// The commit ID for this submodule in the index.
63+
/// The URL of the submodule.
4964
/// </summary>
50-
public virtual ObjectId IndexCommitId { get { return Proxy.git_submodule_index_id(handle); } }
65+
public virtual string Url { get { return url; } }
5166

5267
/// <summary>
53-
/// The commit ID for this submodule in the current HEAD tree.
68+
/// The commit ID for this submodule in the index.
5469
/// </summary>
55-
public virtual ObjectId HeadCommitId { get { return Proxy.git_submodule_head_id(handle); } }
70+
public virtual ObjectId IndexCommitId { get { return indexCommitId.Value; } }
5671

5772
/// <summary>
58-
/// The commit ID for this submodule in the current working directory.
73+
/// The commit ID for this submodule in the current HEAD tree.
5974
/// </summary>
60-
public virtual ObjectId WorkDirCommitId { get { return Proxy.git_submodule_wd_id(handle); } }
75+
public virtual ObjectId HeadCommitId { get { return headCommitId.Value; } }
6176

6277
/// <summary>
63-
/// The URL of the submodule.
78+
/// The commit ID for this submodule in the current working directory.
6479
/// </summary>
65-
public virtual string Url
66-
{
67-
get { return Proxy.git_submodule_url(handle); }
68-
}
80+
public virtual ObjectId WorkDirCommitId { get { return workdirCommitId.Value; } }
6981

7082
/// <summary>
7183
/// The fetchRecurseSubmodules rule for the submodule.
7284
///
7385
/// Note that at this time, LibGit2Sharp does not honor this setting and the
7486
/// fetch functionality current ignores submodules.
7587
/// </summary>
76-
public virtual bool FetchRecurseSubmodulesRule
77-
{
78-
get { return Proxy.git_submodule_fetch_recurse_submodules(handle); }
79-
}
88+
public virtual bool FetchRecurseSubmodulesRule { get { return fetchRecurseSubmodulesRule.Value; } }
8089

8190
/// <summary>
8291
/// The ignore rule of the submodule.
8392
/// </summary>
84-
public virtual SubmoduleIgnore IgnoreRule
85-
{
86-
get { return Proxy.git_submodule_ignore(handle); }
87-
}
93+
public virtual SubmoduleIgnore IgnoreRule { get { return ignoreRule.Value; } }
8894

8995
/// <summary>
9096
/// The update rule of the submodule.
9197
/// </summary>
92-
public virtual SubmoduleUpdate UpdateRule
93-
{
94-
get { return Proxy.git_submodule_update(handle); }
95-
}
98+
public virtual SubmoduleUpdate UpdateRule { get { return updateRule.Value; } }
9699

97100
/// <summary>
98101
/// Retrieves the state of this submodule in the working directory compared to the staging area and the latest commmit.
99102
/// </summary>
100103
/// <returns></returns>
101104
public virtual SubmoduleStatus RetrieveStatus()
102105
{
103-
return Proxy.git_submodule_status(handle);
106+
using (var handle = Proxy.git_submodule_lookup(repo.Handle, Name))
107+
{
108+
return Proxy.git_submodule_status(handle);
109+
}
104110
}
105111

106112
/// <summary>
@@ -141,11 +147,6 @@ public override string ToString()
141147
return Name;
142148
}
143149

144-
internal static Submodule BuildFromPtr(SubmoduleSafeHandle handle, string name)
145-
{
146-
return handle == null ? null : new Submodule(handle, name);
147-
}
148-
149150
private string DebuggerDisplay
150151
{
151152
get

LibGit2Sharp/SubmoduleCollection.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ public virtual Submodule this[string name]
3939
{
4040
Ensure.ArgumentNotNullOrEmptyString(name, "name");
4141

42-
return Submodule.BuildFromPtr(repo.RegisterForCleanup(Proxy.git_submodule_lookup(repo.Handle, name)), name);
42+
using (var handle = Proxy.git_submodule_lookup(repo.Handle, name))
43+
{
44+
return handle == null ? null : new Submodule(repo, name, Proxy.git_submodule_path(handle), Proxy.git_submodule_url(handle));
45+
}
4346
}
4447
}
4548

0 commit comments

Comments
 (0)