diff --git a/src/commands/repo/add.rs b/src/commands/repo/add.rs index 0c00a75..38fe898 100644 --- a/src/commands/repo/add.rs +++ b/src/commands/repo/add.rs @@ -5,6 +5,8 @@ use crate::finder::FinderChoice; use crate::prelude::*; use std::fs; use std::path; +use std::path::{MAIN_SEPARATOR, MAIN_SEPARATOR_STR}; + fn ask_if_should_import_all(finder: &FinderChoice) -> Result { let opts = FinderOpts { @@ -44,6 +46,7 @@ pub fn main(uri: String) -> Result<()> { .with_context(|| format!("Failed to clone `{actual_uri}`"))?; let all_files = filesystem::all_cheat_files(&tmp_pathbuf).join("\n"); + let git_files = filesystem::all_git_files(&tmp_pathbuf).join("\n"); let opts = FinderOpts { suggestion_type: SuggestionType::MultipleSelections, @@ -92,6 +95,30 @@ pub fn main(uri: String) -> Result<()> { .with_context(|| format!("Failed to copy `{}` to `{}`", &from.to_string(), &to.to_string()))?; } + // 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 from = { + let mut p = tmp_pathbuf.clone(); + p.push(&filename); + p + }; + let to = { + let mut p = to_folder.clone(); + p.push(&file); + p + }; + + let path_str = &PathBuf::clone(&to).to_string(); + let local_collection = &path_str.split(MAIN_SEPARATOR).collect::>(); + let local_to_folder = format!("{}{}", &to_folder.to_string(), local_collection[0..&local_collection.len()-1].join(MAIN_SEPARATOR_STR)); + + fs::create_dir_all(&local_to_folder).unwrap_or(()); + fs::copy(&from, format!("{}{}", &to_folder.clone().to_str().unwrap(), &to.clone().to_str().unwrap())) + .with_context(|| format!("Failed to copy `{}` to `{}`", &from.to_string(), &to.to_string()))?; + } + filesystem::remove_dir(&tmp_pathbuf)?; eprintln!( diff --git a/src/filesystem.rs b/src/filesystem.rs index 4d0b4e5..4fbfa77 100644 --- a/src/filesystem.rs +++ b/src/filesystem.rs @@ -29,6 +29,26 @@ pub fn all_cheat_files(path: &Path) -> Vec { .collect::>() } +pub fn all_git_files(path: &Path) -> Vec { + let mut path_str = path.to_str().unwrap().to_owned(); + if path_str.ends_with("/") { + // We're removing the trailing '/' at the end, if it exists + path_str.push_str("/"); + } + + WalkDir::new(path) + .follow_links(true) + .into_iter() + .filter_map(|e| e.ok()) + .map(|e| { + return if e.path().is_file() { + e.path().to_str().unwrap().replace(path_str.as_str(), "").to_string() + } else { "".to_string() }; + }) + .filter(|e| e.contains("/.git/")) + .collect::>() +} + fn paths_from_path_param(env_var: &str) -> impl Iterator { env_var.split(JOIN_SEPARATOR).filter(|folder| folder != &"") }