using System;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
namespace Octokit
{
///
/// Used to create a new Git reference.
///
/// API: https://developer.github.com/v3/git/refs/#create-a-reference
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class NewReference
{
const string _refsPrefix = "refs";
///
/// Initializes a new instance of the class.
///
///
/// The name of the fully qualified reference (ie: refs/heads/master). If it doesn’t start with ‘refs’ and
/// have at least two slashes, it will be rejected.
///
/// The SHA1 value to set this reference to
public NewReference(string reference, string sha)
{
Ensure.ArgumentNotNullOrEmptyString(reference, "reference");
Ensure.ArgumentNotNullOrEmptyString(sha, "sha");
Ref = GetReference(reference);
Sha = sha;
}
///
/// The name of the fully qualified reference (ie: refs/heads/master). If it doesn’t start with ‘refs’ and
/// have at least two slashes, it will be rejected.
///
///
/// The reference.
///
public string Ref { get; private set; }
///
/// The SHA1 value to set this reference to
///
///
/// The sha.
///
public string Sha { get; private set; }
static string GetReference(string reference)
{
var parts = reference.Split('/').ToList();
var refsPart = parts.FirstOrDefault();
if (refsPart != null && refsPart != _refsPrefix)
{
parts.Insert(0, _refsPrefix);
}
if (parts.Count < 3)
{
throw new FormatException("Reference must start with 'refs' and have at least two slashes.");
}
return string.Join("/", parts);
}
internal string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture, "Ref: {0} Sha: {1}", Ref, Sha);
}
}
}
}