Skip to content

feat(ops): Commit filtering support #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,12 @@ which = "4"
[dev-dependencies]
git-fixture = { version = "0.3", features = ["yaml"] }
assert_fs = "1"
eyre = "0.6"
snapbox = "0.2"
regex = "1.5.5"
criterion = "0.3.5"

[[bench]]
harness = false
name = "ops"
path = "benches/ops.rs"
30 changes: 30 additions & 0 deletions benches/ops.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use std::collections::HashSet;

fn get_repo() -> git2::Repository {
let repo_dir =
std::env::var("PATH_TO_REPO").expect("`PATH_TO_REPO` environment variable not set");
git2::Repository::discover(&std::path::PathBuf::from(repo_dir)).unwrap()
}

fn bench_get_changed_paths_between_trees(c: &mut criterion::Criterion) {
c.bench_function("get_changed_paths_between_trees", |b| {
let repo = get_repo();
let oid = repo.head().unwrap().target().unwrap();
let commit = repo.find_commit(oid).unwrap();
let parent = commit.parent(0).unwrap();
let parent_tree = parent.tree().unwrap();
let commit_tree = commit.tree().unwrap();

b.iter(|| -> HashSet<std::path::PathBuf> {
git2_ext::tree::get_changed_paths_between_trees(
&repo,
Some(&parent_tree),
Some(&commit_tree),
)
.unwrap()
});
});
}

criterion::criterion_group!(benches, bench_get_changed_paths_between_trees,);
criterion::criterion_main!(benches);
13 changes: 13 additions & 0 deletions src/bytes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// From git2 crate
#[cfg(unix)]
pub(crate) fn bytes2path(b: &[u8]) -> &std::path::Path {
use std::os::unix::prelude::*;
std::path::Path::new(std::ffi::OsStr::from_bytes(b))
}

// From git2 crate
#[cfg(windows)]
pub(crate) fn bytes2path(b: &[u8]) -> &std::path::Path {
use std::str;
std::path::Path::new(str::from_utf8(b).unwrap())
}
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@

pub mod hooks;
pub mod ops;
pub mod tree;
pub mod utils;

pub(crate) mod bytes;

#[cfg(test)]
mod testing;
46 changes: 26 additions & 20 deletions src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,19 @@ pub fn cherry_pick(
let our_path = conflict
.our
.as_ref()
.map(|c| bytes2path(&c.path))
.or_else(|| conflict.their.as_ref().map(|c| bytes2path(&c.path)))
.or_else(|| conflict.ancestor.as_ref().map(|c| bytes2path(&c.path)))
.map(|c| crate::bytes::bytes2path(&c.path))
.or_else(|| {
conflict
.their
.as_ref()
.map(|c| crate::bytes::bytes2path(&c.path))
})
.or_else(|| {
conflict
.ancestor
.as_ref()
.map(|c| crate::bytes::bytes2path(&c.path))
})
.unwrap_or_else(|| std::path::Path::new("<unknown>"));
format!("{}", our_path.display())
})
Expand Down Expand Up @@ -171,9 +181,19 @@ pub fn squash(
let our_path = conflict
.our
.as_ref()
.map(|c| bytes2path(&c.path))
.or_else(|| conflict.their.as_ref().map(|c| bytes2path(&c.path)))
.or_else(|| conflict.ancestor.as_ref().map(|c| bytes2path(&c.path)))
.map(|c| crate::bytes::bytes2path(&c.path))
.or_else(|| {
conflict
.their
.as_ref()
.map(|c| crate::bytes::bytes2path(&c.path))
})
.or_else(|| {
conflict
.ancestor
.as_ref()
.map(|c| crate::bytes::bytes2path(&c.path))
})
.unwrap_or_else(|| std::path::Path::new("<unknown>"));
format!("{}", our_path.display())
})
Expand Down Expand Up @@ -217,17 +237,3 @@ pub fn reword(
)?;
Ok(new_id)
}

// From git2 crate
#[cfg(unix)]
fn bytes2path(b: &[u8]) -> &std::path::Path {
use std::os::unix::prelude::*;
std::path::Path::new(std::ffi::OsStr::from_bytes(b))
}

// From git2 crate
#[cfg(windows)]
fn bytes2path(b: &[u8]) -> &std::path::Path {
use std::str;
std::path::Path::new(str::from_utf8(b).unwrap())
}
Loading