about summary refs log tree commit diff
path: root/src/actions/shift.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/actions/shift.rs')
-rw-r--r--src/actions/shift.rs67
1 files changed, 37 insertions, 30 deletions
diff --git a/src/actions/shift.rs b/src/actions/shift.rs
index 6880c20..0fbc233 100644
--- a/src/actions/shift.rs
+++ b/src/actions/shift.rs
@@ -1,56 +1,63 @@
 use std::{
-    fs,
+    fs::{self, OpenOptions},
     io::{Seek, Write},
-    path::Path,
 };
 
 use anyhow::Result;
 
 use crate::{
-    files::{Locations, FileState},
-    history::FileHistory,
+    files::{FileState, Locations},
+    history::{FileHistory, RepositoryHistory},
 };
 
 use super::ActionOptions;
 
-pub fn shift(command_options: ActionOptions, path: &str, new_cursor: usize) -> Result<()> {
+pub fn shift(command_options: ActionOptions, new_cursor: usize) -> Result<()> {
     let locations = Locations::from(&command_options);
 
-    match FileState::from_working(&locations, Path::new(path))? {
-        FileState::Tracked(tracked) => {
-            let mut history_file = tracked.load_history_file()?;
+    let repository_index_path = locations.get_repository_index();
+    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 mut file_history = FileHistory::from_file(&mut history_file)?;
-            file_history.set_cursor(new_cursor);
+    repository_history.cursor = new_cursor;
+    repository_history.write_to_file(&mut repository_index_file)?;
 
-            if file_history.file_is_deleted() {
-                fs::remove_file(path)?;
-            } else {
-                let new_content = file_history.get_content();
-                let mut working_file = tracked.create_working_file()?;
+    // TOOD: Only shift files which would be affected by cursor change
 
-                working_file.rewind()?;
-                working_file.set_len(0)?;
+    for state in locations.get_repository_files()? {
+        match state {
+            FileState::Tracked(tracked) => {
+                let mut history_file = tracked.load_history_file()?;
 
-                working_file.write_all(&new_content)?;
-            }
+                let file_history = FileHistory::from_file(&mut history_file)?;
 
-            file_history.write_to_file(&mut history_file)?;
-        }
-        FileState::Deleted(deleted) => {
-            let mut history_file = deleted.load_history_file()?;
+                if file_history.is_file_deleted(new_cursor) {
+                    fs::remove_file(tracked.working_path)?;
+                } else {
+                    let new_content = file_history.get_content(new_cursor);
+                    let mut working_file = tracked.create_working_file()?;
+
+                    working_file.rewind()?;
+                    working_file.set_len(0)?;
+
+                    working_file.write_all(&new_content)?;
+                }
+            }
+            FileState::Deleted(deleted) => {
+                let mut history_file = deleted.load_history_file()?;
 
-            let mut file_history = FileHistory::from_file(&mut history_file)?;
-            file_history.set_cursor(new_cursor);
+                let file_history = FileHistory::from_file(&mut history_file)?;
 
-            if !file_history.file_is_deleted() {
-                let mut new_working_file = deleted.create_working_file(&locations)?;
-                let new_content = file_history.get_content();
+                if !file_history.is_file_deleted(new_cursor) {
+                    let mut new_working_file = deleted.create_working_file(&locations)?;
+                    let new_content = file_history.get_content(new_cursor);
 
-                new_working_file.write_all(&new_content)?;
+                    new_working_file.write_all(&new_content)?;
+                }
             }
+            // TODO: What do we do with untracked files on a shift? Delete them?
+            _ => (),
         }
-        FileState::Untracked(_) => panic!("File is not tracked with Ka."),
     }
 
     Ok(())