Skip to content
Open
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.lock

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

57 changes: 53 additions & 4 deletions gitoxide-core/src/repository/log.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use anyhow::bail;
use gix::bstr::{BString, ByteSlice};
use gix::bstr::{BStr, BString, ByteSlice};

pub fn log(mut repo: gix::Repository, out: &mut dyn std::io::Write, path: Option<BString>) -> anyhow::Result<()> {
repo.object_cache_size_if_unset(repo.compute_object_cache_size_for_tree_diffs(&**repo.index_or_empty()?));
Expand All @@ -25,8 +24,20 @@ fn log_all(repo: gix::Repository, out: &mut dyn std::io::Write) -> Result<(), an
Ok(())
}

fn log_file(_repo: gix::Repository, _out: &mut dyn std::io::Write, _path: BString) -> anyhow::Result<()> {
bail!("File-based lookup isn't yet implemented in a way that is competitively fast");
fn log_file(repo: gix::Repository, out: &mut dyn std::io::Write, path: BString) -> anyhow::Result<()> {
let path = gix::path::to_unix_separators_on_windows(path.as_bstr()).into_owned();
let head = repo.head()?.peel_to_commit()?;
let cache = repo.commit_graph_if_enabled()?;
let topo = gix::traverse::commit::topo::Builder::from_iters(&repo.objects, [head.id], None::<Vec<gix::ObjectId>>)
.build()?;

for info in topo {
let info = info?;
if commit_changes_path(&repo, cache.as_ref(), &info, path.as_ref())? {
write_info(&repo, &mut *out, &info)?;
}
}
Ok(())
}

fn write_info(
Expand All @@ -48,3 +59,41 @@ fn write_info(

Ok(())
}

fn commit_changes_path(
repo: &gix::Repository,
cache: Option<&gix::commitgraph::Graph>,
info: &gix::traverse::commit::Info,
path: &BStr,
) -> anyhow::Result<bool> {
let commit = repo.find_commit(info.id)?;
let commit_tree = commit.tree()?;
let commit_entry = lookup_path_entry(&commit_tree, path)?;

if info.parent_ids.is_empty() {
return Ok(commit_entry.is_some());
}

for (index, parent_id) in info.parent_ids.iter().enumerate() {
if index == 0 && cache.and_then(|graph| graph.maybe_contains_path_by_id(info.id, path)) == Some(false) {
continue;
}

let parent = repo.find_commit(*parent_id)?;
let parent_tree = parent.tree()?;
let parent_entry = lookup_path_entry(&parent_tree, path)?;
if commit_entry != parent_entry {
return Ok(true);
}
}

Ok(false)
}

fn lookup_path_entry(
tree: &gix::Tree<'_>,
path: &BStr,
) -> anyhow::Result<Option<(gix::objs::tree::EntryMode, gix::ObjectId)>> {
let entry = tree.lookup_entry(path.split(|b| *b == b'/'))?;
Ok(entry.map(|entry| (entry.mode(), entry.object_id())))
}
7 changes: 7 additions & 0 deletions gix-blame/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ authors = ["Christoph Rüßler <christoph.ruessler@mailbox.org>", "Sebastian Thi
edition = "2021"
rust-version = "1.82"

[[bench]]
name = "incremental-blame"
harness = false
path = "./benches/incremental_blame.rs"

[features]
## Enable support for the SHA-1 hash by forwarding the feature to dependencies.
sha1 = [
Expand Down Expand Up @@ -38,6 +43,8 @@ smallvec = "1.15.1"
thiserror = "2.0.18"

[dev-dependencies]
criterion = "0.8.0"
gix = { path = "../gix", default-features = false, features = ["basic", "sha1"] }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sebastian Thiel (@Byron) Is it ok to depend on gix here? Since the feature blame is not included, this does not create a circular dependency, but as far as I can tell, currently only gitoxide-core and internal-tools depend on gix, so I wanted to flag this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to avoid it as it slows down tests tremendously due to increased build times.
As the repo seems to be used only to obtain the commit-graph, we should remove this dependency and open it via plumbing directly. Should be easy enough even without re-implementing all Git configuration, so that typical commit-graphs can be opened.

It's a benchmark after all.

gix-ref = { path = "../gix-ref" }
gix-filter = { path = "../gix-filter" }
gix-fs = { path = "../gix-fs" }
Expand Down
134 changes: 134 additions & 0 deletions gix-blame/benches/incremental_blame.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
use std::{
env,
ops::ControlFlow,
path::{Path, PathBuf},
process::Command,
};

use criterion::{criterion_group, criterion_main, Criterion};
use gix_blame::{BlameEntry, BlameRanges, BlameSink};
use gix_object::bstr::BString;

const DEFAULT_BENCH_PATH: &str = "gix-blame/src/file/function.rs";

struct DiscardSink;

impl BlameSink for DiscardSink {
fn push(&mut self, _entry: BlameEntry) -> ControlFlow<()> {
ControlFlow::Continue(())
}
}

fn incremental_options() -> gix_blame::Options {
gix_blame::Options {
diff_algorithm: gix_diff::blob::Algorithm::Histogram,
ranges: BlameRanges::default(),
since: None,
rewrites: Some(gix_diff::Rewrites::default()),
debug_track_path: false,
}
}

fn benchmark_incremental_blame(c: &mut Criterion) {
let repo_path = env::var_os("GIX_BLAME_BENCH_REPO").map_or_else(
|| {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.parent()
.expect("gix-blame crate has a parent directory")
.to_path_buf()
},
PathBuf::from,
);
let source_file_path: BString = env::var("GIX_BLAME_BENCH_PATH")
.unwrap_or_else(|_| DEFAULT_BENCH_PATH.into())
.into_bytes()
.into();
let commit_spec = env::var("GIX_BLAME_BENCH_COMMIT").unwrap_or_else(|_| "HEAD".into());
let git_dir = repo_path.join(".git");

if !has_commit_graph_cache(&git_dir) {
write_changed_path_commit_graph(&repo_path).expect("commit-graph should be writable for benchmark repository");
}

let repo = gix::open(&repo_path).expect("repository can be opened");
let suspect = repo
.rev_parse_single(commit_spec.as_str())
.expect("commit spec resolves to one object")
.detach();

let mut group = c.benchmark_group("gix-blame::incremental");
group.bench_function("without-commit-graph", |b| {
let mut resource_cache = repo
.diff_resource_cache_for_tree_diff()
.expect("tree-diff resource cache can be created");

b.iter(|| {
let mut sink = DiscardSink;
gix_blame::incremental(
&repo,
suspect,
None,
&mut resource_cache,
source_file_path.as_ref(),
&mut sink,
incremental_options(),
)
.expect("incremental blame should succeed");
});
});
group.bench_function("with-commit-graph", |b| {
let mut resource_cache = repo
.diff_resource_cache_for_tree_diff()
.expect("tree-diff resource cache can be created");
let cache = repo.commit_graph().expect("commit-graph can be loaded from repository");
b.iter(|| {
let mut sink = DiscardSink;
gix_blame::incremental(
&repo,
suspect,
Some(&cache),
&mut resource_cache,
source_file_path.as_ref(),
&mut sink,
incremental_options(),
)
.expect("incremental blame should succeed");
});
});
group.finish();
}

fn has_commit_graph_cache(git_dir: &Path) -> bool {
let info_dir = git_dir.join("objects/info");
info_dir.join("commit-graph").is_file() || info_dir.join("commit-graphs").is_dir()
}
Comment on lines +98 to +101
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this potentially available as an API on repo?

Sebastian Thiel (@Byron) It it is not, should it be?

Copy link
Copy Markdown
Member

@Byron Sebastian Thiel (Byron) Apr 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, and judging from how it is used, I think it's fair to just use https://docs.rs/gix/latest/gix/struct.Repository.html#method.commit_graph_if_enabled. While it's doing more than needed here, it's fine in a test.
And I also think it should be used rather than having this helper.


fn write_changed_path_commit_graph(worktree_path: &Path) -> std::io::Result<()> {
let config_status = Command::new("git")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to set this value through an API on repo?

Copy link
Copy Markdown
Member

@Byron Sebastian Thiel (Byron) Apr 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's totally possible, even though I have seen code like this generated a lot as the APIs are cumbersome enough to use at least compared to git2.

I'd even be OK with it but would prefer something like gix_testtools::invoke_bash() as a helper. I will whip one up.

.args(["config", "commitGraph.changedPathsVersion", "2"])
.current_dir(worktree_path)
.status()?;
assert!(
config_status.success(),
"setting commitGraph.changedPathsVersion should succeed"
);

let write_status = Command::new("git")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about adding a comment that we have to shell out to git because gix-commitgraph is currently read-only?

.args([
"commit-graph",
"write",
"--no-progress",
"--reachable",
"--changed-paths",
])
.current_dir(worktree_path)
.status()?;
assert!(
write_status.success(),
"writing changed-path commit-graph should succeed"
);
Ok(())
}

criterion_group!(benches, benchmark_incremental_blame);
criterion_main!(benches);
Loading
Loading