Skip to content

Commit ec7a71f

Browse files
committed
Introduce Stash.Apply and Stash.Drop
1 parent 8ca66fb commit ec7a71f

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,20 @@ internal static extern int git_stash_foreach(
13251325
[DllImport(libgit2)]
13261326
internal static extern int git_stash_drop(RepositorySafeHandle repo, UIntPtr index);
13271327

1328+
[DllImport(libgit2)]
1329+
internal static extern int git_stash_apply(
1330+
RepositorySafeHandle repo,
1331+
UIntPtr index,
1332+
ref GitCheckoutOpts opts,
1333+
StashApplyModifiers flags);
1334+
1335+
[DllImport(libgit2)]
1336+
internal static extern int git_stash_pop(
1337+
RepositorySafeHandle repo,
1338+
UIntPtr index,
1339+
ref GitCheckoutOpts opts,
1340+
StashApplyModifiers flags);
1341+
13281342
[DllImport(libgit2)]
13291343
internal static extern int git_status_file(
13301344
out FileStatus statusflags,

LibGit2Sharp/Core/Proxy.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,6 +2723,32 @@ public static void git_stash_drop(RepositorySafeHandle repo, int index)
27232723
}
27242724
}
27252725

2726+
public static void git_stash_apply(
2727+
RepositorySafeHandle repo,
2728+
int index,
2729+
ref GitCheckoutOpts opts,
2730+
StashApplyModifiers flags)
2731+
{
2732+
using (ThreadAffinity())
2733+
{
2734+
int res = NativeMethods.git_stash_apply(repo, (UIntPtr)index, ref opts, flags);
2735+
Ensure.ZeroResult(res);
2736+
}
2737+
}
2738+
2739+
public static void git_stash_pop(
2740+
RepositorySafeHandle repo,
2741+
int index,
2742+
ref GitCheckoutOpts opts,
2743+
StashApplyModifiers flags)
2744+
{
2745+
using (ThreadAffinity())
2746+
{
2747+
int res = NativeMethods.git_stash_pop(repo, (UIntPtr)index, ref opts, flags);
2748+
Ensure.ZeroResult(res);
2749+
}
2750+
}
2751+
27262752
#endregion
27272753

27282754
#region git_status_

LibGit2Sharp/StashCollection.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,36 @@ public virtual void Remove(int index)
114114
Proxy.git_stash_drop(repo.Handle, index);
115115
}
116116

117+
/// <summary>
118+
/// Applies a single stashed state from the stash list
119+
/// </summary>
120+
/// <param name="index">the index of the stash to remove (0 being the most recent one).</param>
121+
/// <param name="options">the options to use for checking out the stash.</param>
122+
/// <param name="flags">the flags to use for applying the changes.</param>
123+
public virtual void Apply(int index, CheckoutOptions options = null, StashApplyModifiers flags = StashApplyModifiers.Default)
124+
{
125+
using (GitCheckoutOptsWrapper checkoutOptionsWrapper = new GitCheckoutOptsWrapper(options))
126+
{
127+
var opts = checkoutOptionsWrapper.Options;
128+
Proxy.git_stash_apply(repo.Handle, index, ref opts, flags);
129+
}
130+
}
131+
132+
/// <summary>
133+
/// Pops a single stashed state from the stash list
134+
/// </summary>
135+
/// <param name="index">the index of the stash to remove (0 being the most recent one).</param>
136+
/// <param name="options">the options to use for checking out the stash.</param>
137+
/// <param name="flags">the flags to use for applying the changes.</param>
138+
public virtual void Pop(int index, CheckoutOptions options = null, StashApplyModifiers flags = StashApplyModifiers.Default)
139+
{
140+
using (GitCheckoutOptsWrapper checkoutOptionsWrapper = new GitCheckoutOptsWrapper(options))
141+
{
142+
var opts = checkoutOptionsWrapper.Options;
143+
Proxy.git_stash_pop(repo.Handle, index, ref opts, flags);
144+
}
145+
}
146+
117147
private string DebuggerDisplay
118148
{
119149
get

LibGit2Sharp/StashModifiers.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,23 @@ public enum StashModifiers
3131
/// </summary>
3232
IncludeIgnored = (1 << 2),
3333
}
34+
35+
///<summary>
36+
/// Options controlling Stash applying behavior.
37+
///</summary>
38+
[Flags]
39+
public enum StashApplyModifiers
40+
{
41+
/// <summary>
42+
/// Default. Reinstante working directory stashed
43+
/// changes.
44+
/// </summary>
45+
Default = 0,
46+
47+
/// <summary>
48+
/// Reinstate both index and working directory stashed
49+
/// changes.
50+
/// </summary>
51+
ReinstateIndex = (1 << 0),
52+
}
3453
}

0 commit comments

Comments
 (0)