Skip to content

Commit 6f0230d

Browse files
committed
Move Fetch and Pull to be static methods
Since we make no use of these commands being classes. Make them static methods of a static class to give them some namespacing.
1 parent 53dc1c2 commit 6f0230d

File tree

7 files changed

+67
-97
lines changed

7 files changed

+67
-97
lines changed

LibGit2Sharp.Tests/FetchFixture.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void CanFetchIntoAnEmptyRepository(string url)
4444
}
4545

4646
// Perform the actual fetch
47-
new Commands.Fetch(repo, remoteName, new string[0], new FetchOptions { OnUpdateTips = expectedFetchState.RemoteUpdateTipsHandler }, null).Run();
47+
Commands.Fetch(repo, remoteName, new string[0], new FetchOptions { OnUpdateTips = expectedFetchState.RemoteUpdateTipsHandler }, null);
4848

4949
// Verify the expected
5050
expectedFetchState.CheckUpdatedReferences(repo);
@@ -64,10 +64,10 @@ public void CanFetchIntoAnEmptyRepositoryWithCredentials()
6464
repo.Network.Remotes.Add(remoteName, Constants.PrivateRepoUrl);
6565

6666
// Perform the actual fetch
67-
new Commands.Fetch(repo, remoteName, new string[0], new FetchOptions
67+
Commands.Fetch(repo, remoteName, new string[0], new FetchOptions
6868
{
6969
CredentialsProvider = Constants.PrivateRepoCredentials
70-
}, null).Run();
70+
}, null);
7171
}
7272
}
7373

@@ -101,10 +101,10 @@ public void CanFetchAllTagsIntoAnEmptyRepository(string url)
101101
}
102102

103103
// Perform the actual fetch
104-
new Commands.Fetch(repo, remoteName, new string[0], new FetchOptions {
104+
Commands.Fetch(repo, remoteName, new string[0], new FetchOptions {
105105
TagFetchMode = TagFetchMode.All,
106106
OnUpdateTips = expectedFetchState.RemoteUpdateTipsHandler
107-
}, null).Run();
107+
}, null);
108108

109109
// Verify the expected
110110
expectedFetchState.CheckUpdatedReferences(repo);
@@ -147,10 +147,10 @@ public void CanFetchCustomRefSpecsIntoAnEmptyRepository(string url, string local
147147
}
148148

149149
// Perform the actual fetch
150-
new Commands.Fetch(repo, remoteName, new string[] { refSpec }, new FetchOptions {
150+
Commands.Fetch(repo, remoteName, new string[] { refSpec }, new FetchOptions {
151151
TagFetchMode = TagFetchMode.None,
152152
OnUpdateTips = expectedFetchState.RemoteUpdateTipsHandler
153-
}, null).Run();
153+
}, null);
154154

155155
// Verify the expected
156156
expectedFetchState.CheckUpdatedReferences(repo);
@@ -181,7 +181,7 @@ public void FetchRespectsConfiguredAutoTagSetting(TagFetchMode tagFetchMode, int
181181
r => r.TagFetchMode = tagFetchMode);
182182

183183
// Perform the actual fetch.
184-
new Commands.Fetch(repo, remoteName, new string[0], null, null).Run();
184+
Commands.Fetch(repo, remoteName, new string[0], null, null);
185185

186186
// Verify the number of fetched tags.
187187
Assert.Equal(expectedTagCount, repo.Tags.Count());
@@ -199,7 +199,7 @@ public void CanFetchAllTagsAfterAnInitialClone()
199199

200200
using (var repo = new Repository(clonedRepoPath))
201201
{
202-
new Commands.Fetch(repo, "origin", new string[0], new FetchOptions { TagFetchMode = TagFetchMode.All }, null).Run();
202+
Commands.Fetch(repo, "origin", new string[0], new FetchOptions { TagFetchMode = TagFetchMode.All }, null);
203203
}
204204
}
205205

@@ -225,17 +225,17 @@ public void FetchHonorsTheFetchPruneConfigurationEntry()
225225

226226
// No pruning when the configuration entry isn't defined
227227
Assert.Null(clonedRepo.Config.Get<bool>("fetch.prune"));
228-
new Commands.Fetch(clonedRepo, "origin", new string[0], null, null).Run();
228+
Commands.Fetch(clonedRepo, "origin", new string[0], null, null);
229229
Assert.Equal(5, clonedRepo.Branches.Count(b => b.IsRemote));
230230

231231
// No pruning when the configuration entry is set to false
232232
clonedRepo.Config.Set<bool>("fetch.prune", false);
233-
new Commands.Fetch(clonedRepo, "origin", new string[0], null, null).Run();
233+
Commands.Fetch(clonedRepo, "origin", new string[0], null, null);
234234
Assert.Equal(5, clonedRepo.Branches.Count(b => b.IsRemote));
235235

236236
// Auto pruning when the configuration entry is set to true
237237
clonedRepo.Config.Set<bool>("fetch.prune", true);
238-
new Commands.Fetch(clonedRepo, "origin", new string[0], null, null).Run();
238+
Commands.Fetch(clonedRepo, "origin", new string[0], null, null);
239239
Assert.Equal(4, clonedRepo.Branches.Count(b => b.IsRemote));
240240
}
241241
}

LibGit2Sharp.Tests/NetworkFixture.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public void CanPull(FastForwardStrategy fastForwardStrategy)
164164
}
165165
};
166166

167-
MergeResult mergeResult = new Commands.Pull(repo, Constants.Signature, pullOptions).Run();
167+
MergeResult mergeResult = Commands.Pull(repo, Constants.Signature, pullOptions);
168168

169169
if(fastForwardStrategy == FastForwardStrategy.Default || fastForwardStrategy == FastForwardStrategy.FastForwardOnly)
170170
{
@@ -197,7 +197,7 @@ public void CanPullIntoEmptyRepo()
197197
b => b.UpstreamBranch = "refs/heads/master");
198198

199199
// Pull!
200-
MergeResult mergeResult = new Commands.Pull(repo, Constants.Signature, new PullOptions()).Run();
200+
MergeResult mergeResult = Commands.Pull(repo, Constants.Signature, new PullOptions());
201201

202202
Assert.Equal(mergeResult.Status, MergeStatus.FastForward);
203203
Assert.Equal(mergeResult.Commit, repo.Branches["refs/remotes/origin/master"].Tip);
@@ -224,7 +224,7 @@ public void PullWithoutMergeBranchThrows()
224224

225225
try
226226
{
227-
new Commands.Pull(repo, Constants.Signature, new PullOptions()).Run();
227+
Commands.Pull(repo, Constants.Signature, new PullOptions());
228228
}
229229
catch(MergeFetchHeadNotFoundException ex)
230230
{
@@ -252,7 +252,7 @@ public void CanMergeFetchedRefs()
252252
Assert.False(repo.RetrieveStatus().Any());
253253
Assert.Equal(repo.Lookup<Commit>("refs/remotes/origin/master~1"), repo.Head.Tip);
254254

255-
new Commands.Fetch(repo, repo.Head.RemoteName, new string[0], null, null).Run();
255+
Commands.Fetch(repo, repo.Head.RemoteName, new string[0], null, null);
256256

257257
MergeOptions mergeOptions = new MergeOptions()
258258
{
@@ -279,7 +279,7 @@ public void CanPruneRefs()
279279
using (var repo = new Repository(clonedRepoPath))
280280
{
281281
repo.Network.Remotes.Add("pruner", clonedRepoPath2);
282-
new Commands.Fetch(repo, "pruner", new string[0], null, null).Run();
282+
Commands.Fetch(repo, "pruner", new string[0], null, null);
283283
Assert.NotNull(repo.Refs["refs/remotes/pruner/master"]);
284284

285285
// Remove the branch from the source repository
@@ -289,11 +289,11 @@ public void CanPruneRefs()
289289
}
290290

291291
// and by default we don't prune it
292-
new Commands.Fetch(repo, "pruner", new string[0], null, null).Run();
292+
Commands.Fetch(repo, "pruner", new string[0], null, null);
293293
Assert.NotNull(repo.Refs["refs/remotes/pruner/master"]);
294294

295295
// but we do when asked by the user
296-
new Commands.Fetch(repo, "pruner", new string[0], new FetchOptions { Prune = true}, null).Run();
296+
Commands.Fetch(repo, "pruner", new string[0], new FetchOptions { Prune = true}, null);
297297
Assert.Null(repo.Refs["refs/remotes/pruner/master"]);
298298
}
299299
}

LibGit2Sharp.Tests/RepositoryFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,13 @@ public void CanFetchFromRemoteByName()
208208
}
209209

210210
// Perform the actual fetch
211-
new Commands.Fetch(repo, remoteName, new string[0], new FetchOptions { OnUpdateTips = expectedFetchState.RemoteUpdateTipsHandler }, null).Run();
211+
Commands.Fetch(repo, remoteName, new string[0], new FetchOptions { OnUpdateTips = expectedFetchState.RemoteUpdateTipsHandler }, null);
212212

213213
// Verify the expected state
214214
expectedFetchState.CheckUpdatedReferences(repo);
215215

216216
// Now fetch the rest of the tags
217-
new Commands.Fetch(repo, remoteName, new string[0], new FetchOptions { TagFetchMode = TagFetchMode.All }, null).Run();
217+
Commands.Fetch(repo, remoteName, new string[0], new FetchOptions { TagFetchMode = TagFetchMode.All }, null);
218218

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

LibGit2Sharp.Tests/SmartSubtransportFixture.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ public void CustomSmartSubtransportTest(string scheme, string url)
6363
}
6464

6565
// Perform the actual fetch
66-
new Commands.Fetch(repo, remoteName, new string[0],
66+
Commands.Fetch(repo, remoteName, new string[0],
6767
new FetchOptions { OnUpdateTips = expectedFetchState.RemoteUpdateTipsHandler, TagFetchMode = TagFetchMode.Auto },
68-
null).Run();
68+
null);
6969

7070
// Verify the expected
7171
expectedFetchState.CheckUpdatedReferences(repo);
@@ -115,10 +115,10 @@ public void CanUseCredentials(string scheme, string url, string user, string pas
115115
}
116116

117117
// Perform the actual fetch
118-
new Commands.Fetch(repo, remoteName, new string[0], new FetchOptions {
118+
Commands.Fetch(repo, remoteName, new string[0], new FetchOptions {
119119
OnUpdateTips = expectedFetchState.RemoteUpdateTipsHandler, TagFetchMode = TagFetchMode.Auto,
120120
CredentialsProvider = (_user, _valid, _hostname) => new UsernamePasswordCredentials() { Username = "libgit3", Password = "libgit3" },
121-
}, null).Run();
121+
}, null);
122122

123123
// Verify the expected
124124
expectedFetchState.CheckUpdatedReferences(repo);

LibGit2Sharp/Commands/Fetch.cs

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,41 @@
33
using LibGit2Sharp.Core;
44
using LibGit2Sharp.Core.Handles;
55

6-
namespace LibGit2Sharp.Commands
6+
namespace LibGit2Sharp
77
{
88
/// <summary>
9-
/// Fetch from a particular remote or the default
9+
/// Class to serve as namespacing for the command-emulating methods
1010
/// </summary>
11-
public class Fetch
11+
public static partial class Commands
1212
{
13-
private readonly Repository repository;
14-
private readonly string remote;
15-
private readonly FetchOptions options;
16-
private readonly string logMessage;
17-
private readonly IEnumerable<string> refspecs;
18-
19-
/// <summary>
20-
/// Initializes a new instance of the <see cref="LibGit2Sharp.Commands.Fetch"/> class.
21-
/// </summary>
22-
/// <param name="repository">The repository in which to fetch.</param>
23-
/// <param name="remote">The remote to fetch from</param>
24-
/// <param name="options">Fetch options.</param>
25-
/// <param name="logMessage">Log message for any ref updates.</param>
26-
/// <param name="refspecs">List of refspecs to apply as active.</param>
27-
public Fetch(Repository repository, string remote, IEnumerable<string> refspecs, FetchOptions options, string logMessage)
28-
{
29-
Ensure.ArgumentNotNull(remote, "remote");
30-
31-
this.repository = repository;
32-
this.remote = remote;
33-
this.options = options ?? new FetchOptions();
34-
this.logMessage = logMessage;
35-
this.refspecs = refspecs;
36-
}
37-
38-
private RemoteHandle RemoteFromNameOrUrl()
13+
private static RemoteHandle RemoteFromNameOrUrl(RepositoryHandle repoHandle, string remote)
3914
{
4015
RemoteHandle handle = null;
41-
handle = Proxy.git_remote_lookup(repository.Handle, remote, false);
16+
handle = Proxy.git_remote_lookup(repoHandle, remote, false);
4217

4318
// If that wasn't the name of a remote, let's use it as a url
4419
if (handle == null)
4520
{
46-
handle = Proxy.git_remote_create_anonymous(repository.Handle, remote);
21+
handle = Proxy.git_remote_create_anonymous(repoHandle, remote);
4722
}
4823

4924
return handle;
5025
}
5126

5227
/// <summary>
53-
/// Run this command
28+
/// Perform a fetch
5429
/// </summary>
55-
public void Run()
30+
/// <param name="repository">The repository in which to fetch.</param>
31+
/// <param name="remote">The remote to fetch from. Either as a remote name or a URL</param>
32+
/// <param name="options">Fetch options.</param>
33+
/// <param name="logMessage">Log message for any ref updates.</param>
34+
/// <param name="refspecs">List of refspecs to apply as active.</param>
35+
public static void Fetch(Repository repository, string remote, IEnumerable<string> refspecs, FetchOptions options, string logMessage)
5636
{
57-
using (var remoteHandle = RemoteFromNameOrUrl())
37+
Ensure.ArgumentNotNull(remote, "remote");
38+
39+
options = options ?? new FetchOptions();
40+
using (var remoteHandle = RemoteFromNameOrUrl(repository.Handle, remote))
5841
{
5942

6043
var callbacks = new RemoteCallbacks(options);

LibGit2Sharp/Commands/Pull.cs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,27 @@
22
using LibGit2Sharp;
33
using LibGit2Sharp.Core;
44

5-
namespace LibGit2Sharp.Commands
5+
namespace LibGit2Sharp
66
{
77
/// <summary>
88
/// Fetch changes from the configured upstream remote and branch into the branch pointed at by HEAD.
99
/// </summary>
10-
public class Pull
10+
public static partial class Commands
1111
{
12-
private readonly Repository repository;
13-
private readonly Signature merger;
14-
private readonly PullOptions options;
15-
1612
/// <summary>
17-
/// Initializes a new instance of the <see cref="LibGit2Sharp.Commands.Pull"/> class.
13+
/// Fetch changes from the configured upstream remote and branch into the branch pointed at by HEAD.
1814
/// </summary>
1915
/// <param name="repository">The repository.</param>
2016
/// <param name="merger">The signature to use for the merge.</param>
2117
/// <param name="options">The options for fetch and merging.</param>
22-
public Pull(Repository repository, Signature merger, PullOptions options)
18+
public static MergeResult Pull(Repository repository, Signature merger, PullOptions options)
2319
{
2420
Ensure.ArgumentNotNull(repository, "repository");
2521
Ensure.ArgumentNotNull(merger, "merger");
2622
Ensure.ArgumentNotNull(options, "options");
2723

28-
this.repository = repository;
29-
this.merger = merger;
30-
this.options = options;
31-
}
32-
33-
/// <summary>
34-
/// Run this command
35-
/// </summary>
36-
public MergeResult Run()
37-
{
3824

25+
options = options ?? new PullOptions();
3926
Branch currentBranch = repository.Head;
4027

4128
if (!currentBranch.IsTracking)
@@ -48,7 +35,7 @@ public MergeResult Run()
4835
throw new LibGit2SharpException("No upstream remote for the current branch.");
4936
}
5037

51-
new Commands.Fetch(repository, currentBranch.RemoteName, new string[0], options.FetchOptions, null).Run();
38+
Commands.Fetch(repository, currentBranch.RemoteName, new string[0], options.FetchOptions, null);
5239
return repository.MergeFetchedRefs(merger, options.MergeOptions);
5340
}
5441
}

0 commit comments

Comments
 (0)