vdoc: fix handling of .vdocignore files in subdirectories (#21514)

This commit is contained in:
Turiiya 2024-05-16 21:42:28 +02:00 committed by GitHub
parent 9639f55db0
commit 41b7e5fc21
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -4,10 +4,11 @@ import os
struct IgnoreRules { struct IgnoreRules {
mut: mut:
patterns map[string]bool = { // Ignore patterns use the path with a `.vdocignore` file as a base. E.g.:
'testdata': true // `{'<path>': ['<pattern1>', '<pattern2>'], '<path/subpath>': ['<pattern3>']}`
'tests': true patterns map[string][]string = {
'*_test.v': true // Default ignore patterns.
'': ['testdata', 'tests', '*_test.v']
} }
paths map[string]bool paths map[string]bool
} }
@ -25,19 +26,23 @@ fn get_modules(path string) []string {
fn get_paths(path string, mut ignore_rules IgnoreRules) []string { fn get_paths(path string, mut ignore_rules IgnoreRules) []string {
mut res := []string{} mut res := []string{}
for p in os.ls(path) or { return [] } { outer: for p in os.ls(path) or { return [] } {
ignore_rules.get(path) ignore_rules.get(path)
fp := os.join_path(path, p) fp := os.join_path(path, p)
if fp in ignore_rules.paths { if fp in ignore_rules.paths {
continue continue
} }
is_dir := os.is_dir(fp) is_dir := os.is_dir(fp)
if ignore_rules.patterns.keys().any(p == it for ignore_path, patterns in ignore_rules.patterns {
|| (it.contains('*') && p.ends_with(it.all_after('*'))) if fp.starts_with(ignore_path) {
|| (is_dir && it.ends_with('/') && fp.ends_with(it.trim_right('/'))) if patterns.any(p == it
|| (!it.ends_with('/') && it.contains('/') && fp.contains(it))) || (it.contains('*') && p.ends_with(it.all_after('*')))
{ || (is_dir && it.ends_with('/') && fp.ends_with(it.trim_right('/')))
continue || (!it.ends_with('/') && it.contains('/') && fp.contains(it)))
{
continue outer
}
}
} }
if is_dir { if is_dir {
res << get_paths(fp, mut ignore_rules) res << get_paths(fp, mut ignore_rules)
@ -73,7 +78,7 @@ fn (mut ignore_rules IgnoreRules) get(path string) {
// `/a` should ignore `/a` but not `/b/a`. While `a` should ignore `/a` and `/b/a`. // `/a` should ignore `/a` but not `/b/a`. While `a` should ignore `/a` and `/b/a`.
ignore_rules.paths[os.join_path(path, rule.trim_left('/'))] = true ignore_rules.paths[os.join_path(path, rule.trim_left('/'))] = true
} else { } else {
ignore_rules.patterns[rule] = true ignore_rules.patterns[path] << rule
} }
} }
} }