diff --git a/src/commands/add.rs b/src/commands/add.rs index 0ab935b..a7bcfd0 100644 --- a/src/commands/add.rs +++ b/src/commands/add.rs @@ -4,6 +4,7 @@ use clap::Values; use glob::glob; use crate::store::{self, object::Object}; use crate::utils::{self, path}; +use crate::store::object::object::{Obj, ObjMethods}; use crate::utils::nextsyncignore::{self, ignore_file}; use crate::utils::path::{normalize_relative, repo_root, path_buf_to_string}; @@ -73,6 +74,9 @@ fn add_entry(entry: PathBuf, force: bool, added_files: &mut Vec, rules: return; } + // add the parent if there is one and it is not already created + add_parent(entry.clone(), added_files); + added_files.push(path_buf_to_string(entry.strip_prefix(repo_root()).unwrap().to_path_buf())); if entry.is_dir() { add_folder_content(entry.to_path_buf(), added_files); @@ -80,6 +84,20 @@ fn add_entry(entry: PathBuf, force: bool, added_files: &mut Vec, rules: } +fn add_parent(entry: PathBuf, added_files: &mut Vec) { + let test_parent = entry.strip_prefix(repo_root()).unwrap().parent(); + if test_parent.is_none() || test_parent.unwrap() == PathBuf::new() { + return; + } + + let parent = entry.parent().unwrap(); + + if !Obj::from_path(parent).exists_on_remote() { + add_parent(parent.to_path_buf(), added_files); + added_files.push(path_buf_to_string(parent.strip_prefix(repo_root()).unwrap().to_path_buf())); + } +} + fn print_ignored_files(ignored_files: Vec) { if ignored_files.len() > 0 { // todo multiple nextsyncignore diff --git a/src/store/object/object.rs b/src/store/object/object.rs index 315d2db..c7b20eb 100644 --- a/src/store/object/object.rs +++ b/src/store/object/object.rs @@ -31,6 +31,7 @@ pub trait ObjMethods { fn rm(&mut self) -> io::Result<()>; fn rm_node(&mut self) -> io::Result<()>; fn rm_node_down(&mut self) -> io::Result<()>; + fn exists_on_remote(&mut self) -> bool; } pub struct Obj { @@ -126,6 +127,10 @@ impl ObjMethods for Obj { eprintln!("rm: tried to do this on Obj"); Ok(()) } + + fn exists_on_remote(&mut self) -> bool { + PathBuf::from(self.get_hash_path()).exists() + } } impl ObjMethods for Blob { @@ -187,6 +192,10 @@ impl ObjMethods for Blob { fs::remove_file(self.get_file_path())?; Ok(()) } + + fn exists_on_remote(&mut self) -> bool { + self.obj.exists_on_remote() + } } impl ObjMethods for Tree { @@ -258,6 +267,10 @@ impl ObjMethods for Tree { fs::remove_dir_all(self.get_file_path())?; Ok(()) } + + fn exists_on_remote(&mut self) -> bool { + self.obj.exists_on_remote() + } } impl Obj {