ok/jj
1
0
Fork 0
forked from mirrors/jj

working_copy: make FileType variants more similar to TreeValue variants

This change doesn't make much difference yet, but I think it will
enable further improvements.
This commit is contained in:
Martin von Zweigbergk 2021-10-29 22:19:30 -07:00 committed by Martin von Zweigbergk
parent bf61e9cf3e
commit efe7316354

View file

@ -44,8 +44,7 @@ use crate::tree::Diff;
#[derive(Debug, PartialEq, Eq, Clone)] #[derive(Debug, PartialEq, Eq, Clone)]
pub enum FileType { pub enum FileType {
Normal, Normal { executable: bool },
Executable,
Symlink, Symlink,
} }
@ -62,17 +61,15 @@ pub struct FileState {
impl FileState { impl FileState {
fn null() -> FileState { fn null() -> FileState {
FileState { FileState {
file_type: FileType::Normal, file_type: FileType::Normal { executable: false },
mtime: MillisSinceEpoch(0), mtime: MillisSinceEpoch(0),
size: 0, size: 0,
} }
} }
fn mark_executable(&mut self, executable: bool) { fn mark_executable(&mut self, executable: bool) {
match (&self.file_type, executable) { if let FileType::Normal { .. } = &self.file_type {
(FileType::Normal, true) => self.file_type = FileType::Executable, self.file_type = FileType::Normal { executable }
(FileType::Executable, false) => self.file_type = FileType::Normal,
_ => {}
} }
} }
} }
@ -88,9 +85,9 @@ pub struct TreeState {
fn file_state_from_proto(proto: &crate::protos::working_copy::FileState) -> FileState { fn file_state_from_proto(proto: &crate::protos::working_copy::FileState) -> FileState {
let file_type = match proto.file_type { let file_type = match proto.file_type {
crate::protos::working_copy::FileType::Normal => FileType::Normal, crate::protos::working_copy::FileType::Normal => FileType::Normal { executable: false },
crate::protos::working_copy::FileType::Executable => FileType::Normal { executable: true },
crate::protos::working_copy::FileType::Symlink => FileType::Symlink, crate::protos::working_copy::FileType::Symlink => FileType::Symlink,
crate::protos::working_copy::FileType::Executable => FileType::Executable,
}; };
FileState { FileState {
file_type, file_type,
@ -102,9 +99,9 @@ fn file_state_from_proto(proto: &crate::protos::working_copy::FileState) -> File
fn file_state_to_proto(file_state: &FileState) -> crate::protos::working_copy::FileState { fn file_state_to_proto(file_state: &FileState) -> crate::protos::working_copy::FileState {
let mut proto = crate::protos::working_copy::FileState::new(); let mut proto = crate::protos::working_copy::FileState::new();
let file_type = match &file_state.file_type { let file_type = match &file_state.file_type {
FileType::Normal => crate::protos::working_copy::FileType::Normal, FileType::Normal { executable: false } => crate::protos::working_copy::FileType::Normal,
FileType::Normal { executable: true } => crate::protos::working_copy::FileType::Executable,
FileType::Symlink => crate::protos::working_copy::FileType::Symlink, FileType::Symlink => crate::protos::working_copy::FileType::Symlink,
FileType::Executable => crate::protos::working_copy::FileType::Executable,
}; };
proto.file_type = file_type; proto.file_type = file_type;
proto.mtime_millis_since_epoch = file_state.mtime.0; proto.mtime_millis_since_epoch = file_state.mtime.0;
@ -248,9 +245,9 @@ impl TreeState {
#[cfg(windows)] #[cfg(windows)]
let mode = 0; let mode = 0;
if mode & 0o111 != 0 { if mode & 0o111 != 0 {
FileType::Executable FileType::Normal { executable: true }
} else { } else {
FileType::Normal FileType::Normal { executable: false }
} }
}; };
Some(FileState { Some(FileState {
@ -361,7 +358,8 @@ impl TreeState {
None => { None => {
// untracked // untracked
clean = false; clean = false;
executable = new_file_state.file_type == FileType::Executable; executable =
new_file_state.file_type == FileType::Normal { executable: true };
} }
Some(current_entry) => { Some(current_entry) => {
clean = current_entry == &new_file_state clean = current_entry == &new_file_state
@ -370,17 +368,19 @@ impl TreeState {
{ {
// On Windows, we preserve the state we had recorded // On Windows, we preserve the state we had recorded
// when we wrote the file. // when we wrote the file.
executable = current_entry.file_type == FileType::Executable executable =
current_entry.file_type == FileType::Normal { executable: true }
} }
#[cfg(unix)] #[cfg(unix)]
{ {
executable = new_file_state.file_type == FileType::Executable executable = new_file_state.file_type
== FileType::Normal { executable: true }
} }
} }
}; };
if !clean { if !clean {
let file_value = match new_file_state.file_type { let file_value = match new_file_state.file_type {
FileType::Normal | FileType::Executable => { FileType::Normal { .. } => {
let id = self.write_file_to_store(&sub_path, &disk_file); let id = self.write_file_to_store(&sub_path, &disk_file);
TreeValue::Normal { id, executable } TreeValue::Normal { id, executable }
} }