From c98563032c0d71ba83e1b77e5aab21c2a5f6a4c0 Mon Sep 17 00:00:00 2001 From: Mel Date: Fri, 1 Oct 2021 10:52:46 +0200 Subject: Only shift files which would be affected. --- src/actions/shift.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'src/actions/shift.rs') diff --git a/src/actions/shift.rs b/src/actions/shift.rs index 0fbc233..94b8914 100644 --- a/src/actions/shift.rs +++ b/src/actions/shift.rs @@ -1,4 +1,5 @@ use std::{ + collections::HashSet, fs::{self, OpenOptions}, io::{Seek, Write}, }; @@ -19,12 +20,31 @@ pub fn shift(command_options: ActionOptions, new_cursor: usize) -> Result<()> { let mut repository_index_file = OpenOptions::new().write(true).open(repository_index_path)?; let mut repository_history = RepositoryHistory::from_file(&mut repository_index_file)?; + let old_cursor = repository_history.cursor; + repository_history.cursor = new_cursor; repository_history.write_to_file(&mut repository_index_file)?; - // TOOD: Only shift files which would be affected by cursor change + let changes_between_cursors = if old_cursor < new_cursor { + old_cursor..new_cursor + } else { + new_cursor..old_cursor + }; + + let affected_files_by_shift: Result> = repository_history.get_changes() + [changes_between_cursors] + .iter() + .fold(HashSet::new(), |mut acc, change| { + for path in change.affected_files.iter() { + acc.insert(path); + } + acc + }) + .iter() + .map(|path| FileState::from_working(&locations, path)) + .collect(); - for state in locations.get_repository_files()? { + for state in affected_files_by_shift? { match state { FileState::Tracked(tracked) => { let mut history_file = tracked.load_history_file()?; -- cgit 1.4.1