comparison rust/hg-core/src/matchers.rs @ 50861:090658724abf

rust: de-hardcode glob_suffix We're adding patternmatcher in a subsequent commit, and this needs needs to be different for includematcher and patternmatcher.
author Spencer Baugh <sbaugh@janestreet.com>
date Mon, 14 Aug 2023 09:25:36 -0400
parents f50e71fdfcb4
children f874342fa568
comparison
equal deleted inserted replaced
50860:f50e71fdfcb4 50861:090658724abf
652 652
653 /// Returns the regex pattern and a function that matches an `HgPath` against 653 /// Returns the regex pattern and a function that matches an `HgPath` against
654 /// said regex formed by the given ignore patterns. 654 /// said regex formed by the given ignore patterns.
655 fn build_regex_match<'a, 'b>( 655 fn build_regex_match<'a, 'b>(
656 ignore_patterns: &'a [IgnorePattern], 656 ignore_patterns: &'a [IgnorePattern],
657 glob_suffix: &[u8],
657 ) -> PatternResult<(Vec<u8>, IgnoreFnType<'b>)> { 658 ) -> PatternResult<(Vec<u8>, IgnoreFnType<'b>)> {
658 let mut regexps = vec![]; 659 let mut regexps = vec![];
659 let mut exact_set = HashSet::new(); 660 let mut exact_set = HashSet::new();
660 661
661 for pattern in ignore_patterns { 662 for pattern in ignore_patterns {
662 if let Some(re) = build_single_regex(pattern)? { 663 if let Some(re) = build_single_regex(pattern, glob_suffix)? {
663 regexps.push(re); 664 regexps.push(re);
664 } else { 665 } else {
665 let exact = normalize_path_bytes(&pattern.pattern); 666 let exact = normalize_path_bytes(&pattern.pattern);
666 exact_set.insert(HgPathBuf::from_bytes(&exact)); 667 exact_set.insert(HgPathBuf::from_bytes(&exact));
667 } 668 }
778 779
779 /// Returns a function that checks whether a given file (in the general sense) 780 /// Returns a function that checks whether a given file (in the general sense)
780 /// should be matched. 781 /// should be matched.
781 fn build_match<'a>( 782 fn build_match<'a>(
782 ignore_patterns: Vec<IgnorePattern>, 783 ignore_patterns: Vec<IgnorePattern>,
784 glob_suffix: &[u8],
783 ) -> PatternResult<(Vec<u8>, IgnoreFnType<'a>)> { 785 ) -> PatternResult<(Vec<u8>, IgnoreFnType<'a>)> {
784 let mut match_funcs: Vec<IgnoreFnType<'a>> = vec![]; 786 let mut match_funcs: Vec<IgnoreFnType<'a>> = vec![];
785 // For debugging and printing 787 // For debugging and printing
786 let mut patterns = vec![]; 788 let mut patterns = vec![];
787 789
841 843
842 patterns.extend(b"rootfilesin: "); 844 patterns.extend(b"rootfilesin: ");
843 dirs_vec.sort(); 845 dirs_vec.sort();
844 patterns.extend(dirs_vec.escaped_bytes()); 846 patterns.extend(dirs_vec.escaped_bytes());
845 } else { 847 } else {
846 let (new_re, match_func) = build_regex_match(&ignore_patterns)?; 848 let (new_re, match_func) =
849 build_regex_match(&ignore_patterns, glob_suffix)?;
847 patterns = new_re; 850 patterns = new_re;
848 match_funcs.push(match_func) 851 match_funcs.push(match_func)
849 } 852 }
850 } 853 }
851 854
920 parents, 923 parents,
921 } = roots_dirs_and_parents(&ignore_patterns)?; 924 } = roots_dirs_and_parents(&ignore_patterns)?;
922 let prefix = ignore_patterns.iter().all(|k| { 925 let prefix = ignore_patterns.iter().all(|k| {
923 matches!(k.syntax, PatternSyntax::Path | PatternSyntax::RelPath) 926 matches!(k.syntax, PatternSyntax::Path | PatternSyntax::RelPath)
924 }); 927 });
925 let (patterns, match_fn) = build_match(ignore_patterns)?; 928 let (patterns, match_fn) = build_match(ignore_patterns, b"(?:/|$)")?;
926 929
927 Ok(Self { 930 Ok(Self {
928 patterns, 931 patterns,
929 match_fn, 932 match_fn,
930 prefix, 933 prefix,