Skip to content

Commit 5de5182

Browse files
committed
Introduce SubmoduleCollection.Lookup()
1 parent 7b21ffb commit 5de5182

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed
Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Globalization;
32
using LibGit2Sharp.Core.Handles;
43

54
namespace LibGit2Sharp.Core
@@ -16,17 +15,11 @@ public SubmoduleLazyGroup(Repository repo, string name)
1615

1716
protected override void EvaluateInternal(Action<SubmoduleSafeHandle> evaluator)
1817
{
19-
using (var handle = Proxy.git_submodule_lookup(repo.Handle, name))
20-
{
21-
if (handle == null)
22-
{
23-
throw new LibGit2SharpException(string.Format(
24-
CultureInfo.InvariantCulture,
25-
"Submodule lookup failed for '{0}'.", name));
26-
}
27-
28-
evaluator(handle);
29-
}
18+
repo.Submodules.Lookup(name, handle =>
19+
{
20+
evaluator(handle);
21+
return default(object);
22+
}, true);
3023
}
3124
}
3225
}

LibGit2Sharp/SubmoduleCollection.cs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
using System.Collections;
1+
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.Diagnostics;
45
using System.Globalization;
56
using System.Linq;
67
using LibGit2Sharp.Core;
8+
using LibGit2Sharp.Core.Handles;
79

810
namespace LibGit2Sharp
911
{
@@ -39,10 +41,10 @@ public virtual Submodule this[string name]
3941
{
4042
Ensure.ArgumentNotNullOrEmptyString(name, "name");
4143

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-
}
44+
return Lookup(name, handle =>
45+
new Submodule(repo, name,
46+
Proxy.git_submodule_path(handle),
47+
Proxy.git_submodule_url(handle)));
4648
}
4749
}
4850

@@ -68,13 +70,33 @@ IEnumerator IEnumerable.GetEnumerator()
6870

6971
internal bool TryStage(string relativePath, bool writeIndex)
7072
{
71-
using (var handle = Proxy.git_submodule_lookup(repo.Handle, relativePath))
73+
return Lookup(relativePath, handle =>
74+
{
75+
if (handle == null)
76+
return false;
77+
78+
Proxy.git_submodule_add_to_index(handle, writeIndex);
79+
return true;
80+
});
81+
}
82+
83+
internal T Lookup<T>(string name, Func<SubmoduleSafeHandle, T> selector, bool throwIfNotFound = false)
84+
{
85+
using (var handle = Proxy.git_submodule_lookup(repo.Handle, name))
7286
{
73-
if (handle == null)
74-
return false;
87+
if (handle != null)
88+
{
89+
return selector(handle);
90+
}
91+
92+
if (throwIfNotFound)
93+
{
94+
throw new LibGit2SharpException(string.Format(
95+
CultureInfo.InvariantCulture,
96+
"Submodule lookup failed for '{0}'.", name));
97+
}
7598

76-
Proxy.git_submodule_add_to_index(handle, writeIndex);
77-
return true;
99+
return default(T);
78100
}
79101
}
80102

0 commit comments

Comments
 (0)