# HG changeset patch # User Raphaël Gomès # Date 1658157949 -7200 # Node ID 5fbdd88824dcbdc67e336e480657cafcbfb46e28 # Parent a0d189b2e8718033ec6c7587a1d673d399a2c066 rust-filepatterns: allow overriding default syntax This will be used when parsing pattern files other than .hgignore like the sparse spec. diff -r a0d189b2e871 -r 5fbdd88824dc rust/hg-core/src/filepatterns.rs --- a/rust/hg-core/src/filepatterns.rs Tue Jul 12 10:15:52 2022 +0200 +++ b/rust/hg-core/src/filepatterns.rs Mon Jul 18 17:25:49 2022 +0200 @@ -329,6 +329,7 @@ pub fn parse_pattern_file_contents( lines: &[u8], file_path: &Path, + default_syntax_override: Option<&[u8]>, warn: bool, ) -> Result<(Vec, Vec), PatternError> { let comment_regex = Regex::new(r"((?:^|[^\\])(?:\\\\)*)#.*").unwrap(); @@ -338,7 +339,8 @@ let mut inputs: Vec = vec![]; let mut warnings: Vec = vec![]; - let mut current_syntax = b"relre:".as_ref(); + let mut current_syntax = + default_syntax_override.unwrap_or(b"relre:".as_ref()); for (line_number, mut line) in lines.split(|c| *c == b'\n').enumerate() { let line_number = line_number + 1; @@ -413,7 +415,7 @@ match std::fs::read(file_path) { Ok(contents) => { inspect_pattern_bytes(&contents); - parse_pattern_file_contents(&contents, file_path, warn) + parse_pattern_file_contents(&contents, file_path, None, warn) } Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(( vec![], @@ -601,9 +603,14 @@ let lines = b"syntax: glob\n*.elc"; assert_eq!( - parse_pattern_file_contents(lines, Path::new("file_path"), false) - .unwrap() - .0, + parse_pattern_file_contents( + lines, + Path::new("file_path"), + None, + false + ) + .unwrap() + .0, vec![IgnorePattern::new( PatternSyntax::RelGlob, b"*.elc", @@ -614,16 +621,26 @@ let lines = b"syntax: include\nsyntax: glob"; assert_eq!( - parse_pattern_file_contents(lines, Path::new("file_path"), false) - .unwrap() - .0, + parse_pattern_file_contents( + lines, + Path::new("file_path"), + None, + false + ) + .unwrap() + .0, vec![] ); let lines = b"glob:**.o"; assert_eq!( - parse_pattern_file_contents(lines, Path::new("file_path"), false) - .unwrap() - .0, + parse_pattern_file_contents( + lines, + Path::new("file_path"), + None, + false + ) + .unwrap() + .0, vec![IgnorePattern::new( PatternSyntax::RelGlob, b"**.o",