From f6d0bfbc46aca1e4bdb7ad33173e5f4b22ca8427 Mon Sep 17 00:00:00 2001 From: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> Date: Thu, 22 May 2025 23:05:35 +0200 Subject: [PATCH] Tries to fix the last issue of repo sync Signed-off-by: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> --- src/commands/repo/add.rs | 2 +- src/commands/repo/sync.rs | 36 +++++++++++++++++++++++++----------- src/common/git.rs | 20 +++++++++++++++----- 3 files changed, 41 insertions(+), 17 deletions(-) diff --git a/src/commands/repo/add.rs b/src/commands/repo/add.rs index 19833dc..7f4844b 100644 --- a/src/commands/repo/add.rs +++ b/src/commands/repo/add.rs @@ -148,7 +148,7 @@ pub fn main(uri: String, yes_flag: bool, branch: &Option) -> Result<()> &to.clone().to_str().unwrap() ); - eprintln!("=> {}", &complete_local_path); + debug!("=> {}", &complete_local_path); fs::create_dir_all(&local_to_folder).unwrap_or(()); fs::copy(&from, &complete_local_path).with_context(|| { diff --git a/src/commands/repo/sync.rs b/src/commands/repo/sync.rs index 2737a51..d42f211 100644 --- a/src/commands/repo/sync.rs +++ b/src/commands/repo/sync.rs @@ -49,15 +49,19 @@ fn synchronize(cheat_repo: String) -> Result<()> { let git_files = all_git_files(&cheat_path); let mut cheat_dirs: Vec = Vec::new(); + // Now that the folder has been cleaned, we need to fetch the latest HEAD available. + git::fetch_origin(&cheat_repo)?; + // git::pull(&cheat_repo)?; + let status = git::diff(&cheat_repo)?; + + eprintln!("Status: {:?}", status.code()); + return Ok(()); + // We delete them since they are now out of tree for file in cheat_files.iter() { fs::remove_file(&file)?; } - // Now that the folder has been cleaned, we need to fetch the latest HEAD available. - git::fetch_origin(&cheat_repo)?; - git::pull(&cheat_repo)?; - // Now that we've checkout the repository to the latest commit, // we might have a surplus of "illegal" files (i.e. files that should not be present in a cheatsheet repository). // @@ -119,16 +123,26 @@ fn synchronize(cheat_repo: String) -> Result<()> { fs::copy(cheat_file, format!("{}/{}", cheat_repo, filename))?; } + let mut last_path: String = "".to_string(); + for _dir in cheat_dirs { - eprintln!("DIR: {}", &_dir); + eprintln!("DIR: {}, {}", &_dir, &_dir.contains(&last_path)); - // TODO: Fix this loop trying to delete an already deleted path (i.e. the parent has been deleted) - match fs::exists(&_dir) { - Ok(_) => { - fs::remove_dir_all(&_dir)?; - } - Err(_) => { + if _dir.contains(&last_path) { + eprintln!("Found: {}", &_dir); + continue + } else { + // TODO: Fix this loop trying to delete an already deleted path (i.e. the parent has been deleted) + match fs::exists(&_dir) { + Ok(_) => { + fs::remove_dir_all(&_dir)?; + + last_path = _dir.to_owned(); + } + Err(_) => { + + } } } } diff --git a/src/common/git.rs b/src/common/git.rs index e3dad00..edc847c 100644 --- a/src/common/git.rs +++ b/src/common/git.rs @@ -1,6 +1,6 @@ use crate::common::shell::ShellSpawnError; use crate::prelude::*; -use std::process::Command; +use std::process::{Command, ExitStatus}; pub fn shallow_clone(uri: &str, target: &str, branch: &Option) -> Result<()> { // If we target a specific ref, we add the parameter inside the arguments to call @@ -87,22 +87,32 @@ pub fn fetch_origin(uri: &str) -> Result<()> { } /// Restores/Discards any local changes then pulls from the origin. -pub fn pull(uri: &str) -> Result<()> { +pub fn pull(uri: &str) -> Result { Command::new("git") .current_dir(&uri) .args(["restore", "./"]) .spawn()? .wait()?; - Command::new("git") + Ok(Command::new("git") .current_dir(uri) .args(["pull", "origin"]) .spawn()? .wait() - .expect("Unable to git pull"); - Ok(()) + .expect("Unable to git pull")) } +pub fn diff(uri: &str) -> Result { + eprintln!("git --git-dir=\"{}\" diff --quiet", &uri); + + Ok(Command::new("git") + .current_dir(&uri) + .args(["diff", "--quiet"]) + .spawn()? + .wait()?) +} + + #[cfg(test)] mod tests { use super::*;