Skip to content
This repository was archived by the owner on Feb 3, 2023. It is now read-only.

Commit dea2d7b

Browse files
committed
Refactored Network.Fetch() with FetchOptions
Changed the signature to Network.Fetch(Remote, FetchOptions). As of libgit2#536, optional parameters should be grouped in a xxxOptions type. Network.Fetch() has currently 5 optional parameters, and a sixth one (RefSpecs) will be introduced soon.
1 parent d7879b0 commit dea2d7b

File tree

7 files changed

+152
-32
lines changed

7 files changed

+152
-32
lines changed

LibGit2Sharp.Tests/FetchFixture.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void CanFetchIntoAnEmptyRepository(string url)
4242
}
4343

4444
// Perform the actual fetch
45-
repo.Network.Fetch(remote, onUpdateTips: expectedFetchState.RemoteUpdateTipsHandler);
45+
repo.Network.Fetch(remote, new FetchOptions { OnUpdateTips = expectedFetchState.RemoteUpdateTipsHandler });
4646

4747
// Verify the expected
4848
expectedFetchState.CheckUpdatedReferences(repo);
@@ -62,11 +62,14 @@ public void CanFetchIntoAnEmptyRepositoryWithCredentials()
6262
Remote remote = repo.Network.Remotes.Add(remoteName, Constants.PrivateRepoUrl);
6363

6464
// Perform the actual fetch
65-
repo.Network.Fetch(remote, credentials: new Credentials
66-
{
67-
Username = Constants.PrivateRepoUsername,
68-
Password = Constants.PrivateRepoPassword
69-
});
65+
repo.Network.Fetch(remote, new FetchOptions
66+
{
67+
Credentials = new Credentials
68+
{
69+
Username = Constants.PrivateRepoUsername,
70+
Password = Constants.PrivateRepoPassword
71+
}
72+
});
7073
}
7174
}
7275

@@ -94,7 +97,10 @@ public void CanFetchAllTagsIntoAnEmptyRepository(string url)
9497
}
9598

9699
// Perform the actual fetch
97-
repo.Network.Fetch(remote, TagFetchMode.All, onUpdateTips: expectedFetchState.RemoteUpdateTipsHandler);
100+
repo.Network.Fetch(remote, new FetchOptions {
101+
TagFetchMode = TagFetchMode.All,
102+
OnUpdateTips = expectedFetchState.RemoteUpdateTipsHandler
103+
});
98104

99105
// Verify the expected
100106
expectedFetchState.CheckUpdatedReferences(repo);

LibGit2Sharp.Tests/RepositoryFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,13 @@ public void CanFetchFromRemoteByName()
186186
}
187187

188188
// Perform the actual fetch
189-
repo.Fetch(remote.Name, onUpdateTips: expectedFetchState.RemoteUpdateTipsHandler);
189+
repo.Fetch(remote.Name, new FetchOptions { OnUpdateTips = expectedFetchState.RemoteUpdateTipsHandler });
190190

191191
// Verify the expected state
192192
expectedFetchState.CheckUpdatedReferences(repo);
193193

194194
// Now fetch the rest of the tags
195-
repo.Fetch(remote.Name, tagFetchMode: TagFetchMode.All);
195+
repo.Fetch(remote.Name, new FetchOptions { TagFetchMode = TagFetchMode.All });
196196

197197
// Verify that the "nearly-dangling" tag is now in the repo.
198198
Tag nearlyDanglingTag = repo.Tags["nearly-dangling"];

LibGit2Sharp/FetchOptions.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using LibGit2Sharp.Handlers;
2+
3+
namespace LibGit2Sharp
4+
{
5+
/// <summary>
6+
/// Collection of parameters controlling Fetch behavior.
7+
/// </summary>
8+
public sealed class FetchOptions
9+
{
10+
/// <summary>
11+
/// Specifies the tag-following behavior of the fetch operation.
12+
/// <para>
13+
/// If not set, the fetch operation will follow the default behavior for the <see cref="Remote"/>
14+
/// based on the remote's <see cref="Remote.TagFetchMode"/> configuration.
15+
/// </para>
16+
/// <para>If neither this property nor the remote `tagopt` configuration is set,
17+
/// this will default to <see cref="TagFetchMode.Auto"/> (i.e. tags that point to objects
18+
/// retrieved during this fetch will be retrieved as well).</para>
19+
/// </summary>
20+
public TagFetchMode? TagFetchMode { get; set; }
21+
22+
/// <summary>
23+
/// Delegate that progress updates of the network transfer portion of fetch
24+
/// will be reported through.
25+
/// </summary>
26+
public ProgressHandler OnProgress { get; set; }
27+
28+
/// <summary>
29+
/// Delegate that updates of remote tracking branches will be reported through.
30+
/// </summary>
31+
public UpdateTipsHandler OnUpdateTips { get; set; }
32+
33+
/// <summary>
34+
/// Callback method that transfer progress will be reported through.
35+
/// <para>
36+
/// Reports the client's state regarding the received and processed (bytes, objects) from the server.
37+
/// </para>
38+
/// </summary>
39+
public TransferProgressHandler OnTransferProgress { get; set; }
40+
41+
/// <summary>
42+
/// Credentials to use for username/password authentication.
43+
/// </summary>
44+
public Credentials Credentials { get; set; }
45+
}
46+
}

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
<Compile Include="CommitFilter.cs" />
7575
<Compile Include="CommitSortStrategies.cs" />
7676
<Compile Include="CompareOptions.cs" />
77+
<Compile Include="FetchOptions.cs" />
7778
<Compile Include="RefSpec.cs" />
7879
<Compile Include="RefSpecCollection.cs" />
7980
<Compile Include="Core\EncodingMarshaler.cs" />

LibGit2Sharp/Network.cs

Lines changed: 45 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,21 @@ public virtual IEnumerable<DirectReference> ListReferences(string url)
9696
}
9797
}
9898

99-
static void DoFetch(RemoteSafeHandle remoteHandle, GitRemoteCallbacks gitCallbacks, TagFetchMode? tagFetchMode)
99+
static void DoFetch(RemoteSafeHandle remoteHandle, FetchOptions options)
100100
{
101-
if (tagFetchMode.HasValue)
101+
if (options == null)
102102
{
103-
Proxy.git_remote_set_autotag(remoteHandle, tagFetchMode.Value);
103+
options = new FetchOptions();
104104
}
105105

106+
if (options.TagFetchMode.HasValue)
107+
{
108+
Proxy.git_remote_set_autotag(remoteHandle, options.TagFetchMode.Value);
109+
}
110+
111+
var callbacks = new RemoteCallbacks(options);
112+
GitRemoteCallbacks gitCallbacks = callbacks.GenerateCallbacks();
113+
106114
// It is OK to pass the reference to the GitCallbacks directly here because libgit2 makes a copy of
107115
// the data in the git_remote_callbacks structure. If, in the future, libgit2 changes its implementation
108116
// to store a reference to the git_remote_callbacks structure this would introduce a subtle bug
@@ -135,22 +143,48 @@ static void DoFetch(RemoteSafeHandle remoteHandle, GitRemoteCallbacks gitCallbac
135143
/// <param name="onTransferProgress">Callback method that transfer progress will be reported through.
136144
/// Reports the client's state regarding the received and processed (bytes, objects) from the server.</param>
137145
/// <param name="credentials">Credentials to use for username/password authentication.</param>
146+
[Obsolete("This overload will be removed in the next release. Please use Fetch(Remote, FetchOptions) instead.")]
138147
public virtual void Fetch(
139148
Remote remote,
140149
TagFetchMode? tagFetchMode = null,
141150
ProgressHandler onProgress = null,
142151
UpdateTipsHandler onUpdateTips = null,
143152
TransferProgressHandler onTransferProgress = null,
144153
Credentials credentials = null)
154+
{
155+
Fetch(remote, new FetchOptions
156+
{
157+
TagFetchMode = tagFetchMode,
158+
OnProgress = onProgress,
159+
OnUpdateTips = onUpdateTips,
160+
OnTransferProgress = onTransferProgress,
161+
Credentials = credentials
162+
});
163+
}
164+
165+
/// <summary>
166+
/// Fetch from the <see cref="Remote"/>.
167+
/// </summary>
168+
/// <param name="remote">The remote to fetch</param>
169+
public virtual void Fetch(Remote remote)
170+
{
171+
// This overload is required as long as the obsolete overload exists.
172+
// Otherwise, Fetch(Remote) is ambiguous.
173+
Fetch(remote, (FetchOptions)null);
174+
}
175+
176+
/// <summary>
177+
/// Fetch from the <see cref="Remote"/>.
178+
/// </summary>
179+
/// <param name="remote">The remote to fetch</param>
180+
/// <param name="options"><see cref="FetchOptions"/> controlling fetch behavior</param>
181+
public virtual void Fetch(Remote remote, FetchOptions options = null)
145182
{
146183
Ensure.ArgumentNotNull(remote, "remote");
147184

148185
using (RemoteSafeHandle remoteHandle = Proxy.git_remote_load(repository.Handle, remote.Name, true))
149186
{
150-
var callbacks = new RemoteCallbacks(onProgress, onTransferProgress, onUpdateTips, credentials);
151-
GitRemoteCallbacks gitCallbacks = callbacks.GenerateCallbacks();
152-
153-
DoFetch(remoteHandle, gitCallbacks, tagFetchMode);
187+
DoFetch(remoteHandle, options);
154188
}
155189
}
156190

@@ -159,30 +193,20 @@ public virtual void Fetch(
159193
/// </summary>
160194
/// <param name="url">The url to fetch from</param>
161195
/// <param name="refspecs">The list of resfpecs to use</param>
162-
/// <param name="tagFetchMode">Optional parameter indicating what tags to download.</param>
163-
/// <param name="onProgress">Progress callback. Corresponds to libgit2 progress callback.</param>
164-
/// <param name="onUpdateTips">UpdateTips callback. Corresponds to libgit2 update_tips callback.</param>
165-
/// <param name="onTransferProgress">Callback method that transfer progress will be reported through.
166-
/// Reports the client's state regarding the received and processed (bytes, objects) from the server.</param>
167-
/// <param name="credentials">Credentials to use for username/password authentication.</param>
196+
/// <param name="options"><see cref="FetchOptions"/> controlling fetch behavior</param>
168197
public virtual void Fetch(
169198
string url,
170199
IEnumerable<string> refspecs,
171-
TagFetchMode? tagFetchMode = null,
172-
ProgressHandler onProgress = null,
173-
UpdateTipsHandler onUpdateTips = null,
174-
TransferProgressHandler onTransferProgress = null,
175-
Credentials credentials = null)
200+
FetchOptions options = null)
176201
{
177202
Ensure.ArgumentNotNull(url, "url");
203+
Ensure.ArgumentNotNull(refspecs, "refspecs");
178204

179205
using (RemoteSafeHandle remoteHandle = Proxy.git_remote_create_inmemory(repository.Handle, null, url))
180206
{
181207
Proxy.git_remote_set_fetch_refspecs(remoteHandle, refspecs);
182-
var callbacks = new RemoteCallbacks(onProgress, onTransferProgress, onUpdateTips, credentials);
183-
GitRemoteCallbacks gitCallbacks = callbacks.GenerateCallbacks();
184208

185-
DoFetch(remoteHandle, gitCallbacks, tagFetchMode);
209+
DoFetch(remoteHandle, options);
186210
}
187211
}
188212

LibGit2Sharp/RemoteCallbacks.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ internal RemoteCallbacks(
2424
Credentials = credentials;
2525
}
2626

27+
internal RemoteCallbacks(FetchOptions fetchOptions)
28+
{
29+
Ensure.ArgumentNotNull(fetchOptions, "fetchOptions");
30+
Progress = fetchOptions.OnProgress;
31+
DownloadTransferProgress = fetchOptions.OnTransferProgress;
32+
UpdateTips = fetchOptions.OnUpdateTips;
33+
Credentials = fetchOptions.Credentials;
34+
}
35+
2736
#region Delegates
2837

2938
/// <summary>

LibGit2Sharp/RepositoryExtensions.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ public static Commit Commit(this IRepository repository, string message, Signatu
246246
/// <param name="onTransferProgress">Callback method that transfer progress will be reported through.
247247
/// Reports the client's state regarding the received and processed (bytes, objects) from the server.</param>
248248
/// <param name="credentials">Credentials to use for username/password authentication.</param>
249+
[Obsolete("This overload will be removed in the next release. Please use Fetch(Remote, FetchOptions) instead.")]
249250
public static void Fetch(this IRepository repository, string remoteName,
250251
TagFetchMode tagFetchMode = TagFetchMode.Auto,
251252
ProgressHandler onProgress = null,
@@ -257,8 +258,41 @@ public static void Fetch(this IRepository repository, string remoteName,
257258
Ensure.ArgumentNotNullOrEmptyString(remoteName, "remoteName");
258259

259260
Remote remote = repository.Network.Remotes.RemoteForName(remoteName, true);
260-
repository.Network.Fetch(remote, tagFetchMode, onProgress, onUpdateTips,
261-
onTransferProgress, credentials);
261+
repository.Network.Fetch(remote, new FetchOptions
262+
{
263+
TagFetchMode = tagFetchMode,
264+
OnProgress = onProgress,
265+
OnUpdateTips = onUpdateTips,
266+
OnTransferProgress = onTransferProgress,
267+
Credentials = credentials
268+
});
269+
}
270+
271+
/// <summary>
272+
/// Fetch from the specified remote.
273+
/// </summary>
274+
/// <param name="repository">The <see cref="Repository"/> being worked with.</param>
275+
/// <param name="remoteName">The name of the <see cref="Remote"/> to fetch from.</param>
276+
public static void Fetch(this IRepository repository, string remoteName)
277+
{
278+
// This overload is required as long as the obsolete overload exists.
279+
// Otherwise, Fetch(string) is ambiguous.
280+
Fetch(repository, remoteName, (FetchOptions)null);
281+
}
282+
283+
/// <summary>
284+
/// Fetch from the specified remote.
285+
/// </summary>
286+
/// <param name="repository">The <see cref="Repository"/> being worked with.</param>
287+
/// <param name="remoteName">The name of the <see cref="Remote"/> to fetch from.</param>
288+
/// <param name="options"><see cref="FetchOptions"/> controlling fetch behavior</param>
289+
public static void Fetch(this IRepository repository, string remoteName, FetchOptions options = null)
290+
{
291+
Ensure.ArgumentNotNull(repository, "repository");
292+
Ensure.ArgumentNotNullOrEmptyString(remoteName, "remoteName");
293+
294+
Remote remote = repository.Network.Remotes.RemoteForName(remoteName, true);
295+
repository.Network.Fetch(remote, options);
262296
}
263297

264298
/// <summary>

0 commit comments

Comments
 (0)