diff --git a/README.md b/README.md index 03a5475..7b3dd1a 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,6 @@ This script automates the process of backing up your Obsidian vault to a Git rep - [x] Trigger backup after file changes with delay - [x] Maintain git repo in a seperate folder to not have the repo synced by syncthing (copy changed files over) - [x] Push changes to remote repository -- [ ] Some error management - [x] Tray Menu - [x] Exit - [x] Backup now diff --git a/src/gitpush.rs b/src/git.rs similarity index 82% rename from src/gitpush.rs rename to src/git.rs index 5f334d8..054d9c3 100644 --- a/src/gitpush.rs +++ b/src/git.rs @@ -1,15 +1,25 @@ use chrono::Local; -use git2::{Cred, IndexAddOption, PushOptions, RemoteCallbacks, Repository, StatusOptions}; +use git2::{Cred, Error, IndexAddOption, PushOptions, RemoteCallbacks, Repository, StatusOptions}; use keyring::Entry; use std::path::Path; -pub fn backup_changes(repo_path: &Path) -> Result<(), Box> { - let start = std::time::Instant::now(); - +pub fn current_change_count(repo_path: &Path) -> Result { // Open the repository at the provided path let repo = Repository::open(repo_path)?; - // Gather the repository statuses, including untracked files. + // Gather the repository statuses, including untracked files + let mut status_opts = StatusOptions::new(); + status_opts.include_untracked(true); + let statuses = repo.statuses(Some(&mut status_opts))?; + + Ok(statuses.len()) +} + +pub fn backup_changes(repo_path: &Path) -> Result<(), Box> { + // Open the repository at the provided path + let repo = Repository::open(repo_path)?; + + // Gather the repository statuses, including untracked files let mut status_opts = StatusOptions::new(); status_opts.include_untracked(true); let statuses = repo.statuses(Some(&mut status_opts))?; @@ -17,11 +27,10 @@ pub fn backup_changes(repo_path: &Path) -> Result<(), Box // If no changes are detected, exit early if statuses.is_empty() { println!("No changes detected"); - println!("Backup took {:?}", start.elapsed()); return Ok(()); } - // Print out all changes. + // Print out all changes println!("Detected changes:"); for entry in statuses.iter() { let path = entry.path().unwrap_or(""); @@ -72,7 +81,6 @@ pub fn backup_changes(repo_path: &Path) -> Result<(), Box remote.push(&["refs/heads/main"], Some(&mut push_options))?; println!("Changes committed and pushed successfully."); - println!("Backup took {:?}", start.elapsed()); Ok(()) } diff --git a/src/main.rs b/src/main.rs index cb647b1..28e8382 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,7 @@ use winit::{ }; mod filesync; -mod gitpush; +mod git; const SOURCE_VAULT: &str = "X:\\test\\obsidian-vault-source"; // syncthing vault folder const REPO_VAULT: &str = "X:\\test\\obsidian-vault-test"; // git repo folder @@ -31,6 +31,7 @@ struct App { menu_exit_id: Option, last_change_time: Option, duration_threshold: Duration, + current_change_count: usize, } impl ApplicationHandler for App { @@ -70,7 +71,8 @@ impl ApplicationHandler for App { // Handle push now menu item self.last_change_time = None; let repo_dir = Path::new(REPO_VAULT); - gitpush::backup_changes(repo_dir).expect("Changes could not be pushed"); + git::backup_changes(repo_dir).expect("Changes could not be pushed"); + self.current_change_count = 0; } id if id == self.menu_exit_id.as_ref().unwrap() => { println!("---> Exit triggert by menu"); @@ -85,10 +87,11 @@ impl ApplicationHandler for App { self.last_change_time = Some(Instant::now()); let source = Path::new(SOURCE_VAULT); let target = Path::new(REPO_VAULT); - let start = std::time::Instant::now(); filesync::sync_directory(source, target, Some(".git")) .expect("Directories could not be synced"); - println!("Sync took {:?}", start.elapsed()); + self.current_change_count = git::current_change_count(target) + .expect("Changes could not get counted using git"); + println!("Currently changed files: {:?}", self.current_change_count); } } } @@ -102,7 +105,8 @@ impl ApplicationHandler for App { println!("---> Backup triggered by file change"); self.last_change_time = None; let repo_dir = Path::new(REPO_VAULT); - gitpush::backup_changes(repo_dir).expect("Changes could not be pushed"); + git::backup_changes(repo_dir).expect("Changes could not be pushed"); + self.current_change_count = 0; } } @@ -123,11 +127,9 @@ fn main() { // Sync folder and check for changes to back up let source_dir = Path::new(SOURCE_VAULT); let repo_dir = Path::new(REPO_VAULT); - let start = std::time::Instant::now(); filesync::sync_directory(source_dir, repo_dir, Some(".git")) .expect("Directories could not be synced"); - println!("Sync took {:?}", start.elapsed()); - gitpush::backup_changes(repo_dir).expect("Changes could not be pushed"); + git::backup_changes(repo_dir).expect("Changes could not be pushed"); // Create event loop let event_loop = EventLoop::::with_user_event().build().unwrap();