Skip to content

Commit 0f973ac

Browse files
committed
gix-corpus now respects the --trace flag
1 parent 8a4a0af commit 0f973ac

File tree

7 files changed

+80
-26
lines changed

7 files changed

+80
-26
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gitoxide-core/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ estimate-hours = ["dep:itertools", "dep:fs-err", "dep:crossbeam-channel", "dep:s
2323
query = ["dep:rusqlite"]
2424
## Run algorithms on a corpus of repositories and store their results for later comparison and intelligence gathering.
2525
## *Note that* `organize` we need for finding git repositories fast.
26-
corpus = [ "dep:rusqlite", "dep:sysinfo", "organize", "dep:crossbeam-channel", "dep:serde_json", "dep:tracing-forest", "dep:tracing-subscriber", "dep:tracing" ]
26+
corpus = [ "dep:rusqlite", "dep:sysinfo", "organize", "dep:crossbeam-channel", "dep:serde_json", "dep:tracing-forest", "dep:tracing-subscriber", "dep:tracing", "dep:parking_lot" ]
2727

2828
#! ### Mutually Exclusive Networking
2929
#! If both are set, _blocking-client_ will take precedence, allowing `--all-features` to be used.
@@ -72,6 +72,7 @@ smallvec = { version = "1.10.0", optional = true }
7272
rusqlite = { version = "0.29.0", optional = true, features = ["bundled"] }
7373

7474
# for 'corpus'
75+
parking_lot = { version = "0.12.1", optional = true }
7576
sysinfo = { version = "0.29.2", optional = true, default-features = false }
7677
serde_json = { version = "1.0.65", optional = true }
7778
tracing-forest = { version = "0.1.5", features = ["serde"], optional = true }

gitoxide-core/src/corpus/engine.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,20 @@ use std::time::{Duration, Instant};
1212

1313
impl Engine {
1414
/// Open the corpus DB or create it.
15-
pub fn open_or_create(db: PathBuf, gitoxide_version: String, progress: corpus::Progress) -> anyhow::Result<Engine> {
15+
pub fn open_or_create(
16+
db: PathBuf,
17+
gitoxide_version: String,
18+
progress: corpus::Progress,
19+
trace_to_progress: bool,
20+
reverse_trace_lines: bool,
21+
) -> anyhow::Result<Engine> {
1622
let con = crate::corpus::db::create(db).context("Could not open or create database")?;
1723
Ok(Engine {
1824
progress,
1925
con,
2026
gitoxide_version,
27+
trace_to_progress,
28+
reverse_trace_lines,
2129
})
2230
}
2331

@@ -66,7 +74,11 @@ impl Engine {
6674

6775
if task.execute_exclusive || threads == 1 {
6876
let mut run_progress = repo_progress.add_child("set later");
69-
let (_guard, current_id) = corpus::trace::override_thread_subscriber(db_path.as_str())?;
77+
let (_guard, current_id) = corpus::trace::override_thread_subscriber(
78+
db_path.as_str(),
79+
self.trace_to_progress.then(|| task_progress.add_child("trace")),
80+
self.reverse_trace_lines,
81+
)?;
7082

7183
for repo in &repos {
7284
if gix::interrupt::is_triggered() {
@@ -80,7 +92,7 @@ impl Engine {
8092
.display()
8193
));
8294

83-
// TODO: wait for new release to be able to provide run_id via span attributes
95+
// TODO: wait for new release of `tracing-forest` to be able to provide run_id via span attributes
8496
let mut run = Self::insert_run(&self.con, gitoxide_id, runner_id, *task_id, repo.id)?;
8597
current_id.store(run.id, Ordering::SeqCst);
8698
tracing::info_span!("run", run_id = run.id).in_scope(|| {
@@ -106,9 +118,11 @@ impl Engine {
106118
let shared_repo_progress = repo_progress.clone();
107119
let db_path = db_path.clone();
108120
move |tid| {
121+
let mut progress = gix::threading::lock(&shared_repo_progress);
109122
(
110-
corpus::trace::override_thread_subscriber(db_path.as_str()),
111-
gix::threading::lock(&shared_repo_progress).add_child(format!("{tid}")),
123+
// threaded printing is usually spammy, and lines interleave so it's useless.
124+
corpus::trace::override_thread_subscriber(db_path.as_str(), None, false),
125+
progress.add_child(format!("{tid}")),
112126
rusqlite::Connection::open(&db_path),
113127
)
114128
}

gitoxide-core/src/corpus/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ pub struct Engine {
55
progress: Progress,
66
con: rusqlite::Connection,
77
gitoxide_version: String,
8+
trace_to_progress: bool,
9+
reverse_trace_lines: bool,
810
}
911

1012
pub struct RunOutcome {

gitoxide-core/src/corpus/trace.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,64 @@
1+
use gix::progress::DoOrDiscard;
2+
use parking_lot::Mutex;
13
use rusqlite::params;
24
use std::path::Path;
35
use std::sync::atomic::{AtomicU32, Ordering};
4-
use std::sync::{Arc, Mutex};
6+
use std::sync::Arc;
57
use tracing_forest::tree::Tree;
68
use tracing_subscriber::layer::SubscriberExt;
79

10+
type ProgressItem = DoOrDiscard<gix::progress::prodash::tree::Item>;
11+
812
pub fn override_thread_subscriber(
913
db_path: impl AsRef<Path>,
14+
progress: Option<ProgressItem>,
15+
reverse_lines: bool,
1016
) -> anyhow::Result<(tracing::subscriber::DefaultGuard, Arc<AtomicU32>)> {
1117
let current_id = Arc::new(AtomicU32::default());
1218
let processor = tracing_forest::Printer::new().formatter(StoreTreeToDb {
1319
con: Arc::new(Mutex::new(rusqlite::Connection::open(&db_path)?)),
1420
run_id: current_id.clone(),
21+
progress: progress.map(Mutex::new),
22+
reverse_lines,
1523
});
1624
let subscriber = tracing_subscriber::Registry::default().with(tracing_forest::ForestLayer::from(processor));
1725
let guard = tracing::subscriber::set_default(subscriber);
1826
Ok((guard, current_id))
1927
}
2028

2129
pub struct StoreTreeToDb {
22-
pub con: Arc<Mutex<rusqlite::Connection>>,
23-
pub run_id: Arc<AtomicU32>,
30+
con: Arc<Mutex<rusqlite::Connection>>,
31+
run_id: Arc<AtomicU32>,
32+
progress: Option<Mutex<ProgressItem>>,
33+
reverse_lines: bool,
2434
}
2535

2636
impl tracing_forest::printer::Formatter for StoreTreeToDb {
2737
type Error = rusqlite::Error;
2838

2939
fn fmt(&self, tree: &Tree) -> Result<String, Self::Error> {
40+
if let Some((progress, tree)) = self
41+
.progress
42+
.as_ref()
43+
.map(Mutex::lock)
44+
.zip(tracing_forest::printer::Pretty.fmt(tree).ok())
45+
{
46+
use gix::Progress;
47+
if self.reverse_lines {
48+
for line in tree.lines().rev() {
49+
progress.info(line);
50+
}
51+
} else {
52+
for line in tree.lines() {
53+
progress.info(line);
54+
}
55+
}
56+
}
57+
// TODO: wait for new release of `tracing-forest` and load the ID from span fields.
3058
let json = serde_json::to_string_pretty(&tree).expect("serialization to string always works");
3159
let run_id = self.run_id.load(Ordering::SeqCst);
3260
self.con
3361
.lock()
34-
.unwrap()
3562
.execute("UPDATE run SET spans_json = ?1 WHERE id = ?2", params![json, run_id])?;
3663
Ok(String::new())
3764
}

src/plumbing/main.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -136,21 +136,30 @@ pub fn main() -> Result<()> {
136136

137137
match cmd {
138138
#[cfg(feature = "gitoxide-core-tools-corpus")]
139-
Subcommands::Corpus(crate::plumbing::options::corpus::Platform { db, path, cmd }) => prepare_and_run(
140-
"corpus",
141-
trace,
142-
auto_verbose,
143-
progress,
144-
progress_keep_open,
145-
core::corpus::PROGRESS_RANGE,
146-
move |progress, _out, _err| {
147-
let mut engine = core::corpus::Engine::open_or_create(db, env!("GITOXIDE_VERSION").into(), progress)?;
148-
match cmd {
149-
crate::plumbing::options::corpus::SubCommands::Run => engine.run(path, thread_limit),
150-
crate::plumbing::options::corpus::SubCommands::Refresh => engine.refresh(path),
151-
}
152-
},
153-
),
139+
Subcommands::Corpus(crate::plumbing::options::corpus::Platform { db, path, cmd }) => {
140+
let reverse_trace_lines = progress;
141+
prepare_and_run(
142+
"corpus",
143+
trace,
144+
auto_verbose,
145+
progress,
146+
progress_keep_open,
147+
core::corpus::PROGRESS_RANGE,
148+
move |progress, _out, _err| {
149+
let mut engine = core::corpus::Engine::open_or_create(
150+
db,
151+
env!("GITOXIDE_VERSION").into(),
152+
progress,
153+
trace,
154+
reverse_trace_lines,
155+
)?;
156+
match cmd {
157+
crate::plumbing::options::corpus::SubCommands::Run => engine.run(path, thread_limit),
158+
crate::plumbing::options::corpus::SubCommands::Refresh => engine.refresh(path),
159+
}
160+
},
161+
)
162+
}
154163
Subcommands::CommitGraph(cmd) => match cmd {
155164
commitgraph::Subcommands::List { spec } => prepare_and_run(
156165
"commitgraph-list",

src/shared.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ pub mod pretty {
108108
move |tree: &tracing_forest::tree::Tree| -> Result<String, std::fmt::Error> {
109109
use gix::Progress;
110110
use tracing_forest::Formatter;
111-
let tree = tracing_forest::printer::Pretty.fmt(tree)?;
112111
let progress = &mut progress.lock().unwrap();
112+
let tree = tracing_forest::printer::Pretty.fmt(tree)?;
113113
if reverse_lines {
114114
for line in tree.lines().rev() {
115115
progress.info(line);

0 commit comments

Comments
 (0)