use gix_hash::ObjectId;
use gix_object::{tree, tree::EntryMode};
pub type ChangeId = u32;
#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)]
pub enum Relation {
Parent(ChangeId),
ChildOfParent(ChangeId),
}
#[derive(Debug, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)]
pub enum Change {
Addition {
entry_mode: tree::EntryMode,
oid: ObjectId,
relation: Option<Relation>,
},
Deletion {
entry_mode: tree::EntryMode,
oid: ObjectId,
relation: Option<Relation>,
},
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,
relation: _,
}
| Change::Deletion {
oid,
entry_mode,
relation: _,
}
| Change::Modification { oid, entry_mode, .. } => (oid, *entry_mode),
}
}
}
pub type Action = std::ops::ControlFlow<()>;
#[cfg(feature = "blob")]
mod change_impls {
use gix_hash::oid;
use gix_object::tree::EntryMode;
use crate::{
rewrites::tracker::ChangeKind,
tree::visit::{Change, Relation},
};
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 relation(&self) -> Option<Relation> {
match self {
Change::Addition { relation, .. } | Change::Deletion { relation, .. } => *relation,
Change::Modification { .. } => None,
}
}
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>();
let sha1 = 48;
let sha256_extra = 24;
let ceiling = sha1 + sha256_extra;
assert!(
actual <= ceiling,
"{actual} <= {ceiling}: this type shouldn't grow without us knowing"
);
}
}