Skip to content

Commit

Permalink
refactor!: Use the new tree_with_rewrites plumbing implementation.
Browse files Browse the repository at this point in the history
This merges `object::tree::diff::change::Event` into `object::tree::diff::Change`
as well.
  • Loading branch information
Byron committed Oct 7, 2024
1 parent fe307f8 commit bb29cdb
Show file tree
Hide file tree
Showing 9 changed files with 433 additions and 1,765 deletions.
9 changes: 5 additions & 4 deletions gitoxide-core/src/hours/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,19 @@ pub fn spawn_tree_delta_threads<'scope>(
.track_filename()
.track_rewrites(None)
.for_each_to_obtain_tree(&to, |change| {
use gix::object::tree::diff::change::Event::*;
use gix::object::tree::diff::Change::*;
changes.fetch_add(1, Ordering::Relaxed);
match change.event {
match change {
Rewrite { .. } => {
unreachable!("we turned that off")
}
Addition { entry_mode, id } => {
Addition { entry_mode, id, .. } => {
if entry_mode.is_no_tree() {
files.added += 1;
add_lines(line_stats, &lines_count, &mut lines, id);
}
}
Deletion { entry_mode, id } => {
Deletion { entry_mode, id, .. } => {
if entry_mode.is_no_tree() {
files.removed += 1;
remove_lines(line_stats, &lines_count, &mut lines, id);
Expand All @@ -151,6 +151,7 @@ pub fn spawn_tree_delta_threads<'scope>(
previous_entry_mode,
id,
previous_id,
..
} => match (previous_entry_mode.is_blob(), entry_mode.is_blob()) {
(false, false) => {}
(false, true) => {
Expand Down
37 changes: 22 additions & 15 deletions gitoxide-core/src/query/engine/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,31 +210,32 @@ pub fn update(
.track_path()
.track_rewrites(Some(rewrites))
.for_each_to_obtain_tree_with_cache(&to, &mut rewrite_cache, |change| {
use gix::object::tree::diff::change::Event::*;
use gix::object::tree::diff::Change::*;
change_counter.fetch_add(1, Ordering::SeqCst);
match change.event {
Addition { entry_mode, id } => {
match change {
Addition {
entry_mode,
id,
location,
..
} => {
if entry_mode.is_blob_or_symlink() {
add_lines(&mut out, change.location, &lines_counter, id);
add_lines(&mut out, location, &lines_counter, id);
}
}
Modification {
entry_mode,
previous_entry_mode,
id,
previous_id,
location,
} => match (previous_entry_mode.is_blob(), entry_mode.is_blob()) {
(false, false) => {}
(false, true) => {
add_lines(&mut out, change.location, &lines_counter, id);
add_lines(&mut out, location, &lines_counter, id);
}
(true, false) => {
add_lines(
&mut out,
change.location,
&lines_counter,
previous_id,
);
add_lines(&mut out, location, &lines_counter, previous_id);
}
(true, true) => {
if let Ok(cache) =
Expand Down Expand Up @@ -266,7 +267,7 @@ pub fn update(
lines_counter
.fetch_add(nl, Ordering::SeqCst);
out.push(FileChange {
relpath: change.location.to_owned(),
relpath: location.to_owned(),
mode: FileMode::Modified,
source_relpath: None,
lines: Some(lines),
Expand All @@ -281,19 +282,25 @@ pub fn update(
}
}
},
Deletion { entry_mode, id } => {
Deletion {
entry_mode,
id,
location,
..
} => {
if entry_mode.is_blob_or_symlink() {
remove_lines(&mut out, change.location, &lines_counter, id);
remove_lines(&mut out, location, &lines_counter, id);
}
}
Rewrite {
source_location,
diff,
copy,
location,
..
} => {
out.push(FileChange {
relpath: change.location.to_owned(),
relpath: location.to_owned(),
source_relpath: Some(source_location.to_owned()),
mode: if copy { FileMode::Copy } else { FileMode::Rename },
lines: diff.map(|d| LineStats {
Expand Down
2 changes: 1 addition & 1 deletion gix/src/ext/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub use object_id::ObjectIdExt;
pub use reference::ReferenceExt;
#[cfg(feature = "revision")]
pub use rev_spec::RevSpecExt;
pub use tree::{TreeEntryExt, TreeEntryRefExt, TreeIterExt};
pub use tree::{TreeDiffChangeExt, TreeEntryExt, TreeEntryRefExt, TreeIterExt};

mod object_id;
mod reference;
Expand Down
20 changes: 16 additions & 4 deletions gix/src/ext/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ impl TreeIterExt for TreeRefIter<'_> {
}
}

/// Extensions for [EntryRef][gix_object::tree::EntryRef].
/// Extensions for [EntryRef](gix_object::tree::EntryRef).
pub trait TreeEntryRefExt<'a>: 'a {
/// Attach [`Repository`][crate::Repository] to the given tree entry. It can be detached later with `detach()`.
/// Attach [`repo`](crate::Repository) to the given tree entry. It can be detached later with `detach()`.
fn attach<'repo>(self, repo: &'repo crate::Repository) -> crate::object::tree::EntryRef<'repo, 'a>;
}

Expand All @@ -54,9 +54,9 @@ impl<'a> TreeEntryRefExt<'a> for gix_object::tree::EntryRef<'a> {
}
}

/// Extensions for [Entry][gix_object::tree::Entry].
/// Extensions for [Entry](gix_object::tree::Entry).
pub trait TreeEntryExt {
/// Attach [`Repository`][crate::Repository] to the given tree entry. It can be detached later with `detach()`.
/// Attach [`repo`](crate::Repository) to the given tree entry. It can be detached later with `detach()`.
fn attach(self, repo: &crate::Repository) -> crate::object::tree::Entry<'_>;
}

Expand All @@ -65,3 +65,15 @@ impl TreeEntryExt for gix_object::tree::Entry {
crate::object::tree::Entry { inner: self, repo }
}
}

/// Extensions for [Change](gix_diff::tree_with_rewrites::Change).
#[cfg(feature = "blob-diff")]
pub trait TreeDiffChangeExt {
/// Attach [`old_repo`](crate::Repository) and `new_repo` to current instance. It can be detached later with `detach()`.
/// Note that both repositories are usually the same.
fn attach<'old, 'new>(
&self,
old_repo: &'old crate::Repository,
new_repo: &'new crate::Repository,
) -> crate::object::tree::diff::Change<'_, 'old, 'new>;
}
104 changes: 6 additions & 98 deletions gix/src/object/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ use crate::{Blob, ObjectDetached};
pub mod diff {
use std::ops::Range;

use gix_diff::blob::{platform::prepare_diff::Operation, ResourceKind};
use gix_diff::blob::platform::prepare_diff::Operation;

use crate::{
bstr::ByteSlice,
object::{blob::diff::lines::Change, tree::diff::change::Event},
};
use crate::bstr::ByteSlice;

/// A platform to keep temporary information to perform line diffs on modified blobs.
///
Expand All @@ -21,99 +18,10 @@ pub mod diff {

///
pub mod init {
/// The error returned by [`Platform::from_tree_change()`][super::Platform::from_tree_change()].
/// The error returned by [`object::tree::diff::Change::diff`](crate::object::tree::diff::Change::diff()).
pub type Error = gix_diff::blob::platform::set_resource::Error;
}

impl<'a> Platform<'a> {
/// Produce a platform for performing various diffs after obtaining the data from a single `tree_change`.
pub fn from_tree_change(
tree_change: &crate::object::tree::diff::Change<'_, '_, '_>,
resource_cache: &'a mut gix_diff::blob::Platform,
) -> Result<Platform<'a>, init::Error> {
match tree_change.event {
Event::Addition { entry_mode, id } => {
resource_cache.set_resource(
id.repo.object_hash().null(),
entry_mode.kind(),
tree_change.location,
ResourceKind::OldOrSource,
&id.repo.objects,
)?;
resource_cache.set_resource(
id.inner,
entry_mode.kind(),
tree_change.location,
ResourceKind::NewOrDestination,
&id.repo.objects,
)?;
}
Event::Deletion { entry_mode, id } => {
resource_cache.set_resource(
id.inner,
entry_mode.kind(),
tree_change.location,
ResourceKind::OldOrSource,
&id.repo.objects,
)?;
resource_cache.set_resource(
id.repo.object_hash().null(),
entry_mode.kind(),
tree_change.location,
ResourceKind::NewOrDestination,
&id.repo.objects,
)?;
}
Event::Modification {
previous_entry_mode,
previous_id,
entry_mode,
id,
} => {
resource_cache.set_resource(
previous_id.inner,
previous_entry_mode.kind(),
tree_change.location,
ResourceKind::OldOrSource,
&previous_id.repo.objects,
)?;
resource_cache.set_resource(
id.inner,
entry_mode.kind(),
tree_change.location,
ResourceKind::NewOrDestination,
&id.repo.objects,
)?;
}
Event::Rewrite {
source_location,
source_entry_mode,
source_id,
entry_mode,
id,
diff: _,
copy: _,
} => {
resource_cache.set_resource(
source_id.inner,
source_entry_mode.kind(),
source_location,
ResourceKind::OldOrSource,
&source_id.repo.objects,
)?;
resource_cache.set_resource(
id.inner,
entry_mode.kind(),
tree_change.location,
ResourceKind::NewOrDestination,
&id.repo.objects,
)?;
}
}
Ok(Self { resource_cache })
}
}

///
pub mod lines {
use crate::bstr::BStr;
Expand Down Expand Up @@ -195,11 +103,11 @@ pub mod diff {
let hunk_before = &lines[..end_of_before];
let hunk_after = &lines[end_of_before..];
if hunk_after.is_empty() {
err = process_hunk(Change::Deletion { lines: hunk_before }).err();
err = process_hunk(lines::Change::Deletion { lines: hunk_before }).err();
} else if hunk_before.is_empty() {
err = process_hunk(Change::Addition { lines: hunk_after }).err();
err = process_hunk(lines::Change::Addition { lines: hunk_after }).err();
} else {
err = process_hunk(Change::Modification {
err = process_hunk(lines::Change::Modification {
lines_before: hunk_before,
lines_after: hunk_after,
})
Expand Down
Loading

0 comments on commit bb29cdb

Please sign in to comment.