From 37a770e8b43d88ecde7391f8d6cd4ca24ada6e6b Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Mon, 24 Jul 2023 21:34:44 -0700 Subject: [PATCH] working_copy: make `write_conflict_to_store()` also handle conflicts With this change, `write_path_to_store()` contains all the logic for reading a file from disk and writing it to a `TreeBuilder`, making the code for added and modified files more similar. --- lib/src/working_copy.rs | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/lib/src/working_copy.rs b/lib/src/working_copy.rs index 997f8c249..66c674c88 100644 --- a/lib/src/working_copy.rs +++ b/lib/src/working_copy.rs @@ -812,7 +812,8 @@ impl TreeState { (None, Some(new_file_state)) => { // untracked let file_type = new_file_state.file_type.clone(); - let file_value = self.write_path_to_store(repo_path, &disk_path, file_type)?; + let file_value = + self.write_path_to_store(repo_path, &disk_path, None, file_type)?; return Ok(UpdatedFileState::Changed(file_value, new_file_state)); } (Some(current_file_state), Some(new_file_state)) => { @@ -836,13 +837,7 @@ impl TreeState { // 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. let new_tree_value = - if let (Some(TreeValue::Conflict(conflict_id)), FileType::Normal { executable }) = - (current_tree_value, &new_file_type) - { - self.write_conflict_to_store(repo_path, &disk_path, conflict_id, *executable)? - } else { - self.write_path_to_store(repo_path, &disk_path, new_file_type)? - }; + self.write_path_to_store(repo_path, &disk_path, current_tree_value, new_file_type)?; return Ok(UpdatedFileState::Changed(new_tree_value, new_file_state)); } Ok(UpdatedFileState::Unchanged(new_file_state)) @@ -882,18 +877,27 @@ impl TreeState { &self, repo_path: &RepoPath, disk_path: &Path, + current_tree_value: Option, file_type: FileType, ) -> Result { - match file_type { - FileType::Normal { executable } => { - let id = self.write_file_to_store(repo_path, disk_path)?; - Ok(TreeValue::File { id, executable }) + // 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 }) = + (current_tree_value, &file_type) + { + self.write_conflict_to_store(repo_path, disk_path, conflict_id, *executable) + } else { + match file_type { + FileType::Normal { executable } => { + let id = self.write_file_to_store(repo_path, disk_path)?; + Ok(TreeValue::File { id, executable }) + } + 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"), } - 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"), } }