Mercurial > public > mercurial-scm > hg
comparison rust/hg-core/src/filepatterns.rs @ 49482:5fbdd88824dc
rust-filepatterns: allow overriding default syntax
This will be used when parsing pattern files other than .hgignore like the
sparse spec.
author | Rapha?l Gom?s <rgomes@octobus.net> |
---|---|
date | Mon, 18 Jul 2022 17:25:49 +0200 |
parents | 6d69e83e6b6e |
children | 7c93e38a0bbd |
comparison
equal
deleted
inserted
replaced
49481:a0d189b2e871 | 49482:5fbdd88824dc |
---|---|
327 } | 327 } |
328 | 328 |
329 pub fn parse_pattern_file_contents( | 329 pub fn parse_pattern_file_contents( |
330 lines: &[u8], | 330 lines: &[u8], |
331 file_path: &Path, | 331 file_path: &Path, |
332 default_syntax_override: Option<&[u8]>, | |
332 warn: bool, | 333 warn: bool, |
333 ) -> Result<(Vec<IgnorePattern>, Vec<PatternFileWarning>), PatternError> { | 334 ) -> Result<(Vec<IgnorePattern>, Vec<PatternFileWarning>), PatternError> { |
334 let comment_regex = Regex::new(r"((?:^|[^\\])(?:\\\\)*)#.*").unwrap(); | 335 let comment_regex = Regex::new(r"((?:^|[^\\])(?:\\\\)*)#.*").unwrap(); |
335 | 336 |
336 #[allow(clippy::trivial_regex)] | 337 #[allow(clippy::trivial_regex)] |
337 let comment_escape_regex = Regex::new(r"\\#").unwrap(); | 338 let comment_escape_regex = Regex::new(r"\\#").unwrap(); |
338 let mut inputs: Vec<IgnorePattern> = vec![]; | 339 let mut inputs: Vec<IgnorePattern> = vec![]; |
339 let mut warnings: Vec<PatternFileWarning> = vec![]; | 340 let mut warnings: Vec<PatternFileWarning> = vec![]; |
340 | 341 |
341 let mut current_syntax = b"relre:".as_ref(); | 342 let mut current_syntax = |
343 default_syntax_override.unwrap_or(b"relre:".as_ref()); | |
342 | 344 |
343 for (line_number, mut line) in lines.split(|c| *c == b'\n').enumerate() { | 345 for (line_number, mut line) in lines.split(|c| *c == b'\n').enumerate() { |
344 let line_number = line_number + 1; | 346 let line_number = line_number + 1; |
345 | 347 |
346 let line_buf; | 348 let line_buf; |
411 inspect_pattern_bytes: &mut impl FnMut(&[u8]), | 413 inspect_pattern_bytes: &mut impl FnMut(&[u8]), |
412 ) -> Result<(Vec<IgnorePattern>, Vec<PatternFileWarning>), PatternError> { | 414 ) -> Result<(Vec<IgnorePattern>, Vec<PatternFileWarning>), PatternError> { |
413 match std::fs::read(file_path) { | 415 match std::fs::read(file_path) { |
414 Ok(contents) => { | 416 Ok(contents) => { |
415 inspect_pattern_bytes(&contents); | 417 inspect_pattern_bytes(&contents); |
416 parse_pattern_file_contents(&contents, file_path, warn) | 418 parse_pattern_file_contents(&contents, file_path, None, warn) |
417 } | 419 } |
418 Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(( | 420 Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(( |
419 vec![], | 421 vec![], |
420 vec![PatternFileWarning::NoSuchFile(file_path.to_owned())], | 422 vec![PatternFileWarning::NoSuchFile(file_path.to_owned())], |
421 )), | 423 )), |
599 #[test] | 601 #[test] |
600 fn test_parse_pattern_file_contents() { | 602 fn test_parse_pattern_file_contents() { |
601 let lines = b"syntax: glob\n*.elc"; | 603 let lines = b"syntax: glob\n*.elc"; |
602 | 604 |
603 assert_eq!( | 605 assert_eq!( |
604 parse_pattern_file_contents(lines, Path::new("file_path"), false) | 606 parse_pattern_file_contents( |
605 .unwrap() | 607 lines, |
606 .0, | 608 Path::new("file_path"), |
609 None, | |
610 false | |
611 ) | |
612 .unwrap() | |
613 .0, | |
607 vec![IgnorePattern::new( | 614 vec![IgnorePattern::new( |
608 PatternSyntax::RelGlob, | 615 PatternSyntax::RelGlob, |
609 b"*.elc", | 616 b"*.elc", |
610 Path::new("file_path") | 617 Path::new("file_path") |
611 )], | 618 )], |
612 ); | 619 ); |
613 | 620 |
614 let lines = b"syntax: include\nsyntax: glob"; | 621 let lines = b"syntax: include\nsyntax: glob"; |
615 | 622 |
616 assert_eq!( | 623 assert_eq!( |
617 parse_pattern_file_contents(lines, Path::new("file_path"), false) | 624 parse_pattern_file_contents( |
618 .unwrap() | 625 lines, |
619 .0, | 626 Path::new("file_path"), |
627 None, | |
628 false | |
629 ) | |
630 .unwrap() | |
631 .0, | |
620 vec![] | 632 vec![] |
621 ); | 633 ); |
622 let lines = b"glob:**.o"; | 634 let lines = b"glob:**.o"; |
623 assert_eq!( | 635 assert_eq!( |
624 parse_pattern_file_contents(lines, Path::new("file_path"), false) | 636 parse_pattern_file_contents( |
625 .unwrap() | 637 lines, |
626 .0, | 638 Path::new("file_path"), |
639 None, | |
640 false | |
641 ) | |
642 .unwrap() | |
643 .0, | |
627 vec![IgnorePattern::new( | 644 vec![IgnorePattern::new( |
628 PatternSyntax::RelGlob, | 645 PatternSyntax::RelGlob, |
629 b"**.o", | 646 b"**.o", |
630 Path::new("file_path") | 647 Path::new("file_path") |
631 )] | 648 )] |