using System.Globalization; using System.Linq; using LibGit2Sharp.Core; using System.Collections.Generic; using LibGit2Sharp.Handlers; namespace LibGit2Sharp { /// /// Provides helper overloads to a . /// public static class NetworkExtensions { /// /// Push the specified branch to its tracked branch on the remote. /// /// The being worked with. /// The branch to push. /// Handler for reporting failed push updates. /// Credentials to use for user/pass authentication. /// Throws if either the Remote or the UpstreamBranchCanonicalName is not set. public static void Push( this Network network, Branch branch, PushStatusErrorHandler onPushStatusError = null, Credentials credentials = null) { network.Push(new[] { branch }, onPushStatusError, credentials); } /// /// Push the specified branches to their tracked branches on the remote. /// /// The being worked with. /// The branches to push. /// Handler for reporting failed push updates. /// Credentials to use for user/pass authentication. /// Throws if either the Remote or the UpstreamBranchCanonicalName is not set. public static void Push( this Network network, IEnumerable branches, PushStatusErrorHandler onPushStatusError = null, Credentials credentials = null) { var enumeratedBranches = branches as IList ?? 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); } } /// /// Push the objectish to the destination reference on the . /// /// The being worked with. /// The to push to. /// The source objectish to push. /// The reference to update on the remote. /// Credentials to use for user/pass authentication /// Results of the push operation. public static PushResult Push( this Network network, Remote remote, string objectish, string destinationSpec, Credentials credentials = null) { Ensure.ArgumentNotNull(remote, "remote"); Ensure.ArgumentNotNull(objectish, "objectish"); Ensure.ArgumentNotNullOrEmptyString(destinationSpec, "destinationSpec"); return network.Push(remote, string.Format(CultureInfo.InvariantCulture, "{0}:{1}", objectish, destinationSpec), credentials); } /// /// Push specified reference to the . /// /// The being worked with. /// The to push to. /// The pushRefSpec to push. /// Credentials to use for user/pass authentication /// Results of the push operation. public static PushResult Push(this Network network, Remote remote, string pushRefSpec, Credentials credentials = null) { Ensure.ArgumentNotNull(remote, "remote"); Ensure.ArgumentNotNullOrEmptyString(pushRefSpec, "pushRefSpec"); return network.Push(remote, new[] { pushRefSpec }, credentials); } /// /// Push specified references to the . /// /// The being worked with. /// The to push to. /// The pushRefSpecs to push. /// Credentials to use for user/pass authentication /// Results of the push operation. public static PushResult Push(this Network network, Remote remote, IEnumerable pushRefSpecs, Credentials credentials = null) { Ensure.ArgumentNotNull(remote, "remote"); Ensure.ArgumentNotNull(pushRefSpecs, "pushRefSpecs"); var failedRemoteUpdates = new List(); network.Push( remote, pushRefSpecs, failedRemoteUpdates.Add, credentials); return new PushResult(failedRemoteUpdates); } } }