diff options
author | yorah <yoram.harmelin@gmail.com> | 2013-04-05 17:50:08 +0400 |
---|---|---|
committer | yorah <yoram.harmelin@gmail.com> | 2013-04-09 19:04:18 +0400 |
commit | 538fb13f2b753571c0a18e524942cc920dec3d63 (patch) | |
tree | 84dbd4dea92fdc9f16aa144e93a98bc005d412a4 /LibGit2Sharp/NetworkExtensions.cs | |
parent | 97e4d08b636aa607066490edec2dfbd7d8d0fd95 (diff) |
Add Push overloads taking Branch instances
Diffstat (limited to 'LibGit2Sharp/NetworkExtensions.cs')
-rw-r--r-- | LibGit2Sharp/NetworkExtensions.cs | 54 |
1 files changed, 52 insertions, 2 deletions
diff --git a/LibGit2Sharp/NetworkExtensions.cs b/LibGit2Sharp/NetworkExtensions.cs index 3dfdddfb..b675c399 100644 --- a/LibGit2Sharp/NetworkExtensions.cs +++ b/LibGit2Sharp/NetworkExtensions.cs @@ -1,6 +1,8 @@ using System.Globalization; +using System.Linq; using LibGit2Sharp.Core; using System.Collections.Generic; +using LibGit2Sharp.Handlers; namespace LibGit2Sharp { @@ -10,6 +12,54 @@ namespace LibGit2Sharp public static class NetworkExtensions { /// <summary> + /// Push the specified branch to its tracked branch on the remote. + /// </summary> + /// <param name="network">The <see cref="Network"/> being worked with.</param> + /// <param name="branch">The branch to push.</param> + /// <param name="onPushStatusError">Handler for reporting failed push updates.</param> + /// <param name="credentials">Credentials to use for user/pass authentication.</param> + /// <exception cref="LibGit2SharpException">Throws if either the Remote or the UpstreamBranchCanonicalName is not set.</exception> + public static void Push( + this Network network, + Branch branch, + PushStatusErrorHandler onPushStatusError = null, + Credentials credentials = null) + { + network.Push(new[] { branch }, onPushStatusError, credentials); + } + + /// <summary> + /// Push the specified branches to their tracked branches on the remote. + /// </summary> + /// <param name="network">The <see cref="Network"/> being worked with.</param> + /// <param name="branches">The branches to push.</param> + /// <param name="onPushStatusError">Handler for reporting failed push updates.</param> + /// <param name="credentials">Credentials to use for user/pass authentication.</param> + /// <exception cref="LibGit2SharpException">Throws if either the Remote or the UpstreamBranchCanonicalName is not set.</exception> + public static void Push( + this Network network, + IEnumerable<Branch> branches, + PushStatusErrorHandler onPushStatusError = null, + Credentials credentials = null) + { + var enumeratedBranches = branches as IList<Branch> ?? branches.ToList(); + + foreach (var branch in enumeratedBranches) + { + if (string.IsNullOrEmpty(branch.UpstreamBranchCanonicalName)) + { + throw new LibGit2SharpException(string.Format("The branch '{0}' (\"{1}\") that you are trying to push does not track an upstream branch.", + branch.Name, branch.CanonicalName)); + } + } + + foreach (var branch in enumeratedBranches) + { + network.Push(branch.Remote, string.Format("{0}:{1}", branch.CanonicalName, branch.UpstreamBranchCanonicalName), onPushStatusError); + } + } + + /// <summary> /// Push the objectish to the destination reference on the <see cref = "Remote" />. /// </summary> /// <param name="network">The <see cref="Network"/> being worked with.</param> @@ -46,7 +96,7 @@ namespace LibGit2Sharp Ensure.ArgumentNotNull(remote, "remote"); Ensure.ArgumentNotNullOrEmptyString(pushRefSpec, "pushRefSpec"); - return network.Push(remote, new string[] { pushRefSpec }, credentials); + return network.Push(remote, new[] { pushRefSpec }, credentials); } /// <summary> @@ -62,7 +112,7 @@ namespace LibGit2Sharp Ensure.ArgumentNotNull(remote, "remote"); Ensure.ArgumentNotNull(pushRefSpecs, "pushRefSpecs"); - List<PushStatusError> failedRemoteUpdates = new List<PushStatusError>(); + var failedRemoteUpdates = new List<PushStatusError>(); network.Push( remote, |