use gix_hash::ObjectId;
use gix_object::{bstr::BStr, tree, tree::EntryMode};
#[derive(Debug, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)]
pub enum Change {
Addition {
entry_mode: tree::EntryMode,
oid: ObjectId,
},
Deletion {
entry_mode: tree::EntryMode,
oid: ObjectId,
},
Modification {
previous_entry_mode: tree::EntryMode,
previous_oid: ObjectId,
entry_mode: tree::EntryMode,
oid: ObjectId,
},
}
impl Change {
pub fn oid(&self) -> &gix_hash::oid {
match self {
Change::Addition { oid, .. } | Change::Deletion { oid, .. } | Change::Modification { oid, .. } => oid,
}
}
pub fn entry_mode(&self) -> EntryMode {
match self {
Change::Addition { entry_mode, .. }
| Change::Deletion { entry_mode, .. }
| Change::Modification { entry_mode, .. } => *entry_mode,
}
}
pub fn oid_and_entry_mode(&self) -> (&gix_hash::oid, EntryMode) {
match self {
Change::Addition { oid, entry_mode }
| Change::Deletion { oid, entry_mode }
| Change::Modification { oid, entry_mode, .. } => (oid, *entry_mode),
}
}
}
#[derive(Default, Clone, Copy, PartialOrd, PartialEq, Ord, Eq, Hash)]
pub enum Action {
#[default]
Continue,
Cancel,
}
impl Action {
pub fn cancelled(&self) -> bool {
matches!(self, Action::Cancel)
}
}
pub trait Visit {
fn pop_front_tracked_path_and_set_current(&mut self);
fn push_back_tracked_path_component(&mut self, component: &BStr);
fn push_path_component(&mut self, component: &BStr);
fn pop_path_component(&mut self);
fn visit(&mut self, change: Change) -> Action;
}
#[cfg(feature = "blob")]
mod change_impls {
use gix_hash::oid;
use gix_object::tree::EntryMode;
use crate::{rewrites::tracker::ChangeKind, tree::visit::Change};
impl crate::rewrites::tracker::Change for crate::tree::visit::Change {
fn id(&self) -> &oid {
match self {
Change::Addition { oid, .. } | Change::Deletion { oid, .. } | Change::Modification { oid, .. } => oid,
}
}
fn kind(&self) -> ChangeKind {
match self {
Change::Addition { .. } => ChangeKind::Addition,
Change::Deletion { .. } => ChangeKind::Deletion,
Change::Modification { .. } => ChangeKind::Modification,
}
}
fn entry_mode(&self) -> EntryMode {
match self {
Change::Addition { entry_mode, .. }
| Change::Deletion { entry_mode, .. }
| Change::Modification { entry_mode, .. } => *entry_mode,
}
}
fn id_and_entry_mode(&self) -> (&oid, EntryMode) {
match self {
Change::Addition { entry_mode, oid, .. }
| Change::Deletion { entry_mode, oid, .. }
| Change::Modification { entry_mode, oid, .. } => (oid, *entry_mode),
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn size_of_change() {
let actual = std::mem::size_of::<Change>();
assert!(
actual <= 46,
"{actual} <= 46: this type shouldn't grow without us knowing"
)
}
}