Skip to content

Commit

Permalink
Mark submodules as vendored by default
Browse files Browse the repository at this point in the history
  • Loading branch information
spenserblack committed Sep 5, 2023
1 parent 810ff9b commit 924737b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
2 changes: 1 addition & 1 deletion gengo-bin/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl CLI {
let gengo = Builder::new(&self.repository)
.read_limit(self.read_limit)
.build();
let gengo = match gengo {
let mut gengo = match gengo {
Ok(gengo) => gengo,
Err(e) => {
writeln!(err, "failed to create instance: {}", e)?;
Expand Down
2 changes: 1 addition & 1 deletion gengo/benches/run_on_self.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion};
use gengo::Builder;

fn head_benchmark(c: &mut Criterion) {
let gengo = Builder::new(env!("CARGO_MANIFEST_DIR")).build().unwrap();
let mut gengo = Builder::new(env!("CARGO_MANIFEST_DIR")).build().unwrap();
for n in 0..3 {
let rev = format!("HEAD{}", "^".repeat(n));
c.bench_function(&format!("run on {}", rev), |b| {
Expand Down
25 changes: 20 additions & 5 deletions gengo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub use languages::analyzer::Analyzers;
use languages::Category;
pub use languages::Language;
use std::collections::HashMap;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::sync::atomic::Ordering;
use vendored::Vendored;

Expand Down Expand Up @@ -131,7 +131,7 @@ impl Results {

impl Gengo {
/// Analyzes each file in the repository at the given revision.
pub fn analyze(&self, rev: &str) -> Result<Analysis> {
pub fn analyze(&mut self, rev: &str) -> Result<Analysis> {
let repo = self.repository.to_thread_local();
let tree_id = repo.rev_parse_single(rev)?.object()?.peel_to_tree()?.id;
let mut stack = vec![(BString::default(), repo, tree_id)];
Expand All @@ -149,7 +149,14 @@ impl Gengo {
})
.collect::<HashMap<_, _>>()
});
self.analyze_index(&repo.into_sync(), &mut results, state)?;

if let Some(ref submodules) = submodules {
submodules

Check warning on line 154 in gengo/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

gengo/src/lib.rs#L154

Added line #L154 was not covered by tests
.keys()
.for_each(|path| self.vendored.add_dir(path.to_string()));

Check warning on line 156 in gengo/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

gengo/src/lib.rs#L156

Added line #L156 was not covered by tests
}

self.analyze_index(&root, &repo.into_sync(), &mut results, state)?;
all_results.push(results);

if let Some(mut submodules_by_path) = submodules {
Expand All @@ -176,6 +183,7 @@ impl Gengo {

fn analyze_index(
&self,
root: &BString,
repo: &gix::ThreadSafeRepository,
results: &mut Results,
state: GitState,
Expand All @@ -193,7 +201,7 @@ impl Gengo {
else {
return Ok(());
};
self.analyze_blob(path, repo, state, entry)
self.analyze_blob(root, path, repo, state, entry)
},
|| Some(std::time::Duration::from_micros(5)),
std::convert::identity,
Expand All @@ -203,12 +211,19 @@ impl Gengo {

fn analyze_blob(
&self,
root: &BString,
filepath: impl AsRef<Path>,
repo: &gix::Repository,
state: &mut GitState,
result: &mut BlobEntry,
) -> Result<()> {
let filepath = filepath.as_ref();
let mut buf = PathBuf::new();
if !root.is_empty() {
buf.push(root.to_string());

Check warning on line 222 in gengo/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

gengo/src/lib.rs#L222

Added line #L222 was not covered by tests
}
buf.push(&filepath);
let filepath = buf.as_path();

let blob = repo.find_object(result.index_entry.id)?;
let contents = blob.data.as_slice();
state
Expand Down
20 changes: 18 additions & 2 deletions gengo/src/vendored.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
use super::GLOB_MATCH_OPTIONS;
use glob::Pattern;
use std::path::Path;

pub struct Vendored;
pub struct Vendored {
globs: Vec<Pattern>,
}

impl Vendored {
pub fn new() -> Self {
Self
let globs = Vec::new();

Self { globs }
}

pub fn add_dir<P: AsRef<Path>>(&mut self, dir: P) {
let pattern = format!("{}/**", dir.as_ref().display());
let pattern = Pattern::new(&pattern).unwrap();
self.globs.push(pattern);

Check warning on line 19 in gengo/src/vendored.rs

View check run for this annotation

Codecov / codecov/patch

gengo/src/vendored.rs#L16-L19

Added lines #L16 - L19 were not covered by tests
}

pub fn is_vendored<P: AsRef<Path>>(&self, filepath: P, contents: &[u8]) -> bool {
Expand All @@ -17,6 +29,10 @@ impl Vendored {
.components()
.next()
.map_or(false, |c| c.as_os_str() == "node_modules")
|| self
.globs
.iter()
.any(|g| g.matches_path_with(filepath.as_ref(), GLOB_MATCH_OPTIONS))
}

fn is_vendored_with_read<P: AsRef<Path>>(&self, _filepath: P, _contents: &[u8]) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion gengo/tests/gengo_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod util;
fn test_javascript() {
let analyzers = fixture_str!("test_javascript-analyzers.yaml");
let analyzers = Analyzers::from_yaml(analyzers).unwrap();
let gengo = Builder::new(ROOT).analyzers(analyzers).build().unwrap();
let mut gengo = Builder::new(ROOT).analyzers(analyzers).build().unwrap();
let results = gengo.analyze("test/javascript").unwrap();
insta::assert_debug_snapshot!(results);
}

0 comments on commit 924737b

Please sign in to comment.