Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add Stashes.Remove

  • Loading branch information...
commit 7081cd6ebefc3b3ef25e6e3cf531d52b4764cb05 1 parent 01639e2
@Saaman Saaman authored
View
28 LibGit2Sharp.Tests/StashFixture.cs
@@ -3,6 +3,7 @@
using System.Linq;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
+using Xunit.Extensions;
namespace LibGit2Sharp.Tests
{
@@ -21,7 +22,7 @@ public void CannotAddStashAgainstBareRepository()
}
[Fact]
- public void CanAddStash()
+ public void CanAddAndRemoveStash()
{
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath);
using (var repo = new Repository(path.RepositoryPath))
@@ -61,6 +62,17 @@ public void CanAddStash()
// 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);
+
+ //Remove one stash
+ repo.Stashes.Remove("stash@{0}");
+ Assert.Equal(1, repo.Stashes.Count());
+ Stash newTopStash = repo.Stashes.First();
+ Assert.Equal("stash@{0}", newTopStash.CanonicalName);
+ Assert.Equal(stash.Target.Sha, newTopStash.Target.Sha);
+
+ // Stash history has been shifted
+ Assert.Equal(stash.Target.Sha, repo.Lookup<Commit>("stash").Sha);
+ Assert.Equal(stash.Target.Sha, repo.Lookup<Commit>("stash@{0}").Sha);
}
}
@@ -183,5 +195,19 @@ public void CanStashIgnoredFiles()
Assert.NotNull(blob);
}
}
+
+ [Theory]
+ [InlineData("stah@{0}")]
+ [InlineData("stash@{0")]
+ [InlineData("stash@{fake}")]
+ [InlineData("dummy")]
+ public void RemovingStashWithBadParamShouldThrow(string stashRefLog)
+ {
+ TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath);
+ using (var repo = new Repository(path.RepositoryPath))
+ {
+ Assert.Throws<ArgumentException>(() => repo.Stashes.Remove(stashRefLog));
+ }
+ }
}
}
View
3  LibGit2Sharp/Core/NativeMethods.cs
@@ -954,6 +954,9 @@ private static bool IsRunningOnLinux()
IntPtr payload);
[DllImport(libgit2)]
+ internal static extern int git_stash_drop(RepositorySafeHandle repo, UIntPtr index);
+
+ [DllImport(libgit2)]
internal static extern int git_status_file(
out FileStatus statusflags,
RepositorySafeHandle repo,
View
9 LibGit2Sharp/Core/Proxy.cs
@@ -1801,6 +1801,15 @@ public static SignatureSafeHandle git_signature_new(string name, string email, D
GitErrorCode.NotFound);
}
+ public static void git_stash_drop(RepositorySafeHandle repo, int index)
+ {
+ using (ThreadAffinity())
+ {
+ int res = NativeMethods.git_stash_drop(repo, (UIntPtr) index);
+ Ensure.BooleanResult(res);
+ }
+ }
+
#endregion
#region git_status_
View
39 LibGit2Sharp/StashCollection.cs
@@ -1,4 +1,5 @@
-using System.Collections;
+using System;
+using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
@@ -77,6 +78,42 @@ public virtual Stash Add(Signature stasher, string message = null, StashOptions
return new Stash(repo, oid, 0);
}
+ /// <summary>
+ /// Remove a single stashed state from the stash list.
+ /// </summary>
+ /// <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>
+ public virtual void Remove(string stashRefLog)
+ {
+ Ensure.ArgumentNotNullOrEmptyString(stashRefLog, "stashRefLog");
+
+ int index;
+ if(!TryExtractStashIndexFromRefLog(stashRefLog, out index) || index < 0)
+ {
+ throw new ArgumentException("must be a valid stash log reference. Pattern is 'stash@{i}' where 'i' is an integer", "stashRefLog");
+ }
+
+ Proxy.git_stash_drop(repo.Handle, index);
+ }
+
+ private static bool TryExtractStashIndexFromRefLog(string stashRefLog, out int index)
+ {
+ index = -1;
+
+ if (!stashRefLog.StartsWith("stash@{"))
+ {
+ return false;
+ }
+
+ if (!stashRefLog.EndsWith("}"))
+ {
+ return false;
+ }
+
+ var indexAsString = stashRefLog.Substring(7, stashRefLog.Length - 8);
+
+ return int.TryParse(indexAsString, out index);
+ }
+
private string DebuggerDisplay
{
get
Please sign in to comment.
Something went wrong with that request. Please try again.