@@ -71,7 +71,7 @@ public virtual IEnumerable<DirectReference> ListReferences(Remote remote, Creden
7171
7272 using ( RemoteSafeHandle remoteHandle = Proxy . git_remote_lookup ( repository . Handle , remote . Name , true ) )
7373 {
74- var gitCallbacks = new GitRemoteCallbacks { version = 1 } ;
74+ var gitCallbacks = new GitRemoteCallbacks { version = 1 } ;
7575
7676 if ( credentialsProvider != null )
7777 {
@@ -107,59 +107,81 @@ public virtual IEnumerable<DirectReference> ListReferences(string url)
107107 }
108108 }
109109
110- static RemoteSafeHandle BuildRemoteSafeHandle ( RepositorySafeHandle repoHandle , Remote remote , string url )
110+ static RemoteSafeHandle BuildRemoteSafeHandle ( RepositorySafeHandle repoHandle , Remote remote )
111111 {
112- Debug . Assert ( ( remote == null ) ^ ( url == null ) ) ;
112+ Debug . Assert ( repoHandle != null && ! repoHandle . IsClosed && ! repoHandle . IsInvalid ) ;
113+ Debug . Assert ( remote != null && remote . Name != null ) ;
113114
114- RemoteSafeHandle remoteHandle ;
115+ RemoteSafeHandle remoteHandle = Proxy . git_remote_lookup ( repoHandle , remote . Name , true ) ;
116+ Debug . Assert ( remoteHandle != null && ! ( remoteHandle . IsClosed || remoteHandle . IsInvalid ) ) ;
115117
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 ) ) ;
124128
125129 return remoteHandle ;
126130 }
127131
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 )
131137 {
132- if ( options == null )
138+ using ( RemoteSafeHandle remoteHandle = BuildRemoteSafeHandle ( repoHandle , remote ) )
133139 {
134- options = new FetchOptions ( ) ;
140+ DoFetch ( options , remoteHandle , logMessage , refspecs ) ;
135141 }
142+ }
136143
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 ) )
138151 {
139- var callbacks = new RemoteCallbacks ( options ) ;
140- GitRemoteCallbacks gitCallbacks = callbacks . GenerateCallbacks ( ) ;
152+ DoFetch ( options , remoteHandle , logMessage , refspecs ) ;
153+ }
154+ }
141155
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+ } ;
160178
161- Proxy . git_remote_fetch ( remoteHandle , refspecs , fetchOptions , logMessage ) ;
179+ if ( options . TagFetchMode . HasValue )
180+ {
181+ fetchOptions . download_tags = options . TagFetchMode . Value ;
162182 }
183+
184+ Proxy . git_remote_fetch ( remoteHandle , refspecs , fetchOptions , logMessage ) ;
163185 }
164186
165187 /// <summary>
@@ -201,7 +223,7 @@ public virtual void Fetch(Remote remote, FetchOptions options, string logMessage
201223 {
202224 Ensure . ArgumentNotNull ( remote , "remote" ) ;
203225
204- DoFetch ( repository . Handle , remote , null , options , logMessage , new string [ 0 ] ) ;
226+ DoFetch ( repository . Handle , remote , options , logMessage , new string [ 0 ] ) ;
205227 }
206228
207229 /// <summary>
@@ -248,7 +270,7 @@ public virtual void Fetch(Remote remote, IEnumerable<string> refspecs, FetchOpti
248270 Ensure . ArgumentNotNull ( remote , "remote" ) ;
249271 Ensure . ArgumentNotNull ( refspecs , "refspecs" ) ;
250272
251- DoFetch ( repository . Handle , remote , null , options , logMessage , refspecs ) ;
273+ DoFetch ( repository . Handle , remote , options , logMessage , refspecs ) ;
252274 }
253275
254276 /// <summary>
@@ -307,7 +329,7 @@ public virtual void Fetch(
307329 Ensure . ArgumentNotNull ( url , "url" ) ;
308330 Ensure . ArgumentNotNull ( refspecs , "refspecs" ) ;
309331
310- DoFetch ( repository . Handle , null , url , options , logMessage , refspecs ) ;
332+ DoFetch ( repository . Handle , url , options , logMessage , refspecs ) ;
311333 }
312334
313335 /// <summary>
0 commit comments