Skip to content

Commit 9cb76e9

Browse files
committed
Cleanup entry::mode API
1 parent f2a9b3f commit 9cb76e9

File tree

3 files changed

+53
-29
lines changed

3 files changed

+53
-29
lines changed

gix-index/src/entry/mod.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ pub type Stage = u32;
33

44
///
55
pub mod mode;
6-
pub use mode::Mode;
76

87
mod flags;
98
pub(crate) use flags::at_rest;
@@ -13,6 +12,29 @@ pub use flags::Flags;
1312
pub mod stat;
1413
mod write;
1514

15+
use bitflags::bitflags;
16+
17+
// TODO: we essentially treat this as an enum withj the only exception being
18+
// that `FILE_EXECUTABLE.contains(FILE)` works might want to turn this into an
19+
// enum proper
20+
bitflags! {
21+
/// The kind of file of an entry.
22+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
23+
pub struct Mode: u32 {
24+
/// directory (only used for sparse checkouts), equivalent to a tree, which is _excluded_ from the index via
25+
/// cone-mode.
26+
const DIR = 0o040000;
27+
/// regular file
28+
const FILE = 0o100644;
29+
/// regular file, executable
30+
const FILE_EXECUTABLE = 0o100755;
31+
/// Symbolic link
32+
const SYMLINK = 0o120000;
33+
/// A git commit for submodules
34+
const COMMIT = 0o160000;
35+
}
36+
}
37+
1638
/// An entry's filesystem stat information.
1739
#[derive(Debug, Default, PartialEq, Eq, Hash, Ord, PartialOrd, Clone, Copy)]
1840
#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]

gix-index/src/entry/mode.rs

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,15 @@
1-
use bitflags::bitflags;
2-
3-
// TODO: we essentially treat this as an enum withj the only exception being
4-
// that `FILE_EXECUTABLE.contains(FILE)` works might want to turn this into an
5-
// enum proper
6-
bitflags! {
7-
/// The kind of file of an entry.
8-
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
9-
pub struct Mode: u32 {
10-
/// directory (only used for sparse checkouts), equivalent to a tree, which is _excluded_ from the index via
11-
/// cone-mode.
12-
const DIR = 0o040000;
13-
/// regular file
14-
const FILE = 0o100644;
15-
/// regular file, executable
16-
const FILE_EXECUTABLE = 0o100755;
17-
/// Symbolic link
18-
const SYMLINK = 0o120000;
19-
/// A git commit for submodules
20-
const COMMIT = 0o160000;
21-
}
22-
}
1+
use crate::entry::Mode;
232

243
#[cfg(unix)]
254
/// Returns whether a a file has the executable permission set
26-
pub fn is_executable(metadata: &std::fs::Metadata) -> bool {
5+
fn is_executable(metadata: &std::fs::Metadata) -> bool {
276
use std::os::unix::fs::MetadataExt;
287
(metadata.mode() & 0o100) != 0
298
}
309

3110
#[cfg(not(unix))]
3211
/// Returns whether a a file has the executable permission set
33-
pub fn is_executable(_metadata: &std::fs::Metadata) -> bool {
12+
fn is_executable(_metadata: &std::fs::Metadata) -> bool {
3413
false
3514
}
3615

@@ -80,11 +59,16 @@ pub enum Change {
8059
}
8160

8261
impl Change {
83-
/// Applies this change to a `Mode`
84-
pub fn apply(self, mode: &mut Mode) {
85-
*mode = match self {
62+
/// Applies this change to a `Mode` by updating it in place
63+
pub fn update(self, mode: &mut Mode) {
64+
*mode = self.apply(*mode);
65+
}
66+
67+
/// Applies this change to a `Mode` by updating it in place
68+
pub fn apply(self, mode: Mode) -> Mode {
69+
match self {
8670
Change::Type { new_mode } => new_mode,
87-
Change::ExecutableBit => match *mode {
71+
Change::ExecutableBit => match mode {
8872
Mode::FILE => Mode::FILE_EXECUTABLE,
8973
Mode::FILE_EXECUTABLE => Mode::FILE,
9074
_ => unreachable!("invalid mode change: can't flip executable bit of {mode:?}"),

gix-index/tests/index/entry.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,21 @@ mod stat {
319319
);
320320
}
321321
}
322+
323+
mod mode {
324+
use gix_index::entry::mode::Change;
325+
use gix_index::entry::Mode;
326+
327+
#[test]
328+
fn apply_change() {
329+
assert_eq!(Change::ExecutableBit.apply(Mode::FILE), Mode::FILE_EXECUTABLE);
330+
assert_eq!(Change::ExecutableBit.apply(Mode::FILE_EXECUTABLE), Mode::FILE);
331+
assert_eq!(
332+
Change::Type {
333+
new_mode: Mode::SYMLINK
334+
}
335+
.apply(Mode::FILE),
336+
Mode::SYMLINK
337+
);
338+
}
339+
}

0 commit comments

Comments
 (0)