|
| 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 = repository.Network.Remotes.RemoteForName(currentBranch.RemoteName)) |
| 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 | + |
0 commit comments