Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7081cd6

Browse files
committed
Add Stashes.Remove
1 parent 01639e2 commit 7081cd6

File tree

4 files changed

+77
-2
lines changed

4 files changed

+77
-2
lines changed

LibGit2Sharp.Tests/StashFixture.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using LibGit2Sharp.Tests.TestHelpers;
55
using Xunit;
6+
using Xunit.Extensions;
67

78
namespace LibGit2Sharp.Tests
89
{
@@ -21,7 +22,7 @@ public void CannotAddStashAgainstBareRepository()
2122
}
2223

2324
[Fact]
24-
public void CanAddStash()
25+
public void CanAddAndRemoveStash()
2526
{
2627
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath);
2728
using (var repo = new Repository(path.RepositoryPath))
@@ -61,6 +62,17 @@ public void CanAddStash()
6162
// Stash history has been shifted
6263
Assert.Equal(repo.Lookup<Commit>("stash@{0}").Sha, secondStash.Target.Sha);
6364
Assert.Equal(repo.Lookup<Commit>("stash@{1}").Sha, stash.Target.Sha);
65+
66+
//Remove one stash
67+
repo.Stashes.Remove("stash@{0}");
68+
Assert.Equal(1, repo.Stashes.Count());
69+
Stash newTopStash = repo.Stashes.First();
70+
Assert.Equal("stash@{0}", newTopStash.CanonicalName);
71+
Assert.Equal(stash.Target.Sha, newTopStash.Target.Sha);
72+
73+
// Stash history has been shifted
74+
Assert.Equal(stash.Target.Sha, repo.Lookup<Commit>("stash").Sha);
75+
Assert.Equal(stash.Target.Sha, repo.Lookup<Commit>("stash@{0}").Sha);
6476
}
6577
}
6678

@@ -183,5 +195,19 @@ public void CanStashIgnoredFiles()
183195
Assert.NotNull(blob);
184196
}
185197
}
198+
199+
[Theory]
200+
[InlineData("stah@{0}")]
201+
[InlineData("stash@{0")]
202+
[InlineData("stash@{fake}")]
203+
[InlineData("dummy")]
204+
public void RemovingStashWithBadParamShouldThrow(string stashRefLog)
205+
{
206+
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath);
207+
using (var repo = new Repository(path.RepositoryPath))
208+
{
209+
Assert.Throws<ArgumentException>(() => repo.Stashes.Remove(stashRefLog));
210+
}
211+
}
186212
}
187213
}

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,9 @@ internal static extern int git_stash_foreach(
953953
git_stash_cb callback,
954954
IntPtr payload);
955955

956+
[DllImport(libgit2)]
957+
internal static extern int git_stash_drop(RepositorySafeHandle repo, UIntPtr index);
958+
956959
[DllImport(libgit2)]
957960
internal static extern int git_status_file(
958961
out FileStatus statusflags,

LibGit2Sharp/Core/Proxy.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,6 +1801,15 @@ public static ICollection<TResult> git_stash_foreach<TResult>(
18011801
GitErrorCode.NotFound);
18021802
}
18031803

1804+
public static void git_stash_drop(RepositorySafeHandle repo, int index)
1805+
{
1806+
using (ThreadAffinity())
1807+
{
1808+
int res = NativeMethods.git_stash_drop(repo, (UIntPtr) index);
1809+
Ensure.BooleanResult(res);
1810+
}
1811+
}
1812+
18041813
#endregion
18051814

18061815
#region git_status_

LibGit2Sharp/StashCollection.cs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections;
1+
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.Diagnostics;
45
using System.Globalization;
@@ -77,6 +78,42 @@ public virtual Stash Add(Signature stasher, string message = null, StashOptions
7778
return new Stash(repo, oid, 0);
7879
}
7980

81+
/// <summary>
82+
/// Remove a single stashed state from the stash list.
83+
/// </summary>
84+
/// <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>
85+
public virtual void Remove(string stashRefLog)
86+
{
87+
Ensure.ArgumentNotNullOrEmptyString(stashRefLog, "stashRefLog");
88+
89+
int index;
90+
if(!TryExtractStashIndexFromRefLog(stashRefLog, out index) || index < 0)
91+
{
92+
throw new ArgumentException("must be a valid stash log reference. Pattern is 'stash@{i}' where 'i' is an integer", "stashRefLog");
93+
}
94+
95+
Proxy.git_stash_drop(repo.Handle, index);
96+
}
97+
98+
private static bool TryExtractStashIndexFromRefLog(string stashRefLog, out int index)
99+
{
100+
index = -1;
101+
102+
if (!stashRefLog.StartsWith("stash@{"))
103+
{
104+
return false;
105+
}
106+
107+
if (!stashRefLog.EndsWith("}"))
108+
{
109+
return false;
110+
}
111+
112+
var indexAsString = stashRefLog.Substring(7, stashRefLog.Length - 8);
113+
114+
return int.TryParse(indexAsString, out index);
115+
}
116+
80117
private string DebuggerDisplay
81118
{
82119
get

0 commit comments

Comments
 (0)