Skip to content

Git ignore patterns evaluation should be in-order #1759

@gdlol

Description

@gdlol

I believe current implementation does not conform to Git behavior, where negation patterns are evaluated at the end so files in un-ignored directories cannot be ignored again:

public (bool hasMatchingRule, bool isIgnored) IsIgnored(string path)
{
if (!path.StartsWith(basePath, StringComparison.Ordinal))
{
return (false, false);
}
var pathRelativeToIgnoreFile =
path.Length > basePath.Length
? path[basePath.Length..].Replace('\\', '/')
: string.Empty;
var isIgnored = false;
var hasMatchingRule = false;
foreach (var rule in this.positives)
{
var isMatch = rule.IsMatch(pathRelativeToIgnoreFile);
if (isMatch)
{
hasMatchingRule = true;
isIgnored = true;
break;
}
}
foreach (var rule in this.negatives)
{
var isMatch = rule.IsMatch(pathRelativeToIgnoreFile);
if (isMatch)
{
hasMatchingRule = true;
if (isIgnored)
{
isIgnored = false;
break;
}
}
}
return (hasMatchingRule, isIgnored);
}

Environments:

  • CSharpier Version: 1.2.1
  • Running CSharpier via: dotnet cli
  • Operating System: Linux
  • .csharpierrc Settings: NA
  • .editorconfig Settings: NA

Steps to reproduce:
setup:

git init
mkdir -p Experimental/App123/IgnoreMe/
echo -e "Experimental/*\n\!App123/\nIgnoreMe/" > .gitignore
echo -e "int  n;" > Experimental/test.cs
echo -e "int  n;" > Experimental/App123/IgnoreMe/test.cs

Experimental/App123/IgnoreMe/test.cs should be ignored:

git check-ignore --verbose --non-matching Experimental/test.cs Experimental/App123/IgnoreMe/test.cs

output:

.gitignore:1:Experimental/*     Experimental/test.cs
.gitignore:3:IgnoreMe/  Experimental/App123/IgnoreMe/test.cs

Expected behavior:

dnx csharpier@1.2.1 -- check .

output:

Checked 0 files in 0ms.

Actual behavior:

dnx csharpier@1.2.1 -- check .

output:

Error ./Experimental/App123/IgnoreMe/test.cs - Was not formatted.
  ----------------------------- Expected: Around Line 1 -----------------------------
  int n;
  ----------------------------- Actual: Around Line 1 -----------------------------
  int  n;
  
Checked 1 files in 0ms.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions