From f10043a4009b0067fe85a1663b96e6649c4ba44f Mon Sep 17 00:00:00 2001 From: Petr Gladkikh Date: Fri, 10 Nov 2023 17:53:53 +0100 Subject: [PATCH] Correct track opening procedure; Initialization cleanup Avoid unnecessary version increases each time the app starts. --- src/project.rs | 7 +++---- src/track_history.rs | 7 ++++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/project.rs b/src/project.rs index 1d2142d..dabbd91 100644 --- a/src/project.rs +++ b/src/project.rs @@ -26,13 +26,12 @@ impl Project { let mut snapshots_dir = directory.clone(); snapshots_dir.push(Self::SNAPSHOTS_DIR_NAME); - let mut history = if snapshots_dir.is_dir() { - TrackHistory::with_directory(&snapshots_dir) - } else { + let mut history = TrackHistory::with_directory(&snapshots_dir); + if !snapshots_dir.is_dir() { fs::create_dir_all(&snapshots_dir).expect( format!("create project directory {:?}", directory.to_string_lossy()).as_str(), ); - TrackHistory::with_directory(&snapshots_dir).init(&source_file) + history = history.init(&source_file) }; history.open(); Project { diff --git a/src/track_history.rs b/src/track_history.rs index 0129fc9..90e8c80 100644 --- a/src/track_history.rs +++ b/src/track_history.rs @@ -90,8 +90,8 @@ impl TrackHistory { let track_version = self.with_track(|t| t.version); if track_version != self.track_version { self.push(); - // It is possible co keep these, but that would complicate implementation, and UI. self.discard_tail(); + self.track_version = track_version; } } @@ -182,7 +182,6 @@ impl TrackHistory { pub fn with_directory(directory: &PathBuf) -> Self { dbg!("history directory", directory.to_string_lossy()); - Self::check_directory_writable(directory); Self { directory: directory.to_owned(), version: 0, @@ -212,11 +211,13 @@ impl TrackHistory { } pub fn open(&mut self) { + Self::check_directory_writable(&self.directory); dbg!(self.list_revisions().collect::>()); if let Some(meta) = self.load_meta() { self.version = meta.current_version; let file_path = self.current_snapshot_path(); - self.update_track(None, |track| track.load_from(&file_path)); + let mut track = self.track.write().expect("Write to track."); + track.load_from(&file_path); } else { panic!("Cannot load revision history metadata."); }