Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion LibGit2Sharp.Tests/StashFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
using Xunit.Extensions;

namespace LibGit2Sharp.Tests
{
Expand All @@ -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))
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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));
}
}
}
}
3 changes: 3 additions & 0 deletions LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,9 @@ internal static extern int git_stash_foreach(
git_stash_cb callback,
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,
Expand Down
9 changes: 9 additions & 0 deletions LibGit2Sharp/Core/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1801,6 +1801,15 @@ public static ICollection<TResult> git_stash_foreach<TResult>(
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_
Expand Down
39 changes: 38 additions & 1 deletion LibGit2Sharp/StashCollection.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
Expand Down Expand Up @@ -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");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential nitpick: "... where is a positive integer" ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Saaman Could you include this fix as a separate commit while working on #371?

}

Proxy.git_stash_drop(repo.Handle, index);
}

private static bool TryExtractStashIndexFromRefLog(string stashRefLog, out int index)
{
index = -1;

if (!stashRefLog.StartsWith("stash@{"))
{
return false;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style related nitpick. Can you please surround this statement with braces?

}

if (!stashRefLog.EndsWith("}"))
{
return false;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some braces would also be required here

}

var indexAsString = stashRefLog.Substring(7, stashRefLog.Length - 8);

return int.TryParse(indexAsString, out index);
}

private string DebuggerDisplay
{
get
Expand Down