save tree objects when cloning

This commit is contained in:
grimhilt
2023-06-05 23:08:11 +02:00
parent 2117ce3016
commit fe990e4213
7 changed files with 150 additions and 7 deletions

View File

@@ -10,11 +10,12 @@ use xml::reader::{EventReader, XmlEvent};
use crate::services::api::ApiError;
use crate::services::list_folders::ListFolders;
use crate::services::download_files::DownloadFiles;
use crate::utils::object;
use crate::commands;
pub fn clone(remote: Values<'_>) {
let url = remote.clone().next().unwrap();
let (domain, tmp_user, path_str) = get_url_props(url);
let (domain, tmp_user, path_str) = get_url_props(url);
let path = Path::new(path_str);
let username = match tmp_user {
Some(u) => u,
@@ -38,19 +39,21 @@ pub fn clone(remote: Values<'_>) {
}
url_request.push_str(folder.as_str());
// request folder content
let mut body = Default::default();
tokio::runtime::Runtime::new().unwrap().block_on(async {
body = ListFolders::new(url_request.as_str())
.send_with_res()
.await;
});
// create folder
if first_iter {
first_iter = false;
if DirBuilder::new().create(path.file_name().unwrap()).is_err() {
// todo add second parameter to save in a folder
eprintln!("fatal: directory already exist");
// destination path 'path' already exists and is not an empty directory.
std::process::exit(1);
//std::process::exit(1);
} else {
commands::init::init(Some(env::current_dir().unwrap().to_str().unwrap()));
}
@@ -60,6 +63,17 @@ pub fn clone(remote: Values<'_>) {
DirBuilder::new().recursive(true).create(path.unwrap());
}
// add folder to the structure
if !first_iter {
let mut path_folder = Path::new(&folder).strip_prefix("/remote.php/dav/files/");
path_folder = path_folder.unwrap().strip_prefix(username);
let mut root = path_folder.clone().unwrap().iter();
let o = root.next();
path_folder = path_folder.unwrap().strip_prefix(o.unwrap());
object::add_tree(&path_folder.unwrap());
}
// find folders and files in response
let objects = get_objects_xml(body);
let mut iter = objects.iter();
iter.next(); // jump first element which the folder fetched
@@ -70,6 +84,7 @@ pub fn clone(remote: Values<'_>) {
files.push(object.to_string());
}
}
first_iter = false;
}
download_files(&domain, username, files);

View File

@@ -1,6 +1,5 @@
use std::fs::{DirBuilder, File};
use std::path::PathBuf;
use clap::Values;
use std::env;
pub fn init(directory: Option<&str>) {
@@ -18,6 +17,13 @@ pub fn init(directory: Option<&str>) {
Err(_) => println!("Error: cannot create directory"),
};
path.push("objects");
match builder.create(path.clone()) {
Ok(()) => println!("Directory successfuly created"),
Err(_) => println!("Error: cannot create directory"),
};
path.pop();
path.push("HEAD");
match File::create(path.clone()) {
Ok(_) => println!("File successfuly created"),

View File

@@ -107,8 +107,6 @@ fn find_missing_elements(hashes: &mut HashSet<String>, objects: &mut Vec<String>
// find it on the list of hashes
if hashes.contains(&hash) {
println!("o");
println!("{}", object.clone());
duplicate.push(object.clone());
if remove_option == RemoveSide::Left || remove_option == RemoveSide::Both {
hashes.remove(&hash);