History

refactor

Sidney Douw committed 4 years ago

Commit 476fa561 parent 9ceea272 files · +53 −27Browse files

Unified · Split

2 files
  1. git-pathspec/src/parse.rs +44 −22
  2. git-pathspec/tests/pathspec.rs +9 −5
git-pathspec/src/parse.rs+44−22
@@ -9,6 +9,8 @@ pub enum Error { · 8 unmodified lines
99 EmptyString,
1010 #[error("Found {:?}, which is not a valid keyword", found_keyword)]
1111 InvalidKeyword { found_keyword: BString },
12+ #[error("Unimplemented pathspec magic {:?}", found_short_keyword)]
13+ Unimplemented { found_short_keyword: BString },
1214 #[error("Missing ')' at the end of pathspec magic in {:?}", pathspec)]
1315 MissingClosingParenthesis { pathspec: BString },
1416 #[error("Attribute has non-ascii characters or starts with '-': {:?}", attribute)]
@@ -33,31 +35,17 @@ impl Pattern { · 18 unmodified lines
3335 search_mode: SearchMode::ShellGlob,
3436 attributes: Vec::new(),
3537 };
38+
3639 let mut cursor = 0;
3740 if input.first() == Some(&b':') {
3841 cursor += 1;
39− while let Some(&b) = input.get(cursor) {
42+ p.signature |= parse_short_keywords(&input, &mut cursor)?;
43+ if let Some(b'(') = input.get(cursor) {
4044 cursor += 1;
41− match b {
42− b':' => break,
43− b'/' => p.signature |= MagicSignature::TOP,
44− b'^' | b'!' => p.signature |= MagicSignature::EXCLUDE,
45− b'(' => {
46− let end = input.find(")").ok_or(Error::MissingClosingParenthesis {
47− pathspec: BString::from(input),
48− })?;
49− let pat = parse_keywords(&input[cursor..end])?;
50−
51− cursor = end + 1;
52− p.search_mode = pat.search_mode;
53− p.attributes = pat.attributes;
54− p.signature |= pat.signature;
55− }
56− _ => {
57− cursor -= 1;
58− break;
59− }
60− }
45+ let pat = parse_long_keywords(&input, &mut cursor)?;
46+ p.search_mode = pat.search_mode;
47+ p.attributes = pat.attributes;
48+ p.signature |= pat.signature;
6149 }
6250 }
6351
@@ -66,7 +54,41 @@ impl Pattern { · 2 unmodified lines
6654 }
6755 }
6856
69−fn parse_keywords(input: &[u8]) -> Result<Pattern, Error> {
57+fn parse_short_keywords(input: &[u8], cursor: &mut usize) -> Result<MagicSignature, Error> {
58+ let unimplemented_chars = vec![
59+ b'"', b'#', b'%', b'&', b'\'', b',', b'-', b';', b'<', b'=', b'>', b'@', b'_', b'`', b'~',
60+ ];
61+ let mut signature = MagicSignature::empty();
62+
63+ while let Some(&b) = input.get(*cursor) {
64+ *cursor += 1;
65+ signature |= match b {
66+ b'/' => MagicSignature::TOP,
67+ b'^' | b'!' => MagicSignature::EXCLUDE,
68+ b':' => break,
69+ _ if unimplemented_chars.contains(&b) => {
70+ return Err(Error::Unimplemented {
71+ found_short_keyword: vec![b].into(),
72+ });
73+ }
74+ _ => {
75+ *cursor -= 1;
76+ break;
77+ }
78+ }
79+ }
80+
81+ return Ok(signature);
82+}
83+
84+fn parse_long_keywords(input: &[u8], cursor: &mut usize) -> Result<Pattern, Error> {
85+ let end = input.find(")").ok_or(Error::MissingClosingParenthesis {
86+ pathspec: BString::from(input),
87+ })?;
88+
89+ let input = &input[*cursor..end];
90+ *cursor = end + 1;
91+
7092 let mut p = Pattern {
7193 path: BString::default(),
7294 signature: MagicSignature::empty(),
git-pathspec/tests/pathspec.rs+9−5
@@ -41,11 +41,13 @@ mod succeed { · 40 unmodified lines
4141 #[test]
4242 fn empty_signatures() {
4343 let inputs = vec![
44+ (".", pat_with_path(".")),
4445 ("some/path", pat_with_path("some/path")),
4546 (":some/path", pat_with_path("some/path")),
4647 (":()some/path", pat_with_path("some/path")),
4748 ("::some/path", pat_with_path("some/path")),
4849 (":::some/path", pat_with_path(":some/path")),
50+ (":():some/path", pat_with_path(":some/path")),
4951 ];
5052
5153 check_valid_inputs(inputs)
@@ -68,8 +70,8 @@ mod succeed { · 16 unmodified lines
6870 pat_with_path_and_sig("some/path", MagicSignature::TOP | MagicSignature::EXCLUDE),
6971 ),
7072 (
71− ":!/^/:",
72− pat_with_path_and_sig("", MagicSignature::TOP | MagicSignature::EXCLUDE),
73+ ":!/^/:some/path",
74+ pat_with_path_and_sig("some/path", MagicSignature::TOP | MagicSignature::EXCLUDE),
7375 ),
7476 ];
7577
@@ -219,16 +221,18 @@ mod fail { · 143 unmodified lines
219221 }
220222
221223 #[test]
222− #[ignore]
223224 fn invalid_short_signatures() {
224− let inputs = vec![":=()"];
225+ let inputs = vec![
226+ ":\"()", ":#()", ":%()", ":&()", ":'()", ":,()", ":-()", ":;()", ":<()", ":=()", ":>()", ":@()", ":_()",
227+ ":`()", ":~()",
228+ ];
225229
226230 inputs.into_iter().for_each(|input| {
227231 assert!(!is_valid_in_git(input), "This pathspec is valid in git: {}", input);
228232
229233 let output = git_pathspec::parse(input.as_bytes());
230234 assert!(output.is_err());
231− // assert!(matches!(output.unwrap_err(), Error::InvalidShortSignature { .. }));
235+ assert!(matches!(output.unwrap_err(), Error::Unimplemented { .. }));
232236 });
233237 }
234238

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t