Skip to content

Commit

Permalink
fix!: use dyn trait where possible.
Browse files Browse the repository at this point in the history
This reduces compile time due to avoiding duplication.
  • Loading branch information
Byron committed Sep 6, 2023
1 parent 24dd870 commit c1c88fd
Show file tree
Hide file tree
Showing 286 changed files with 1,669 additions and 1,612 deletions.
2 changes: 1 addition & 1 deletion gitoxide-core/src/commitgraph/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub(crate) mod function {
W1: io::Write,
W2: io::Write,
{
let g = Graph::at(path).with_context(|| "Could not open commit graph")?;
let g = Graph::at(path.as_ref()).with_context(|| "Could not open commit graph")?;

#[allow(clippy::unnecessary_wraps, unknown_lints)]
fn noop_processor(_commit: &gix::commitgraph::file::Commit<'_>) -> std::result::Result<(), std::fmt::Error> {
Expand Down
3 changes: 2 additions & 1 deletion gitoxide-core/src/corpus/run.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use gix::progress::DynNestedProgress;
use std::{path::Path, sync::atomic::AtomicBool};

use crate::{
Expand Down Expand Up @@ -141,7 +142,7 @@ impl Execute for VerifyOdb {
crate::repository::verify::integrity(
repo,
std::io::sink(),
progress,
progress.add_child("integrity".into()),
should_interrupt,
crate::repository::verify::Context {
output_statistics: None,
Expand Down
8 changes: 4 additions & 4 deletions gitoxide-core/src/index/checkout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ pub fn checkout_exclusive(
}
}
},
&mut files,
&mut bytes,
&files,
&bytes,
should_interrupt,
opts,
),
Expand All @@ -116,8 +116,8 @@ pub fn checkout_exclusive(
buf.clear();
Ok(gix::objs::BlobRef { data: buf })
},
&mut files,
&mut bytes,
&files,
&bytes,
should_interrupt,
opts,
),
Expand Down
4 changes: 2 additions & 2 deletions gitoxide-core/src/organize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ pub fn find_git_repository_workdirs(
fn find_origin_remote(repo: &Path) -> anyhow::Result<Option<gix_url::Url>> {
let non_bare = repo.join(".git").join("config");
let local = gix::config::Source::Local;
let config = gix::config::File::from_path_no_includes(non_bare.as_path(), local)
.or_else(|_| gix::config::File::from_path_no_includes(repo.join("config").as_path(), local))?;
let config = gix::config::File::from_path_no_includes(non_bare.as_path().into(), local)
.or_else(|_| gix::config::File::from_path_no_includes(repo.join("config"), local))?;
Ok(config
.string_by_key("remote.origin.url")
.map(|url| gix_url::Url::from_bytes(url.as_ref()))
Expand Down
21 changes: 10 additions & 11 deletions gitoxide-core/src/pack/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,13 @@ where
P: NestedProgress,
P::SubProgress: 'static,
{
type ObjectIdIter = dyn Iterator<Item = Result<ObjectId, Box<dyn std::error::Error + Send + Sync>>> + Send;

let repo = gix::discover(repository_path)?.into_sync();
progress.init(Some(2), progress::steps());
let tips = tips.into_iter();
let make_cancellation_err = || anyhow!("Cancelled by user");
let (mut handle, input): (
_,
Box<dyn Iterator<Item = Result<ObjectId, input_iteration::Error>> + Send>,
) = match input {
let (mut handle, mut input): (_, Box<ObjectIdIter>) = match input {
None => {
let mut progress = progress.add_child("traversing");
progress.init(None, progress::count("commits"));
Expand All @@ -141,7 +140,7 @@ where
let handle = handle.clone();
move |oid, buf| handle.find_commit_iter(oid, buf).map(|t| t.0)
})
.map(|res| res.map_err(Into::into).map(|c| c.id))
.map(|res| res.map_err(|err| Box::new(err) as Box<_>).map(|c| c.id))
.inspect(move |_| progress.inc()),
);
(handle, iter)
Expand All @@ -157,7 +156,7 @@ where
.lines()
.map(|hex_id| {
hex_id
.map_err(Into::into)
.map_err(|err| Box::new(err) as Box<_>)
.and_then(|hex_id| ObjectId::from_hex(hex_id.as_bytes()).map_err(Into::into))
})
.inspect(move |_| progress.inc()),
Expand Down Expand Up @@ -207,7 +206,7 @@ where
pack::data::output::count::objects(
handle.clone(),
input,
progress,
&progress,
&interrupt::IS_INTERRUPTED,
pack::data::output::count::objects::Options {
thread_limit,
Expand All @@ -217,9 +216,9 @@ where
)?
} else {
pack::data::output::count::objects_unthreaded(
handle.clone(),
input,
progress,
&handle,
&mut input,
&progress,
&interrupt::IS_INTERRUPTED,
input_object_expansion,
)?
Expand All @@ -236,7 +235,7 @@ where
InOrderIter::from(pack::data::output::entry::iter_from_counts(
counts,
handle,
progress,
Box::new(progress),
pack::data::output::entry::iter_from_counts::Options {
thread_limit,
mode: pack::data::output::entry::iter_from_counts::Mode::PackCopyAndBaseObjects,
Expand Down
31 changes: 17 additions & 14 deletions gitoxide-core/src/pack/explode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,22 @@ enum OutputWriter {
}

impl gix::odb::Write for OutputWriter {
type Error = Error;

fn write_buf(&self, kind: object::Kind, from: &[u8]) -> Result<ObjectId, Self::Error> {
fn write_buf(&self, kind: object::Kind, from: &[u8]) -> Result<ObjectId, gix::odb::write::Error> {
match self {
OutputWriter::Loose(db) => db.write_buf(kind, from).map_err(Into::into),
OutputWriter::Sink(db) => db.write_buf(kind, from).map_err(Into::into),
OutputWriter::Loose(db) => db.write_buf(kind, from),
OutputWriter::Sink(db) => db.write_buf(kind, from),
}
}

fn write_stream(&self, kind: object::Kind, size: u64, from: impl Read) -> Result<ObjectId, Self::Error> {
fn write_stream(
&self,
kind: object::Kind,
size: u64,
from: &mut dyn Read,
) -> Result<ObjectId, gix::odb::write::Error> {
match self {
OutputWriter::Loose(db) => db.write_stream(kind, size, from).map_err(Into::into),
OutputWriter::Sink(db) => db.write_stream(kind, size, from).map_err(Into::into),
OutputWriter::Loose(db) => db.write_stream(kind, size, from),
OutputWriter::Sink(db) => db.write_stream(kind, size, from),
}
}
}
Expand Down Expand Up @@ -137,7 +140,7 @@ pub fn pack_or_pack_index(
pack_path: impl AsRef<Path>,
object_path: Option<impl AsRef<Path>>,
check: SafetyCheck,
progress: impl NestedProgress,
mut progress: impl NestedProgress + 'static,
Context {
thread_limit,
delete_pack,
Expand Down Expand Up @@ -178,11 +181,11 @@ pub fn pack_or_pack_index(
|_| pack::index::traverse::Algorithm::Lookup,
);

let pack::index::traverse::Outcome { progress, .. } = bundle
let pack::index::traverse::Outcome { .. } = bundle
.index
.traverse(
&bundle.pack,
progress,
&mut progress,
&should_interrupt,
{
let object_path = object_path.map(|p| p.as_ref().to_owned());
Expand All @@ -193,7 +196,7 @@ pub fn pack_or_pack_index(
let mut read_buf = Vec::new();
move |object_kind, buf, index_entry, progress| {
let written_id = out.write_buf(object_kind, buf).map_err(|err| Error::Write {
source: Box::new(err) as Box<dyn std::error::Error + Send + Sync>,
source: err,
kind: object_kind,
id: index_entry.oid,
})?;
Expand All @@ -213,13 +216,13 @@ pub fn pack_or_pack_index(
}
if let Some(verifier) = loose_odb.as_ref() {
let obj = verifier
.try_find(written_id, &mut read_buf)
.try_find(&written_id, &mut read_buf)
.map_err(|err| Error::WrittenFileCorrupt {
source: err,
id: written_id,
})?
.ok_or(Error::WrittenFileMissing { id: written_id })?;
obj.verify_checksum(written_id)?;
obj.verify_checksum(&written_id)?;
}
Ok(())
}
Expand Down
16 changes: 6 additions & 10 deletions gitoxide-core/src/pack/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,12 @@ pub enum PathOrRead {
Read(Box<dyn std::io::Read + Send + 'static>),
}

pub fn from_pack<P>(
pub fn from_pack(
pack: PathOrRead,
directory: Option<PathBuf>,
progress: P,
mut progress: impl NestedProgress + 'static,
ctx: Context<'static, impl io::Write>,
) -> anyhow::Result<()>
where
P: NestedProgress,
P::SubProgress: 'static,
{
) -> anyhow::Result<()> {
use anyhow::Context;
let options = pack::bundle::write::Options {
thread_limit: ctx.thread_limit,
Expand All @@ -94,10 +90,10 @@ where
let pack_len = pack.metadata()?.len();
let pack_file = fs::File::open(pack)?;
pack::Bundle::write_to_directory_eagerly(
pack_file,
Box::new(pack_file),
Some(pack_len),
directory,
progress,
&mut progress,
ctx.should_interrupt,
None,
options,
Expand All @@ -107,7 +103,7 @@ where
input,
None,
directory,
progress,
&mut progress,
ctx.should_interrupt,
None,
options,
Expand Down
8 changes: 4 additions & 4 deletions gitoxide-core/src/pack/multi_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ pub const PROGRESS_RANGE: std::ops::RangeInclusive<u8> = 1..=3;

pub fn verify(
multi_index_path: PathBuf,
progress: impl NestedProgress,
mut progress: impl NestedProgress + 'static,
should_interrupt: &AtomicBool,
) -> anyhow::Result<()> {
gix::odb::pack::multi_index::File::at(multi_index_path)?.verify_integrity_fast(progress, should_interrupt)?;
gix::odb::pack::multi_index::File::at(multi_index_path)?.verify_integrity_fast(&mut progress, should_interrupt)?;
Ok(())
}

pub fn create(
index_paths: Vec<PathBuf>,
output_path: PathBuf,
progress: impl NestedProgress,
mut progress: impl NestedProgress + 'static,
should_interrupt: &AtomicBool,
object_hash: gix::hash::Kind,
) -> anyhow::Result<()> {
Expand All @@ -31,7 +31,7 @@ pub fn create(
gix::odb::pack::multi_index::File::write_from_index_paths(
index_paths,
&mut out,
progress,
&mut progress,
should_interrupt,
gix::odb::pack::multi_index::write::Options { object_hash },
)?;
Expand Down
22 changes: 14 additions & 8 deletions gitoxide-core/src/pack/receive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ mod blocking_io {
fn receive_pack(
&mut self,
input: impl BufRead,
progress: impl NestedProgress,
progress: impl NestedProgress + 'static,
refs: &[Ref],
_previous_response: &Response,
) -> io::Result<()> {
Expand All @@ -156,7 +156,7 @@ mod blocking_io {
) -> anyhow::Result<()>
where
W: std::io::Write,
P: NestedProgress,
P: NestedProgress + 'static,
P::SubProgress: 'static,
{
let transport = net::connect(
Expand Down Expand Up @@ -212,7 +212,7 @@ mod async_io {
async fn receive_pack(
&mut self,
input: impl AsyncBufRead + Unpin + 'async_trait,
progress: impl gix::NestedProgress,
progress: impl gix::NestedProgress + 'static,
refs: &[Ref],
_previous_response: &Response,
) -> io::Result<()> {
Expand Down Expand Up @@ -367,8 +367,8 @@ fn receive_pack_blocking<W: io::Write>(
mut directory: Option<PathBuf>,
mut refs_directory: Option<PathBuf>,
ctx: &mut Context<W>,
input: impl io::BufRead,
progress: impl NestedProgress,
mut input: impl io::BufRead,
mut progress: impl NestedProgress + 'static,
refs: &[Ref],
) -> io::Result<()> {
let options = pack::bundle::write::Options {
Expand All @@ -377,9 +377,15 @@ fn receive_pack_blocking<W: io::Write>(
iteration_mode: pack::data::input::Mode::Verify,
object_hash: ctx.object_hash,
};
let outcome =
pack::Bundle::write_to_directory(input, directory.take(), progress, &ctx.should_interrupt, None, options)
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
let outcome = pack::Bundle::write_to_directory(
&mut input,
directory.take().as_deref(),
&mut progress,
&ctx.should_interrupt,
None,
options,
)
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;

if let Some(directory) = refs_directory.take() {
write_raw_refs(refs, directory)?;
Expand Down
8 changes: 4 additions & 4 deletions gitoxide-core/src/pack/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl<const SIZE: usize> pack::cache::DecodeEntry for EitherCache<SIZE> {

pub fn pack_or_pack_index<W1, W2>(
path: impl AsRef<Path>,
mut progress: impl NestedProgress,
mut progress: impl NestedProgress + 'static,
Context {
mut out,
mut err,
Expand Down Expand Up @@ -121,7 +121,7 @@ where
let res = match ext {
"pack" => {
let pack = odb::pack::data::File::at(path, object_hash).with_context(|| "Could not open pack file")?;
pack.verify_checksum(progress.add_child("Sha1 of pack"), should_interrupt)
pack.verify_checksum(&mut progress.add_child("Sha1 of pack"), should_interrupt)
.map(|id| (id, None))?
}
"idx" => {
Expand Down Expand Up @@ -151,7 +151,7 @@ where
thread_limit
}
}),
progress,
&mut progress,
should_interrupt,
)
.map(|o| (o.actual_index_checksum, o.pack_traverse_statistics))
Expand All @@ -161,7 +161,7 @@ where
match path.file_name() {
Some(file_name) if file_name == "multi-pack-index" => {
let multi_index = gix::odb::pack::multi_index::File::at(path)?;
let res = multi_index.verify_integrity(progress, should_interrupt, gix::odb::pack::index::verify::integrity::Options{
let res = multi_index.verify_integrity(&mut progress, should_interrupt, gix::odb::pack::index::verify::integrity::Options{
verify_mode: mode,
traversal: algorithm.into(),
thread_limit,
Expand Down
2 changes: 1 addition & 1 deletion gitoxide-core/src/repository/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn stream(
.ok_or_else(|| anyhow!("Adding files requires a worktree directory that contains them"))?,
)?;
for path in add_paths {
stream.add_entry_from_path(&root, &gix::path::realpath(path)?)?;
stream.add_entry_from_path(&root, &gix::path::realpath(&path)?)?;
}
}
for (path, content) in files {
Expand Down
6 changes: 5 additions & 1 deletion gitoxide-core/src/repository/attributes/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub struct Options {
}

pub(crate) mod function {
use std::borrow::Cow;
use std::{io, path::Path};

use anyhow::{anyhow, bail};
Expand Down Expand Up @@ -38,7 +39,10 @@ pub(crate) mod function {
match input {
PathsOrPatterns::Paths(paths) => {
for path in paths {
let is_dir = gix::path::from_bstr(path.as_ref()).metadata().ok().map(|m| m.is_dir());
let is_dir = gix::path::from_bstr(Cow::Borrowed(path.as_ref()))
.metadata()
.ok()
.map(|m| m.is_dir());

let entry = cache.at_entry(path.as_slice(), is_dir)?;
if !entry.matching_attributes(&mut matches) {
Expand Down
Loading

0 comments on commit c1c88fd

Please sign in to comment.