fix(add): add directory implicitly

This commit is contained in:
grimhilt 2024-03-10 17:29:37 +01:00
parent fe628ffc9f
commit 34dee1ceb6
2 changed files with 31 additions and 0 deletions

View File

@ -4,6 +4,7 @@ use clap::Values;
use glob::glob; use glob::glob;
use crate::store::{self, object::Object}; use crate::store::{self, object::Object};
use crate::utils::{self, path}; use crate::utils::{self, path};
use crate::store::object::object::{Obj, ObjMethods};
use crate::utils::nextsyncignore::{self, ignore_file}; use crate::utils::nextsyncignore::{self, ignore_file};
use crate::utils::path::{normalize_relative, repo_root, path_buf_to_string}; 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<String>, rules:
return; 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())); added_files.push(path_buf_to_string(entry.strip_prefix(repo_root()).unwrap().to_path_buf()));
if entry.is_dir() { if entry.is_dir() {
add_folder_content(entry.to_path_buf(), added_files); add_folder_content(entry.to_path_buf(), added_files);
@ -80,6 +84,20 @@ fn add_entry(entry: PathBuf, force: bool, added_files: &mut Vec<String>, rules:
} }
fn add_parent(entry: PathBuf, added_files: &mut Vec<String>) {
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<String>) { fn print_ignored_files(ignored_files: Vec<String>) {
if ignored_files.len() > 0 { if ignored_files.len() > 0 {
// todo multiple nextsyncignore // todo multiple nextsyncignore

View File

@ -31,6 +31,7 @@ pub trait ObjMethods {
fn rm(&mut self) -> io::Result<()>; fn rm(&mut self) -> io::Result<()>;
fn rm_node(&mut self) -> io::Result<()>; fn rm_node(&mut self) -> io::Result<()>;
fn rm_node_down(&mut self) -> io::Result<()>; fn rm_node_down(&mut self) -> io::Result<()>;
fn exists_on_remote(&mut self) -> bool;
} }
pub struct Obj { pub struct Obj {
@ -126,6 +127,10 @@ impl ObjMethods for Obj {
eprintln!("rm: tried to do this on Obj"); eprintln!("rm: tried to do this on Obj");
Ok(()) Ok(())
} }
fn exists_on_remote(&mut self) -> bool {
PathBuf::from(self.get_hash_path()).exists()
}
} }
impl ObjMethods for Blob { impl ObjMethods for Blob {
@ -187,6 +192,10 @@ impl ObjMethods for Blob {
fs::remove_file(self.get_file_path())?; fs::remove_file(self.get_file_path())?;
Ok(()) Ok(())
} }
fn exists_on_remote(&mut self) -> bool {
self.obj.exists_on_remote()
}
} }
impl ObjMethods for Tree { impl ObjMethods for Tree {
@ -258,6 +267,10 @@ impl ObjMethods for Tree {
fs::remove_dir_all(self.get_file_path())?; fs::remove_dir_all(self.get_file_path())?;
Ok(()) Ok(())
} }
fn exists_on_remote(&mut self) -> bool {
self.obj.exists_on_remote()
}
} }
impl Obj { impl Obj {