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
1425 "git-testtools",1425 "git-testtools",
1426 "once_cell",1426 "once_cell",
1427 "thiserror",1427 "thiserror",
1428 "walkdir",
1429]1428]
1430 1429
1431[[package]]1430[[package]]
git-config/Cargo.toml+1−1
@@ -14,7 +14,7 @@ include = ["src/**/*", "LICENSE-*", "README.md", "CHANGELOG.md"] · 13 unmodified lines
14# serde = ["serde_crate"]14# serde = ["serde_crate"]
15 15
16[dependencies]16[dependencies]
17git-features = { version = "^0.21.0", path = "../git-features"}17git-features = { version = "^0.21.0", path = "../git-features" }
18git-path = { version = "^0.1.2", path = "../git-path" }18git-path = { version = "^0.1.2", path = "../git-path" }
19git-sec = { version = "^0.1.1", path = "../git-sec" }19git-sec = { version = "^0.1.1", path = "../git-sec" }
20git-ref = { version = "0.13.0", path = "../git-ref" }20git-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
24[dev-dependencies]24[dev-dependencies]
25git-testtools = { path = "../tests/tools" }25git-testtools = { path = "../tests/tools" }
26once_cell = "1.12.0"26once_cell = "1.12.0"
27walkdir = "2.3.2"
git-pathspec/tests/fixtures/generate_pathspec_baseline.sh+2−0
@@ -1,6 +1,8 @@
1#!/bin/bash1#!/bin/bash
2set -eu -o pipefail2set -eu -o pipefail
3 3
4git init;
5
4function baseline() {6function baseline() {
5 local pathspec=$1 # first argument is the pathspec to test7 local pathspec=$1 # first argument is the pathspec to test
6 8
git-pathspec/tests/pathspec.rs+14−17
@@ -1,29 +1,25 @@
1use bstr::{BString, ByteSlice};1use bstr::{BStr, BString, ByteSlice};
2use git_attributes::State;2use git_attributes::State;
3use git_pathspec::{MagicSignature, Pattern, SearchMode};3use git_pathspec::{MagicSignature, Pattern, SearchMode};
4use once_cell::sync::Lazy;4use once_cell::sync::Lazy;
5use std::collections::HashMap;5use std::collections::HashMap;
6 6
7static BASELINE: Lazy<HashMap<BString, BString>> = Lazy::new(|| {7pub use git_testtools::Result;
8 let mut baseline_map = HashMap::new();
9 8
9static BASELINE: Lazy<HashMap<BString, usize>> = Lazy::new(|| {
10 let base = git_testtools::scripted_fixture_repo_read_only("generate_pathspec_baseline.sh").unwrap();10 let base = git_testtools::scripted_fixture_repo_read_only("generate_pathspec_baseline.sh").unwrap();
11 11
12 let dirwalk = walkdir::WalkDir::new(base)12 (|| -> Result<_> {
13 .max_depth(1)13 let mut map = HashMap::new();
14 .follow_links(false)14 let baseline = std::fs::read(base.join("baseline.git"))?;
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();
20 let mut lines = baseline.lines();15 let mut lines = baseline.lines();
21 while let Some(spec) = lines.next() {16 while let Some(spec) = lines.next() {
22 let exit_code = lines.next().unwrap().into();17 let exit_code = lines.next().expect("two lines per baseline").to_str()?.parse()?;
23 baseline_map.insert(spec.into(), exit_code);18 map.insert(spec.into(), exit_code);
24 }19 }
25 }20 Ok(map)
26 baseline_map21 })()
22 .unwrap()
27});23});
28 24
29mod succeed {25mod succeed {
@@ -393,8 +389,9 @@ fn pat(path: &str, signature: MagicSignature, search_mode: SearchMode, attribute · 363 unmodified lines
393}389}
394 390
395fn check_against_baseline(pathspec: &str) -> bool {391fn check_against_baseline(pathspec: &str) -> bool {
392 let key: &BStr = pathspec.into();
396 let base = BASELINE393 let base = BASELINE
397 .get(&BString::from(pathspec))394 .get(key)
398 .expect(&format!("missing baseline for pathspec: {:?}", pathspec));395 .expect(&format!("missing baseline for pathspec: {:?}", pathspec));
399 *base == BString::from("0")396 *base == 0
400}397}