Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Prettify Stash.Apply and Stash.Drop
  • Loading branch information
Therzok committed Apr 6, 2015
commit b5d7b391c10aef0c3a00cdd5ef4541abe87b2990
Binary file removed Lib/NativeBinaries/amd64/git2-47f3740.dll
Binary file not shown.
Binary file added Lib/NativeBinaries/amd64/git2-b2d0243.dll
Binary file not shown.
Binary file not shown.
Binary file removed Lib/NativeBinaries/x86/git2-47f3740.dll
Binary file not shown.
Binary file added Lib/NativeBinaries/x86/git2-b2d0243.dll
Binary file not shown.
Binary file not shown.
93 changes: 93 additions & 0 deletions LibGit2Sharp.Tests/StashFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,99 @@ public void CanStashAndKeepIndex()
}
}

[Fact]
public void CanStashAndApplyWithOptions()
{
string path = SandboxStandardTestRepo();
using (var repo = new Repository(path))
{
var stasher = Constants.Signature;

const string filename = "staged_file_path.txt";
Touch(repo.Info.WorkingDirectory, filename, "I'm staged\n");
repo.Stage(filename);

repo.Stashes.Add(stasher, "This stash with default options");
Assert.Equal(StashApplyStatus.Applied, repo.Stashes.Apply(0));

Assert.Equal(FileStatus.Untracked, repo.RetrieveStatus(filename));
Assert.Equal(1, repo.Stashes.Count());

repo.Stage(filename);

repo.Stashes.Add(stasher, "This stash with default options");
Assert.Equal(StashApplyStatus.Applied, repo.Stashes.Apply(0, StashApplyModifiers.ReinstateIndex));

Assert.Equal(FileStatus.Added, repo.RetrieveStatus(filename));
Assert.Equal(2, repo.Stashes.Count());
}
}

[Fact]
public void CanStashAndPop()
{
string path = SandboxStandardTestRepo();
using (var repo = new Repository(path))
{
var stasher = Constants.Signature;

const string filename = "staged_file_path.txt";
Touch(repo.Info.WorkingDirectory, filename, "I'm staged\n");
repo.Stage(filename);

repo.Stashes.Add(stasher, "This stash with default options");
Assert.Equal(StashApplyStatus.Applied, repo.Stashes.Pop(0));

Assert.Equal(FileStatus.Untracked, repo.RetrieveStatus(filename));
Assert.Equal(0, repo.Stashes.Count());
}
}

[Fact]
public void StashReportsConflictsWhenReinstated()
{
string path = SandboxStandardTestRepo();
using (var repo = new Repository(path))
{
var stasher = Constants.Signature;

const string filename = "staged_file_path.txt";
const string filename2 = "unstaged_file_path.txt";
Touch(repo.Info.WorkingDirectory, filename, "I'm staged\n");
repo.Stage(filename);
Touch(repo.Info.WorkingDirectory, filename2, "I'm unstaged\n");

repo.Stashes.Add(stasher, "This stash with default options");

Touch(repo.Info.WorkingDirectory, filename, "I'm another staged\n");
repo.Stage(filename);
Touch(repo.Info.WorkingDirectory, filename2, "I'm unstaged another staged\n");

Assert.Equal(StashApplyStatus.Conflicts, repo.Stashes.Pop(0, StashApplyModifiers.ReinstateIndex));

// TODO: Find out why repo.Index.Conflicts doesn't have any data.
Assert.NotNull(repo.Index.Conflicts[filename]);
}
}

[Fact]
public void StashReportsExistingInWorkDir()
{
string path = SandboxStandardTestRepo();
using (var repo = new Repository(path))
{
var stasher = Constants.Signature;

const string filename = "unstaged_file_path.txt";
Touch(repo.Info.WorkingDirectory, filename, "I'm unstaged\n");

repo.Stashes.Add(stasher, "This stash with default options", StashModifiers.IncludeUntracked);
Touch(repo.Info.WorkingDirectory, filename, "I'm another unstaged\n");

Assert.Equal(StashApplyStatus.UntrackedExist, repo.Stashes.Pop(0));
}
}

[Fact]
public void CanStashIgnoredFiles()
{
Expand Down
1 change: 1 addition & 0 deletions LibGit2Sharp/Core/GitCheckoutOpts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ internal struct GitCheckoutOpts
public GitStrArray paths;

public IntPtr baseline;
public IntPtr baseline_index;
public IntPtr target_directory;

public IntPtr ancestor_label;
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/Core/NativeDllName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ namespace LibGit2Sharp.Core
{
internal static class NativeDllName
{
public const string Name = "git2-47f3740";
public const string Name = "git2-b2d0243";
}
}
24 changes: 18 additions & 6 deletions LibGit2Sharp/Core/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2723,29 +2723,41 @@ public static void git_stash_drop(RepositorySafeHandle repo, int index)
}
}

public static void git_stash_apply(
static StashApplyStatus get_stash_status(int res)
{
if (res == (int)GitErrorCode.MergeConflict)
{
return StashApplyStatus.Conflicts;
}
if (res == (int)GitErrorCode.Exists)
{
return StashApplyStatus.UntrackedExist;
}
Ensure.ZeroResult(res);
return StashApplyStatus.Applied;
}

public static StashApplyStatus git_stash_apply(
RepositorySafeHandle repo,
int index,
ref GitCheckoutOpts opts,
StashApplyModifiers flags)
{
using (ThreadAffinity())
{
int res = NativeMethods.git_stash_apply(repo, (UIntPtr)index, ref opts, flags);
Ensure.ZeroResult(res);
return get_stash_status(NativeMethods.git_stash_apply(repo, (UIntPtr)index, ref opts, flags));
}
}

public static void git_stash_pop(
public static StashApplyStatus git_stash_pop(
RepositorySafeHandle repo,
int index,
ref GitCheckoutOpts opts,
StashApplyModifiers flags)
{
using (ThreadAffinity())
{
int res = NativeMethods.git_stash_pop(repo, (UIntPtr)index, ref opts, flags);
Ensure.ZeroResult(res);
return get_stash_status(NativeMethods.git_stash_pop(repo, (UIntPtr)index, ref opts, flags));
}
}

Expand Down
1 change: 1 addition & 0 deletions LibGit2Sharp/LibGit2Sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
<Compile Include="RevertResult.cs" />
<Compile Include="RevertOptions.cs" />
<Compile Include="StageOptions.cs" />
<Compile Include="StashApplyStatus.cs" />
<Compile Include="StatusOptions.cs" />
<Compile Include="SimilarityOptions.cs" />
<Compile Include="Log.cs" />
Expand Down
30 changes: 30 additions & 0 deletions LibGit2Sharp/StashApplyStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LibGit2Sharp
{
/// <summary>
/// The status of what happened as a result of a stash application.
/// </summary>
public enum StashApplyStatus
{
/// <summary>
/// The changes were successfully stashed.
/// </summary>
Applied,

/// <summary>
/// The stash application resulted in conflicts.
/// </summary>
Conflicts,

/// <summary>
/// The stash application was not applied due to existing
/// untracked files that would be overwritten by the stash
/// contents.
/// </summary>
UntrackedExist,
}
}
12 changes: 6 additions & 6 deletions LibGit2Sharp/StashCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ public virtual void Remove(int index)
/// <param name="index">the index of the stash to remove (0 being the most recent one).</param>
/// <param name="options">the options to use for checking out the stash.</param>
/// <param name="flags">the flags to use for applying the changes.</param>
public virtual void Apply(int index, CheckoutOptions options = null, StashApplyModifiers flags = StashApplyModifiers.Default)
public virtual StashApplyStatus Apply(int index, StashApplyModifiers flags = StashApplyModifiers.Default, CheckoutOptions options = null)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Probably this could be abstracted in a StashApplyOptions.

class StashApplyOptions
{
    StashApplyModifiers flags;
    CheckoutOptions checkoutOptions;
}

/cc @nulltoken

{
using (GitCheckoutOptsWrapper checkoutOptionsWrapper = new GitCheckoutOptsWrapper(options))
using (GitCheckoutOptsWrapper checkoutOptionsWrapper = new GitCheckoutOptsWrapper(options ?? new CheckoutOptions()))
{
var opts = checkoutOptionsWrapper.Options;
Proxy.git_stash_apply(repo.Handle, index, ref opts, flags);
return Proxy.git_stash_apply(repo.Handle, index, ref opts, flags);
}
}

Expand All @@ -135,12 +135,12 @@ public virtual void Apply(int index, CheckoutOptions options = null, StashApplyM
/// <param name="index">the index of the stash to remove (0 being the most recent one).</param>
/// <param name="options">the options to use for checking out the stash.</param>
/// <param name="flags">the flags to use for applying the changes.</param>
public virtual void Pop(int index, CheckoutOptions options = null, StashApplyModifiers flags = StashApplyModifiers.Default)
public virtual StashApplyStatus Pop(int index, StashApplyModifiers flags = StashApplyModifiers.Default, CheckoutOptions options = null)
{
using (GitCheckoutOptsWrapper checkoutOptionsWrapper = new GitCheckoutOptsWrapper(options))
using (GitCheckoutOptsWrapper checkoutOptionsWrapper = new GitCheckoutOptsWrapper(options ?? new CheckoutOptions()))
{
var opts = checkoutOptionsWrapper.Options;
Proxy.git_stash_pop(repo.Handle, index, ref opts, flags);
return Proxy.git_stash_pop(repo.Handle, index, ref opts, flags);
}
}

Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/libgit2_hash.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
47f37400253210f483d84fb9c2ecf44fb5986849
b2d02433141ddc4c914a6b0e0027f6c58fc11cb6
16 changes: 8 additions & 8 deletions nuget.package/build/LibGit2Sharp.props
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Condition=" '$(OS)' == 'Windows_NT' ">
<None Include="$(MSBuildThisFileDirectory)\..\..\lib\net40\NativeBinaries\amd64\git2-47f3740.dll">
<Link>NativeBinaries\amd64\git2-47f3740.dll</Link>
<None Include="$(MSBuildThisFileDirectory)\..\..\lib\net40\NativeBinaries\amd64\git2-b2d0243.dll">
<Link>NativeBinaries\amd64\git2-b2d0243.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="$(MSBuildThisFileDirectory)\..\..\lib\net40\NativeBinaries\amd64\git2-47f3740.pdb">
<Link>NativeBinaries\amd64\git2-47f3740.pdb</Link>
<None Include="$(MSBuildThisFileDirectory)\..\..\lib\net40\NativeBinaries\amd64\git2-b2d0243.pdb">
<Link>NativeBinaries\amd64\git2-b2d0243.pdb</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="$(MSBuildThisFileDirectory)\..\..\lib\net40\NativeBinaries\x86\git2-47f3740.dll">
<Link>NativeBinaries\x86\git2-47f3740.dll</Link>
<None Include="$(MSBuildThisFileDirectory)\..\..\lib\net40\NativeBinaries\x86\git2-b2d0243.dll">
<Link>NativeBinaries\x86\git2-b2d0243.dll</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="$(MSBuildThisFileDirectory)\..\..\lib\net40\NativeBinaries\x86\git2-47f3740.pdb">
<Link>NativeBinaries\x86\git2-47f3740.pdb</Link>
<None Include="$(MSBuildThisFileDirectory)\..\..\lib\net40\NativeBinaries\x86\git2-b2d0243.pdb">
<Link>NativeBinaries\x86\git2-b2d0243.pdb</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
Expand Down