using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.Linq; using LibGit2Sharp.Core; namespace LibGit2Sharp { /// /// The collection of s in a /// [DebuggerDisplay("{DebuggerDisplay,nq}")] public class TagCollection : IEnumerable { internal readonly Repository repo; /// /// Needed for mocking purposes. /// protected TagCollection() { } /// /// Initializes a new instance of the class. /// /// The repo. internal TagCollection(Repository repo) { this.repo = repo; } /// /// Gets the with the specified name. /// public virtual Tag this[string name] { get { Ensure.ArgumentNotNullOrEmptyString(name, "name"); var canonicalName = NormalizeToCanonicalName(name); var reference = repo.Refs.Resolve(canonicalName); return reference == null ? null : new Tag(repo, reference, canonicalName); } } #region IEnumerable Members /// /// Returns an enumerator that iterates through the collection. /// /// An object that can be used to iterate through the collection. public virtual IEnumerator GetEnumerator() { return Proxy .git_tag_list(repo.Handle) .Select(n => this[n]) .GetEnumerator(); } /// /// Returns an enumerator that iterates through the collection. /// /// An object that can be used to iterate through the collection. IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } #endregion /// /// Creates an annotated tag with the specified name. /// /// The name. /// The target . /// The tagger. /// The message. /// True to allow silent overwriting a potentially existing tag, false otherwise. /// The added . public virtual Tag Add(string name, GitObject target, Signature tagger, string message, bool allowOverwrite = false) { Ensure.ArgumentNotNullOrEmptyString(name, "name"); Ensure.ArgumentNotNull(target, "target"); Ensure.ArgumentNotNull(tagger, "tagger"); Ensure.ArgumentNotNull(message, "message"); string prettifiedMessage = Proxy.git_message_prettify(message); Proxy.git_tag_create(repo.Handle, name, target, tagger, prettifiedMessage, allowOverwrite); return this[name]; } /// /// Creates a lightweight tag with the specified name. /// /// The name. /// The target . /// True to allow silent overwriting a potentially existing tag, false otherwise. /// The added . public virtual Tag Add(string name, GitObject target, bool allowOverwrite = false) { Ensure.ArgumentNotNullOrEmptyString(name, "name"); Ensure.ArgumentNotNull(target, "target"); Proxy.git_tag_create_lightweight(repo.Handle, name, target, allowOverwrite); return this[name]; } /// /// Deletes the tag with the specified name. /// /// The tag to delete. public virtual void Remove(Tag tag) { Ensure.ArgumentNotNull(tag, "tag"); this.Remove(tag.CanonicalName); } private static string NormalizeToCanonicalName(string name) { Ensure.ArgumentNotNullOrEmptyString(name, "name"); if (name.LooksLikeTag()) { return name; } return string.Concat(Reference.TagPrefix, name); } internal static string UnCanonicalizeName(string name) { Ensure.ArgumentNotNullOrEmptyString(name, "name"); if (!name.LooksLikeTag()) { return name; } return name.Substring(Reference.TagPrefix.Length); } private string DebuggerDisplay { get { return string.Format(CultureInfo.InvariantCulture, "Count = {0}", this.Count()); } } } }