Skip to content

Commit 2f54550

Browse files
committed
Add stash with paths options
1 parent 50d6978 commit 2f54550

File tree

6 files changed

+96
-0
lines changed

6 files changed

+96
-0
lines changed

LibGit2Sharp.Tests/StashFixture.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,22 @@ public void CanStashAndApplyWithOptions()
237237
}
238238
}
239239

240+
[Fact]
241+
public void CanStashWithFilePaths()
242+
{
243+
string path = SandboxStandardTestRepo();
244+
using (var repo = new Repository(path))
245+
{
246+
var stasher = Constants.Signature;
247+
248+
const string filename = "modified_unstaged_file.txt";
249+
250+
var stash = repo.Stashes.Add(stasher, "This stash one file", StashModifiers.Default, new[] { filename });
251+
252+
Assert.NotNull(stash);
253+
}
254+
}
255+
240256
[Fact]
241257
public void CanStashAndPop()
242258
{
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Runtime.InteropServices;
2+
3+
namespace LibGit2Sharp.Core
4+
{
5+
[StructLayout(LayoutKind.Sequential)]
6+
internal unsafe struct GitStashSaveOpts
7+
{
8+
public GitStashSaveOpts(
9+
StashModifiers flags,
10+
git_signature* stasher,
11+
string message,
12+
GitStrArray paths
13+
)
14+
{
15+
Version = 1;
16+
Flags = flags;
17+
Stasher = stasher;
18+
Message = message;
19+
Paths = paths;
20+
}
21+
22+
public uint Version;
23+
public StashModifiers Flags;
24+
public git_signature* Stasher;
25+
public string Message;
26+
public GitStrArray Paths;
27+
}
28+
}

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,6 +1744,12 @@ internal static extern unsafe int git_stash_save(
17441744
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string message,
17451745
StashModifiers flags);
17461746

1747+
[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
1748+
internal static extern unsafe int git_stash_save_with_opts(
1749+
out GitOid stashOid,
1750+
git_repository* repo,
1751+
ref GitStashSaveOpts saveOpts);
1752+
17471753
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
17481754
internal delegate int git_stash_cb(
17491755
UIntPtr index,

LibGit2Sharp/Core/Proxy.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2857,6 +2857,35 @@ public static unsafe ObjectId git_stash_save(
28572857
}
28582858
}
28592859

2860+
public static unsafe ObjectId git_stash_save_with_opts(
2861+
RepositoryHandle repo,
2862+
Signature stasher,
2863+
string message,
2864+
StashModifiers options,
2865+
string[] paths)
2866+
{
2867+
using var sigHandle = stasher.BuildHandle();
2868+
using var pathStrArray = GitStrArrayManaged.BuildFrom(paths ?? []);
2869+
2870+
var stashOpts = new GitStashSaveOpts(
2871+
options,
2872+
sigHandle,
2873+
message,
2874+
pathStrArray.Array
2875+
);
2876+
2877+
int res = NativeMethods.git_stash_save_with_opts(out var stashOid, repo, ref stashOpts);
2878+
2879+
if (res == (int)GitErrorCode.NotFound)
2880+
{
2881+
return null;
2882+
}
2883+
2884+
Ensure.Int32Result(res);
2885+
2886+
return new ObjectId(stashOid);
2887+
}
2888+
28602889
public static unsafe ICollection<TResult> git_stash_foreach<TResult>(
28612890
RepositoryHandle repo,
28622891
Func<int, IntPtr, GitOid, TResult> resultSelector)

LibGit2Sharp/StashCollection.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,18 @@ public virtual Stash Add(Signature stasher, string message)
113113
return Add(stasher, message, StashModifiers.Default);
114114
}
115115

116+
public virtual Stash Add(Signature stasher, string message, StashModifiers options, string[] paths)
117+
{
118+
ObjectId oid = Proxy.git_stash_save_with_opts(repo.Handle, stasher, message, options, paths);
119+
120+
if (oid == null)
121+
{
122+
return null;
123+
}
124+
125+
return new Stash(repo, oid, 0);
126+
}
127+
116128
/// <summary>
117129
/// Creates a stash with the specified message.
118130
/// </summary>

LibGit2Sharp/StashModifiers.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,10 @@ public enum StashModifiers
3030
/// cleaned up from the working directory
3131
/// </summary>
3232
IncludeIgnored = (1 << 2),
33+
34+
/// <summary>
35+
/// All changes in the index and working directory are left intact
36+
/// </summary>
37+
KeepAll = (1 << 3),
3338
}
3439
}

0 commit comments

Comments
 (0)