Skip to content

Commit 4ede553

Browse files
authored
Merge pull request #15365 from RasmusWL/lgtm_index_filter_handling
Tree sitter extractor: Proper handling of `LGTM_INDEX_FILTERS`
2 parents 03a125d + 0722303 commit 4ede553

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

shared/tree-sitter-extractor/src/autobuilder.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,35 @@ impl Autobuilder {
7474
cmd.arg("--working-dir=.");
7575
cmd.arg(&self.database);
7676

77-
for line in env::var("LGTM_INDEX_FILTERS")
78-
.unwrap_or_default()
79-
.split('\n')
80-
{
77+
// LGTM_INDEX_FILTERS is a prioritized list of include/exclude filters, where
78+
// later filters take priority over earlier ones.
79+
// 1) If we only see includes, we should ignore everything else, which is
80+
// achieved by using `--also-match={filter}`.
81+
// 2) if we see both includes and excludes, we process them in order by using
82+
// `--also-match={filter}` for includes and `--also-match=!{filter}` for
83+
// excludes.
84+
// 3) If we only see excludes, we should accept everything else. Naive solution
85+
// of just using `--also-match=!{filter}` is not good enough, since nothing
86+
// will make the `--also-match`` pass for any file. In that case, we add a dummy
87+
// initial `--also-match=**/*``to get the desired behavior.
88+
let tmp = env::var("LGTM_INDEX_FILTERS").unwrap_or_default();
89+
let lgtm_index_filters = tmp.split('\n');
90+
let lgtm_index_filters_has_include = lgtm_index_filters
91+
.clone()
92+
.any(|s| s.starts_with("include:"));
93+
let lgtm_index_filters_has_exclude = lgtm_index_filters
94+
.clone()
95+
.any(|s| s.starts_with("exclude:"));
96+
97+
if !lgtm_index_filters_has_include && lgtm_index_filters_has_exclude {
98+
cmd.arg("--also-match=**/*");
99+
}
100+
101+
for line in lgtm_index_filters {
81102
if let Some(stripped) = line.strip_prefix("include:") {
82103
cmd.arg("--also-match=".to_owned() + stripped);
83104
} else if let Some(stripped) = line.strip_prefix("exclude:") {
84-
cmd.arg("--exclude=".to_owned() + stripped);
105+
cmd.arg("--also-match=!".to_owned() + stripped);
85106
}
86107
}
87108
let exit = &cmd.spawn()?.wait()?;

0 commit comments

Comments
 (0)