|
1 | 1 | using System;
|
2 | 2 | using System.Collections.Generic;
|
| 3 | +using System.Diagnostics; |
3 | 4 | using System.Globalization;
|
4 | 5 | using System.Linq;
|
| 6 | +using System.Security.Policy; |
5 | 7 | using LibGit2Sharp.Core;
|
6 | 8 | using LibGit2Sharp.Core.Handles;
|
7 | 9 | using LibGit2Sharp.Handlers;
|
@@ -104,32 +106,60 @@ public virtual IEnumerable<DirectReference> ListReferences(string url)
|
104 | 106 | }
|
105 | 107 | }
|
106 | 108 |
|
107 |
| - static void DoFetch(RemoteSafeHandle remoteHandle, FetchOptions options, string logMessage) |
| 109 | + static RemoteSafeHandle Build(RepositorySafeHandle repoHandle, Remote remote, string url) |
| 110 | + { |
| 111 | + Debug.Assert((remote == null) ^ (url == null)); |
| 112 | + |
| 113 | + RemoteSafeHandle remoteHandle; |
| 114 | + |
| 115 | + if (url != null) |
| 116 | + { |
| 117 | + remoteHandle = Proxy.git_remote_create_anonymous(repoHandle, url, null); |
| 118 | + } |
| 119 | + else |
| 120 | + { |
| 121 | + remoteHandle = Proxy.git_remote_lookup(repoHandle, remote.Name, true); |
| 122 | + } |
| 123 | + |
| 124 | + return remoteHandle; |
| 125 | + } |
| 126 | + |
| 127 | + static void DoFetch(RepositorySafeHandle repoHandle, Remote remote, string url, |
| 128 | + FetchOptions options, string logMessage, |
| 129 | + IEnumerable<string> refspecs = null) |
108 | 130 | {
|
109 | 131 | if (options == null)
|
110 | 132 | {
|
111 | 133 | options = new FetchOptions();
|
112 | 134 | }
|
113 | 135 |
|
114 |
| - if (options.TagFetchMode.HasValue) |
| 136 | + using (RemoteSafeHandle remoteHandle = Build(repoHandle, remote, url)) |
115 | 137 | {
|
116 |
| - Proxy.git_remote_set_autotag(remoteHandle, options.TagFetchMode.Value); |
117 |
| - } |
| 138 | + if (options.TagFetchMode.HasValue) |
| 139 | + { |
| 140 | + Proxy.git_remote_set_autotag(repoHandle, remote.Name, options.TagFetchMode.Value); |
| 141 | + } |
| 142 | + |
| 143 | + if (refspecs != null) |
| 144 | + { |
| 145 | + Proxy.git_remote_set_fetch_refspecs(remoteHandle, refspecs); |
| 146 | + } |
118 | 147 |
|
119 |
| - var callbacks = new RemoteCallbacks(options); |
120 |
| - GitRemoteCallbacks gitCallbacks = callbacks.GenerateCallbacks(); |
| 148 | + var callbacks = new RemoteCallbacks(options); |
| 149 | + GitRemoteCallbacks gitCallbacks = callbacks.GenerateCallbacks(); |
121 | 150 |
|
122 |
| - // It is OK to pass the reference to the GitCallbacks directly here because libgit2 makes a copy of |
123 |
| - // the data in the git_remote_callbacks structure. If, in the future, libgit2 changes its implementation |
124 |
| - // to store a reference to the git_remote_callbacks structure this would introduce a subtle bug |
125 |
| - // where the managed layer could move the git_remote_callbacks to a different location in memory, |
126 |
| - // but libgit2 would still reference the old address. |
127 |
| - // |
128 |
| - // Also, if GitRemoteCallbacks were a class instead of a struct, we would need to guard against |
129 |
| - // GC occuring in between setting the remote callbacks and actual usage in one of the functions afterwords. |
130 |
| - Proxy.git_remote_set_callbacks(remoteHandle, ref gitCallbacks); |
| 151 | + // It is OK to pass the reference to the GitCallbacks directly here because libgit2 makes a copy of |
| 152 | + // the data in the git_remote_callbacks structure. If, in the future, libgit2 changes its implementation |
| 153 | + // to store a reference to the git_remote_callbacks structure this would introduce a subtle bug |
| 154 | + // where the managed layer could move the git_remote_callbacks to a different location in memory, |
| 155 | + // but libgit2 would still reference the old address. |
| 156 | + // |
| 157 | + // Also, if GitRemoteCallbacks were a class instead of a struct, we would need to guard against |
| 158 | + // GC occuring in between setting the remote callbacks and actual usage in one of the functions afterwords. |
| 159 | + Proxy.git_remote_set_callbacks(remoteHandle, ref gitCallbacks); |
131 | 160 |
|
132 |
| - Proxy.git_remote_fetch(remoteHandle, logMessage); |
| 161 | + Proxy.git_remote_fetch(remoteHandle, logMessage); |
| 162 | + } |
133 | 163 | }
|
134 | 164 |
|
135 | 165 | /// <summary>
|
@@ -171,10 +201,7 @@ public virtual void Fetch(Remote remote, FetchOptions options, string logMessage
|
171 | 201 | {
|
172 | 202 | Ensure.ArgumentNotNull(remote, "remote");
|
173 | 203 |
|
174 |
| - using (RemoteSafeHandle remoteHandle = Proxy.git_remote_lookup(repository.Handle, remote.Name, true)) |
175 |
| - { |
176 |
| - DoFetch(remoteHandle, options, logMessage); |
177 |
| - } |
| 204 | + DoFetch(repository.Handle, remote, null, options, logMessage); |
178 | 205 | }
|
179 | 206 |
|
180 | 207 | /// <summary>
|
@@ -221,12 +248,7 @@ public virtual void Fetch(Remote remote, IEnumerable<string> refspecs, FetchOpti
|
221 | 248 | Ensure.ArgumentNotNull(remote, "remote");
|
222 | 249 | Ensure.ArgumentNotNull(refspecs, "refspecs");
|
223 | 250 |
|
224 |
| - using (RemoteSafeHandle remoteHandle = Proxy.git_remote_lookup(repository.Handle, remote.Name, true)) |
225 |
| - { |
226 |
| - Proxy.git_remote_set_fetch_refspecs(remoteHandle, refspecs); |
227 |
| - |
228 |
| - DoFetch(remoteHandle, options, logMessage); |
229 |
| - } |
| 251 | + DoFetch(repository.Handle, remote, null, options, logMessage, refspecs); |
230 | 252 | }
|
231 | 253 |
|
232 | 254 | /// <summary>
|
@@ -285,12 +307,7 @@ public virtual void Fetch(
|
285 | 307 | Ensure.ArgumentNotNull(url, "url");
|
286 | 308 | Ensure.ArgumentNotNull(refspecs, "refspecs");
|
287 | 309 |
|
288 |
| - using (RemoteSafeHandle remoteHandle = Proxy.git_remote_create_anonymous(repository.Handle, url, null)) |
289 |
| - { |
290 |
| - Proxy.git_remote_set_fetch_refspecs(remoteHandle, refspecs); |
291 |
| - |
292 |
| - DoFetch(remoteHandle, options, logMessage); |
293 |
| - } |
| 310 | + DoFetch(repository.Handle, null, options, logMessage, refspecs); |
294 | 311 | }
|
295 | 312 |
|
296 | 313 | /// <summary>
|
|
0 commit comments