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

Explore gix APIs, experiment with gix-blame API #1453

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
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
11 changes: 11 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ is usable to some extent.
* `gitoxide-core`
* **very early** _(possibly without any documentation and many rough edges)_
* [gix-merge](https://github.com/GitoxideLabs/gitoxide/blob/main/crate-status.md#gix-merge)
* [gix-blame](https://github.com/GitoxideLabs/gitoxide/blob/main/crate-status.md#gix-blame)
* **idea** _(just a name placeholder)_
* [gix-note](https://github.com/GitoxideLabs/gitoxide/blob/main/crate-status.md#gix-note)
* [gix-fetchhead](https://github.com/GitoxideLabs/gitoxide/blob/main/crate-status.md#gix-fetchhead)
Expand Down
9 changes: 9 additions & 0 deletions crate-status.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,15 @@ Check out the [performance discussion][gix-diff-performance] as well.
* [x] API documentation
* [ ] Examples

### gix-blame

* [ ] commit-annotations for a single file
- [ ] progress
- [ ] interruptability
- [ ] streaming
* [x] API documentation
* [ ] Examples

### gix-traverse

Check out the [performance discussion][gix-traverse-performance] as well.
Expand Down
2 changes: 1 addition & 1 deletion gitoxide-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ serde = ["gix/serde", "dep:serde_json", "dep:serde", "bytesize/serde"]

[dependencies]
# deselect everything else (like "performance") as this should be controllable by the parent application.
gix = { version = "^0.67.0", path = "../gix", default-features = false, features = ["blob-merge", "blob-diff", "revision", "mailmap", "excludes", "attributes", "worktree-mutation", "credentials", "interrupt", "status", "dirwalk"] }
gix = { version = "^0.67.0", path = "../gix", default-features = false, features = ["blob-merge", "blob-diff", "blame", "revision", "mailmap", "excludes", "attributes", "worktree-mutation", "credentials", "interrupt", "status", "dirwalk"] }
gix-pack-for-configuration-only = { package = "gix-pack", version = "^0.54.0", path = "../gix-pack", default-features = false, features = ["pack-cache-lru-dynamic", "pack-cache-lru-static", "generate", "streaming-input"] }
gix-transport-configuration-only = { package = "gix-transport", version = "^0.43.0", path = "../gix-transport", default-features = false }
gix-archive-for-configuration-only = { package = "gix-archive", version = "^0.16.0", path = "../gix-archive", optional = true, features = ["tar", "tar_gz"] }
Expand Down
62 changes: 62 additions & 0 deletions gitoxide-core/src/repository/blame.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use std::{ffi::OsStr, path::PathBuf, str::Lines};

use anyhow::anyhow;
use gix::bstr::BStr;

pub fn blame_file(mut repo: gix::Repository, file: &OsStr, out: impl std::io::Write) -> anyhow::Result<()> {
repo.object_cache_size_if_unset(repo.compute_object_cache_size_for_tree_diffs(&**repo.index_or_empty()?));

let suspect = repo.head()?.peel_to_commit_in_place()?;
let traverse: Vec<_> =
gix::traverse::commit::topo::Builder::from_iters(&repo.objects, [suspect.id], None::<Vec<gix::ObjectId>>)
.build()?
.collect();
let mut resource_cache = repo.diff_resource_cache_for_tree_diff()?;

let work_dir: PathBuf = repo
.work_dir()
.ok_or_else(|| anyhow!("blame needs a workdir, but there is none"))?
.into();
let file_path: &BStr = gix::path::os_str_into_bstr(file)?;

let blame_entries = gix::blame::blame_file(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Byron This makes compiliation on CI fail as gix::blame is behind a feature flag. I feel like I’ve hit this issue already in the past, but I forgot how to resolve it. Would you be able to help me? :-)

Copy link
Member

Choose a reason for hiding this comment

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

That's interesting - looks like the blame feature disappeared from the list somehow. In any case, it's back now and CI should get past this point (even though clippy has some legit complaints I locally at least).

&repo.objects,
traverse,
&mut resource_cache,
suspect.id,
work_dir.clone(),
file_path,
)?;

let absolute_path = work_dir.join(file);
let file_content = std::fs::read_to_string(absolute_path)?;
let lines = file_content.lines();

write_blame_entries(out, lines, blame_entries)?;

Ok(())
}

fn write_blame_entries(
mut out: impl std::io::Write,
mut lines: Lines<'_>,
blame_entries: Vec<gix::blame::BlameEntry>,
) -> Result<(), std::io::Error> {
for blame_entry in blame_entries {
for line_number in blame_entry.range_in_blamed_file {
let line = lines.next().unwrap();

writeln!(
out,
"{} {} {}",
blame_entry.commit_id.to_hex_with_len(8),
// `line_number` is 0-based, but we want to show 1-based line numbers (as `git`
// does).
line_number + 1,
line
)?;
}
}

Ok(())
}
1 change: 1 addition & 0 deletions gitoxide-core/src/repository/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub enum PathsOrPatterns {
pub mod archive;
pub mod cat;
pub use cat::function::cat;
pub mod blame;
pub mod commit;
pub mod config;
mod credential;
Expand Down
12 changes: 11 additions & 1 deletion gix-blame/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ name = "gix-blame"
version = "0.0.0"
repository = "https://github.com/GitoxideLabs/gitoxide"
license = "MIT OR Apache-2.0"
description = "A crate of the gitoxide project dedicated implementing a 'blame' algorithm"
description = "A crate of the gitoxide project dedicated to implementing a 'blame' algorithm"
authors = ["Christoph Rüßler <[email protected]>", "Sebastian Thiel <[email protected]>"]
edition = "2021"
rust-version = "1.65"
Expand All @@ -14,6 +14,16 @@ rust-version = "1.65"
doctest = false

[dependencies]
gix-diff = { version = "^0.47.0", path = "../gix-diff", default-features = false, features = ["blob"] }
gix-object = { version = "^0.45.0", path = "../gix-object" }
gix-hash = { version = "^0.15.0", path = "../gix-hash" }
gix-worktree = { version = "^0.37.0", path = "../gix-worktree", default-features = false, features = ["attributes"] }
gix-traverse = { version = "^0.42.0", path = "../gix-traverse" }

[dev-dependencies]
gix-ref = { version = "^0.48.0", path = "../gix-ref" }
gix-filter = { version = "^0.14.0", path = "../gix-filter" }
gix-fs = { version = "^0.12.0", path = "../gix-fs" }
gix-index = { version = "^0.36.0", path = "../gix-index" }
gix-odb = { version = "^0.64.0", path = "../gix-odb" }
gix-testtools = { path = "../tests/tools" }
Loading
Loading