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
20 changes: 16 additions & 4 deletions Src/CSharpier.Cli/IgnoreFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,13 @@ private static void AddDefaultRules(IgnoreWithBasePath ignore)

// this will return the ignore paths in order of priority
// the first csharpierignore it finds at or above the path
// and then all .gitignores (at or above) it finds in order from closest to further away
// and then all .gitignores (at or above stopping once it encounters a .git directory) it finds in order from closest to further away
private static List<string> FindIgnorePaths(string baseDirectoryPath, IFileSystem fileSystem)
{
var result = new List<string>();
string? foundCSharpierIgnoreFilePath = null;
var directoryInfo = fileSystem.DirectoryInfo.New(baseDirectoryPath);
var includeGitIgnores = true;
while (directoryInfo != null)
{
if (foundCSharpierIgnoreFilePath is null)
Expand All @@ -125,10 +126,21 @@ private static List<string> FindIgnorePaths(string baseDirectoryPath, IFileSyste
}
}

var gitIgnoreFilePath = fileSystem.Path.Combine(directoryInfo.FullName, ".gitignore");
if (fileSystem.File.Exists(gitIgnoreFilePath))
if (includeGitIgnores)
{
result.Add(gitIgnoreFilePath);
var gitIgnoreFilePath = fileSystem.Path.Combine(
directoryInfo.FullName,
".gitignore"
);
if (fileSystem.File.Exists(gitIgnoreFilePath))
{
result.Add(gitIgnoreFilePath);
}
}

if (fileSystem.Directory.Exists(Path.Combine(directoryInfo.FullName, ".git")))
{
includeGitIgnores = false;
}

directoryInfo = directoryInfo.Parent;
Expand Down
13 changes: 13 additions & 0 deletions Src/CSharpier.Tests/CommandLineFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,19 @@ bool isIgnored
.StartWith(isIgnored ? "Formatted 0 files in " : "Formatted 1 files in ");
}

[Test]
public void Gitignore_Outside_Git_Is_Not_Used()
{
var context = new TestContext();
context.WhenAFileExists("Sub/File.cs", UnformattedClassContent);
context.WhenAFileExists(".gitignore", "*.cs");
context.WhenAFileExists("Sub/.git/test.txt", string.Empty);

var result = Format(context, directoryOrFilePaths: "Sub");

result.OutputLines.FirstOrDefault().Should().StartWith("Formatted 1 files in ");
}

[Test]
public void Write_Stdout_Should_Only_Write_File()
{
Expand Down