Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 20 additions & 4 deletions LibGit2Sharp.Tests/StageFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void StagingAnUnknownFileThrowsIfExplicitPath(string relativePath, FileSt
Assert.Null(repo.Index[relativePath]);
Assert.Equal(status, repo.Index.RetrieveStatus(relativePath));

Assert.Throws<UnmatchedPathException>(() => repo.Index.Stage(relativePath, new ExplicitPathsOptions()));
Assert.Throws<UnmatchedPathException>(() => repo.Index.Stage(relativePath, new StageOptions { ExplicitPathsOptions = new ExplicitPathsOptions() }));
}
}

Expand All @@ -82,7 +82,7 @@ public void CanStageAnUnknownFileWithLaxUnmatchedExplicitPathsValidation(string
Assert.Equal(status, repo.Index.RetrieveStatus(relativePath));

Assert.DoesNotThrow(() => repo.Index.Stage(relativePath));
Assert.DoesNotThrow(() => repo.Index.Stage(relativePath, new ExplicitPathsOptions { ShouldFailOnUnmatchedPath = false }));
Assert.DoesNotThrow(() => repo.Index.Stage(relativePath, new StageOptions { ExplicitPathsOptions = new ExplicitPathsOptions { ShouldFailOnUnmatchedPath = false } }));

Assert.Equal(status, repo.Index.RetrieveStatus(relativePath));
}
Expand All @@ -99,7 +99,7 @@ public void StagingAnUnknownFileWithLaxExplicitPathsValidationDoesntThrow(string
Assert.Equal(status, repo.Index.RetrieveStatus(relativePath));

repo.Index.Stage(relativePath);
repo.Index.Stage(relativePath, new ExplicitPathsOptions { ShouldFailOnUnmatchedPath = false });
repo.Index.Stage(relativePath, new StageOptions { ExplicitPathsOptions = new ExplicitPathsOptions { ShouldFailOnUnmatchedPath = false } });
}
}

Expand Down Expand Up @@ -299,6 +299,22 @@ public void CanStageWithMultiplePathspecs()
}
}

[Theory]
[InlineData("ignored_file.txt")]
[InlineData("ignored_folder/file.txt")]
public void CanIgnoreIgnoredPaths(string path)
{
using (var repo = new Repository(CloneStandardTestRepo()))
{
Touch(repo.Info.WorkingDirectory, ".gitignore", "ignored_file.txt\nignored_folder/\n");
Touch(repo.Info.WorkingDirectory, path, "This file is ignored.");

Assert.Equal(FileStatus.Ignored, repo.Index.RetrieveStatus(path));
repo.Index.Stage("*");
Assert.Equal(FileStatus.Ignored, repo.Index.RetrieveStatus(path));
}
}

[Theory]
[InlineData("ignored_file.txt")]
[InlineData("ignored_folder/file.txt")]
Expand All @@ -310,7 +326,7 @@ public void CanStageIgnoredPaths(string path)
Touch(repo.Info.WorkingDirectory, path, "This file is ignored.");

Assert.Equal(FileStatus.Ignored, repo.Index.RetrieveStatus(path));
repo.Index.Stage(path);
repo.Index.Stage(path, new StageOptions { IncludeIgnored = true });
Assert.Equal(FileStatus.Added, repo.Index.RetrieveStatus(path));
}
}
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp.Tests/UnstageFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void CanStageAndUnstageAnIgnoredFile()

Assert.Equal(FileStatus.Ignored, repo.Index.RetrieveStatus(relativePath));

repo.Index.Stage(relativePath);
repo.Index.Stage(relativePath, new StageOptions { IncludeIgnored = true });
Assert.Equal(FileStatus.Added, repo.Index.RetrieveStatus(relativePath));

repo.Index.Unstage(relativePath);
Expand Down
46 changes: 42 additions & 4 deletions LibGit2Sharp/Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,32 +129,70 @@ IEnumerator IEnumerable.GetEnumerator()

/// <summary>
/// Promotes to the staging area the latest modifications of a file in the working directory (addition, updation or removal).
///
/// If this path is ignored by configuration then it will not be staged.
/// </summary>
/// <param name="path">The path of the file within the working directory.</param>
/// <param name="explicitPathsOptions">
/// If set, the passed <paramref name="path"/> will be treated as explicit paths.
/// Use these options to determine how unmatched explicit paths should be handled.
/// </param>
public virtual void Stage(string path, ExplicitPathsOptions explicitPathsOptions = null)
[Obsolete("This will be removed in a future release. Supply ExplicitPathsOptions to StageOptions.")]
public virtual void Stage(string path, ExplicitPathsOptions explicitPathsOptions)
{
Stage(path, new StageOptions { ExplicitPathsOptions = explicitPathsOptions });
}

/// <summary>
/// Promotes to the staging area the latest modifications of a file in the working directory (addition, updation or removal).
///
/// If this path is ignored by configuration then it will not be staged unless <see cref="StageOptions.IncludeIgnored"/> is unset.
/// </summary>
/// <param name="path">The path of the file within the working directory.</param>
/// <param name="stageOptions">If set, determines how paths will be staged.</param>
public virtual void Stage(string path, StageOptions stageOptions = null)
Copy link
Member

Choose a reason for hiding this comment

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

Old overloads should be retained as [Obsolete].

{
Ensure.ArgumentNotNull(path, "path");

Stage(new[] { path }, explicitPathsOptions);
Stage(new[] { path }, stageOptions);
}

/// <summary>
/// Promotes to the staging area the latest modifications of a collection of files in the working directory (addition, updation or removal).
///
/// Any paths (even those listed explicitly) that are ignored by configuration will not be staged.
/// </summary>
/// <param name="paths">The collection of paths of the files within the working directory.</param>
/// <param name="explicitPathsOptions">
/// If set, the passed <paramref name="paths"/> will be treated as explicit paths.
/// Use these options to determine how unmatched explicit paths should be handled.
/// </param>
public virtual void Stage(IEnumerable<string> paths, ExplicitPathsOptions explicitPathsOptions = null)
[Obsolete("This will be removed in a future release. Supply ExplicitPathsOptions to StageOptions.")]
public virtual void Stage(IEnumerable<string> paths, ExplicitPathsOptions explicitPathsOptions)
{
Stage(paths, new StageOptions { ExplicitPathsOptions = explicitPathsOptions });
}

/// <summary>
/// Promotes to the staging area the latest modifications of a collection of files in the working directory (addition, updation or removal).
///
/// Any paths (even those listed explicitly) that are ignored by configuration will not be staged unless <see cref="StageOptions.IncludeIgnored"/> is unset.
/// </summary>
/// <param name="paths">The collection of paths of the files within the working directory.</param>
/// <param name="stageOptions">If set, determines how paths will be staged.</param>
public virtual void Stage(IEnumerable<string> paths, StageOptions stageOptions = null)
{
Ensure.ArgumentNotNull(paths, "paths");

var changes = repo.Diff.Compare<TreeChanges>(DiffModifiers.IncludeUntracked | DiffModifiers.IncludeIgnored, paths, explicitPathsOptions);
DiffModifiers diffModifiers = DiffModifiers.IncludeUntracked;
ExplicitPathsOptions explicitPathsOptions = stageOptions != null ? stageOptions.ExplicitPathsOptions : null;

if (stageOptions != null && stageOptions.IncludeIgnored)
{
diffModifiers |= DiffModifiers.IncludeIgnored;
}

var changes = repo.Diff.Compare<TreeChanges>(diffModifiers, paths, explicitPathsOptions);

foreach (var treeEntryChanges in changes)
{
Expand Down
3 changes: 2 additions & 1 deletion LibGit2Sharp/LibGit2Sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
<Compile Include="RevertResult.cs" />
<Compile Include="SignatureExtensions.cs" />
<Compile Include="RevertOptions.cs" />
<Compile Include="StageOptions.cs" />
<Compile Include="StatusOptions.cs" />
<Compile Include="SimilarityOptions.cs" />
<Compile Include="UnbornBranchException.cs" />
Expand Down Expand Up @@ -351,4 +352,4 @@
</CreateItem>
<Copy SourceFiles="@(NativeBinaries)" DestinationFiles="@(NativeBinaries->'$(OutputPath)NativeBinaries\%(RecursiveDir)%(Filename)%(Extension)')" SkipUnchangedFiles="true" />
</Target>
</Project>
</Project>
22 changes: 22 additions & 0 deletions LibGit2Sharp/StageOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;

namespace LibGit2Sharp
{
/// <summary>
/// Options to define file staging behavior.
/// </summary>
public sealed class StageOptions
{
/// <summary>
/// Stage ignored files. (Default = false)
/// </summary>
public bool IncludeIgnored { get; set; }

/// <summary>
/// If set, the passed paths will be treated as explicit paths.
/// Use these options to determine how unmatched explicit paths
/// should be handled. (Default = null)
/// </summary>
public ExplicitPathsOptions ExplicitPathsOptions { get; set; }
}
}