From f9e0feaaf827b69971bf89f8edd1d2f1f8cbb867 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Fri, 11 Aug 2023 22:13:25 -0700 Subject: [PATCH] working_copy: return early from `write_path_to_store()` for non-files Almost the entire method deals with `FileType::Normal`, so we can reduce indentation and repeated matching on the file type by doing it early and returning in the non-normal-file cases. --- lib/src/working_copy.rs | 46 +++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/lib/src/working_copy.rs b/lib/src/working_copy.rs index 414a95e31..294267516 100644 --- a/lib/src/working_copy.rs +++ b/lib/src/working_copy.rs @@ -961,11 +961,18 @@ impl TreeState { current_tree_value: Option, file_type: FileType, ) -> Result { + let executable = match file_type { + FileType::Normal { executable } => executable, + FileType::Symlink => { + let id = self.write_symlink_to_store(repo_path, disk_path)?; + return Ok(TreeValue::Symlink(id)); + } + FileType::GitSubmodule => panic!("git submodule cannot be written to store"), + }; + // If the file contained a conflict before and is now a normal file on disk, we // try to parse any conflict markers in the file into a conflict. - if let (Some(TreeValue::Conflict(conflict_id)), FileType::Normal { executable }) = - (¤t_tree_value, &file_type) - { + if let Some(TreeValue::Conflict(conflict_id)) = ¤t_tree_value { let conflict = self.store.read_conflict(repo_path, conflict_id)?; if let Some(old_file_ids) = conflict.to_file_merge() { let mut file = File::open(disk_path).map_err(|err| SnapshotError::IoError { @@ -987,8 +994,6 @@ impl TreeState { .unwrap(); match new_file_ids.into_resolved() { Ok(file_id) => { - #[cfg(unix)] - let executable = *executable; #[cfg(windows)] let executable = { let () = executable; // use the variable @@ -1014,27 +1019,18 @@ impl TreeState { Ok(TreeValue::Conflict(conflict_id.clone())) } } else { - match file_type { - FileType::Normal { executable } => { - let id = self.write_file_to_store(repo_path, disk_path)?; - // On Windows, we preserve the executable bit from the current tree. - #[cfg(windows)] - let executable = { - let () = executable; // use the variable - if let Some(TreeValue::File { id: _, executable }) = current_tree_value { - executable - } else { - false - } - }; - Ok(TreeValue::File { id, executable }) + let id = self.write_file_to_store(repo_path, disk_path)?; + // On Windows, we preserve the executable bit from the current tree. + #[cfg(windows)] + let executable = { + let () = executable; // use the variable + if let Some(TreeValue::File { id: _, executable }) = current_tree_value { + executable + } else { + false } - FileType::Symlink => { - let id = self.write_symlink_to_store(repo_path, disk_path)?; - Ok(TreeValue::Symlink(id)) - } - FileType::GitSubmodule => panic!("git submodule cannot be written to store"), - } + }; + Ok(TreeValue::File { id, executable }) } }