using System;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
namespace Octokit.Helpers
{
///
/// Represents operations to simplify working with references
///
public static class ReferenceExtensions
{
///
/// Creates a branch, based off the branch specified.
///
/// The this method extends
/// The owner of the repository.
/// The name of the repository.
/// The new branch name
/// The to base the branch from
public static async Task CreateBranch(this IReferencesClient referencesClient, string owner, string name, string branchName, Reference baseReference)
{
Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner));
Ensure.ArgumentNotNullOrEmptyString(name, nameof(name));
Ensure.ArgumentNotNullOrEmptyString(branchName, nameof(branchName));
Ensure.ArgumentNotNull(baseReference, nameof(baseReference));
if (branchName.StartsWith("refs/heads"))
{
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "The specified branch name '{0}' appears to be a ref name and not a branch name because it starts with the string 'refs/heads'. Either specify just the branch name or use the Create method if you need to specify the full ref name", branchName), "branchName");
}
var newReference = new NewReference("refs/heads/" + branchName, baseReference.Object.Sha);
return await referencesClient.Create(owner, name, newReference).ConfigureAwait(false);
}
///
/// Creates a branch, based off the master branch.
///
/// The this method extends
/// The owner of the repository.
/// The name of the repository.
/// The new branch name
[Obsolete("This function is hard-coded to master branch, which is no longer a valid assumption for the base branch. Instead of making a second request to figure out the default branch, this extension method will be removed in a future release. Please check the default branch on the repository and use the overload passing in the default branch name")]
public static async Task CreateBranch(this IReferencesClient referencesClient, string owner, string name, string branchName)
{
Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner));
Ensure.ArgumentNotNullOrEmptyString(name, nameof(name));
Ensure.ArgumentNotNullOrEmptyString(branchName, nameof(branchName));
if (branchName.StartsWith("refs/heads"))
{
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "The specified branch name '{0}' appears to be a ref name and not a branch name because it starts with the string 'refs/heads'. Either specify just the branch name or use the Create method if you need to specify the full ref name", branchName), "branchName");
}
var baseBranch = await referencesClient.Get(owner, name, "heads/master").ConfigureAwait(false);
var newReference = new NewReference("refs/heads/" + branchName, baseBranch.Object.Sha);
return await referencesClient.Create(owner, name, newReference).ConfigureAwait(false);
}
///
/// Creates a branch, based off the default branch.
///
/// The this method extends
/// The owner of the repository.
/// The name of the repository.
/// The new branch name
/// The repository's default branch name
public static async Task CreateBranch(this IReferencesClient referencesClient, string owner, string name, string branchName, string defaultBranchName)
{
Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner));
Ensure.ArgumentNotNullOrEmptyString(name, nameof(name));
Ensure.ArgumentNotNullOrEmptyString(branchName, nameof(branchName));
if (branchName.StartsWith("refs/heads"))
{
throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "The specified branch name '{0}' appears to be a ref name and not a branch name because it starts with the string 'refs/heads'. Either specify just the branch name or use the Create method if you need to specify the full ref name", branchName), "branchName");
}
var baseBranch = await referencesClient.Get(owner, name, $"heads/{defaultBranchName}").ConfigureAwait(false);
var newReference = new NewReference("refs/heads/" + branchName, baseBranch.Object.Sha);
return await referencesClient.Create(owner, name, newReference).ConfigureAwait(false);
}
}
}