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

0 commit comments

Comments
 (0)