Permalink
Browse files

Add Repository.Stashes.GetEnumerator()

  • Loading branch information...
1 parent 0ab83f0 commit fd218c07649bce79b63cae868be30f077fb1ac33 @Saaman Saaman committed with nulltoken Feb 20, 2013
@@ -1,5 +1,6 @@
using System;
using System.IO;
+using System.Linq;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
@@ -53,8 +54,13 @@ public void CanAddStash()
Assert.Equal("stash@{0}", stash.CanonicalName);
Assert.Contains("My second stash", secondStash.Message);
+ Assert.Equal(2, repo.Stashes.Count());
+ Assert.Equal("stash@{0}", repo.Stashes.First().CanonicalName);
+ Assert.Equal("stash@{1}", repo.Stashes.Last().CanonicalName);
+
// Stash history has been shifted
Assert.Equal(repo.Lookup<Commit>("stash@{0}").Sha, secondStash.Target.Sha);
+ Assert.Equal(repo.Lookup<Commit>("stash@{1}").Sha, stash.Target.Sha);
}
}
@@ -912,6 +912,18 @@ private static bool IsRunningOnLinux()
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string message,
StashOptions flags);
+ internal delegate int git_stash_cb(
+ UIntPtr index,
+ IntPtr message,
+ ref GitOid stash_id,
+ IntPtr payload);
+
+ [DllImport(libgit2)]
+ internal static extern int git_stash_foreach(
+ RepositorySafeHandle repo,
+ git_stash_cb callback,
+ IntPtr payload);
+
[DllImport(libgit2)]
internal static extern int git_status_file(
out FileStatus statusflags,
View
@@ -1772,6 +1772,18 @@ public static SignatureSafeHandle git_signature_new(string name, string email, D
return new ObjectId(stashOid);
}
}
+
+ public static ICollection<TResult> git_stash_foreach<TResult>(
+ RepositorySafeHandle repo,
+ Func<int, IntPtr, GitOid, TResult> resultSelector)
+ {
+ return git_foreach(
+ resultSelector,
+ c => NativeMethods.git_stash_foreach(
+ repo, (UIntPtr i, IntPtr m, ref GitOid x, IntPtr p) => c((int)i, m, x, p), IntPtr.Zero),
+ GitErrorCode.NotFound);
+ }
+
#endregion
#region git_status_
@@ -2039,6 +2051,30 @@ public static ObjectId git_treebuilder_write(RepositorySafeHandle repo, TreeBuil
}
}
+ private static ICollection<TResult> git_foreach<T1, T2, T3, TResult>(
+ Func<T1, T2, T3, TResult> resultSelector,
+ Func<Func<T1, T2, T3, IntPtr, int>, int> iterator,
+ params GitErrorCode[] ignoredErrorCodes)
+ {
+ using (ThreadAffinity())
+ {
+ var result = new List<TResult>();
+ var res = iterator((w, x, y, payload) =>
+ {
+ result.Add(resultSelector(w, x, y));
+ return 0;
+ });
+
+ if (ignoredErrorCodes != null && ignoredErrorCodes.Contains((GitErrorCode)res))
+ {
+ return new TResult[0];
+ }
+
+ Ensure.ZeroResult(res);
+ return result;
+ }
+ }
+
public delegate TResult Func<T1, T2, T3, T4, T5, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5);
private static ICollection<TResult> git_foreach<T1, T2, T3, T4, TResult>(
View
@@ -12,10 +12,9 @@ public class Stash : ReferenceWrapper<Commit>
protected Stash()
{ }
- internal Stash(Repository repo, ObjectId targetId)
- : base(repo, new DirectReference("stash@{0}", repo, targetId), r => r.CanonicalName)
- {
- }
+ internal Stash(Repository repo, ObjectId targetId, int index)
+ : base(repo, new DirectReference(string.Format("stash@{{{0}}}", index), repo, targetId), r => r.CanonicalName)
+ { }
/// <summary>
/// Gets the <see cref = "Commit" /> that this stash points to.
@@ -1,11 +1,17 @@
-using LibGit2Sharp.Core;
+using System.Collections;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Globalization;
+using System.Linq;
+using LibGit2Sharp.Core;
namespace LibGit2Sharp
{
/// <summary>
/// The collection of <see cref = "Stash" />es in a <see cref = "Repository" />
/// </summary>
- public class StashCollection
+ [DebuggerDisplay("{DebuggerDisplay,nq}")]
+ public class StashCollection : IEnumerable<Stash>
{
internal readonly Repository repo;
@@ -24,6 +30,29 @@ internal StashCollection(Repository repo)
this.repo = repo;
}
+ #region Implementation of IEnumerable
+
+ /// <summary>
+ /// Returns an enumerator that iterates through the collection.
+ /// </summary>
+ /// <returns>An <see cref = "IEnumerator{T}" /> object that can be used to iterate through the collection.</returns>
+ public IEnumerator<Stash> GetEnumerator()
+ {
+ return Proxy.git_stash_foreach(repo.Handle,
+ (index, message, commitId) => new Stash(repo, new ObjectId(commitId), index)).GetEnumerator();
+ }
+
+ /// <summary>
+ /// Returns an enumerator that iterates through the collection.
+ /// </summary>
+ /// <returns>An <see cref = "IEnumerator" /> object that can be used to iterate through the collection.</returns>
+ IEnumerator IEnumerable.GetEnumerator()
+ {
+ return GetEnumerator();
+ }
+
+ #endregion
+
/// <summary>
/// Creates a stash with the specified message.
/// </summary>
@@ -45,7 +74,16 @@ public virtual Stash Add(Signature stasher, string message = null, StashOptions
return null;
}
- return new Stash(repo, oid);
+ return new Stash(repo, oid, 0);
+ }
+
+ private string DebuggerDisplay
+ {
+ get
+ {
+ return string.Format(CultureInfo.InvariantCulture,
+ "Count = {0}", this.Count());
+ }
}
}
}

0 comments on commit fd218c0

Please sign in to comment.