History

refactor

simplify baseline parsing

Sebastian Thiel committed 4 years ago

Commit 02fba2c1 parent 44991d35 files · +17 −20Browse files

Unified · Split

5 files
  1. Cargo.lock +0 −1
  2. git-config/Cargo.toml +1 −1
  3. git-pathspec/Cargo.toml +0 −1
  4. git-pathspec/tests/fixtures/generate_pathspec_baseline.sh +2 −0
  5. git-pathspec/tests/pathspec.rs +14 −17
Cargo.lock+0−1
@@ -1425,7 +1425,6 @@ dependencies = [ · 1424 unmodified lines
14251425 "git-testtools",
14261426 "once_cell",
14271427 "thiserror",
1428− "walkdir",
14291428 ]
14301429
14311430 [[package]]
git-config/Cargo.toml+1−1
@@ -14,7 +14,7 @@ include = ["src/**/*", "LICENSE-*", "README.md", "CHANGELOG.md"] · 13 unmodified lines
1414 # serde = ["serde_crate"]
1515
1616 [dependencies]
17−git-features = { version = "^0.21.0", path = "../git-features"}
17+git-features = { version = "^0.21.0", path = "../git-features" }
1818 git-path = { version = "^0.1.2", path = "../git-path" }
1919 git-sec = { version = "^0.1.1", path = "../git-sec" }
2020 git-ref = { version = "0.13.0", path = "../git-ref" }
git-pathspec/Cargo.toml+0−1
@@ -24,4 +24,3 @@ compact_str = "0.3.2" · 23 unmodified lines
2424 [dev-dependencies]
2525 git-testtools = { path = "../tests/tools" }
2626 once_cell = "1.12.0"
27−walkdir = "2.3.2"
git-pathspec/tests/fixtures/generate_pathspec_baseline.sh+2−0
@@ -1,6 +1,8 @@
11 #!/bin/bash
22 set -eu -o pipefail
33
4+git init;
5+
46 function baseline() {
57 local pathspec=$1 # first argument is the pathspec to test
68
git-pathspec/tests/pathspec.rs+14−17
@@ -1,29 +1,25 @@
1−use bstr::{BString, ByteSlice};
1+use bstr::{BStr, BString, ByteSlice};
22 use git_attributes::State;
33 use git_pathspec::{MagicSignature, Pattern, SearchMode};
44 use once_cell::sync::Lazy;
55 use std::collections::HashMap;
66
7−static BASELINE: Lazy<HashMap<BString, BString>> = Lazy::new(|| {
8− let mut baseline_map = HashMap::new();
7+pub use git_testtools::Result;
98
9+static BASELINE: Lazy<HashMap<BString, usize>> = Lazy::new(|| {
1010 let base = git_testtools::scripted_fixture_repo_read_only("generate_pathspec_baseline.sh").unwrap();
1111
12− let dirwalk = walkdir::WalkDir::new(base)
13− .max_depth(1)
14− .follow_links(false)
15− .into_iter()
16− .filter_map(|e| e.ok().and_then(|e| (e.file_name() == "baseline.git").then(|| e)));
17−
18− for baseline_entry in dirwalk {
19− let baseline = std::fs::read(baseline_entry.path()).unwrap();
12+ (|| -> Result<_> {
13+ let mut map = HashMap::new();
14+ let baseline = std::fs::read(base.join("baseline.git"))?;
2015 let mut lines = baseline.lines();
2116 while let Some(spec) = lines.next() {
22− let exit_code = lines.next().unwrap().into();
23− baseline_map.insert(spec.into(), exit_code);
17+ let exit_code = lines.next().expect("two lines per baseline").to_str()?.parse()?;
18+ map.insert(spec.into(), exit_code);
2419 }
25− }
26− baseline_map
20+ Ok(map)
21+ })()
22+ .unwrap()
2723 });
2824
2925 mod succeed {
@@ -393,8 +389,9 @@ fn pat(path: &str, signature: MagicSignature, search_mode: SearchMode, attribute · 363 unmodified lines
393389 }
394390
395391 fn check_against_baseline(pathspec: &str) -> bool {
392+ let key: &BStr = pathspec.into();
396393 let base = BASELINE
397− .get(&BString::from(pathspec))
394+ .get(key)
398395 .expect(&format!("missing baseline for pathspec: {:?}", pathspec));
399− *base == BString::from("0")
396+ *base == 0
400397 }