Skip to content

Commit 31bf2d6

Browse files
yorahnulltoken
authored andcommitted
Make StashCollection.Remove() take an index (int) param
I think we can safely avoid forcing the client to pass us the index using a "stash@{n}" pattern.
1 parent 7750d5b commit 31bf2d6

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

LibGit2Sharp.Tests/StashFixture.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void CanAddAndRemoveStash()
6464
Assert.Equal(repo.Lookup<Commit>("stash@{1}").Sha, stash.Target.Sha);
6565

6666
//Remove one stash
67-
repo.Stashes.Remove("stash@{0}");
67+
repo.Stashes.Remove(0);
6868
Assert.Equal(1, repo.Stashes.Count());
6969
Stash newTopStash = repo.Stashes.First();
7070
Assert.Equal("stash@{0}", newTopStash.CanonicalName);
@@ -197,16 +197,14 @@ public void CanStashIgnoredFiles()
197197
}
198198

199199
[Theory]
200-
[InlineData("stah@{0}")]
201-
[InlineData("stash@{0")]
202-
[InlineData("stash@{fake}")]
203-
[InlineData("dummy")]
204-
public void RemovingStashWithBadParamShouldThrow(string stashRefLog)
200+
[InlineData(-1)]
201+
[InlineData(-42)]
202+
public void RemovingStashWithBadParamShouldThrow(int badIndex)
205203
{
206204
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath);
207205
using (var repo = new Repository(path.RepositoryPath))
208206
{
209-
Assert.Throws<ArgumentException>(() => repo.Stashes.Remove(stashRefLog));
207+
Assert.Throws<ArgumentException>(() => repo.Stashes.Remove(badIndex));
210208
}
211209
}
212210
}

LibGit2Sharp/StashCollection.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ internal StashCollection(Repository repo)
3535

3636
/// <summary>
3737
/// Returns an enumerator that iterates through the collection.
38+
/// <para>
39+
/// The enumerator returns the stashes by descending order (last stash is returned first).
40+
/// </para>
3841
/// </summary>
3942
/// <returns>An <see cref = "IEnumerator{T}" /> object that can be used to iterate through the collection.</returns>
4043
public IEnumerator<Stash> GetEnumerator()
@@ -78,21 +81,36 @@ public virtual Stash Add(Signature stasher, string message = null, StashOptions
7881
return new Stash(repo, oid, 0);
7982
}
8083

84+
/// <summary>
85+
/// Remove a single stashed state from the stash list.
86+
/// </summary>
87+
/// <param name = "index">The index of the stash to remove (0 being the most recent one).</param>
88+
public virtual void Remove(int index)
89+
{
90+
if (index < 0)
91+
{
92+
throw new ArgumentException("The passed index must be a positive integer.", "index");
93+
}
94+
95+
Proxy.git_stash_drop(repo.Handle, index);
96+
}
97+
8198
/// <summary>
8299
/// Remove a single stashed state from the stash list.
83100
/// </summary>
84101
/// <param name = "stashRefLog">The log reference of the stash to delete. Pattern is "stash@{i}" where i is the index of the stash to remove</param>
102+
[Obsolete("This method will be removed in the next release. Please use Repository.Stashes.Remove(int) instead.")]
85103
public virtual void Remove(string stashRefLog)
86104
{
87105
Ensure.ArgumentNotNullOrEmptyString(stashRefLog, "stashRefLog");
88106

89107
int index;
90-
if(!TryExtractStashIndexFromRefLog(stashRefLog, out index) || index < 0)
108+
if (!TryExtractStashIndexFromRefLog(stashRefLog, out index) || index < 0)
91109
{
92110
throw new ArgumentException("must be a valid stash log reference. Pattern is 'stash@{i}' where 'i' is an integer", "stashRefLog");
93111
}
94112

95-
Proxy.git_stash_drop(repo.Handle, index);
113+
Remove(index);
96114
}
97115

98116
private static bool TryExtractStashIndexFromRefLog(string stashRefLog, out int index)

0 commit comments

Comments
 (0)