Skip to content

Commit 7f38b93

Browse files
committed
feat: entry::Stage can now be converted into entry::Flags.
This makes it easier to change the stage of an entry.
1 parent 36aaf17 commit 7f38b93

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

gix-index/src/entry/flags.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ bitflags! {
6060
}
6161

6262
impl Flags {
63+
/// Create a new instance whose stage is set to `stage`.
64+
pub fn from_stage(stage: Stage) -> Self {
65+
Flags::from_bits((stage as u32) << 12).expect("stage can only be valid flags")
66+
}
67+
6368
/// Return the stage as extracted from the bits of this instance.
6469
pub fn stage(&self) -> Stage {
6570
match self.stage_raw() {
@@ -96,6 +101,12 @@ impl Flags {
96101
}
97102
}
98103

104+
impl From<Stage> for Flags {
105+
fn from(value: Stage) -> Self {
106+
Flags::from_stage(value)
107+
}
108+
}
109+
99110
pub(crate) mod at_rest {
100111
use bitflags::bitflags;
101112

gix-index/tests/index/entry/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
mod flags {
2+
use gix_index::entry::{Flags, Stage};
3+
4+
#[test]
5+
fn from_stage() {
6+
for stage in [Stage::Unconflicted, Stage::Base, Stage::Ours, Stage::Theirs] {
7+
let actual = Flags::from_stage(stage);
8+
assert_eq!(actual.stage(), stage);
9+
let actual: Flags = stage.into();
10+
assert_eq!(actual.stage(), stage);
11+
}
12+
}
13+
}
114
mod mode;
215
mod stat;
316
mod time;

gix-index/tests/index/entry/mode.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,18 @@ fn apply() {
1212
Mode::SYMLINK
1313
);
1414
}
15+
16+
#[test]
17+
fn debug() {
18+
assert_eq!(
19+
format!("{:?}", Mode::FILE),
20+
"Mode(FILE)",
21+
"Assure the debug output is easy to understand"
22+
);
23+
24+
assert_eq!(
25+
format!("{:?}", Mode::from_bits(0o120744)),
26+
"Some(Mode(FILE | SYMLINK | 0x40))",
27+
"strange modes are also mostly legible"
28+
);
29+
}

0 commit comments

Comments
 (0)