using System; using System.Globalization; using LibGit2Sharp.Core; namespace LibGit2Sharp { /// /// A branch is a special kind of reference /// public class Branch : IEquatable { private static readonly LambdaEqualityHelper equalityHelper = new LambdaEqualityHelper(new Func[] {x => x.CanonicalName, x => x.Tip}); private readonly Repository repo; /// /// Initializes a new instance of the class. /// /// The commit which is pointed at by this Branch /// The repo. /// The full name of the reference internal Branch(string canonicalName, Commit tip, Repository repo) { this.repo = repo; CanonicalName = canonicalName; Tip = tip; } /// /// Gets the full name of this branch. /// public string CanonicalName { get; private set; } /// /// Gets the name of this branch. /// public string Name { get { return ShortenName(CanonicalName); } } /// /// Gets a value indicating whether this instance is a remote. /// /// /// true if this instance is remote; otherwise, false. /// public bool IsRemote { get { return IsRemoteBranch(CanonicalName); } } /// /// Gets a value indicating whether this instance is current branch (HEAD) in the repository. /// /// /// true if this instance is current branch; otherwise, false. /// public bool IsCurrentRepositoryHead { get { return repo.Refs[CanonicalName].ResolveToDirectReference() == repo.Refs["HEAD"].ResolveToDirectReference(); } } /// /// Gets the commit id that this branch points to. /// public Commit Tip { get; private set; } /// /// Gets the commits on this branch. (Starts walking from the References's target). /// public ICommitCollection Commits { get { return repo.Commits.QueryBy(new Filter{Since = this}); } } #region IEquatable Members /// /// Determines whether the specified is equal to the current . /// /// The to compare with the current . /// True if the specified is equal to the current ; otherwise, false. public bool Equals(Branch other) { return equalityHelper.Equals(this, other); } #endregion /// /// Determines whether the specified is equal to the current . /// /// The to compare with the current . /// True if the specified is equal to the current ; otherwise, false. public override bool Equals(object obj) { return Equals(obj as Branch); } /// /// Returns the hash code for this instance. /// /// A 32-bit signed integer hash code. public override int GetHashCode() { return equalityHelper.GetHashCode(this); } private static bool IsRemoteBranch(string canonicalName) { return canonicalName.StartsWith("refs/remotes/", StringComparison.Ordinal); } private static string ShortenName(string branchName) { if (branchName.StartsWith("refs/heads/", StringComparison.Ordinal)) { return branchName.Substring("refs/heads/".Length); } if (branchName.StartsWith("refs/remotes/", StringComparison.Ordinal)) { return branchName.Substring("refs/remotes/".Length); } throw new ArgumentException(string.Format(CultureInfo.InvariantCulture,"'{0}' does not look like a valid branch name.", branchName)); } /// /// Tests if two are equal. /// /// First to compare. /// Second to compare. /// True if the two objects are equal; false otherwise. public static bool operator ==(Branch left, Branch right) { return Equals(left, right); } /// /// Tests if two are different. /// /// First to compare. /// Second to compare. /// True if the two objects are different; false otherwise. public static bool operator !=(Branch left, Branch right) { return !Equals(left, right); } /// /// Returns the , a representation of the current . /// /// The that represents the current . public override string ToString() { return CanonicalName; } } }