gix_index/entry/
mode.rs

1use crate::entry::Mode;
2
3impl Mode {
4    /// Return `true` if this is a sparse entry, as it points to a directory which usually isn't what an 'unsparse' index tracks.
5    pub fn is_sparse(&self) -> bool {
6        *self == Self::DIR
7    }
8
9    /// Return `true` if this is a submodule entry.
10    pub fn is_submodule(&self) -> bool {
11        *self == Self::DIR | Self::SYMLINK
12    }
13
14    /// Convert this instance to a tree's entry mode, or return `None` if for some
15    /// and unexpected reason the bitflags don't resemble any known entry-mode.
16    pub fn to_tree_entry_mode(&self) -> Option<gix_object::tree::EntryMode> {
17        gix_object::tree::EntryMode::try_from(self.bits()).ok()
18    }
19
20    /// Compares this mode to the file system version ([`std::fs::symlink_metadata`])
21    /// and returns the change needed to update this mode to match the file.
22    ///
23    /// * if `has_symlinks` is false symlink entries will simply check if there
24    ///   is a normal file on disk
25    /// * if `executable_bit` is false the executable bit will not be compared
26    ///   `Change::ExecutableBit` will never be generated
27    ///
28    /// If there is a type change then we will use whatever information is
29    /// present on the FS. Specifically if `has_symlinks` is false we will
30    /// never generate `Change::TypeChange { new_mode: Mode::SYMLINK }`. and
31    /// if `executable_bit` is false we will never generate `Change::TypeChange
32    /// { new_mode: Mode::FILE_EXECUTABLE }` (all files are assumed to be not
33    /// executable). That means that unstaging and staging files can be a lossy
34    /// operation on such file systems.
35    ///
36    /// If a directory replaced a normal file/symlink we assume that the
37    /// directory is a submodule. Normal (non-submodule) directories would
38    /// cause a file to be deleted from the index and should be handled before
39    /// calling this function.
40    ///
41    /// If the stat information belongs to something other than a normal file/
42    /// directory (like a socket) we just return an identity change (non-files
43    /// can not be committed to git).
44    pub fn change_to_match_fs(
45        self,
46        stat: &crate::fs::Metadata,
47        has_symlinks: bool,
48        executable_bit: bool,
49    ) -> Option<Change> {
50        match self {
51            Mode::FILE if !stat.is_file() => (),
52            Mode::SYMLINK if has_symlinks && !stat.is_symlink() => (),
53            Mode::SYMLINK if !has_symlinks && !stat.is_file() => (),
54            Mode::COMMIT | Mode::DIR if !stat.is_dir() => (),
55            Mode::FILE if executable_bit && stat.is_executable() => return Some(Change::ExecutableBit),
56            Mode::FILE_EXECUTABLE if executable_bit && !stat.is_executable() => return Some(Change::ExecutableBit),
57            _ => return None,
58        };
59        let new_mode = if stat.is_dir() {
60            Mode::COMMIT
61        } else if executable_bit && stat.is_executable() {
62            Mode::FILE_EXECUTABLE
63        } else if has_symlinks && stat.is_symlink() {
64            Mode::SYMLINK
65        } else {
66            Mode::FILE
67        };
68        Some(Change::Type { new_mode })
69    }
70}
71
72impl From<gix_object::tree::EntryMode> for Mode {
73    fn from(value: gix_object::tree::EntryMode) -> Self {
74        Self::from_bits_truncate(u32::from(value.0))
75    }
76}
77
78/// A change of a [`Mode`].
79#[derive(Debug, Copy, Clone, PartialEq, Eq)]
80pub enum Change {
81    /// The type of mode changed, like symlink => file.
82    Type {
83        /// The mode representing the new index type.
84        new_mode: Mode,
85    },
86    /// The executable permission of this file has changed.
87    ExecutableBit,
88}
89
90impl Change {
91    /// Applies this change to `mode` and returns the changed one.
92    pub fn apply(self, mode: Mode) -> Mode {
93        match self {
94            Change::Type { new_mode } => new_mode,
95            Change::ExecutableBit => match mode {
96                Mode::FILE => Mode::FILE_EXECUTABLE,
97                Mode::FILE_EXECUTABLE => Mode::FILE,
98                _ => unreachable!("invalid mode change: can't flip executable bit of {mode:?}"),
99            },
100        }
101    }
102}