8000 Index.Stage: do not stage ignored files · GiTechLab/libgit2sharp@cf8c5fb · GitHub
[go: up one dir, main page]

Skip to content

Commit cf8c5fb

Browse files
author
Edward Thomson
committed
Index.Stage: do not stage ignored files
Stage() now respects ignore files by default, similar to git.git. Users accustomed to the old behavior may use the new StageOptions class (in particular the IncludeIgnored bool) to override this behavior.
1 parent ad12cca commit cf8c5fb

File tree

5 files changed

+87
-10
lines changed

5 files changed

+87
-10
lines changed

LibGit2Sharp.Tests/StageFixture.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void StagingAnUnknownFileThrowsIfExplicitPath(string relativePath, FileSt
6767
Assert.Null(repo.Index[relativePath]);
6868
Assert.Equal(status, repo.Index.RetrieveStatus(relativePath));
6969

70-
Assert.Throws<UnmatchedPathException>(() => repo.Index.Stage(relativePath, new ExplicitPathsOptions()));
70+
Assert.Throws<UnmatchedPathException>(() => repo.Index.Stage(relativePath, new StageOptions { ExplicitPathsOptions = new ExplicitPathsOptions() }));
7171
}
7272
}
7373

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

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

8787
Assert.Equal(status, repo.Index.RetrieveStatus(relativePath));
8888
}
@@ -99,7 +99,7 @@ public void StagingAnUnknownFileWithLaxExplicitPathsValidationDoesntThrow(string
9999
Assert.Equal(status, repo.Index.RetrieveStatus(relativePath));
100100

101101
repo.Index.Stage(relativePath);
102-
repo.Index.Stage(relativePath, new ExplicitPathsOptions { ShouldFailOnUnmatchedPath = false });
102+
repo.Index.Stage(relativePath, new StageOptions { ExplicitPathsOptions = new ExplicitPathsOptions { ShouldFailOnUnmatchedPath = false } });
103103
}
104104
}
105105

@@ -299,6 +299,22 @@ public void CanStageWithMultiplePathspecs()
299299
}
300300
}
301301

302+
[Theory]
303+
[InlineData("ignored_file.txt")]
304+
[InlineData("ignored_folder/file.txt")]
305+
public void CanIgnoreIgnoredPaths(string path)
306+
{
307+
using (var repo = new Repository(CloneStandardTestRepo()))
308+
{
309+
Touch(repo.Info.WorkingDirectory, ".gitignore", "ignored_file.txt\nignored_folder/\n");
310+
Touch(repo.Info.WorkingDirectory, path, "This file is ignored.");
311+
312+
Assert.Equal(FileStatus.Ignored, repo.Index.RetrieveStatus(path));
313+
repo.Index.Stage("*");
314+
Assert.Equal(FileStatus.Ignored, repo.Index.RetrieveStatus(path));
315+
}
316+
}
317+
302318
[Theory]
303319
[InlineData("ignored_file.txt")]
304320
[InlineData("ignored_folder/file.txt")]
@@ -310,7 +326,7 @@ public void CanStageIgnoredPaths(string path)
310326
Touch(repo.Info.WorkingDirectory, path, "This file is ignored.");
311327

312328
Assert.Equal(FileStatus.Ignored, repo.Index.RetrieveStatus(path));
313-
repo.Index.Stage(path);
329+
repo.Index.Stage(path, new StageOptions { IncludeIgnored = true });
314330
Assert.Equal(FileStatus.Added, repo.Index.RetrieveStatus(path));
315331
}
316332
}

LibGit2Sharp.Tests/UnstageFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void CanStageAndUnstageAnIgnoredFile()
5050

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

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

5656
repo.Index.Unstage(relativePath);

LibGit2Sharp/Index.cs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,32 +129,70 @@ IEnumerator IEnumerable.GetEnumerator()
129129

130130
/// <summary>
131131
/// Promotes to the staging area the latest modifications of a file in the working directory (addition, updation or removal).
132+
///
133+
/// If this path is ignored by configuration then it will not be staged.
132134
/// </summary>
133135
/// <param name="path">The path of the file within the working directory.</param>
134136
/// <param name="explicitPathsOptions">
135137
/// If set, the passed <paramref name="path"/> will be treated as explicit paths.
136138
/// Use these options to determine how unmatched explicit paths should be handled.
137139
/// </param>
138-
public virtual void Stage(string path, ExplicitPathsOptions explicitPathsOptions = null)
140+
[Obsolete("This will be removed in a future release. Supply ExplicitPathsOptions to StageOptions.")]
141+
public virtual void Stage(string path, ExplicitPathsOptions explicitPathsOptions)
142+
{
143+
Stage(path, new StageOptions { ExplicitPathsOptions = explicitPathsOptions });
144+
}
145+
146+
/// <summary>
147+
/// Promotes to the staging area the latest modifications of a file in the working directory (addition, updation or removal).
148+
///
149+
/// If this path is ignored by configuration then it will not be staged unless <see cref="StageOptions.IncludeIgnored"/> is unset.
150+
/// </summary>
151+
/// <param name="path">The path of the file within the working directory.</param>
152+
/// <param name="stageOptions">If set, determines how paths will be staged.</param>
153+
public virtual void Stage(string path, StageOptions stageOptions = null)
139154
{
140155
Ensure.ArgumentNotNull(path, "path");
141156

142-
Stage(new[] { path }, explicitPathsOptions);
157+
Stage(new[] { path }, stageOptions);
143158
}
144159

145160
/// <summary>
146161
/// Promotes to the staging area the latest modifications of a collection of files in the working directory (addition, updation or removal).
162+
///
163+
/// Any paths (even those listed explicitly) that are ignored by configuration will not be staged.
147164
/// </summary>
148165
/// <param name="paths">The collection of paths of the files within the working directory.</param>
149166
/// <param name="explicitPathsOptions">
150167
/// If set, the passed <paramref name="paths"/> will be treated as explicit paths.
151168
/// Use these options to determine how unmatched explicit paths should be handled.
152169
/// </param>
153-
public virtual void Stage(IEnumerable<string> paths, ExplicitPathsOptions explicitPathsOptions = null)
170+
[Obsolete("This will be removed in a future release. Supply ExplicitPathsOptions to StageOptions.")]
171+
public virtual void Stage(IEnumerable<string> paths, ExplicitPathsOptions explicitPathsOptions)
172+
{
173+
Stage(paths, new StageOptions { ExplicitPathsOptions = explicitPathsOptions });
174+
}
175+
176+
/// <summary>
177+
/// Promotes to the staging area the latest modifications of a collection of files in the working directory (addition, updation or removal).
178+
///
179+
/// Any paths (even those listed explicitly) that are ignored by configuration will not be staged unless <see cref="StageOptions.IncludeIgnored"/> is unset.
180+
/// </summary>
181+
/// <param name="paths">The collection of paths of the files within the working directory.</param>
182+
/// <param name="stageOptions">If set, determines how paths will be staged.</param>
183+
public virtual void Stage(IEnumerable<string> paths, StageOptions stageOptions = null)
154184
{
155185
Ensure.ArgumentNotNull(paths, "paths");
156186

157-
var changes = repo.Diff.Compare<TreeChanges>(DiffModifiers.IncludeUntracked | DiffModifiers.IncludeIgnored, paths, explicitPathsOptions);
187+
DiffModifiers diffModifiers = DiffModifiers.IncludeUntracked;
188+
ExplicitPathsOptions explicitPathsOptions = stageOptions != null ? stageOptions.ExplicitPathsOptions : null;
189+
190+
if (stageOptions != null && stageOptions.IncludeIgnored)
191+
{
192+
diffModifiers |= DiffModifiers.IncludeIgnored;
193+
}
194+
195+
var changes = repo.Diff.Compare<TreeChanges>(diffModifiers, paths, explicitPathsOptions);
158196

159197
foreach (var treeEntryChanges in changes)
160198
{

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
<Compile Include="RevertResult.cs" />
133133
<Compile Include="SignatureExtensions.cs" />
134134
<Compile Include="RevertOptions.cs" />
135+
<Compile Include="StageOptions.cs" />
135136
<Compile Include="StatusOptions.cs" />
136137
<Compile Include="SimilarityOptions.cs" />
137138
<Compile Include="UnbornBranchException.cs" />
@@ -351,4 +352,4 @@
351352
</CreateItem>
352353
<Copy SourceFiles="@(NativeBinaries)" DestinationFiles="@(NativeBinaries->'$(OutputPath)NativeBinaries\%(RecursiveDir)%(Filename)%(Extension)')" SkipUnchangedFiles="true" />
353354
</Target>
354-
</Project>
355+
</Project>

LibGit2Sharp/StageOptions.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
3+
namespace LibGit2Sharp
4+
{
5+
/// <summary>
6+
/// Options to define file staging behavior.
7+
/// </summary>
8+
public sealed class StageOptions
9+
{
10+
/// <summary>
11+
/// Stage ignored files. (Default = false)
12+
/// </summary>
13+
public bool IncludeIgnored { get; set; }
14+
15+
/// <summary>
16+
/// If set, the passed paths will be treated as explicit paths.
17+
/// Use these options to determine how unmatched explicit paths
18+
/// should be handled. (Default = null)
19+
/// </summary>
20+
public ExplicitPathsOptions ExplicitPathsOptions { get; set; }
21+
}
22+
}

0 commit comments

Comments
 (0)
0