ignoredBy static method
Whether the .gitignore text ignores the file relative (a path
from the ignore file's folder, / between parts), as git reads it:
*, ? and […] in a name, ** across folders, a leading /
anchoring a pattern, a pattern of one name matching at any depth, a
trailing / matching only folders (a folder on the file's way), and
the last matching line deciding — a ! line un-ignores.
Implementation
static bool ignoredBy(String text, String relative) {
final parts = relative.split('/');
var ignored = false;
for (final raw in text.split(RegExp(r'\r?\n'))) {
var line = raw.trimRight();
if (line.isEmpty || line.startsWith('#')) continue;
final negate = line.startsWith('!');
if (negate) line = line.substring(1);
final folderOnly = line.endsWith('/');
if (folderOnly) line = line.substring(0, line.length - 1);
if (line.isEmpty) continue;
final anchored = line.contains('/');
if (line.startsWith('/')) line = line.substring(1);
final pattern = _glob(line);
// A folder on the file's way matches (and so does the file itself,
// unless the pattern is for folders only).
for (var n = 1; n <= parts.length; n++) {
if (folderOnly && n == parts.length) break;
final matches = anchored
? pattern.hasMatch(parts.take(n).join('/'))
: pattern.hasMatch(parts[n - 1]);
if (matches) {
ignored = !negate;
break;
}
}
}
return ignored;
}