From 55da5ae8cf731c768d6cc580693068e64234a4af Mon Sep 17 00:00:00 2001 From: Mel Date: Sat, 2 Oct 2021 19:55:01 +0200 Subject: Use filesystem abstraction throughout Ka. --- src/actions/create.rs | 25 +++++++++++------------- src/actions/shift.rs | 40 ++++++++++++++++---------------------- src/actions/update.rs | 53 ++++++++++++++++++++++----------------------------- 3 files changed, 50 insertions(+), 68 deletions(-) (limited to 'src/actions') 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: &mut FS, cursor: usize, file_state: &FileState, locations: &Locations, -) -> Result> { +) -> Result> { 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); -- cgit 1.4.1