diff --git a/src/clone.rs b/src/clone.rs index 70749952803668e31ee6a3ab2a565bed02fc8cc1..e8a14e9a86cebee84c67b708f527f59db758af61 100644 --- a/src/clone.rs +++ b/src/clone.rs @@ -1,5 +1,6 @@ use std::path::PathBuf; use std::process::exit; +use std::process::Command; use colored::Colorize; @@ -36,6 +37,49 @@ fn check_remote_dir(url: &PathBuf, adress: &str) { } } +/// Function used to get borg repository folder only if +/// the project is inside a git repository and a results folder exits +/// # Return +/// Absolute path to borg repository +pub fn get_borg_absolute_folder() -> PathBuf { + let output = Command::new("git") + .arg("rev-parse") + .arg("--show-cdup") + .output() + .unwrap(); + match output.status.code().unwrap() { + 128 => { + eprintln!("Not a git repository !"); + exit(128); + } + 127 => { + eprintln!("Git not found !"); + exit(127); + } + 0 => (), + num => { + eprintln!("{}", String::from_utf8(output.stderr).unwrap()); + exit(num) + } + } + let mut string_path = String::from_utf8(output.stdout).unwrap(); + if string_path.ends_with('\n') { + string_path.pop(); + } + if string_path.is_empty() { + string_path.push_str("./"); + } + let mut p = PathBuf::from(&string_path).canonicalize().unwrap(); + let mut results = p.clone(); + p.push(".borg"); + results.push("results"); + if !results.is_dir() { + eprintln!("{} not found !", results.to_str().unwrap()); + exit(1); + } + p +} + /// Create a push the tar achive on the selected remote path /// # Arguments /// - `remote`: The name of a remote @@ -44,25 +88,27 @@ fn check_remote_dir(url: &PathBuf, adress: &str) { /// - `mode`: The mode used for checkout pub fn clone(path: &str, hooks: bool, compression: &str, mode: &str) -> () { let (adress, url) = push::split_path(path); - let (borg_path, _) = init::get_borg_folder(); - if borg_path.is_dir() { - eprintln!("{}: {} already exits.", "error".red(), &borg_path.display()); + let borg_folder = get_borg_absolute_folder(); + if borg_folder.is_dir() { + eprintln!( + "{}: {} already exits.", + "error".red(), + &borg_folder.display() + ); exit(1); } - let borg_folder = borg_path.canonicalize().unwrap(); check_remote_dir(&url, &adress); - push::copy_file(&borg_folder, &url, &adress, "pull"); + push::copy_file(&borg_folder, &url, &adress, "clone"); let name = url.file_name().unwrap().to_str().unwrap(); configt::create_named_local_config(name); let pf = url.parent().unwrap().to_path_buf(); let full_url = if adress != "file" { format!("{}:{}", &adress, &pf.display()) } else { - url.to_str().unwrap().to_owned() + pf.to_str().unwrap().to_owned() }; remote::update_config("origin", &full_url, false); - let gitignore = init::get_gitignore_file(); - init::update_gitignore(&gitignore); + init::update_gitignores(); if hooks { create_hooks::create_hooks(compression, mode); }