From dfcb2e217373c0fe856f34c3e153bc8790e1a21e Mon Sep 17 00:00:00 2001 From: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> Date: Sun, 1 Jun 2025 21:23:01 +0200 Subject: [PATCH] Added some tests Signed-off-by: alexis-opolka <53085471+alexis-opolka@users.noreply.github.com> --- src/commands/repo/add.rs | 32 ++++++++++++++++++-------- src/commands/repo/sync.rs | 4 ++-- src/common/git.rs | 48 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 12 deletions(-) diff --git a/src/commands/repo/add.rs b/src/commands/repo/add.rs index 1539c0b..93a452e 100644 --- a/src/commands/repo/add.rs +++ b/src/commands/repo/add.rs @@ -9,6 +9,7 @@ use crate::prelude::*; use std::fs; use std::path; use std::path::{MAIN_SEPARATOR, MAIN_SEPARATOR_STR}; +use tracing_subscriber::fmt::format; fn ask_if_should_import_all(finder: &FinderChoice) -> Result { let opts = FinderOpts { @@ -122,21 +123,25 @@ pub fn main(uri: String, yes_flag: bool, branch: &Option) -> Result<()> // We are copying the .git folder along with the cheat files // For more details, see: (https://github.com/denisidoro/navi/issues/733) for file in git_files.split('\n') { - let filename = format!("{}{}", &tmp_path_str, &file); + let file_path = format!("{}{}", &tmp_path_str, &file); let from = { let mut p = tmp_pathbuf.clone(); - p.push(&filename); - p - }; - let to = { - let mut p = to_folder.clone(); - p.push(file); + p.push(&file_path); p }; + + eprintln!("{file_path}"); - let path_str = &PathBuf::clone(&to).to_string(); + + let path_str = format!("{}{}{}", to_folder.to_string(), path::MAIN_SEPARATOR, &file_path); let local_collection = &path_str.split(MAIN_SEPARATOR).collect::>(); - let collection_str = local_collection[0..&local_collection.len() - 1].join(MAIN_SEPARATOR_STR); + let collection_str = if cfg!(windows) { + eprintln!("{:?}", local_collection); + + local_collection[1..&local_collection.len() - 1].join(MAIN_SEPARATOR_STR) + } else { + local_collection[0..&local_collection.len() - 1].join(MAIN_SEPARATOR_STR) + }; // This should be able to fix an issue with the clone on windows where both // to_folder and collection_str are equal @@ -156,12 +161,19 @@ pub fn main(uri: String, yes_flag: bool, branch: &Option) -> Result<()> format!( "{}{}", &to_folder.to_string(), - &to.to_string() + &collection_str ) } else { to_folder.to_string() }; + + eprintln!("=> (&to_folder.to_string() == &collection_str) = {}", &to_folder.to_string() == &collection_str); + eprintln!("=> To_folder: {}", &to_folder.to_string()); + eprintln!("=> Collection: {}", &collection_str); + eprintln!("=> local_to_folder: {}", &local_to_folder); + eprintln!("=> complete_local_path: {}", &complete_local_path); + debug!("=> {}", &complete_local_path); fs::create_dir_all(&local_to_folder).unwrap_or(()); diff --git a/src/commands/repo/sync.rs b/src/commands/repo/sync.rs index d42f211..d10e139 100644 --- a/src/commands/repo/sync.rs +++ b/src/commands/repo/sync.rs @@ -52,9 +52,9 @@ fn synchronize(cheat_repo: String) -> Result<()> { // 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)?; + let r = git::remote_update(&cheat_repo)?; - eprintln!("Status: {:?}", status.code()); + eprintln!("Status: {:?}", r.code()); return Ok(()); // We delete them since they are now out of tree diff --git a/src/common/git.rs b/src/common/git.rs index edc847c..c332bcd 100644 --- a/src/common/git.rs +++ b/src/common/git.rs @@ -112,6 +112,54 @@ pub fn diff(uri: &str) -> Result { .wait()?) } +pub fn remote_update(uri: &str) -> Result { + Ok(Command::new("git") + .current_dir(&uri) + .args(["remote", "update"]) + .spawn()? + .wait()? + ) +} + +/// Gets the local state of a git repository against its remote +/// +/// Return: an i8 integer with the following values: +/// - "-2" => There has been an error within this function +/// - "-1" => This repository is behind the remote +/// - "0" => This repository is the same as the remote +/// - "1" => This repository is ahead the remote +pub fn get_local_state(uri: &str) -> i8 { + + // We're updating the remote + let remote_update = Command::new("git") + .current_dir(&uri) + .args(["remote", "update"]) + .spawn().unwrap() + .wait().unwrap(); + + // If we couldn't update the remotes, we can't continue and compare them, + // we need to send an error and quit. + if ! remote_update.success() { + error!("Unable to update the remote (i.e. git remote update)"); + + return -2; + } + + let local_hash = Command::new("git") + .current_dir(&uri) + .args(["rev-parse", "@"]) + .spawn().unwrap().stdout.take().unwrap(); + + let remote_hash = Command::new("git") + .current_dir(&uri) + .args(["rev-parse", "\"@{u}\""]) + .spawn().unwrap().stdout.take().unwrap(); + + eprintln!("{:?} => {:?}", local_hash, remote_hash); + + -1 +} + #[cfg(test)] mod tests {