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 stat.is_symlink() => return None,
53            Mode::SYMLINK if has_symlinks && !stat.is_symlink() => (),
54            Mode::SYMLINK if !has_symlinks && !stat.is_file() => (),
55            Mode::COMMIT | Mode::DIR if !stat.is_dir() => (),
56            Mode::FILE if executable_bit && stat.is_executable() => return Some(Change::ExecutableBit),
57            Mode::FILE_EXECUTABLE if executable_bit && !stat.is_executable() => return Some(Change::ExecutableBit),
58            _ => return None,
59        }
60        let new_mode = if stat.is_dir() {
61            Mode::COMMIT
62        } else if executable_bit && stat.is_executable() {
63            Mode::FILE_EXECUTABLE
64        } else if has_symlinks && stat.is_symlink() {
65            Mode::SYMLINK
66        } else {
67            Mode::FILE
68        };
69        Some(Change::Type { new_mode })
70    }
71}
72
73impl From<gix_object::tree::EntryMode> for Mode {
74    fn from(value: gix_object::tree::EntryMode) -> Self {
75        let value: u16 = value.value();
76        Self::from_bits_truncate(u32::from(value))
77    }
78}
79
80/// A change of a [`Mode`].
81#[derive(Debug, Copy, Clone, PartialEq, Eq)]
82pub enum Change {
83    /// The type of mode changed, like symlink => file.
84    Type {
85        /// The mode representing the new index type.
86        new_mode: Mode,
87    },
88    /// The executable permission of this file has changed.
89    ExecutableBit,
90}
91
92impl Change {
93    /// Applies this change to `mode` and returns the changed one.
94    pub fn apply(self, mode: Mode) -> Mode {
95        match self {
96            Change::Type { new_mode } => new_mode,
97            Change::ExecutableBit => match mode {
98                Mode::FILE => Mode::FILE_EXECUTABLE,
99                Mode::FILE_EXECUTABLE => Mode::FILE,
100                _ => unreachable!("invalid mode change: can't flip executable bit of {mode:?}"),
101            },
102        }
103    }
104}