@@ -71,7 +71,7 @@ public virtual IEnumerable<DirectReference> ListReferences(Remote remote, Creden
71
71
72
72
using ( RemoteSafeHandle remoteHandle = Proxy . git_remote_lookup ( repository . Handle , remote . Name , true ) )
73
73
{
74
- var gitCallbacks = new GitRemoteCallbacks { version = 1 } ;
74
+ var gitCallbacks = new GitRemoteCallbacks { version = 1 } ;
75
75
76
76
if ( credentialsProvider != null )
77
77
{
@@ -107,59 +107,81 @@ public virtual IEnumerable<DirectReference> ListReferences(string url)
107
107
}
108
108
}
109
109
110
- static RemoteSafeHandle BuildRemoteSafeHandle ( RepositorySafeHandle repoHandle , Remote remote , string url )
110
+ static RemoteSafeHandle BuildRemoteSafeHandle ( RepositorySafeHandle repoHandle , Remote remote )
111
111
{
112
- Debug . Assert ( ( remote == null ) ^ ( url == null ) ) ;
112
+ Debug . Assert ( repoHandle != null && ! repoHandle . IsClosed && ! repoHandle . IsInvalid ) ;
113
+ Debug . Assert ( remote != null && remote . Name != null ) ;
113
114
114
- RemoteSafeHandle remoteHandle ;
115
+ RemoteSafeHandle remoteHandle = Proxy . git_remote_lookup ( repoHandle , remote . Name , true ) ;
116
+ Debug . Assert ( remoteHandle != null && ! ( remoteHandle . IsClosed || remoteHandle . IsInvalid ) ) ;
115
117
116
- if ( url != null )
117
- {
118
- remoteHandle = Proxy . git_remote_create_anonymous ( repoHandle , url ) ;
119
- }
120
- else
121
- {
122
- remoteHandle = Proxy . git_remote_lookup ( repoHandle , remote . Name , true ) ;
123
- }
118
+ return remoteHandle ;
119
+ }
120
+
121
+ static RemoteSafeHandle BuildRemoteSafeHandle ( RepositorySafeHandle repoHandle , string url )
122
+ {
123
+ Debug . Assert ( repoHandle != null && ! repoHandle . IsClosed && ! repoHandle . IsInvalid ) ;
124
+ Debug . Assert ( url != null ) ;
125
+
126
+ RemoteSafeHandle remoteHandle = Proxy . git_remote_create_anonymous ( repoHandle , url ) ;
127
+ Debug . Assert ( remoteHandle != null && ! ( remoteHandle . IsClosed || remoteHandle . IsInvalid ) ) ;
124
128
125
129
return remoteHandle ;
126
130
}
127
131
128
- static void DoFetch ( RepositorySafeHandle repoHandle , Remote remote , string url ,
129
- FetchOptions options , string logMessage ,
130
- IEnumerable < string > refspecs )
132
+ static void DoFetch ( RepositorySafeHandle repoHandle ,
133
+ Remote remote ,
134
+ FetchOptions options ,
135
+ string logMessage ,
136
+ IEnumerable < string > refspecs )
131
137
{
132
- if ( options == null )
138
+ using ( RemoteSafeHandle remoteHandle = BuildRemoteSafeHandle ( repoHandle , remote ) )
133
139
{
134
- options = new FetchOptions ( ) ;
140
+ DoFetch ( options , remoteHandle , logMessage , refspecs ) ;
135
141
}
142
+ }
136
143
137
- using ( RemoteSafeHandle remoteHandle = BuildRemoteSafeHandle ( repoHandle , remote , url ) )
144
+ static void DoFetch ( RepositorySafeHandle repoHandle ,
145
+ string url ,
146
+ FetchOptions options ,
147
+ string logMessage ,
148
+ IEnumerable < string > refspecs )
149
+ {
150
+ using ( RemoteSafeHandle remoteHandle = BuildRemoteSafeHandle ( repoHandle , url ) )
138
151
{
139
- var callbacks = new RemoteCallbacks ( options ) ;
140
- GitRemoteCallbacks gitCallbacks = callbacks . GenerateCallbacks ( ) ;
152
+ DoFetch ( options , remoteHandle , logMessage , refspecs ) ;
153
+ }
154
+ }
141
155
142
- // It is OK to pass the reference to the GitCallbacks directly here because libgit2 makes a copy of
143
- // the data in the git_remote_callbacks structure. If, in the future, libgit2 changes its implementation
144
- // to store a reference to the git_remote_callbacks structure this would introduce a subtle bug
145
- // where the managed layer could move the git_remote_callbacks to a different location in memory,
146
- // but libgit2 would still reference the old address.
147
- //
148
- // Also, if GitRemoteCallbacks were a class instead of a struct, we would need to guard against
149
- // GC occuring in between setting the remote callbacks and actual usage in one of the functions afterwords.
150
- var fetchOptions = new GitFetchOptions
151
- {
152
- RemoteCallbacks = gitCallbacks ,
153
- download_tags = Proxy . git_remote_autotag ( remoteHandle ) ,
154
- } ;
155
-
156
- if ( options . TagFetchMode . HasValue )
157
- {
158
- fetchOptions . download_tags = options . TagFetchMode . Value ;
159
- }
156
+ private static void DoFetch ( FetchOptions options , RemoteSafeHandle remoteHandle , string logMessage , IEnumerable < string > refspecs )
157
+ {
158
+ Debug . Assert ( remoteHandle != null && ! remoteHandle . IsClosed && ! remoteHandle . IsInvalid ) ;
159
+
160
+ options = options ?? new FetchOptions ( ) ;
161
+
162
+ var callbacks = new RemoteCallbacks ( options ) ;
163
+ GitRemoteCallbacks gitCallbacks = callbacks . GenerateCallbacks ( ) ;
164
+
165
+ // It is OK to pass the reference to the GitCallbacks directly here because libgit2 makes a copy of
166
+ // the data in the git_remote_callbacks structure. If, in the future, libgit2 changes its implementation
167
+ // to store a reference to the git_remote_callbacks structure this would introduce a subtle bug
168
+ // where the managed layer could move the git_remote_callbacks to a different location in memory,
169
+ // but libgit2 would still reference the old address.
170
+ //
171
+ // Also, if GitRemoteCallbacks were a class instead of a struct, we would need to guard against
172
+ // GC occuring in between setting the remote callbacks and actual usage in one of the functions afterwords.
173
+ var fetchOptions = new GitFetchOptions
174
+ {
175
+ RemoteCallbacks = gitCallbacks ,
176
+ download_tags = Proxy . git_remote_autotag ( remoteHandle ) ,
177
+ } ;
160
178
161
- Proxy . git_remote_fetch ( remoteHandle , refspecs , fetchOptions , logMessage ) ;
179
+ if ( options . TagFetchMode . HasValue )
180
+ {
181
+ fetchOptions . download_tags = options . TagFetchMode . Value ;
162
182
}
183
+
184
+ Proxy . git_remote_fetch ( remoteHandle , refspecs , fetchOptions , logMessage ) ;
163
185
}
164
186
165
187
/// <summary>
@@ -201,7 +223,7 @@ public virtual void Fetch(Remote remote, FetchOptions options, string logMessage
201
223
{
202
224
Ensure . ArgumentNotNull ( remote , "remote" ) ;
203
225
204
- DoFetch ( repository . Handle , remote , null , options , logMessage , new string [ 0 ] ) ;
226
+ DoFetch ( repository . Handle , remote , options , logMessage , new string [ 0 ] ) ;
205
227
}
206
228
207
229
/// <summary>
@@ -248,7 +270,7 @@ public virtual void Fetch(Remote remote, IEnumerable<string> refspecs, FetchOpti
248
270
Ensure . ArgumentNotNull ( remote , "remote" ) ;
249
271
Ensure . ArgumentNotNull ( refspecs , "refspecs" ) ;
250
272
251
- DoFetch ( repository . Handle , remote , null , options , logMessage , refspecs ) ;
273
+ DoFetch ( repository . Handle , remote , options , logMessage , refspecs ) ;
252
274
}
253
275
254
276
/// <summary>
@@ -307,7 +329,7 @@ public virtual void Fetch(
307
329
Ensure . ArgumentNotNull ( url , "url" ) ;
308
330
Ensure . ArgumentNotNull ( refspecs , "refspecs" ) ;
309
331
310
- DoFetch ( repository . Handle , null , url , options , logMessage , refspecs ) ;
332
+ DoFetch ( repository . Handle , url , options , logMessage , refspecs ) ;
311
333
}
312
334
313
335
/// <summary>
0 commit comments