Add variable to keep track of changed files to be commited

This commit is contained in:
Robin Gottschalk 2025-02-02 20:37:32 +01:00
parent aa9209b7d6
commit bf70e1b7f9
3 changed files with 26 additions and 17 deletions

View File

@ -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

View File

@ -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<dyn std::error::Error>> {
let start = std::time::Instant::now();
pub fn current_change_count(repo_path: &Path) -> Result<usize, Error> {
// 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<dyn std::error::Error>> {
// 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<dyn std::error::Error>
// 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("<unknown>");
@ -72,7 +81,6 @@ pub fn backup_changes(repo_path: &Path) -> Result<(), Box<dyn std::error::Error>
remote.push(&["refs/heads/main"], Some(&mut push_options))?;
println!("Changes committed and pushed successfully.");
println!("Backup took {:?}", start.elapsed());
Ok(())
}

View File

@ -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<MenuId>,
last_change_time: Option<Instant>,
duration_threshold: Duration,
current_change_count: usize,
}
impl ApplicationHandler<UserEvent> for App {
@ -70,7 +71,8 @@ impl ApplicationHandler<UserEvent> 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<UserEvent> 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<UserEvent> 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::<UserEvent>::with_user_event().build().unwrap();