git_index/entry/
mod.rs

1/// The stage of an entry, one of 0 = base, 1 = ours, 2 = theirs
2pub type Stage = u32;
3
4mod mode;
5pub use mode::Mode;
6
7mod flags;
8pub(crate) use flags::at_rest;
9pub use flags::Flags;
10
11mod write;
12
13/// The time component in a [`Stat`] struct.
14#[derive(Debug, Default, PartialEq, Eq, Hash, Ord, PartialOrd, Clone, Copy)]
15#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
16pub struct Time {
17    /// The amount of seconds elapsed since EPOCH
18    pub secs: u32,
19    /// The amount of nanoseconds elapsed in the current second, ranging from 0 to 999.999.999 .
20    pub nsecs: u32,
21}
22
23/// An entry's filesystem stat information.
24#[derive(Debug, Default, PartialEq, Eq, Hash, Ord, PartialOrd, Clone, Copy)]
25#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
26pub struct Stat {
27    /// Modification time
28    pub mtime: Time,
29    /// Creation time
30    pub ctime: Time,
31    /// Device number
32    pub dev: u32,
33    /// Inode number
34    pub ino: u32,
35    /// User id of the owner
36    pub uid: u32,
37    /// Group id of the owning group
38    pub gid: u32,
39    /// The size of bytes on disk. Capped to u32 so files bigger than that will need thorough additional checking
40    pub size: u32,
41}
42
43mod access {
44    use bstr::{BStr, ByteSlice};
45
46    use crate::{entry, Entry, State};
47
48    impl Entry {
49        /// Return an entry's path, relative to the repository, which is extracted from its owning `state`.
50        pub fn path<'a>(&self, state: &'a State) -> &'a BStr {
51            state.path_backing[self.path.clone()].as_bstr()
52        }
53
54        /// Return an entry's path using the given `backing`.
55        pub fn path_in<'backing>(&self, backing: &'backing crate::PathStorageRef) -> &'backing BStr {
56            backing[self.path.clone()].as_bstr()
57        }
58
59        /// Return an entry's stage.
60        pub fn stage(&self) -> entry::Stage {
61            self.flags.stage()
62        }
63    }
64}
65
66mod _impls {
67    use std::{cmp::Ordering, ops::Add, time::SystemTime};
68
69    use bstr::BStr;
70
71    use crate::{entry::Time, Entry, State};
72
73    impl From<SystemTime> for Time {
74        fn from(s: SystemTime) -> Self {
75            let d = s
76                .duration_since(std::time::UNIX_EPOCH)
77                .expect("system time is not before unix epoch!");
78            Time {
79                secs: d.as_secs() as u32,
80                nsecs: d.subsec_nanos(),
81            }
82        }
83    }
84
85    impl From<Time> for SystemTime {
86        fn from(s: Time) -> Self {
87            std::time::UNIX_EPOCH.add(std::time::Duration::new(s.secs.into(), s.nsecs))
88        }
89    }
90
91    impl Entry {
92        /// Compare one entry to another by their path, by comparing only their common path portion byte by byte, then resorting to
93        /// entry length and stage.
94        pub fn cmp(&self, other: &Self, state: &State) -> Ordering {
95            let lhs = self.path(state);
96            let rhs = other.path(state);
97            Entry::cmp_filepaths(lhs, rhs).then_with(|| self.stage().cmp(&other.stage()))
98        }
99
100        /// Compare one entry to another by their path, by comparing only their common path portion byte by byte, then resorting to
101        /// entry length.
102        pub fn cmp_filepaths(a: &BStr, b: &BStr) -> Ordering {
103            let common_len = a.len().min(b.len());
104            a[..common_len]
105                .cmp(&b[..common_len])
106                .then_with(|| a.len().cmp(&b.len()))
107        }
108    }
109}