Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
7 changes: 7 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())))
}
Loading