1
- using System . Collections ;
1
+ using System ;
2
+ using System . Collections ;
2
3
using System . Collections . Generic ;
3
4
using System . Diagnostics ;
4
5
using System . Globalization ;
5
6
using System . Linq ;
6
7
using LibGit2Sharp . Core ;
8
+ using LibGit2Sharp . Core . Handles ;
7
9
8
10
namespace LibGit2Sharp
9
11
{
@@ -39,10 +41,10 @@ public virtual Submodule this[string name]
39
41
{
40
42
Ensure . ArgumentNotNullOrEmptyString ( name , "name" ) ;
41
43
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 ) ) ) ;
46
48
}
47
49
}
48
50
@@ -68,13 +70,33 @@ IEnumerator IEnumerable.GetEnumerator()
68
70
69
71
internal bool TryStage ( string relativePath , bool writeIndex )
70
72
{
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 ) )
72
86
{
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
+ }
75
98
76
- Proxy . git_submodule_add_to_index ( handle , writeIndex ) ;
77
- return true ;
99
+ return default ( T ) ;
78
100
}
79
101
}
80
102
0 commit comments