Skip to content
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

Use dyn where possible, including dyn based progress traits #1008

Merged
merged 4 commits into from
Sep 6, 2023
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ gix = { version = "^0.53.0", path = "gix", default-features = false }
time = "0.3.23"

clap = { version = "4.1.1", features = ["derive", "cargo"] }
prodash = { version = "25.0.0", optional = true, default-features = false }
prodash = { version = "26.2.0", optional = true, default-features = false }
is-terminal = { version = "0.4.0", optional = true }
env_logger = { version = "0.10.0", default-features = false }
crosstermion = { version = "0.11.0", optional = true, default-features = false }
Expand Down
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
18 changes: 8 additions & 10 deletions gitoxide-core/src/corpus/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{

use anyhow::{bail, Context};
use bytesize::ByteSize;
use gix::Progress;
use gix::{Count, NestedProgress, Progress};
use rusqlite::params;

use super::db;
Expand Down Expand Up @@ -55,7 +55,7 @@ impl Engine {
pub fn refresh(&mut self, corpus_path: PathBuf) -> anyhow::Result<()> {
let (corpus_path, corpus_id) = self.prepare_corpus_path(corpus_path)?;
let repos = self.refresh_repos(&corpus_path, corpus_id)?;
self.state.progress.set_name("refresh repos");
self.state.progress.set_name("refresh repos".into());
self.state.progress.info(format!(
"Added or updated {} repositories under {corpus_path:?}",
repos.len()
Expand Down Expand Up @@ -87,7 +87,7 @@ impl Engine {
repo_progress.init(Some(repos.len()), gix::progress::count("repos"));
if task.execute_exclusive || threads == 1 || dry_run {
if dry_run {
repo_progress.set_name("WOULD run");
repo_progress.set_name("WOULD run".into());
for repo in &repos {
repo_progress.info(format!(
"{}",
Expand Down Expand Up @@ -202,13 +202,11 @@ impl Engine {
task.perform(&mut run, &repo.path, progress, Some(1), should_interrupt);
});
if let Some(err) = run.error.as_deref() {
num_errors.fetch_add(1, Ordering::SeqCst);
num_errors.fetch_add(1, Ordering::Relaxed);
progress.fail(err.to_owned());
}
Self::update_run(con, run)?;
if let Some(counter) = counter.as_ref() {
counter.fetch_add(1, Ordering::SeqCst);
}
counter.fetch_add(1, Ordering::Relaxed);
Ok(())
},
|| (!gix::interrupt::is_triggered()).then(|| Duration::from_millis(100)),
Expand Down Expand Up @@ -243,7 +241,7 @@ impl Engine {
corpus_id: db::Id,
sql_suffix: Option<&str>,
) -> anyhow::Result<Vec<db::Repo>> {
self.state.progress.set_name("query db-repos");
self.state.progress.set_name("query db-repos".into());
self.state.progress.init(None, gix::progress::count("repos"));

Ok(self
Expand All @@ -267,7 +265,7 @@ impl Engine {

fn refresh_repos(&mut self, corpus_path: &Path, corpus_id: db::Id) -> anyhow::Result<Vec<db::Repo>> {
let start = Instant::now();
self.state.progress.set_name("refresh");
self.state.progress.set_name("refresh".into());
self.state.progress.init(None, gix::progress::count("repos"));

let repos = std::thread::scope({
Expand Down Expand Up @@ -302,7 +300,7 @@ impl Engine {

let find_progress = progress.add_child("find");
let write_db = scope.spawn(move || -> anyhow::Result<Vec<db::Repo>> {
progress.set_name("write to DB");
progress.set_name("write to DB".into());
progress.init(None, gix::progress::count("repos"));

let mut out = Vec::new();
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
4 changes: 2 additions & 2 deletions gitoxide-core/src/corpus/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ impl tracing_forest::printer::Formatter for StoreTreeToDb {
use gix::Progress;
if self.reverse_lines {
for line in tree.lines().rev() {
progress.info(line);
progress.info(line.into());
}
} else {
for line in tree.lines() {
progress.info(line);
progress.info(line.into());
}
}
}
Expand Down
33 changes: 9 additions & 24 deletions gitoxide-core/src/hours/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,7 @@ pub fn estimate_hours(
}
}

type CommitChangeLineCounters = (
Option<Arc<AtomicUsize>>,
Option<Arc<AtomicUsize>>,
Option<Arc<AtomicUsize>>,
);
type CommitChangeLineCounters = (Arc<AtomicUsize>, Arc<AtomicUsize>, Arc<AtomicUsize>);

type SpawnResultWithReturnChannelAndWorkers<'scope> = (
crossbeam_channel::Sender<Vec<(CommitIdx, Option<gix::hash::ObjectId>, gix::hash::ObjectId)>>,
Expand All @@ -95,7 +91,7 @@ pub fn spawn_tree_delta_threads<'scope>(
let rx = rx.clone();
move || -> Result<_, anyhow::Error> {
let mut out = Vec::new();
let (commit_counter, change_counter, lines_counter) = stats_counters;
let (commits, changes, lines_count) = stats_counters;
let mut attributes = line_stats
.then(|| -> anyhow::Result<_> {
repo.index_or_load_from_head().map_err(Into::into).and_then(|index| {
Expand All @@ -115,9 +111,7 @@ pub fn spawn_tree_delta_threads<'scope>(
.transpose()?;
for chunk in rx {
for (commit_idx, parent_commit, commit) in chunk {
if let Some(c) = commit_counter.as_ref() {
c.fetch_add(1, Ordering::SeqCst);
}
commits.fetch_add(1, Ordering::Relaxed);
if gix::interrupt::is_triggered() {
return Ok(out);
}
Expand All @@ -139,23 +133,21 @@ pub fn spawn_tree_delta_threads<'scope>(
.track_rewrites(None)
.for_each_to_obtain_tree(&to, |change| {
use gix::object::tree::diff::change::Event::*;
if let Some(c) = change_counter.as_ref() {
c.fetch_add(1, Ordering::SeqCst);
}
changes.fetch_add(1, Ordering::Relaxed);
match change.event {
Rewrite { .. } => {
unreachable!("we turned that off")
}
Addition { entry_mode, id } => {
if entry_mode.is_no_tree() {
files.added += 1;
add_lines(line_stats, lines_counter.as_deref(), &mut lines, id);
add_lines(line_stats, &lines_count, &mut lines, id);
}
}
Deletion { entry_mode, id } => {
if entry_mode.is_no_tree() {
files.removed += 1;
remove_lines(line_stats, lines_counter.as_deref(), &mut lines, id);
remove_lines(line_stats, &lines_count, &mut lines, id);
}
}
Modification {
Expand All @@ -168,16 +160,11 @@ pub fn spawn_tree_delta_threads<'scope>(
(false, false) => {}
(false, true) => {
files.added += 1;
add_lines(line_stats, lines_counter.as_deref(), &mut lines, id);
add_lines(line_stats, &lines_count, &mut lines, id);
}
(true, false) => {
files.removed += 1;
remove_lines(
line_stats,
lines_counter.as_deref(),
&mut lines,
previous_id,
);
remove_lines(line_stats, &lines_count, &mut lines, previous_id);
}
(true, true) => {
files.modified += 1;
Expand All @@ -203,9 +190,7 @@ pub fn spawn_tree_delta_threads<'scope>(
nl += counts.insertions as usize + counts.removals as usize;
lines.added += counts.insertions as usize;
lines.removed += counts.removals as usize;
if let Some(c) = lines_counter.as_ref() {
c.fetch_add(nl, Ordering::SeqCst);
}
lines_count.fetch_add(nl, Ordering::Relaxed);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions gitoxide-core/src/hours/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use gix::{
actor,
bstr::{BStr, ByteSlice},
prelude::*,
progress, Progress,
progress, Count, NestedProgress, Progress,
};

/// Additional configuration for the hours estimation functionality.
Expand Down Expand Up @@ -49,7 +49,7 @@ pub fn estimate<W, P>(
) -> anyhow::Result<()>
where
W: io::Write,
P: Progress,
P: NestedProgress,
{
let repo = gix::discover(working_dir)?;
let commit_id = repo.rev_parse_single(rev_spec)?.detach();
Expand Down
12 changes: 4 additions & 8 deletions gitoxide-core/src/hours/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,22 +158,18 @@ impl LineStats {
/// An index able to address any commit
pub type CommitIdx = u32;

pub fn add_lines(line_stats: bool, lines_counter: Option<&AtomicUsize>, lines: &mut LineStats, id: gix::Id<'_>) {
pub fn add_lines(line_stats: bool, lines_counter: &AtomicUsize, lines: &mut LineStats, id: gix::Id<'_>) {
if let Some(Ok(blob)) = line_stats.then(|| id.object()) {
let nl = blob.data.lines_with_terminator().count();
lines.added += nl;
if let Some(c) = lines_counter {
c.fetch_add(nl, Ordering::SeqCst);
}
lines_counter.fetch_add(nl, Ordering::Relaxed);
}
}

pub fn remove_lines(line_stats: bool, lines_counter: Option<&AtomicUsize>, lines: &mut LineStats, id: gix::Id<'_>) {
pub fn remove_lines(line_stats: bool, lines_counter: &AtomicUsize, lines: &mut LineStats, id: gix::Id<'_>) {
if let Some(Ok(blob)) = line_stats.then(|| id.object()) {
let nl = blob.data.lines_with_terminator().count();
lines.removed += nl;
if let Some(c) = lines_counter {
c.fetch_add(nl, Ordering::SeqCst);
}
lines_counter.fetch_add(nl, Ordering::Relaxed);
}
}
12 changes: 6 additions & 6 deletions gitoxide-core/src/index/checkout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
};

use anyhow::bail;
use gix::{odb::FindExt, worktree::state::checkout, Progress};
use gix::{odb::FindExt, worktree::state::checkout, NestedProgress, Progress};

use crate::{
index,
Expand All @@ -16,7 +16,7 @@ pub fn checkout_exclusive(
dest_directory: impl AsRef<Path>,
repo: Option<PathBuf>,
mut err: impl std::io::Write,
mut progress: impl Progress,
mut progress: impl NestedProgress,
should_interrupt: &AtomicBool,
index::checkout_exclusive::Options {
index: Options { object_hash, .. },
Expand Down 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
10 changes: 5 additions & 5 deletions gitoxide-core/src/organize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
path::{Path, PathBuf},
};

use gix::{objs::bstr::ByteSlice, progress, Progress};
use gix::{objs::bstr::ByteSlice, progress, NestedProgress, Progress};

#[derive(Default, Copy, Clone, Eq, PartialEq)]
pub enum Mode {
Expand Down 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 Expand Up @@ -207,7 +207,7 @@ fn handle(
}

/// Find all working directories in the given `source_dir` and print them to `out` while providing `progress`.
pub fn discover<P: Progress>(
pub fn discover<P: NestedProgress>(
source_dir: impl AsRef<Path>,
mut out: impl std::io::Write,
mut progress: P,
Expand All @@ -222,7 +222,7 @@ pub fn discover<P: Progress>(
Ok(())
}

pub fn run<P: Progress>(
pub fn run<P: NestedProgress>(
mode: Mode,
source_dir: impl AsRef<Path>,
destination: impl AsRef<Path>,
Expand Down
Loading
Loading