using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.Linq; using LibGit2Sharp.Core; namespace LibGit2Sharp { /// /// The collection of es in a /// [DebuggerDisplay("{DebuggerDisplay,nq}")] public class StashCollection : IEnumerable { internal readonly Repository repo; /// /// Needed for mocking purposes. /// protected StashCollection() { } /// /// Initializes a new instance of the class. /// /// The repo. internal StashCollection(Repository repo) { this.repo = repo; } #region Implementation of IEnumerable /// /// Returns an enumerator that iterates through the collection. /// /// The enumerator returns the stashes by descending order (last stash is returned first). /// /// /// An object that can be used to iterate through the collection. public virtual IEnumerator GetEnumerator() { return Proxy.git_stash_foreach(repo.Handle, (index, message, commitId) => new Stash(repo, new ObjectId(commitId), index)).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 /// /// Gets the corresponding to the specified index (0 being the most recent one). /// public virtual Stash this[int index] { get { if (index < 0) { throw new ArgumentOutOfRangeException("index", "The passed index must be a positive integer."); } GitObject stashCommit = repo.Lookup( string.Format(CultureInfo.InvariantCulture, "stash@{{{0}}}", index), GitObjectType.Commit, LookUpOptions.None); return stashCommit == null ? null : new Stash(repo, stashCommit.Id, index); } } /// /// Creates a stash with the specified message. /// /// The of the user who stashes /// The message of the stash. /// A combination of flags /// the newly created public virtual Stash Add(Signature stasher, string message = null, StashModifiers options = StashModifiers.Default) { Ensure.ArgumentNotNull(stasher, "stasher"); string prettifiedMessage = Proxy.git_message_prettify(string.IsNullOrEmpty(message) ? string.Empty : message); ObjectId oid = Proxy.git_stash_save(repo.Handle, stasher, prettifiedMessage, options); // in case there is nothing to stash if (oid == null) { return null; } return new Stash(repo, oid, 0); } /// /// Remove a single stashed state from the stash list. /// /// The index of the stash to remove (0 being the most recent one). public virtual void Remove(int index) { if (index < 0) { throw new ArgumentException("The passed index must be a positive integer.", "index"); } Proxy.git_stash_drop(repo.Handle, index); } private string DebuggerDisplay { get { return string.Format(CultureInfo.InvariantCulture, "Count = {0}", this.Count()); } } } }