codeowners icon indicating copy to clipboard operation
codeowners copied to clipboard

Handle non-recursive directory syntax

Open mrphlip opened this issue 2 years ago • 3 comments

The Github codeowners docs say:

# The `docs/*` pattern will match files like
# `docs/getting-started.md` but not further nested files like
# `docs/build-app/troubleshooting.md`.
docs/*  [email protected]

However:

>>> from codeowners import CodeOwners
>>> owner = CodeOwners("docs/* [email protected]")
>>> owner.of("docs/build-app/troubleshooting.md")
[('EMAIL', '[email protected]')]

mrphlip avatar Apr 19 '22 07:04 mrphlip

I think this behavior matches git's ignore behavior:

diff --git a/codeowners/test_codeowners.py b/codeowners/test_codeowners.py
index 7a9172a..def241d 100644
--- a/codeowners/test_codeowners.py
+++ b/codeowners/test_codeowners.py
@@ -479,6 +479,14 @@ GO_CODEOWNER_EXAMPLES = [
             # "bar[0-5].log": True,
         },
     ),
+    ex(
+        name="non-recursive",
+        pattern="docs/*",
+        paths={
+            "docs/getting-started.md": True,
+            "docs/build-app/troubleshooting.md": True,
+        },
+    ),
]

Passes Git's checkignore:

+ ./.venv/bin/pytest
========================================= test session starts =========================================
platform darwin -- Python 3.7.3, pytest-6.2.1, py-1.10.0, pluggy-0.13.1
rootdir: /Users/steve/projects/codeowners, configfile: tox.ini
collected 111 items                                                                                   

codeowners/test_codeowners.py ................................................................. [ 58%]
..............................................                                                  [100%]

========================================= 111 passed in 2.74s =========================================

sbdchd avatar Apr 20 '22 02:04 sbdchd

Hmm, it does look like Github's behaviour doesn't match .gitignore for this pattern...

A .gitignore pattern that reads docs/* does cause everything in that directory to be ignored, including subdirs. But a CODEOWNERS pattern that reads docs/* only applies to files in that directory, not including subdirs.

I just spun up a test repo, I have a CODEOWNERS file that has a docs/* rule, and it does apply to files in that folder however it does not apply to files in subdirs...

mrphlip avatar Apr 20 '22 07:04 mrphlip

For added confusion, the Git docs also say:

The pattern "foo/*", matches "foo/test.json" (a regular file), "foo/bar" (a directory), but it does not match "foo/bar/hello.c" (a regular file), as the asterisk in the pattern does not match "bar/hello.c" which has a slash in it.

However, the actual behaviour of Git does not match this:

$ cat .gitignore
foo/*
$ git check-ignore -v foo/bar/hello.c
.gitignore:1:foo/*      foo/bar/hello.c
$ git add foo/bar/hello.c
The following paths are ignored by one of your .gitignore files:
foo/bar
hint: Use -f if you really want to add them.
hint: Turn this message off by running
hint: "git config advice.addIgnoredFile false"

So it looks like Github's codeowners behaviour is matching the Git docs, but is not matching the actual Git behaviour, so trying to test this by comparing it to git check-ignore isn't going to be completely accurate.

mrphlip avatar Apr 27 '22 01:04 mrphlip