Skip to content
Snippets Groups Projects
Verified Commit 72965662 authored by nfontrod's avatar nfontrod
Browse files

src/clone.rs: major fixes in clone function

parent 592156bd
No related branches found
No related tags found
No related merge requests found
use std::path::PathBuf; use std::path::PathBuf;
use std::process::exit; use std::process::exit;
use std::process::Command;
use colored::Colorize; use colored::Colorize;
...@@ -36,6 +37,49 @@ fn check_remote_dir(url: &PathBuf, adress: &str) { ...@@ -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 /// Create a push the tar achive on the selected remote path
/// # Arguments /// # Arguments
/// - `remote`: The name of a remote /// - `remote`: The name of a remote
...@@ -44,25 +88,27 @@ fn check_remote_dir(url: &PathBuf, adress: &str) { ...@@ -44,25 +88,27 @@ fn check_remote_dir(url: &PathBuf, adress: &str) {
/// - `mode`: The mode used for checkout /// - `mode`: The mode used for checkout
pub fn clone(path: &str, hooks: bool, compression: &str, mode: &str) -> () { pub fn clone(path: &str, hooks: bool, compression: &str, mode: &str) -> () {
let (adress, url) = push::split_path(path); let (adress, url) = push::split_path(path);
let (borg_path, _) = init::get_borg_folder(); let borg_folder = get_borg_absolute_folder();
if borg_path.is_dir() { if borg_folder.is_dir() {
eprintln!("{}: {} already exits.", "error".red(), &borg_path.display()); eprintln!(
"{}: {} already exits.",
"error".red(),
&borg_folder.display()
);
exit(1); exit(1);
} }
let borg_folder = borg_path.canonicalize().unwrap();
check_remote_dir(&url, &adress); 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(); let name = url.file_name().unwrap().to_str().unwrap();
configt::create_named_local_config(name); configt::create_named_local_config(name);
let pf = url.parent().unwrap().to_path_buf(); let pf = url.parent().unwrap().to_path_buf();
let full_url = if adress != "file" { let full_url = if adress != "file" {
format!("{}:{}", &adress, &pf.display()) format!("{}:{}", &adress, &pf.display())
} else { } else {
url.to_str().unwrap().to_owned() pf.to_str().unwrap().to_owned()
}; };
remote::update_config("origin", &full_url, false); remote::update_config("origin", &full_url, false);
let gitignore = init::get_gitignore_file(); init::update_gitignores();
init::update_gitignore(&gitignore);
if hooks { if hooks {
create_hooks::create_hooks(compression, mode); create_hooks::create_hooks(compression, mode);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment