diff --git a/Src/CSharpier.Cli/IgnoreFile.cs b/Src/CSharpier.Cli/IgnoreFile.cs index 884ea597a..c6fcd91b0 100644 --- a/Src/CSharpier.Cli/IgnoreFile.cs +++ b/Src/CSharpier.Cli/IgnoreFile.cs @@ -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 FindIgnorePaths(string baseDirectoryPath, IFileSystem fileSystem) { var result = new List(); string? foundCSharpierIgnoreFilePath = null; var directoryInfo = fileSystem.DirectoryInfo.New(baseDirectoryPath); + var includeGitIgnores = true; while (directoryInfo != null) { if (foundCSharpierIgnoreFilePath is null) @@ -125,10 +126,21 @@ private static List 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; diff --git a/Src/CSharpier.Tests/CommandLineFormatterTests.cs b/Src/CSharpier.Tests/CommandLineFormatterTests.cs index b98260cbf..74093a6c8 100644 --- a/Src/CSharpier.Tests/CommandLineFormatterTests.cs +++ b/Src/CSharpier.Tests/CommandLineFormatterTests.cs @@ -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() {