Skip to content

Commit 6185c08

Browse files
yorahnulltoken
authored andcommitted
Add an indexer to the StashCollection
1 parent 31bf2d6 commit 6185c08

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

LibGit2Sharp.Tests/StashFixture.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,5 +207,71 @@ public void RemovingStashWithBadParamShouldThrow(int badIndex)
207207
Assert.Throws<ArgumentException>(() => repo.Stashes.Remove(badIndex));
208208
}
209209
}
210+
211+
[Fact]
212+
public void CanGetStashByIndexer()
213+
{
214+
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath);
215+
using (var repo = new Repository(path.RepositoryPath))
216+
{
217+
var stasher = DummySignature;
218+
const string firstStashMessage = "My very first stash";
219+
const string secondStashMessage = "My second stash";
220+
const string thirdStashMessage = "My third stash";
221+
222+
// Create first stash
223+
Stash firstStash = repo.Stashes.Add(stasher, firstStashMessage, StashOptions.IncludeUntracked);
224+
Assert.NotNull(firstStash);
225+
226+
// Create second stash
227+
string newFileFullPath = Path.Combine(repo.Info.WorkingDirectory, "stash_candidate.txt");
228+
File.WriteAllText(newFileFullPath, "Oh, I'm going to be stashed!\n");
229+
230+
Stash secondStash = repo.Stashes.Add(stasher, secondStashMessage, StashOptions.IncludeUntracked);
231+
Assert.NotNull(secondStash);
232+
233+
// Create third stash
234+
newFileFullPath = Path.Combine(repo.Info.WorkingDirectory, "stash_candidate_again.txt");
235+
File.WriteAllText(newFileFullPath, "Oh, I'm going to be stashed!\n");
236+
237+
238+
Stash thirdStash = repo.Stashes.Add(stasher, thirdStashMessage, StashOptions.IncludeUntracked);
239+
Assert.NotNull(thirdStash);
240+
241+
// Get by indexer
242+
Assert.Equal(3, repo.Stashes.Count());
243+
Assert.Equal("stash@{0}", repo.Stashes[0].CanonicalName);
244+
Assert.Contains(thirdStashMessage, repo.Stashes[0].Message);
245+
Assert.Equal(thirdStash.Target, repo.Stashes[0].Target);
246+
Assert.Equal("stash@{1}", repo.Stashes[1].CanonicalName);
247+
Assert.Contains(secondStashMessage, repo.Stashes[1].Message);
248+
Assert.Equal(secondStash.Target, repo.Stashes[1].Target);
249+
Assert.Equal("stash@{2}", repo.Stashes[2].CanonicalName);
250+
Assert.Contains(firstStashMessage, repo.Stashes[2].Message);
251+
Assert.Equal(firstStash.Target, repo.Stashes[2].Target);
252+
}
253+
}
254+
255+
[Theory]
256+
[InlineData(-1)]
257+
[InlineData(-42)]
258+
public void GettingStashWithBadIndexThrows(int badIndex)
259+
{
260+
using (var repo = new Repository(StandardTestRepoWorkingDirPath))
261+
{
262+
Assert.Throws<ArgumentOutOfRangeException>(() => repo.Stashes[badIndex]);
263+
}
264+
}
265+
266+
[Theory]
267+
[InlineData(28)]
268+
[InlineData(42)]
269+
public void GettingAStashThatDoesNotExistReturnsNull(int bigIndex)
270+
{
271+
using (var repo = new Repository(StandardTestRepoWorkingDirPath))
272+
{
273+
Assert.Null(repo.Stashes[bigIndex]);
274+
}
275+
}
210276
}
211277
}

LibGit2Sharp/StashCollection.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,24 @@ IEnumerator IEnumerable.GetEnumerator()
5757

5858
#endregion
5959

60+
/// <summary>
61+
/// Gets the <see cref = "Stash" /> corresponding to the specified index (0 being the most recent one).
62+
/// </summary>
63+
public virtual Stash this[int index]
64+
{
65+
get
66+
{
67+
if (index < 0)
68+
{
69+
throw new ArgumentOutOfRangeException("index", "The passed index must be a positive integer.");
70+
}
71+
72+
GitObject stashCommit = repo.Lookup(string.Format("stash@{{{0}}}", index), GitObjectType.Commit, LookUpOptions.None);
73+
74+
return stashCommit == null ? null : new Stash(repo, stashCommit.Id, index);
75+
}
76+
}
77+
6078
/// <summary>
6179
/// Creates a stash with the specified message.
6280
/// </summary>

0 commit comments

Comments
 (0)