8000 Add Repository.Stashes.GetEnumerator() · mm201/libgit2sharp@fd218c0 · GitHub
[go: up one dir, main page]

Skip to content

Commit fd218c0

Browse files
Saamannulltoken
authored andcommitted
Add Repository.Stashes.GetEnumerator()
1 parent 0ab83f0 commit fd218c0

File tree

5 files changed

+98
-7
lines changed

5 files changed

+98
-7
lines changed

LibGit2Sharp.Tests/StashFixture.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Linq;
34
using LibGit2Sharp.Tests.TestHelpers;
45
using Xunit;
56

@@ -53,8 +54,13 @@ public void CanAddStash()
5354
Assert.Equal("stash@{0}", stash.CanonicalName);
5455
Assert.Contains("My second stash", secondStash.Message);
5556

57+
Assert.Equal(2, repo.Stashes.Count());
58+
Assert.Equal("stash@{0}", repo.Stashes.First().CanonicalName);
59+
Assert.Equal("stash@{1}", repo.Stashes.Last().CanonicalName);
60+
5661
// Stash history has been shifted
5762
Assert.Equal(repo.Lookup<Commit>("stash@{0}").Sha, secondStash.Target.Sha);
63+
Assert.Equal(repo.Lookup<Commit>("stash@{1}").Sha, stash.Target.Sha);
5864
}
5965
}
6066

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,18 @@ internal static extern int git_stash_save(
912912
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string message,
913913
StashOptions flags);
914914

915+
internal delegate int git_stash_cb(
916+
UIntPtr index,
917+
IntPtr message,
918+
ref GitOid stash_id,
919+
IntPtr payload);
920+
921+
[DllImport(libgit2)]
922+
internal static extern int git_stash_foreach(
923+
RepositorySafeHandle repo,
924+
git_stash_cb callback,
925+
IntPtr payload);
926+
915927
[DllImport(libgit2)]
916928
internal static extern int git_status_file(
917929
out FileStatus statusflags,

LibGit2Sharp/Core/Proxy.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,6 +1772,18 @@ public static ObjectId git_stash_save(
17721772
return new ObjectId(stashOid);
17731773
}
17741774
}
1775+
1776+
public static ICollection<TResult> git_stash_foreach<TResult>(
1777+
RepositorySafeHandle repo,
1778+
Func<int, IntPtr, GitOid, TResult> resultSelector)
1779+
{
1780+
return git_foreach(
1781+
resultSelector,
1782+
c => NativeMethods.git_stash_foreach(
1783+
repo, (UIntPtr i, IntPtr m, ref GitOid x, IntPtr p) => c((int)i, m, x, p), IntPtr.Zero),
1784+
GitErrorCode.NotFound);
1785+
}
1786+
17751787
#endregion
17761788

17771789
#region git_status_
@@ -2039,6 +2051,30 @@ private static ICollection<TResult> git_foreach<T1, T2, TResult>(
20392051
}
20402052
}
20412053

2054+
private static ICollection<TResult> git_foreach<T1, T2, T3, TResult>(
2055+
Func<T1, T2, T3, TResult> resultSelector,
2056+
Func<Func<T1, T2, T3, IntPtr, int>, int> iterator,
2057+
params GitErrorCode[] ignoredErrorCodes)
2058+
{
2059+
using (ThreadAffinity())
2060+
{
2061+
var result = new List<TResult>();
2062+
var res = iterator((w, x, y, payload) =>
2063+
{
2064+
result.Add(resultSelector(w, x, y));
2065+
return 0;
2066+
});
2067+
2068+
if (ignoredErrorCodes != null && ignoredErrorCodes.Contains((GitErrorCode)res))
2069+
{
2070+
return new TResult[0];
2071+
}
2072+
2073+
Ensure.ZeroResult(res);
2074+
return result;
2075+
}
2076+
}
2077+
20422078
public delegate TResult Func<T1, T2, T3, T4, T5, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5);
20432079

20442080
private static ICollection<TResult> git_foreach<T1, T2, T3, T4, TResult>(

LibGit2Sharp/Stash.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ public class Stash : ReferenceWrapper<Commit>
1212
protected Stash()
1313
{ }
1414

15-
internal Stash(Repository repo, ObjectId targetId)
16-
: base(repo, new DirectReference("stash@{0}", repo, targetId), r => r.CanonicalName)
17-
{
18-
}
15+
internal Stash(Repository repo, ObjectId targetId, int index)
16+
: base(repo, new DirectReference(string.Format("stash@{{{0}}}", index), repo, targetId), r => r.CanonicalName)
17+
{ }
1918

2019
/// <summary>
2120
/// Gets the <see cref = "Commit" /> that this stash points to.

LibGit2Sharp/StashCollection.cs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
using LibGit2Sharp.Core;
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Globalization;
5+
using System.Linq;
6+
using LibGit2Sharp.Core;
27

38
namespace LibGit2Sharp
49
{
510
/// <summary>
611
/// The collection of <see cref = "Stash" />es in a <see cref = "Repository" />
712
/// </summary>
8-
public class StashCollection
13+
[DebuggerDisplay("{DebuggerDisplay,nq}")]
14+
public class StashCollection : IEnumerable<Stash>
915
{
1016
internal readonly Repository repo;
1117

@@ -24,6 +30,29 @@ internal StashCollection(Repository repo)
2430
this.repo = repo;
2531
}
2632

33+
#region Implementation of IEnumerable
34+
35+
/// <summary>
36+
/// Returns an enumerator that iterates through the collection.
37+
/// </summary>
38+
/// <returns>An <see cref = "IEnumerator{T}" /> object that can be used to iterate through the collection.</returns>
39+
public IEnumerator<Stash> GetEnumerator()
40+
{
41+
return Proxy.git_stash_foreach(repo.Handle,
42+
(index, message, commitId) => new Stash(repo, new ObjectId(commitId), index)).GetEnumerator();
43+
}
44+
45+
/// <summary>
46+
/// Returns an enumerator that iterates through the collection.
47+
/// </summary>
48+
/// <returns>An <see cref = "IEnumerator" /> object that can be used to iterate through the collection.</returns>
49+
IEnumerator IEnumerable.GetEnumerator()
50+
{
51+
return GetEnumerator();
52+
}
53+
54+
#endregion
55+
2756
/// <summary>
2857
/// Creates a stash with the specified message.
2958
/// </summary>
@@ -45,7 +74,16 @@ public virtual Stash Add(Signature stasher, string message = null, StashOptions
4574
return null;
4675
}
4776

48-
return new Stash(repo, oid);
77+
return new Stash(repo, oid, 0);
78+
}
79+
80+
private string DebuggerDisplay
81+
{
82+
get
83+
{
84+
return string.Format(CultureInfo.InvariantCulture,
85+
"Count = {0}", this.Count());
86+
}
4987
}
5088
}
5189
}

0 commit comments

Comments
 (0)
0