Permalink
Browse files

Move Fetch in Network namespace

  • Loading branch information...
yorah authored and nulltoken committed Apr 3, 2013
1 parent 4cb81d1 commit 7750d5b73ec03507eb3565f7341ffba22191f24e
Showing with 81 additions and 55 deletions.
  1. +3 −3 LibGit2Sharp.Tests/FetchFixture.cs
  2. +73 −1 LibGit2Sharp/Network.cs
  3. +4 −50 LibGit2Sharp/Remote.cs
  4. +1 −1 LibGit2Sharp/RepositoryExtensions.cs
@@ -39,7 +39,7 @@ public void CanFetchIntoAnEmptyRepository(string url)
}
// Perform the actual fetch
- remote.Fetch(onUpdateTips: expectedFetchState.RemoteUpdateTipsHandler);
+ repo.Network.Fetch(remote, onUpdateTips: expectedFetchState.RemoteUpdateTipsHandler);
// Verify the expected
expectedFetchState.CheckUpdatedReferences(repo);
@@ -58,7 +58,7 @@ public void CanFetchIntoAnEmptyRepositoryWithCredentials()
Remote remote = repo.Network.Remotes.Add(remoteName, Constants.PrivateRepoUrl);
// Perform the actual fetch
- remote.Fetch(credentials: new Credentials
+ repo.Network.Fetch(remote, credentials: new Credentials
{
Username = Constants.PrivateRepoUsername,
Password = Constants.PrivateRepoPassword
@@ -95,7 +95,7 @@ public void CanFetchAllTagsIntoAnEmptyRepository(string url)
}
// Perform the actual fetch
- remote.Fetch(TagFetchMode.All, onUpdateTips: expectedFetchState.RemoteUpdateTipsHandler);
+ repo.Network.Fetch(remote, TagFetchMode.All, onUpdateTips: expectedFetchState.RemoteUpdateTipsHandler);
// Verify the expected
expectedFetchState.CheckUpdatedReferences(repo);
View
@@ -89,6 +89,78 @@ public virtual IEnumerable<DirectReference> ListReferences(Remote remote)
return directReferences;
}
+ /// <summary>
+ /// Fetch from the <see cref = "Remote" />.
+ /// </summary>
+ /// <param name="remote">The remote to fetch</param>
+ /// <param name="tagFetchMode">Optional parameter indicating what tags to download.</param>
+ /// <param name="onProgress">Progress callback. Corresponds to libgit2 progress callback.</param>
+ /// <param name="onCompletion">Completion callback. Corresponds to libgit2 completion callback.</param>
+ /// <param name="onUpdateTips">UpdateTips callback. Corresponds to libgit2 update_tips callback.</param>
+ /// <param name="onTransferProgress">Callback method that transfer progress will be reported through.
+ /// Reports the client's state regarding the received and processed (bytes, objects) from the server.</param>
+ /// <param name="credentials">Credentials to use for username/password authentication.</param>
+ public virtual void Fetch(
+ Remote remote,
+ TagFetchMode tagFetchMode = TagFetchMode.Auto,
+ ProgressHandler onProgress = null,
+ CompletionHandler onCompletion = null,
+ UpdateTipsHandler onUpdateTips = null,
+ TransferProgressHandler onTransferProgress = null,
+ Credentials credentials = null)
+ {
+ Ensure.ArgumentNotNull(remote, "remote");
+
+ // We need to keep a reference to the git_cred_acquire_cb callback around
+ // so it will not be garbage collected before we are done with it.
+ // Note that we also have a GC.KeepAlive call at the end of the method.
+ NativeMethods.git_cred_acquire_cb credentialCallback = null;
+
+ using (RemoteSafeHandle remoteHandle = Proxy.git_remote_load(repository.Handle, remote.Name, true))
+ {
+ var callbacks = new RemoteCallbacks(onProgress, onCompletion, onUpdateTips);
+ GitRemoteCallbacks gitCallbacks = callbacks.GenerateCallbacks();
+
+ Proxy.git_remote_set_autotag(remoteHandle, tagFetchMode);
+
+ if (credentials != null)
+ {
+ credentialCallback = (out IntPtr cred, IntPtr url, IntPtr username_from_url, uint types, IntPtr payload) =>
+ NativeMethods.git_cred_userpass_plaintext_new(out cred, credentials.Username, credentials.Password);
+
+ Proxy.git_remote_set_cred_acquire_cb(
+ remoteHandle,
+ credentialCallback,
+ IntPtr.Zero);
+ }
+
+ // It is OK to pass the reference to the GitCallbacks directly here because libgit2 makes a copy of
+ // the data in the git_remote_callbacks structure. If, in the future, libgit2 changes its implementation
+ // to store a reference to the git_remote_callbacks structure this would introduce a subtle bug
+ // where the managed layer could move the git_remote_callbacks to a different location in memory,
+ // but libgit2 would still reference the old address.
+ //
+ // Also, if GitRemoteCallbacks were a class instead of a struct, we would need to guard against
+ // GC occuring in between setting the remote callbacks and actual usage in one of the functions afterwords.
+ Proxy.git_remote_set_callbacks(remoteHandle, ref gitCallbacks);
+
+ try
+ {
+ Proxy.git_remote_connect(remoteHandle, GitDirection.Fetch);
+ Proxy.git_remote_download(remoteHandle, onTransferProgress);
+ Proxy.git_remote_update_tips(remoteHandle);
+ }
+ finally
+ {
+ Proxy.git_remote_disconnect(remoteHandle);
+ }
+ }
+
+ // To be safe, make sure the credential callback is kept until
+ // alive until at least this point.
+ GC.KeepAlive(credentialCallback);
+ }
+
/// <summary>
/// Push the objectish to the destination reference on the <see cref = "Remote" />.
/// </summary>
@@ -205,7 +277,7 @@ public virtual IEnumerable<DirectReference> ListReferences(Remote remote)
}
}
- // To be safe, make sure the credential callback is kept until
+ // To be safe, make sure the credential callback is kept
// alive until at least this point.
GC.KeepAlive(credentialCallback);
}
View
@@ -27,8 +27,8 @@ protected Remote()
private Remote(Repository repository, string name, string url)
{
this.repository = repository;
- this.Name = name;
- this.Url = url;
+ Name = name;
+ Url = url;
}
internal static Remote BuildFromPtr(RemoteSafeHandle handle, Repository repo)
@@ -61,6 +61,7 @@ internal static Remote BuildFromPtr(RemoteSafeHandle handle, Repository repo)
/// <param name="onTransferProgress">Callback method that transfer progress will be reported through.
/// Reports the client's state regarding the received and processed (bytes, objects) from the server.</param>
/// <param name="credentials">Credentials to use for username/password authentication.</param>
+ [Obsolete("This method will be removed in the next release. Please use Repository.Network.Fetch() instead.")]
public virtual void Fetch(
TagFetchMode tagFetchMode = TagFetchMode.Auto,
ProgressHandler onProgress = null,
@@ -69,54 +70,7 @@ internal static Remote BuildFromPtr(RemoteSafeHandle handle, Repository repo)
TransferProgressHandler onTransferProgress = null,
Credentials credentials = null)
{
- // We need to keep a reference to the git_cred_acquire_cb callback around
- // so it will not be garbage collected before we are done with it.
- // Note that we also have a GC.KeepAlive call at the end of the method.
- NativeMethods.git_cred_acquire_cb credentialCallback = null;
-
- using (RemoteSafeHandle remoteHandle = Proxy.git_remote_load(repository.Handle, this.Name, true))
- {
- var callbacks = new RemoteCallbacks(onProgress, onCompletion, onUpdateTips);
- GitRemoteCallbacks gitCallbacks = callbacks.GenerateCallbacks();
-
- Proxy.git_remote_set_autotag(remoteHandle, tagFetchMode);
-
- if (credentials != null)
- {
- credentialCallback = (out IntPtr cred, IntPtr url, IntPtr username_from_url, uint types, IntPtr payload) =>
- NativeMethods.git_cred_userpass_plaintext_new(out cred, credentials.Username, credentials.Password);
-
- Proxy.git_remote_set_cred_acquire_cb(
- remoteHandle,
- credentialCallback,
- IntPtr.Zero);
- }
-
- // It is OK to pass the reference to the GitCallbacks directly here because libgit2 makes a copy of
- // the data in the git_remote_callbacks structure. If, in the future, libgit2 changes its implementation
- // to store a reference to the git_remote_callbacks structure this would introduce a subtle bug
- // where the managed layer could move the git_remote_callbacks to a different location in memory,
- // but libgit2 would still reference the old address.
- //
- // Also, if GitRemoteCallbacks were a class instead of a struct, we would need to guard against
- // GC occuring in between setting the remote callbacks and actual usage in one of the functions afterwords.
- Proxy.git_remote_set_callbacks(remoteHandle, ref gitCallbacks);
-
- try
- {
- Proxy.git_remote_connect(remoteHandle, GitDirection.Fetch);
- Proxy.git_remote_download(remoteHandle, onTransferProgress);
- Proxy.git_remote_update_tips(remoteHandle);
- }
- finally
- {
- Proxy.git_remote_disconnect(remoteHandle);
- }
- }
-
- // To be safe, make sure the credential callback is kept until
- // alive until at least this point.
- GC.KeepAlive(credentialCallback);
+ repository.Network.Fetch(this, tagFetchMode, onProgress, onCompletion, onUpdateTips, onTransferProgress, credentials);
}
/// <summary>
@@ -216,7 +216,7 @@ public static Commit Commit(this IRepository repository, string message, Signatu
Ensure.ArgumentNotNullOrEmptyString(remoteName, "remoteName");
Remote remote = repository.Network.Remotes.RemoteForName(remoteName, true);
- remote.Fetch(tagFetchMode, onProgress, onCompletion, onUpdateTips,
+ repository.Network.Fetch(remote, tagFetchMode, onProgress, onCompletion, onUpdateTips,
onTransferProgress, credentials);
}

0 comments on commit 7750d5b

Please sign in to comment.