Allowlist for .gitignore
19 points by izissise
19 points by izissise
you shouldn't need prefixless double-globs, gitignore will treat * as meaning "any file in any subdirectory" and then you can !/.gitignore, !*.rs, !Cargo.toml, etcetc
Specific quotations from the gitignore(5) man page supporting this (with mild added nuance):
If there is a separator at the beginning or middle (or both) of the pattern, then the pattern is relative to the directory level of the particular .gitignore file itself. Otherwise the pattern may also match at any level below the .gitignore level.
A leading "**" followed by a slash means match in all directories. For example, "**/foo" matches file or directory "foo" anywhere, the same as pattern "foo". "**/foo/bar" matches file or directory "bar" anywhere that is directly under directory "foo".
Depedning on the project, the Con about «spurious files harder to notice» can also become «easier to forget to add a file that should be tracked»
I realise getting a large team to behave can be hard, but this is how git already works if you don't git add .. Is it really necessary to co-opt a different feature just to stop people abusing this one?
Big fan of the allowlist approach. I stumbled upon a similar pattern using:
/*
/**/*.*
to disallow all files (with extension) and everything in root. Then allowing a directory + sub directories is easy. I typically allow files by extension like OP.
Interesting idea, I've done this with .dockerignore files before (in both monorepos and app repos) to great success. Hadn't thought to do it with tracked files in git though.
Same here. Been doing this with my .dockerignore for a good while now, but didn't think about .gitignore. Will give this a try.
Can you, in a monorepo situation, sync this with the build and owners methods?
Anything I build is ignored, so doing a diff between when I hit build and when it exits gives me a list of files I want to throw away most of the time?
Anything that isn't owned by a specific person or team should not be in there, so exclude it. Make sure owners is also allowlist only.