Skip to content

Commit

Permalink
refactor(working_copy): return new file state from update_file_state
Browse files Browse the repository at this point in the history
  • Loading branch information
arxanas authored and martinvonz committed Jul 28, 2023
1 parent c409f4e commit 9d8702b
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions lib/src/working_copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,10 @@ pub enum TreeStateError {
}

enum UpdatedFileState {
Unchanged,
Ignored,
Unchanged(FileState),
Deleted,
Changed(TreeValue),
Changed(TreeValue, FileState),
}

impl TreeState {
Expand Down Expand Up @@ -692,18 +693,23 @@ impl TreeState {
if let Some(progress) = progress {
progress(&sub_path);
}
let update = self.update_file_state(
let update = self.get_updated_file_state(
&sub_path,
&entry,
git_ignore.as_ref(),
&current_tree,
)?;
match update {
UpdatedFileState::Unchanged => {}
UpdatedFileState::Ignored => {}
UpdatedFileState::Unchanged(file_state) => {
self.file_states.insert(sub_path, file_state);
}
UpdatedFileState::Deleted => {
self.file_states.remove(&sub_path);
tree_builder.remove(sub_path);
}
UpdatedFileState::Changed(tree_value) => {
UpdatedFileState::Changed(tree_value, file_state) => {
self.file_states.insert(sub_path.clone(), file_state);
tree_builder.set(sub_path, tree_value);
}
}
Expand Down Expand Up @@ -802,20 +808,20 @@ impl TreeState {
}
}

fn update_file_state(
&mut self,
fn get_updated_file_state(
&self,
repo_path: &RepoPath,
dir_entry: &DirEntry,
git_ignore: &GitIgnoreFile,
current_tree: &Tree,
) -> Result<UpdatedFileState, SnapshotError> {
let maybe_current_file_state = self.file_states.get_mut(repo_path);
let maybe_current_file_state = self.file_states.get(repo_path);
if maybe_current_file_state.is_none()
&& git_ignore.matches_file(&repo_path.to_internal_file_string())
{
// If it wasn't already tracked and it matches the ignored paths, then
// ignore it.
return Ok(UpdatedFileState::Unchanged);
return Ok(UpdatedFileState::Ignored);
}

let disk_path = dir_entry.path();
Expand All @@ -827,16 +833,13 @@ impl TreeState {
let (current_file_state, new_file_state) =
match (maybe_current_file_state, maybe_new_file_state) {
(_, None) => {
// Tracked file replaced by Unix socket or such
self.file_states.remove(repo_path);
return Ok(UpdatedFileState::Deleted);
}
(None, Some(new_file_state)) => {
// untracked
let file_type = new_file_state.file_type.clone();
self.file_states.insert(repo_path.clone(), new_file_state);
let file_value = self.write_path_to_store(repo_path, &disk_path, file_type)?;
return Ok(UpdatedFileState::Changed(file_value));
return Ok(UpdatedFileState::Changed(file_value, new_file_state));
}
(Some(current_file_state), Some(new_file_state)) => {
(current_file_state, new_file_state)
Expand All @@ -855,7 +858,6 @@ impl TreeState {
// then we don't know if the file was modified before or after this state file.
if current_file_state != &new_file_state || current_file_state.mtime >= self.own_mtime {
let new_file_type = new_file_state.file_type.clone();
*current_file_state = new_file_state;
let current_tree_value = current_tree.path_value(repo_path);
// 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.
Expand All @@ -867,9 +869,9 @@ impl TreeState {
} else {
self.write_path_to_store(repo_path, &disk_path, new_file_type)?
};
return Ok(UpdatedFileState::Changed(new_tree_value));
return Ok(UpdatedFileState::Changed(new_tree_value, new_file_state));
}
Ok(UpdatedFileState::Unchanged)
Ok(UpdatedFileState::Unchanged(new_file_state))
}

fn write_conflict_to_store(
Expand Down

0 comments on commit 9d8702b

Please sign in to comment.