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
9 EmptyString,9 EmptyString,
10 #[error("Found {:?}, which is not a valid keyword", found_keyword)]10 #[error("Found {:?}, which is not a valid keyword", found_keyword)]
11 InvalidKeyword { found_keyword: BString },11 InvalidKeyword { found_keyword: BString },
12 #[error("Unimplemented pathspec magic {:?}", found_short_keyword)]
13 Unimplemented { found_short_keyword: BString },
12 #[error("Missing ')' at the end of pathspec magic in {:?}", pathspec)]14 #[error("Missing ')' at the end of pathspec magic in {:?}", pathspec)]
13 MissingClosingParenthesis { pathspec: BString },15 MissingClosingParenthesis { pathspec: BString },
14 #[error("Attribute has non-ascii characters or starts with '-': {:?}", attribute)]16 #[error("Attribute has non-ascii characters or starts with '-': {:?}", attribute)]
@@ -33,31 +35,17 @@ impl Pattern { · 18 unmodified lines
33 search_mode: SearchMode::ShellGlob,35 search_mode: SearchMode::ShellGlob,
34 attributes: Vec::new(),36 attributes: Vec::new(),
35 };37 };
38
36 let mut cursor = 0;39 let mut cursor = 0;
37 if input.first() == Some(&b':') {40 if input.first() == Some(&b':') {
38 cursor += 1;41 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) {
40 cursor += 1;44 cursor += 1;
41 match b {45 let pat = parse_long_keywords(&input, &mut cursor)?;
42 b':' => break,46 p.search_mode = pat.search_mode;
43 b'/' => p.signature |= MagicSignature::TOP,47 p.attributes = pat.attributes;
44 b'^' | b'!' => p.signature |= MagicSignature::EXCLUDE,48 p.signature |= pat.signature;
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 }
61 }49 }
62 }50 }
63 51
@@ -66,7 +54,41 @@ impl Pattern { · 2 unmodified lines
66 }54 }
67}55}
68 56
69fn parse_keywords(input: &[u8]) -> Result<Pattern, Error> {57fn 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
84fn 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
70 let mut p = Pattern {92 let mut p = Pattern {
71 path: BString::default(),93 path: BString::default(),
72 signature: MagicSignature::empty(),94 signature: MagicSignature::empty(),
git-pathspec/tests/pathspec.rs+9−5
@@ -41,11 +41,13 @@ mod succeed { · 40 unmodified lines
41 #[test]41 #[test]
42 fn empty_signatures() {42 fn empty_signatures() {
43 let inputs = vec![43 let inputs = vec![
44 (".", pat_with_path(".")),
44 ("some/path", pat_with_path("some/path")),45 ("some/path", pat_with_path("some/path")),
45 (":some/path", pat_with_path("some/path")),46 (":some/path", pat_with_path("some/path")),
46 (":()some/path", pat_with_path("some/path")),47 (":()some/path", pat_with_path("some/path")),
47 ("::some/path", pat_with_path("some/path")),48 ("::some/path", pat_with_path("some/path")),
48 (":::some/path", pat_with_path(":some/path")),49 (":::some/path", pat_with_path(":some/path")),
50 (":():some/path", pat_with_path(":some/path")),
49 ];51 ];
50 52
51 check_valid_inputs(inputs)53 check_valid_inputs(inputs)
@@ -68,8 +70,8 @@ mod succeed { · 16 unmodified lines
68 pat_with_path_and_sig("some/path", MagicSignature::TOP | MagicSignature::EXCLUDE),70 pat_with_path_and_sig("some/path", MagicSignature::TOP | MagicSignature::EXCLUDE),
69 ),71 ),
70 (72 (
71 ":!/^/:",73 ":!/^/:some/path",
72 pat_with_path_and_sig("", MagicSignature::TOP | MagicSignature::EXCLUDE),74 pat_with_path_and_sig("some/path", MagicSignature::TOP | MagicSignature::EXCLUDE),
73 ),75 ),
74 ];76 ];
75 77
@@ -219,16 +221,18 @@ mod fail { · 143 unmodified lines
219 }221 }
220 222
221 #[test]223 #[test]
222 #[ignore]
223 fn invalid_short_signatures() {224 fn invalid_short_signatures() {
224 let inputs = vec![":=()"];225 let inputs = vec![
226 ":\"()", ":#()", ":%()", ":&()", ":'()", ":,()", ":-()", ":;()", ":<()", ":=()", ":>()", ":@()", ":_()",
227 ":`()", ":~()",
228 ];
225 229
226 inputs.into_iter().for_each(|input| {230 inputs.into_iter().for_each(|input| {
227 assert!(!is_valid_in_git(input), "This pathspec is valid in git: {}", input);231 assert!(!is_valid_in_git(input), "This pathspec is valid in git: {}", input);
228 232
229 let output = git_pathspec::parse(input.as_bytes());233 let output = git_pathspec::parse(input.as_bytes());
230 assert!(output.is_err());234 assert!(output.is_err());
231 // assert!(matches!(output.unwrap_err(), Error::InvalidShortSignature { .. }));235 assert!(matches!(output.unwrap_err(), Error::Unimplemented { .. }));
232 });236 });
233 }237 }
234 238

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