about summary refs log tree commit diff
path: root/src/actions
diff options
context:
space:
mode:
authorMel <einebeere@gmail.com>2021-10-02 19:55:01 +0200
committerMel <einebeere@gmail.com>2021-10-02 19:55:01 +0200
commit55da5ae8cf731c768d6cc580693068e64234a4af (patch)
tree9024bf8c315f1c150a468db6791acbeec2907601 /src/actions
parentc1eccadf76ca7625d8cff9a52480168aab876e62 (diff)
downloadka-55da5ae8cf731c768d6cc580693068e64234a4af.tar.zst
ka-55da5ae8cf731c768d6cc580693068e64234a4af.zip
Use filesystem abstraction throughout Ka.
Diffstat (limited to 'src/actions')
-rw-r--r--src/actions/create.rs25
-rw-r--r--src/actions/shift.rs40
-rw-r--r--src/actions/update.rs53
3 files changed, 50 insertions, 68 deletions
diff --git a/src/actions/create.rs b/src/actions/create.rs
index 472cbe3..20dbff7 100644
--- a/src/actions/create.rs
+++ b/src/actions/create.rs
@@ -1,26 +1,23 @@
-use std::fs::{self, File};
-
+use crate::{actions::update, files::Locations, filesystem::Fs, history::RepositoryHistory};
 use anyhow::Result;
 
-use crate::{actions::update, files::Locations, history::RepositoryHistory};
-
 use super::ActionOptions;
 
-pub fn create(command_options: ActionOptions) -> Result<()> {
+pub fn create(command_options: ActionOptions, fs: &mut impl Fs) -> Result<()> {
     let locations = Locations::from(&command_options);
+    // FIXME: Re-add old repository deletion.
+    // if locations.ka_path.exists() {
+    //     fs::remove_dir_all(locations.ka_path.as_path())?;
+    // }
 
-    if locations.ka_path.exists() {
-        fs::remove_dir_all(locations.ka_path.as_path())?;
-    }
-
-    fs::create_dir(&locations.ka_path)?;
-    fs::create_dir(&locations.ka_files_path)?;
+    // fs::create_dir(&locations.ka_path)?;
+    // fs::create_dir(&locations.ka_files_path)?;
 
-    let mut index_file = File::create(locations.get_repository_index())?;
+    let mut index_file = fs.create_file(&locations.get_repository_index_path())?;
     let empty_history = RepositoryHistory::default();
-    empty_history.write_to_file(&mut index_file)?;
+    empty_history.write_to_file(fs, &mut index_file)?;
 
-    update(command_options)?;
+    update(command_options, fs)?;
 
     Ok(())
 }
diff --git a/src/actions/shift.rs b/src/actions/shift.rs
index 94b8914..8f37874 100644
--- a/src/actions/shift.rs
+++ b/src/actions/shift.rs
@@ -1,29 +1,26 @@
-use std::{
-    collections::HashSet,
-    fs::{self, OpenOptions},
-    io::{Seek, Write},
-};
+use std::collections::HashSet;
 
 use anyhow::Result;
 
 use crate::{
     files::{FileState, Locations},
+    filesystem::Fs,
     history::{FileHistory, RepositoryHistory},
 };
 
 use super::ActionOptions;
 
-pub fn shift(command_options: ActionOptions, new_cursor: usize) -> Result<()> {
+pub fn shift(command_options: ActionOptions, fs: &mut impl Fs, new_cursor: usize) -> Result<()> {
     let locations = Locations::from(&command_options);
 
-    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 repository_index_path = locations.get_repository_index_path();
+    let mut repository_index_file = fs.open_writable_file(&repository_index_path)?;
+    let mut repository_history = RepositoryHistory::from_file(fs, &mut repository_index_file)?;
 
     let old_cursor = repository_history.cursor;
 
     repository_history.cursor = new_cursor;
-    repository_history.write_to_file(&mut repository_index_file)?;
+    repository_history.write_to_file(fs, &mut repository_index_file)?;
 
     let changes_between_cursors = if old_cursor < new_cursor {
         old_cursor..new_cursor
@@ -47,32 +44,27 @@ pub fn shift(command_options: ActionOptions, new_cursor: usize) -> Result<()> {
     for state in affected_files_by_shift? {
         match state {
             FileState::Tracked(tracked) => {
-                let mut history_file = tracked.load_history_file()?;
+                let mut history_file = tracked.load_history_file(fs)?;
 
-                let file_history = FileHistory::from_file(&mut history_file)?;
+                let file_history = FileHistory::from_file(fs, &mut history_file)?;
 
                 if file_history.is_file_deleted(new_cursor) {
-                    fs::remove_file(tracked.working_path)?;
+                    fs.delete_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)?;
+                    let mut working_file = tracked.create_working_file(fs)?;
+                    fs.write_to_file(&mut working_file, new_content)?;
                 }
             }
             FileState::Deleted(deleted) => {
-                let mut history_file = deleted.load_history_file()?;
+                let mut history_file = deleted.load_history_file(fs)?;
 
-                let file_history = FileHistory::from_file(&mut history_file)?;
+                let file_history = FileHistory::from_file(fs, &mut history_file)?;
 
                 if !file_history.is_file_deleted(new_cursor) {
-                    let mut new_working_file = deleted.create_working_file(&locations)?;
+                    let mut new_working_file = deleted.create_working_file(fs, &locations)?;
                     let new_content = file_history.get_content(new_cursor);
-
-                    new_working_file.write_all(&new_content)?;
+                    fs.write_to_file(&mut new_working_file, new_content)?;
                 }
             }
             // TODO: What do we do with untracked files on a shift? Delete them?
diff --git a/src/actions/update.rs b/src/actions/update.rs
index a9bc0d2..8201862 100644
--- a/src/actions/update.rs
+++ b/src/actions/update.rs
@@ -1,43 +1,38 @@
-use std::{
-    fs::{File, OpenOptions},
-    io::Read,
-    time::SystemTime,
-};
+use std::time::SystemTime;
 
 use anyhow::{Context, Result};
 
 use crate::{
     diff::ContentChange,
     files::{FileState, Locations},
+    filesystem::Fs,
     history::{FileChange, FileChangeVariant, FileHistory, RepositoryChange, RepositoryHistory},
 };
 
 use super::ActionOptions;
 
-pub fn update(command_options: ActionOptions) -> Result<()> {
+pub fn update(command_options: ActionOptions, fs: &mut impl Fs) -> Result<()> {
     let timestamp = SystemTime::now()
         .duration_since(SystemTime::UNIX_EPOCH)?
         .as_secs();
 
     let locations = Locations::from(&command_options);
 
-    let repository_index_path = locations.get_repository_index();
-    let mut repository_index_file = OpenOptions::new()
-        .read(true)
-        .write(true)
-        .open(repository_index_path)?;
-    let mut repository_history = RepositoryHistory::from_file(&mut repository_index_file)?;
+    let repository_index_path = locations.get_repository_index_path();
+    let mut repository_index_file = fs.open_writable_file(&repository_index_path)?;
+    let mut repository_history = RepositoryHistory::from_file(fs, &mut repository_index_file)?;
 
     let entries = locations
-        .get_repository_files()
+        .get_repository_files(fs)
         .context("Could not traverse files.")?;
 
     let mut affected_files = Vec::new();
 
     for state in entries {
-        let changed_file = get_new_history_for_file(repository_history.cursor, &state, &locations)?;
+        let changed_file =
+            get_new_history_for_file(fs, repository_history.cursor, &state, &locations)?;
         if let Some((mut history_file, new_file_history)) = changed_file {
-            new_file_history.write_to_file(&mut history_file)?;
+            new_file_history.write_to_file(fs, &mut history_file)?;
             affected_files.push(state.get_working_path(&locations)?);
         }
     }
@@ -49,21 +44,22 @@ pub fn update(command_options: ActionOptions) -> Result<()> {
         });
         repository_history.cursor += 1;
 
-        repository_history.write_to_file(&mut repository_index_file)?;
+        repository_history.write_to_file(fs, &mut repository_index_file)?;
     }
 
     Ok(())
 }
 
-fn get_new_history_for_file(
+fn get_new_history_for_file<FS: Fs>(
+    fs: &mut FS,
     cursor: usize,
     file_state: &FileState,
     locations: &Locations,
-) -> Result<Option<(File, FileHistory)>> {
+) -> Result<Option<(FS::File, FileHistory)>> {
     match file_state {
         FileState::Deleted(deleted) => {
-            let mut history_file = deleted.load_history_file()?;
-            let file_history = FileHistory::from_file(&mut history_file)?;
+            let mut history_file = deleted.load_history_file(fs)?;
+            let file_history = FileHistory::from_file(fs, &mut history_file)?;
             if !file_history.is_file_deleted(cursor) {
                 let mut new_history = file_history;
                 new_history.add_change(FileChange {
@@ -76,10 +72,9 @@ fn get_new_history_for_file(
             }
         }
         FileState::Untracked(untracked) => {
-            let mut file = untracked.load_file()?;
+            let mut file = untracked.load_file(fs)?;
 
-            let mut file_content = Vec::new();
-            file.read_to_end(&mut file_content)?;
+            let file_content = fs.read_from_file(&mut file)?;
 
             let change = FileChange {
                 change_index: cursor + 1,
@@ -93,19 +88,17 @@ fn get_new_history_for_file(
             new_history.add_change(change);
 
             Ok(Some((
-                untracked.create_history_file(locations)?,
+                untracked.create_history_file(fs, locations)?,
                 new_history,
             )))
         }
         FileState::Tracked(tracked) => {
-            let mut history_file = tracked.load_history_file()?;
-            let mut working_file = tracked.load_working_file()?;
-
-            let file_history = FileHistory::from_file(&mut history_file)?;
+            let mut history_file = tracked.load_history_file(fs)?;
+            let mut working_file = tracked.load_working_file(fs)?;
 
-            let mut new_content = Vec::new();
-            working_file.read_to_end(&mut new_content)?;
+            let file_history = FileHistory::from_file(fs, &mut history_file)?;
 
+            let new_content = fs.read_from_file(&mut working_file)?;
             let old_content = file_history.get_content(cursor);
 
             let changes = ContentChange::diff(&old_content, &new_content);