about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2021-10-01 10:52:46 +0200
committerMel <einebeere@gmail.com>2021-10-01 10:52:46 +0200
commitc98563032c0d71ba83e1b77e5aab21c2a5f6a4c0 (patch)
tree83e28688af439b312ce7d7a878cd70b92942b3e4 /src
parent3f5c5026909af023c8a3885a4a3d97c669302c1b (diff)
downloadka-c98563032c0d71ba83e1b77e5aab21c2a5f6a4c0.tar.zst
ka-c98563032c0d71ba83e1b77e5aab21c2a5f6a4c0.zip
Only shift files which would be affected.
Diffstat (limited to 'src')
-rw-r--r--src/actions/shift.rs24
-rw-r--r--src/history.rs4
2 files changed, 26 insertions, 2 deletions
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<Vec<FileState>> = 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()?;
diff --git a/src/history.rs b/src/history.rs
index c474143..72640b6 100644
--- a/src/history.rs
+++ b/src/history.rs
@@ -34,6 +34,10 @@ impl RepositoryHistory {
         Ok(())
     }
 
+    pub fn get_changes(&self) -> &Vec<RepositoryChange> {
+        &self.changes
+    }
+
     pub fn add_change(&mut self, change: RepositoryChange) {
         self.changes.push(change);
     }