Skip to content

Commit db5ba8a

Browse files
committed
Convert Pull into a command
This makes it clearer that what we have there is a command and not a method fundamental to the Git system.
1 parent 5e683b9 commit db5ba8a

File tree

4 files changed

+68
-20
lines changed

4 files changed

+68
-20
lines changed

LibGit2Sharp.Tests/NetworkFixture.cs

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

167-
MergeResult mergeResult = repo.Network.Pull(Constants.Signature, pullOptions);
167+
MergeResult mergeResult = new Commands.Pull(repo, Constants.Signature, pullOptions).Run();
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 = repo.Network.Pull(Constants.Signature, new PullOptions());
200+
MergeResult mergeResult = new Commands.Pull(repo, Constants.Signature, new PullOptions()).Run();
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-
repo.Network.Pull(Constants.Signature, new PullOptions());
227+
new Commands.Pull(repo, Constants.Signature, new PullOptions()).Run();
228228
}
229229
catch(MergeFetchHeadNotFoundException ex)
230230
{

LibGit2Sharp/Commands/Pull.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using LibGit2Sharp;
3+
using LibGit2Sharp.Core;
4+
5+
namespace LibGit2Sharp.Commands
6+
{
7+
/// <summary>
8+
/// Fetch changes from the configured upstream remote and branch into the branch pointed at by HEAD.
9+
/// </summary>
10+
public class Pull
11+
{
12+
private readonly Repository repository;
13+
private readonly Signature merger;
14+
private readonly PullOptions options;
15+
16+
/// <summary>
17+
/// Initializes a new instance of the <see cref="LibGit2Sharp.Commands.Pull"/> class.
18+
/// </summary>
19+
/// <param name="repository">The repository.</param>
20+
/// <param name="merger">The signature to use for the merge.</param>
21+
/// <param name="options">The options for fetch and merging.</param>
22+
public Pull(Repository repository, Signature merger, PullOptions options)
23+
{
24+
Ensure.ArgumentNotNull(repository, "repository");
25+
Ensure.ArgumentNotNull(merger, "merger");
26+
Ensure.ArgumentNotNull(options, "options");
27+
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+
{
38+
39+
Branch currentBranch = repository.Head;
40+
41+
if (!currentBranch.IsTracking)
42+
{
43+
throw new LibGit2SharpException("There is no tracking information for the current branch.");
44+
}
45+
46+
using (var remote = currentBranch.Remote)
47+
{
48+
if (remote == null)
49+
{
50+
throw new LibGit2SharpException("No upstream remote for the current branch.");
51+
}
52+
53+
repository.Network.Fetch(remote, options.FetchOptions);
54+
return repository.MergeFetchedRefs(merger, options.MergeOptions);
55+
}
56+
}
57+
}
58+
}
59+

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@
350350
<Compile Include="Core\Handles\Libgit2Object.cs" />
351351
<Compile Include="Core\GitCredential.cs" />
352352
<Compile Include="Core\GitCredentialUserpass.cs" />
353+
<Compile Include="Commands\Pull.cs" />
353354
</ItemGroup>
354355
<ItemGroup>
355356
<CodeAnalysisDictionary Include="CustomDictionary.xml" />
@@ -386,4 +387,7 @@
386387
<Target Name="AfterBuild">
387388
</Target>
388389
-->
390+
<ItemGroup>
391+
<Folder Include="Commands\" />
392+
</ItemGroup>
389393
</Project>

LibGit2Sharp/Network.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -546,25 +546,10 @@ public virtual void Push(Remote remote, IEnumerable<string> pushRefSpecs, PushOp
546546
/// </summary>
547547
/// <param name="merger">If the merge is a non-fast forward merge that generates a merge commit, the <see cref="Signature"/> of who made the merge.</param>
548548
/// <param name="options">Specifies optional parameters controlling merge behavior of pull; if null, the defaults are used.</param>
549+
[Obsolete("This method is deprecated. Please use the LibGit2Sharp.Commands.Pull class")]
549550
public virtual MergeResult Pull(Signature merger, PullOptions options)
550551
{
551-
Ensure.ArgumentNotNull(merger, "merger");
552-
Ensure.ArgumentNotNull(options, "options");
553-
554-
Branch currentBranch = repository.Head;
555-
556-
if (!currentBranch.IsTracking)
557-
{
558-
throw new LibGit2SharpException("There is no tracking information for the current branch.");
559-
}
560-
561-
if (currentBranch.Remote == null)
562-
{
563-
throw new LibGit2SharpException("No upstream remote for the current branch.");
564-
}
565-
566-
Fetch(currentBranch.Remote, options.FetchOptions);
567-
return repository.MergeFetchedRefs(merger, options.MergeOptions);
552+
return new Commands.Pull(repository, merger, options).Run();
568553
}
569554

570555
/// <summary>

0 commit comments

Comments
 (0)