Replace libgit2 with gitoxide

This commit is contained in:
blinxen 2025-02-22 00:51:44 +01:00
parent 5c43ddb56c
commit 5159355c2e
5 changed files with 1026 additions and 132 deletions

View File

@ -6,10 +6,6 @@ updates:
interval: monthly interval: monthly
time: "04:00" time: "04:00"
timezone: Europe/Berlin timezone: Europe/Berlin
ignore:
- dependency-name: git2
versions:
- 0.13.17
- package-ecosystem: gitsubmodule - package-ecosystem: gitsubmodule
directory: "/" directory: "/"
schedule: schedule:

963
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -31,7 +31,7 @@ minimal-application = [
"regex-onig", "regex-onig",
"wild", "wild",
] ]
git = ["git2"] # Support indicating git modifications git = ["gix"] # Support indicating git modifications
paging = ["shell-words", "grep-cli"] # Support applying a pager on the output paging = ["shell-words", "grep-cli"] # Support applying a pager on the output
lessopen = ["execute"] # Support $LESSOPEN preprocessor lessopen = ["execute"] # Support $LESSOPEN preprocessor
build-assets = ["syntect/yaml-load", "syntect/plist-load", "regex", "walkdir"] build-assets = ["syntect/yaml-load", "syntect/plist-load", "regex", "walkdir"]
@ -69,10 +69,11 @@ encoding_rs = "0.8.35"
execute = { version = "0.2.13", optional = true } execute = { version = "0.2.13", optional = true }
terminal-colorsaurus = "0.4" terminal-colorsaurus = "0.4"
[dependencies.git2] [dependencies.gix]
version = "0.20" version = "0.70"
optional = true optional = true
default-features = false default-features = false
features = ["blob-diff"]
[dependencies.syntect] [dependencies.syntect]
version = "5.2.0" version = "5.2.0"

View File

@ -1,11 +1,15 @@
#![cfg(feature = "git")] #![cfg(feature = "git")]
use gix::diff::blob::pipeline::{Mode, WorktreeRoots};
use gix::diff::blob::{Algorithm, ResourceKind, Sink};
use gix::index::hash::Kind;
use gix::object::tree::EntryKind;
use gix::{self, ObjectId};
use path_abs::PathInfo;
use std::collections::HashMap; use std::collections::HashMap;
use std::fs; use std::ops::Range;
use std::path::Path; use std::path::Path;
use git2::{DiffOptions, IntoCString, Repository};
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub enum LineChange { pub enum LineChange {
Added, Added,
@ -16,68 +20,73 @@ pub enum LineChange {
pub type LineChanges = HashMap<u32, LineChange>; pub type LineChanges = HashMap<u32, LineChange>;
pub fn get_git_diff(filename: &Path) -> Option<LineChanges> { struct DiffStepper(LineChanges);
let repo = Repository::discover(filename).ok()?;
let repo_path_absolute = fs::canonicalize(repo.workdir()?).ok()?; impl Sink for DiffStepper {
type Out = LineChanges;
let filepath_absolute = fs::canonicalize(filename).ok()?; fn process_change(&mut self, before: Range<u32>, after: Range<u32>) {
let filepath_relative_to_repo = filepath_absolute.strip_prefix(&repo_path_absolute).ok()?; if before.is_empty() && !after.is_empty() {
for line in after {
let mut diff_options = DiffOptions::new(); self.0.insert(line + 1, LineChange::Added);
let pathspec = filepath_relative_to_repo.into_c_string().ok()?; }
diff_options.pathspec(pathspec); } else if after.is_empty() && !before.is_empty() {
diff_options.context_lines(0); if after.start == 0 {
self.0.insert(1, LineChange::RemovedAbove);
let diff = repo } else {
.diff_index_to_workdir(None, Some(&mut diff_options)) self.0.insert(after.start, LineChange::RemovedBelow);
.ok()?; }
} else {
let mut line_changes: LineChanges = HashMap::new(); for line in after {
self.0.insert(line + 1, LineChange::Modified);
let mark_section =
|line_changes: &mut LineChanges, start: u32, end: i32, change: LineChange| {
for line in start..=end as u32 {
line_changes.insert(line, change);
} }
}; };
}
let _ = diff.foreach( fn finish(self) -> Self::Out {
&mut |_, _| true, self.0
None, }
Some(&mut |delta, hunk| { }
let path = delta.new_file().path().unwrap_or_else(|| Path::new(""));
pub fn get_git_diff(filename: &Path) -> Option<LineChanges> {
if filepath_relative_to_repo != path { let filepath_absolute = filename.canonicalize().ok()?;
return false; let repository = gix::discover(filepath_absolute.parent().ok()?).unwrap();
} let repo_path_absolute = repository.work_dir()?.canonicalize().ok()?;
let filepath_relative_to_repo = filepath_absolute.strip_prefix(&repo_path_absolute).ok()?;
let old_lines = hunk.old_lines(); let mut cache = repository
let new_start = hunk.new_start(); .diff_resource_cache(
let new_lines = hunk.new_lines(); Mode::ToGit,
let new_end = (new_start + new_lines) as i32 - 1; WorktreeRoots {
old_root: None,
if old_lines == 0 && new_lines > 0 { new_root: repository.work_dir().map(Path::to_path_buf),
mark_section(&mut line_changes, new_start, new_end, LineChange::Added); },
} else if new_lines == 0 && old_lines > 0 { )
if new_start == 0 { .ok()?;
mark_section(&mut line_changes, 1, 1, LineChange::RemovedAbove); cache
} else { .set_resource(
mark_section( repository
&mut line_changes, .head_tree()
new_start, .ok()?
new_start as i32, .lookup_entry_by_path(filepath_relative_to_repo.to_str()?).ok()??
LineChange::RemovedBelow, .object_id(),
); EntryKind::Blob,
} filepath_relative_to_repo.to_str()?.into(),
} else { ResourceKind::OldOrSource,
mark_section(&mut line_changes, new_start, new_end, LineChange::Modified); &repository,
} )
.ok()?;
true cache
}), .set_resource(
None, ObjectId::null(Kind::Sha1),
); EntryKind::Blob,
filepath_relative_to_repo.to_str()?.into(),
Some(line_changes) ResourceKind::NewOrDestination,
&repository,
)
.ok()?;
return Some(gix::diff::blob::diff(
Algorithm::Myers,
&cache.prepare_diff().ok()?.interned_input(),
DiffStepper(HashMap::new()),
));
} }

View File

@ -1,15 +1,13 @@
use std::env; use std::env;
use std::fs::{self, File}; use std::fs::{self, File};
use std::io::Read; use std::io::Read;
use std::path::{Path, PathBuf}; use std::path::PathBuf;
use std::process::Command; use std::process::Command;
use gix::bstr::BString;
use gix::objs::tree;
use tempfile::TempDir; use tempfile::TempDir;
use git2::build::CheckoutBuilder;
use git2::Repository;
use git2::Signature;
pub struct BatTester { pub struct BatTester {
/// Temporary working directory /// Temporary working directory
temp_dir: TempDir, temp_dir: TempDir,
@ -33,7 +31,6 @@ impl BatTester {
]) ])
.output() .output()
.expect("bat failed"); .expect("bat failed");
// have to do the replace because the filename in the header changes based on the current working directory // have to do the replace because the filename in the header changes based on the current working directory
let actual = String::from_utf8_lossy(&output.stdout) let actual = String::from_utf8_lossy(&output.stdout)
.as_ref() .as_ref()
@ -68,35 +65,31 @@ impl Default for BatTester {
} }
} }
fn create_sample_directory() -> Result<TempDir, git2::Error> { fn create_sample_directory() -> Result<TempDir, Box<dyn std::error::Error>> {
// Create temp directory and initialize repository // Create temp directory and initialize repository
let temp_dir = TempDir::new().expect("Temp directory"); let temp_dir = TempDir::new().expect("Temp directory");
let repo = Repository::init(&temp_dir)?; let repo = gix::init(&temp_dir)?;
let mut tree = gix::objs::Tree::empty();
// Copy over `sample.rs` // Create sample.rs from snapshot file
let sample_path = temp_dir.path().join("sample.rs"); let blob_id = repo.write_blob_stream(File::open("tests/snapshots/sample.rs")?)?;
println!("{:?}", &sample_path); let entry = tree::Entry {
fs::copy("tests/snapshots/sample.rs", &sample_path).expect("successful copy"); mode: tree::EntryMode::from(tree::EntryKind::Blob),
oid: blob_id.object()?.id,
filename: BString::from("sample.rs"),
};
tree.entries.push(entry);
let tree_id = repo.write_object(tree)?;
// Commit let commit_id = repo.commit(
let mut index = repo.index()?; "HEAD",
index.add_path(Path::new("sample.rs"))?;
let oid = index.write_tree()?;
let signature = Signature::now("bat test runner", "bat@test.runner")?;
let tree = repo.find_tree(oid)?;
let _ = repo.commit(
Some("HEAD"), // point HEAD to our new commit
&signature, // author
&signature, // committer
"initial commit", "initial commit",
&tree, tree_id,
&[], gix::commit::NO_PARENT_IDS
); )?;
let mut opts = CheckoutBuilder::new(); assert_eq!(commit_id, repo.head_id()?);
repo.checkout_head(Some(opts.force()))?;
fs::copy("tests/snapshots/sample.modified.rs", &sample_path).expect("successful copy"); fs::copy("tests/snapshots/sample.modified.rs", temp_dir.path().join("sample.rs")).expect("successful copy");
Ok(temp_dir) Ok(temp_dir)
} }