Skip to content

Commit 957530c

Browse files
committed
Prettify Stash.Apply and Stash.Drop
1 parent ca88162 commit 957530c

File tree

15 files changed

+159
-22
lines changed

15 files changed

+159
-22
lines changed
-1010 KB
Binary file not shown.
1020 KB
Binary file not shown.
-757 KB
Binary file not shown.
761 KB
Binary file not shown.

LibGit2Sharp.Tests/StashFixture.cs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,99 @@ public void CanStashAndKeepIndex()
176176
}
177177
}
178178

179+
[Fact]
180+
public void CanStashAndApplyWithOptions()
181+
{
182+
string path = SandboxStandardTestRepo();
183+
using (var repo = new Repository(path))
184+
{
185+
var stasher = Constants.Signature;
186+
187+
const string filename = "staged_file_path.txt";
188+
Touch(repo.Info.WorkingDirectory, filename, "I'm staged\n");
189+
repo.Stage(filename);
190+
191+
repo.Stashes.Add(stasher, "This stash with default options");
192+
Assert.Equal(StashApplyStatus.Applied, repo.Stashes.Apply(0));
193+
194+
Assert.Equal(FileStatus.Untracked, repo.RetrieveStatus(filename));
195+
Assert.Equal(1, repo.Stashes.Count());
196+
197+
repo.Stage(filename);
198+
199+
repo.Stashes.Add(stasher, "This stash with default options");
200+
Assert.Equal(StashApplyStatus.Applied, repo.Stashes.Apply(0, StashApplyModifiers.ReinstateIndex));
201+
202+
Assert.Equal(FileStatus.Added, repo.RetrieveStatus(filename));
203+
Assert.Equal(2, repo.Stashes.Count());
204+
}
205+
}
206+
207+
[Fact]
208+
public void CanStashAndPop()
209+
{
210+
string path = SandboxStandardTestRepo();
211+
using (var repo = new Repository(path))
212+
{
213+
var stasher = Constants.Signature;
214+
215+
const string filename = "staged_file_path.txt";
216+
Touch(repo.Info.WorkingDirectory, filename, "I'm staged\n");
217+
repo.Stage(filename);
218+
219+
repo.Stashes.Add(stasher, "This stash with default options");
220+
Assert.Equal(StashApplyStatus.Applied, repo.Stashes.Pop(0));
221+
222+
Assert.Equal(FileStatus.Untracked, repo.RetrieveStatus(filename));
223+
Assert.Equal(0, repo.Stashes.Count());
224+
}
225+
}
226+
227+
[Fact]
228+
public void StashReportsConflictsWhenReinstated()
229+
{
230+
string path = SandboxStandardTestRepo();
231+
using (var repo = new Repository(path))
232+
{
233+
var stasher = Constants.Signature;
234+
235+
const string filename = "staged_file_path.txt";
236+
const string filename2 = "unstaged_file_path.txt";
237+
Touch(repo.Info.WorkingDirectory, filename, "I'm staged\n");
238+
repo.Stage(filename);
239+
Touch(repo.Info.WorkingDirectory, filename2, "I'm unstaged\n");
240+
241+
repo.Stashes.Add(stasher, "This stash with default options");
242+
243+
Touch(repo.Info.WorkingDirectory, filename, "I'm another staged\n");
244+
repo.Stage(filename);
245+
Touch(repo.Info.WorkingDirectory, filename2, "I'm unstaged another staged\n");
246+
247+
Assert.Equal(StashApplyStatus.Conflicts, repo.Stashes.Pop(0, StashApplyModifiers.ReinstateIndex));
248+
249+
// TODO: Find out why repo.Index.Conflicts doesn't have any data.
250+
Assert.NotNull(repo.Index.Conflicts[filename]);
251+
}
252+
}
253+
254+
[Fact]
255+
public void StashReportsExistingInWorkDir()
256+
{
257+
string path = SandboxStandardTestRepo();
258+
using (var repo = new Repository(path))
259+
{
260+
var stasher = Constants.Signature;
261+
262+
const string filename = "unstaged_file_path.txt";
263+
Touch(repo.Info.WorkingDirectory, filename, "I'm unstaged\n");
264+
265+
repo.Stashes.Add(stasher, "This stash with default options", StashModifiers.IncludeUntracked);
266+
Touch(repo.Info.WorkingDirectory, filename, "I'm another unstaged\n");
267+
268+
Assert.Equal(StashApplyStatus.UntrackedExist, repo.Stashes.Pop(0));
269+
}
270+
}
271+
179272
[Fact]
180273
public void CanStashIgnoredFiles()
181274
{

LibGit2Sharp/Core/GitCheckoutOpts.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ internal struct GitCheckoutOpts
158158
public GitStrArray paths;
159159

160160
public IntPtr baseline;
161+
public IntPtr baseline_index;
161162
public IntPtr target_directory;
162163

163164
public IntPtr ancestor_label;

LibGit2Sharp/Core/NativeDllName.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ namespace LibGit2Sharp.Core
22
{
33
internal static class NativeDllName
44
{
5-
public const string Name = "git2-9bbc8f3";
5+
public const string Name = "git2-b2d0243";
66
}
77
}

LibGit2Sharp/Core/Proxy.cs

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

2726-
public static void git_stash_apply(
2726+
static StashApplyStatus get_stash_status(int res)
2727+
{
2728+
if (res == (int)GitErrorCode.MergeConflict)
2729+
{
2730+
return StashApplyStatus.Conflicts;
2731+
}
2732+
if (res == (int)GitErrorCode.Exists)
2733+
{
2734+
return StashApplyStatus.UntrackedExist;
2735+
}
2736+
Ensure.ZeroResult(res);
2737+
return StashApplyStatus.Applied;
2738+
}
2739+
2740+
public static StashApplyStatus git_stash_apply(
27272741
RepositorySafeHandle repo,
27282742
int index,
27292743
ref GitCheckoutOpts opts,
27302744
StashApplyModifiers flags)
27312745
{
27322746
using (ThreadAffinity())
27332747
{
2734-
int res = NativeMethods.git_stash_apply(repo, (UIntPtr)index, ref opts, flags);
2735-
Ensure.ZeroResult(res);
2748+
return get_stash_status(NativeMethods.git_stash_apply(repo, (UIntPtr)index, ref opts, flags));
27362749
}
27372750
}
27382751

2739-
public static void git_stash_pop(
2752+
public static StashApplyStatus git_stash_pop(
27402753
RepositorySafeHandle repo,
27412754
int index,
27422755
ref GitCheckoutOpts opts,
27432756
StashApplyModifiers flags)
27442757
{
27452758
using (ThreadAffinity())
27462759
{
2747-
int res = NativeMethods.git_stash_pop(repo, (UIntPtr)index, ref opts, flags);
2748-
Ensure.ZeroResult(res);
2760+
return get_stash_status(NativeMethods.git_stash_pop(repo, (UIntPtr)index, ref opts, flags));
27492761
}
27502762
}
27512763

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
<Compile Include="RevertResult.cs" />
135135
<Compile Include="RevertOptions.cs" />
136136
<Compile Include="StageOptions.cs" />
137+
<Compile Include="StashApplyStatus.cs" />
137138
<Compile Include="StatusOptions.cs" />
138139
<Compile Include="SimilarityOptions.cs" />
139140
<Compile Include="Log.cs" />

LibGit2Sharp/StashApplyStatus.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace LibGit2Sharp
7+
{
8+
/// <summary>
9+
/// The status of what happened as a result of a stash application.
10+
/// </summary>
11+
public enum StashApplyStatus
12+
{
13+
/// <summary>
14+
/// The changes were successfully stashed.
15+
/// </summary>
16+
Applied,
17+
18+
/// <summary>
19+
/// The stash application resulted in conflicts.
20+
/// </summary>
21+
Conflicts,
22+
23+
/// <summary>
24+
/// The stash application was not applied due to existing
25+
/// untracked files that would be overwritten by the stash
26+
/// contents.
27+
/// </summary>
28+
UntrackedExist,
29+
}
30+
}

LibGit2Sharp/StashCollection.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ public virtual void Remove(int index)
120120
/// <param name="index">the index of the stash to remove (0 being the most recent one).</param>
121121
/// <param name="options">the options to use for checking out the stash.</param>
122122
/// <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)
123+
public virtual StashApplyStatus Apply(int index, StashApplyModifiers flags = StashApplyModifiers.Default, CheckoutOptions options = null)
124124
{
125-
using (GitCheckoutOptsWrapper checkoutOptionsWrapper = new GitCheckoutOptsWrapper(options))
125+
using (GitCheckoutOptsWrapper checkoutOptionsWrapper = new GitCheckoutOptsWrapper(options ?? new CheckoutOptions()))
126126
{
127127
var opts = checkoutOptionsWrapper.Options;
128-
Proxy.git_stash_apply(repo.Handle, index, ref opts, flags);
128+
return Proxy.git_stash_apply(repo.Handle, index, ref opts, flags);
129129
}
130130
}
131131

@@ -135,12 +135,12 @@ public virtual void Apply(int index, CheckoutOptions options = null, StashApplyM
135135
/// <param name="index">the index of the stash to remove (0 being the most recent one).</param>
136136
/// <param name="options">the options to use for checking out the stash.</param>
137137
/// <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)
138+
public virtual StashApplyStatus Pop(int index, StashApplyModifiers flags = StashApplyModifiers.Default, CheckoutOptions options = null)
139139
{
140-
using (GitCheckoutOptsWrapper checkoutOptionsWrapper = new GitCheckoutOptsWrapper(options))
140+
using (GitCheckoutOptsWrapper checkoutOptionsWrapper = new GitCheckoutOptsWrapper(options ?? new CheckoutOptions()))
141141
{
142142
var opts = checkoutOptionsWrapper.Options;
143-
Proxy.git_stash_pop(repo.Handle, index, ref opts, flags);
143+
return Proxy.git_stash_pop(repo.Handle, index, ref opts, flags);
144144
}
145145
}
146146

LibGit2Sharp/libgit2_hash.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9bbc8f350b80a5a6e94651ec667cf9e5d545b317
1+
b2d02433141ddc4c914a6b0e0027f6c58fc11cb6

nuget.package/build/LibGit2Sharp.props

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<ItemGroup Condition=" '$(OS)' == 'Windows_NT' ">
4-
<None Include="$(MSBuildThisFileDirectory)\..\..\lib\net40\NativeBinaries\amd64\git2-9bbc8f3.dll">
5-
<Link>NativeBinaries\amd64\git2-9bbc8f3.dll</Link>
4+
<None Include="$(MSBuildThisFileDirectory)\..\..\lib\net40\NativeBinaries\amd64\git2-b2d0243.dll">
5+
<Link>NativeBinaries\amd64\git2-b2d0243.dll</Link>
66
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
77
</None>
8-
<None Include="$(MSBuildThisFileDirectory)\..\..\lib\net40\NativeBinaries\amd64\git2-9bbc8f3.pdb">
9-
<Link>NativeBinaries\amd64\git2-9bbc8f3.pdb</Link>
8+
<None Include="$(MSBuildThisFileDirectory)\..\..\lib\net40\NativeBinaries\amd64\git2-b2d0243.pdb">
9+
<Link>NativeBinaries\amd64\git2-b2d0243.pdb</Link>
1010
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1111
</None>
12-
<None Include="$(MSBuildThisFileDirectory)\..\..\lib\net40\NativeBinaries\x86\git2-9bbc8f3.dll">
13-
<Link>NativeBinaries\x86\git2-9bbc8f3.dll</Link>
12+
<None Include="$(MSBuildThisFileDirectory)\..\..\lib\net40\NativeBinaries\x86\git2-b2d0243.dll">
13+
<Link>NativeBinaries\x86\git2-b2d0243.dll</Link>
1414
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1515
</None>
16-
<None Include="$(MSBuildThisFileDirectory)\..\..\lib\net40\NativeBinaries\x86\git2-9bbc8f3.pdb">
17-
<Link>NativeBinaries\x86\git2-9bbc8f3.pdb</Link>
16+
<None Include="$(MSBuildThisFileDirectory)\..\..\lib\net40\NativeBinaries\x86\git2-b2d0243.pdb">
17+
<Link>NativeBinaries\x86\git2-b2d0243.pdb</Link>
1818
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1919
</None>
2020
</ItemGroup>

0 commit comments

Comments
 (0)