88
99import pathspec
1010
11- from ..ignore import get_ignore_specs , should_ignore
11+ from ..ignore import get_ignore_specs , get_whitelist_spec , is_whitelisted , should_ignore
1212from ..tokens import count_tokens
1313from .config import LIMITS
1414from .config .extensions import CODE_EXTENSIONS , CONFIG_EXTENSIONS , DOC_EXTENSIONS
@@ -220,6 +220,7 @@ def build_diff_context(
220220 ignore_file : Path | None = None ,
221221 no_default_ignores : bool = False ,
222222 full : bool = False ,
223+ whitelist_file : Path | None = None ,
223224) -> dict [str , Any ]:
224225 _validate_inputs (root_dir , alpha , tau , budget_tokens )
225226 root_dir = root_dir .resolve ()
@@ -229,6 +230,7 @@ def build_diff_context(
229230 base_rev , head_rev = split_diff_range (diff_range )
230231 is_working_tree_diff = base_rev is None and head_rev is None
231232 combined_spec = get_ignore_specs (root_dir , ignore_file , no_default_ignores , None )
233+ wl_spec = get_whitelist_spec (whitelist_file , root_dir )
232234
233235 untracked : list [Path ] = []
234236 if is_working_tree_diff :
@@ -247,13 +249,15 @@ def build_diff_context(
247249 changed_files = [_normalize_path (p , root_dir ) for p in changed_files ]
248250 changed_files .extend (untracked )
249251 changed_files = _filter_ignored (changed_files , root_dir , combined_spec )
252+ changed_files = _filter_whitelist (changed_files , root_dir , wl_spec )
250253
251254 preferred_revs = _build_preferred_revs (base_rev , head_rev )
252255
253256 seen_frag_ids : set [FragmentId ] = set ()
254257 all_fragments = _process_files_for_fragments (changed_files , root_dir , preferred_revs , seen_frag_ids )
255258
256259 all_candidate_files = _collect_candidate_files (root_dir , set (changed_files ), combined_spec )
260+ all_candidate_files = _filter_whitelist (all_candidate_files , root_dir , wl_spec )
257261
258262 edge_discovered = discover_all_related_files (changed_files , all_candidate_files , root_dir )
259263 edge_discovered = [_normalize_path (p , root_dir ) for p in edge_discovered ]
@@ -586,6 +590,24 @@ def _collect_expansion_files(
586590 return list (expansion_files )
587591
588592
593+ def _filter_whitelist (
594+ files : list [Path ],
595+ root_dir : Path ,
596+ wl_spec : pathspec .PathSpec | None ,
597+ ) -> list [Path ]:
598+ if wl_spec is None :
599+ return files
600+ result : list [Path ] = []
601+ for file_path in files :
602+ try :
603+ rel_path = file_path .relative_to (root_dir ).as_posix ()
604+ if is_whitelisted (rel_path , wl_spec ):
605+ result .append (file_path )
606+ except ValueError :
607+ pass
608+ return result
609+
610+
589611def _filter_ignored (
590612 files : list [Path ],
591613 root_dir : Path ,
0 commit comments