8000 spike: remove handle from submodule · libgit2/libgit2sharp@0662c27 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0662c27

Browse files
committed
spike: remove handle from submodule
1 parent 2f7a40a commit 0662c27

File tree

8 files changed

+76
-69
lines changed

8 files changed

+76
-69
lines changed

LibGit2Sharp.Tests/SubmoduleFixture.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -107,27 +107,6 @@ public void CanEnumerateRepositorySubmodules()
107107
}
108108
}
109109

110-
[Theory]
111-
[InlineData("sm_changed_head")]
112-
[InlineData("sm_changed_head/")]
113-
public void CanStageChangeInSubmoduleViaSubmoduleStage(string submodulePath)
114-
{
115-
var path = BuildTemporarySubmoduleClone();
116-
117-
using (var repo = new Repository(path))
118-
{
119-
var submodule = repo.Submodules[submodulePath];
120-
121-
var statusBefore = submodule.RetrieveStatus();
122-
Assert.Equal(SubmoduleStatus.WorkDirModified, statusBefore & SubmoduleStatus.WorkDirModified);
123-
124-
submodule.Stage();
125-
126-
var statusAfter = submodule.RetrieveStatus();
127-
Assert.Equal(SubmoduleStatus.IndexModified, statusAfter & SubmoduleStatus.IndexModified);
128-
}
129-
}
130-
131110
[Theory]
132111
[InlineData("sm_changed_head")]
133112
// [InlineData("sm_changed_head/")] // AmbiguousSpecificationException - Do we want to support this?

LibGit2Sharp/Core/GitObjectLazyGroup.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@ namespace LibGit2Sharp.Core
55
{
66
internal class GitObjectLazyGroup : LazyGroup<GitObjectSafeHandle>
77
{
8-
private readonly Repository repo;
98
private readonly ObjectId id;
109

1110
public GitObjectLazyGroup(Repository repo, ObjectId id)
11+
: base(repo)
1212
{
13-
this.repo = repo;
1413
this.id = id;
1514
}
1615

1716
protected override void EvaluateInternal(Action<GitObjectSafeHandle> evaluator)
1817
{
19-
using (var osw = new ObjectSafeWrapper(id, repo.Handle))
18+
using (var osw = new ObjectSafeWrapper(id, Repo.Handle))
2019
{
2120
evaluator(osw.ObjectPtr);
2221
}

LibGit2Sharp/Core/LazyGroup.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ internal abstract class LazyGroup<T>
99
private readonly IList<IEvaluator<T>> evaluators = new List<IEvaluator<T>>();
1010
private readonly object @lock = new object();
1111
private bool evaluated;
12+
private Repository repo;
13+
14+
protected LazyGroup(Repository repo)
15+
{
16+
this.repo = repo;
17+
}
18+
19+
protected Repository Repo
20+
{
21+
get { return repo; }
22+
}
1223

1324
public ILazy<TResult> AddLazy<TResult>(Func<T, TResult> func)
1425
{
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using LibGit2Sharp.Core.Handles;
3+
4+
namespace LibGit2Sharp.Core
5+
{
6+
internal class SubmoduleLazyGroup : LazyGroup<SubmoduleSafeHandle>
7+
{
8+
private readonly string name;
9+
10+
public SubmoduleLazyGroup(Repository repo, string name)
11+
: base(repo)
12+
{
13+
this.name = name;
14+
}
15+
16+
protected override void EvaluateInternal(Action<SubmoduleSafeHandle> evaluator)
17+
{
18+
using (var handle = Proxy.git_submodule_lookup(Repo.Handle, name))
19+
{
20+
evaluator(handle);
21+
}
22+
}
23+
}
24+
}

LibGit2Sharp/Index.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,10 @@ public virtual void Stage(IEnumerable<string> paths)
175175
var submodule = repo.Submodules[relativePath];
176176
if (submodule != null)
177177
{
178-
submodule.Stage();
178+
using (var handle = Proxy.git_submodule_lookup(repo.Handle, submodule.Name))
179+
{
180+
Proxy.git_submodule_add_to_index(handle, true);
181+
}
179182
}
180183
else
181184
{

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
<Compile Include="Core\Handles\GitFetchSpecHandle.cs" />
7373
<Compile Include="Core\PathCase.cs" />
7474
<Compile Include="Core\Handles\SubmoduleSafeHandle.cs" />
75+
<Compile Include="Core\SubmoduleLazyGroup.cs" />
7576
<Compile Include="Network.cs" />
7677
<Compile Include="Core\GitRemoteHead.cs" />
7778
<Compile Include="Stash.cs" />

LibGit2Sharp/Submodule.cs

Lines changed: 33 additions & 43 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
{
@@ -13,25 +11,43 @@ namespace LibGit2Sharp
1311
[DebuggerDisplay("{DebuggerDisplay,nq}")]
1412
public class Submodule : IEquatable<Submodule>
1513
{
14+
private readonly SubmoduleLazyGroup group;
15+
1616
private static readonly LambdaEqualityHelper<Submodule> equalityHelper =
1717
new LambdaEqualityHelper<Submodule>(x => x.Name, x => x.HeadCommitId);
1818

19-
private readonly SubmoduleSafeHandle handle;
2019
private readonly string name;
21-
private readonly Lazy<string> lazyPath;
20+
private readonly ILazy<string> lazyPath;
21+
private readonly ILazy<ObjectId> lazyIndexCommitId;
22+
private readonly ILazy<ObjectId> lazyHeadCommitId;
23+
private readonly ILazy<ObjectId> lazyWorkdirCommitId;
24+
private readonly ILazy<string> lazyUrl;
25+
private readonly ILazy<bool> lazyFetchRecurseSubmodulesRule;
26+
private readonly ILazy<SubmoduleIgnore> lazyIgnoreRule;
27+
private readonly ILazy<SubmoduleUpdate> lazyUpdateRule;
28+
private readonly ILazy<SubmoduleStatus> lazyStatus;
2229

2330
/// <summary>
2431
/// Needed for mocking purposes.
2532
/// </summary>
2633
protected Submodule()
2734
{ }
2835

29-
private Submodule(SubmoduleSafeHandle handle, string name)
36+
internal Submodule(Repository repo, string name)
3037
{
31-
this.handle = handle;
3238
this.name = name;
3339

34-
lazyPath = new Lazy<string>(() => Proxy.git_submodule_path(handle));
40+
group = new SubmoduleLazyGroup(repo, name);
41+
lazyPath = group.AddLazy(Proxy.git_submodule_path);
42+
lazyIndexCommitId = group.AddLazy(Proxy.git_submodule_index_id);
43+
lazyHeadCommitId = group.AddLazy(Proxy.git_submodule_head_id);
44+
lazyWorkdirCommitId = group.AddLazy(Proxy.git_submodule_wd_id);
45+
lazyUrl = group.AddLazy(Proxy.git_submodule_url);
46+
47+
lazyFetchRecurseSubmodulesRule = group.AddLazy(Proxy.git_submodule_fetch_recurse_submodules);
48+
lazyIgnoreRule = group.AddLazy(Proxy.git_submodule_ignore);
49+
lazyUpdateRule = group.AddLazy(Proxy.git_submodule_update);
50+
lazyStatus = group.AddLazy(Proxy.git_submodule_status);
3551
}
3652

3753
/// <summary>
@@ -47,73 +63,52 @@ private Submodule(SubmoduleSafeHandle handle, string name)
4763
/// <summary>
4864
/// The commit ID for this submodule in the index.
4965
/// </summary>
50-
public virtual ObjectId IndexCommitId { get { return Proxy.git_submodule_index_id(handle); } }
66+
public virtual ObjectId IndexCommitId { get { return lazyIndexCommitId.Value; } }
5167

5268
/// <summary>
5369
/// The commit ID for this submodule in the current HEAD tree.
5470
/// </summary>
55-
public virtual ObjectId HeadCommitId { get { return Proxy.git_submodule_head_id(handle); } }
71+
public virtual ObjectId HeadCommitId { get { return lazyHeadCommitId.Value; } }
5672

5773
/// <summary>
5874
/// The commit ID for this submodule in the current working directory.
5975
/// </summary>
60-
public virtual ObjectId WorkDirCommitId { get { return Proxy.git_submodule_wd_id(handle); } }
76+
public virtual ObjectId WorkDirCommitId { get { return lazyWorkdirCommitId.Value; } }
6177

6278
/// <summary>
6379
/// The URL of the submodule.
6480
/// </summary>
65-
public virtual string Url
66-
{
67-
get { return Proxy.git_submodule_url(handle); }
68-
}
81+
public virtual string Url { get { return lazyUrl.Value; } }
6982

7083
/// <summary>
7184
/// The fetchRecurseSubmodules rule for the submodule.
7285
///
7386
/// Note that at this time, LibGit2Sharp does not honor this setting and the
7487
/// fetch functionality current ignores submodules.
7588
/// </summary>
76-
public virtual bool FetchRecurseSubmodules
89+
public virtual bool FetchRecurseSubmodulesRule
7790
{
78-
get { return Proxy.git_submodule_fetch_recurse_submodules(handle); }
91+
get { return lazyFetchRecurseSubmodulesRule.Value; }
7992
}
8093

8194
/// <summary>
8295
/// The ignore rule of the submodule.
8396
/// </summary>
84-
public virtual SubmoduleIgnore Ignore
85-
{
86-
get { return Proxy.git_submodule_ignore(handle); }
87-
}
97+
public virtual SubmoduleIgnore IgnoreRule { get { return lazyIgnoreRule.Value; } }
8898

8999
/// <summary>
90100
/// The update rule of the submodule.
91101
/// </summary>
92-
public virtual SubmoduleUpdate Update
93-
{
94-
get { return Proxy.git_submodule_update(handle); }
95-
}
96-
97-
/// <summary>
98-
/// Add current submodule HEAD commit to index of superproject.
99-
/// </summary>
100-
public virtual void Stage()
101-
{
102-
Stage(true);
103-
}
104-
105-
internal virtual void Stage(bool writeIndex)
106-
{
107-
Proxy.git_submodule_add_to_index(handle, writeIndex);
108-
}
102+
public virtual SubmoduleUpdate UpdateRule { get { return lazyUpdateRule.Value; } }
109103

110104
/// <summary>
111105
/// Retrieves the state of this submodule in the working directory compared to the staging area and the latest commmit.
112106
/// </summary>
113107
/// <returns></returns>
114108
public virtual SubmoduleStatus RetrieveStatus()
115109
{
116-
return Proxy.git_submodule_status(handle);
110+
// Should be a property? Should it be dynamic?
111+
return lazyStatus.Value;
117112
}
118113

119114
/// <summary>
@@ -154,11 +149,6 @@ public override string ToString()
154149
return Name;
155150
}
156151

157-
internal static Submodule BuildFromPtr(SubmoduleSafeHandle handle, string name)
158-
{
159-
return handle == null ? null : new Submodule(handle, name);
160-
}
161-
162152
private string DebuggerDisplay
163153
{
164154
get

LibGit2Sharp/SubmoduleCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ 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+
return new Submodule(repo, name);
4343
}
4444
}
4545

0 commit comments

Comments
 (0)
0