feat: push deletion

This commit is contained in:
grimhilt 2024-05-02 18:36:09 +02:00
parent 4504b98112
commit 939b6f2fe3
7 changed files with 70 additions and 17 deletions

View File

@ -73,9 +73,16 @@ pub trait PushChange {
}; };
// check if remote is newest // check if remote is newest
let last_sync_ts = Blob::from_path(obj.path.clone()) let last_sync_ts = {
if obj.otype == String::from("blob") {
Blob::from_path(obj.path.clone())
.saved_remote_ts() .saved_remote_ts()
.parse::<i64>().unwrap(); .parse::<i64>().unwrap()
} else {
// todo timestamp on tree
99999999999999
}
};
let remote_ts = obj_data.lastmodified.unwrap().timestamp_millis(); let remote_ts = obj_data.lastmodified.unwrap().timestamp_millis();
if last_sync_ts < remote_ts { if last_sync_ts < remote_ts {

View File

@ -178,8 +178,7 @@ impl ObjMethods for Blob {
fn rm_node(&mut self) -> io::Result<()> { fn rm_node(&mut self) -> io::Result<()> {
// remove self object and children object // remove self object and children object
self.rm_node_down(); let _ = self.rm_node_down();
self.obj.rm_node() self.obj.rm_node()
} }
@ -246,7 +245,7 @@ impl ObjMethods for Tree {
fn rm_node(&mut self) -> io::Result<()> { fn rm_node(&mut self) -> io::Result<()> {
// remove self object and children object // remove self object and children object
self.rm_node_down(); let _ = self.rm_node_down();
self.obj.rm_node() self.obj.rm_node()
} }
@ -336,7 +335,7 @@ impl Obj {
pub fn from_line(line: String, base_dir: Option<PathBuf>) -> Box<dyn ObjMethods> { pub fn from_line(line: String, base_dir: Option<PathBuf>) -> Box<dyn ObjMethods> {
let mut split = line.trim().rsplit(' '); let mut split = line.trim().rsplit(' ');
if split.clone().count() != 3 { if split.clone().count() != 3 {
eprintln!("fatal: invalid object(s)"); eprintln!("fatal: invalid object(s) ({})", line.trim());
std::process::exit(1); std::process::exit(1);
} }

View File

@ -11,7 +11,6 @@ pub struct Tree {
is_head: bool, is_head: bool,
} }
impl Tree { impl Tree {
pub fn new(obj: Obj) -> Self { pub fn new(obj: Obj) -> Self {
Tree { Tree {
@ -42,9 +41,10 @@ impl Tree {
if let Ok(file) = File::open(self.get_obj_path()) { if let Ok(file) = File::open(self.get_obj_path()) {
self.buf_reader = Some(BufReader::new(file)); self.buf_reader = Some(BufReader::new(file));
// skip first line if is head // skip first line (declaration) if is not head
if !self.is_head { if !self.is_head {
self.next(); let mut line = String::new();
self.buf_reader.as_mut().unwrap().read_line(&mut line);
} }
} }
} }
@ -99,11 +99,11 @@ impl Tree {
.open(self.get_obj_path())?; .open(self.get_obj_path())?;
// update date for all parent // update date for all parent
if up_parent { // if up_parent {
if let Err(err) = update_dates(self.get_relative_file_path(), date) { // if let Err(err) = update_dates(self.get_relative_file_path(), date) {
eprintln!("err: updating parent date of {}: {}", self.get_relative_file_path().display(), err); // eprintln!("err: updating parent date of {}: {}", self.get_relative_file_path().display(), err);
} // }
} // }
writeln!(file, "{}", content)?; writeln!(file, "{}", content)?;

View File

@ -1,4 +1,3 @@
mod utils; mod utils;
use utils::{utils::*, status_utils::*, server::ServerTest, client::ClientTest}; use utils::{utils::*, status_utils::*, server::ServerTest, client::ClientTest};
@ -151,7 +150,6 @@ mod push_tests {
// remove it // remove it
let _ = client.remove_file("file1"); let _ = client.remove_file("file1");
client.run_cmd_ok("add file1"); client.run_cmd_ok("add file1");
dbg!(client.get_status());
client.run_cmd_ok("push"); client.run_cmd_ok("push");
// tests // tests
@ -161,4 +159,27 @@ mod push_tests {
client.clean(); client.clean();
server.clean(); server.clean();
} }
#[test]
fn push_dir_deletion() {
let (mut client, mut server) = init_test();
// push dir and file2
let _ = client.add_dir("dir");
let _ = client.add_file("dir/file2", "bar");
client.run_cmd_ok("add dir");
client.run_cmd_ok("push");
// tests
assert!(server.has_file("dir/file2", "bar"));
// push deletion
let _ = client.remove_dir("dir");
client.run_cmd_ok("add dir");
client.run_cmd_ok("push");
assert!(server.has_not_dir("dir"));
client.clean();
server.clean();
}
} }

View File

@ -104,6 +104,14 @@ impl ClientTest {
Ok(()) Ok(())
} }
pub fn remove_dir(&mut self, name: &str) -> std::io::Result<()> {
let mut path = self.volume.clone();
path.push_str("/");
path.push_str(name);
fs::remove_dir_all(path)?;
Ok(())
}
pub fn has_file(&mut self, file: &str, content: &str) -> bool { pub fn has_file(&mut self, file: &str, content: &str) -> bool {
let full_path = PathBuf::from(self.volume.clone()).join(file); let full_path = PathBuf::from(self.volume.clone()).join(file);

View File

@ -36,3 +36,15 @@ pub fn has_not_file(full_path: PathBuf, file: &str, test_id: String) -> bool
return true; return true;
} }
#[cfg(test)]
pub fn has_not_dir(full_path: PathBuf, dir: &str, test_id: String) -> bool
{
if full_path.exists() {
println!("id: {}", test_id.clone());
eprintln!("Dir '{}' exists but it shouldn't", dir);
return false;
}
return true;
}

View File

@ -113,5 +113,11 @@ impl ServerTest {
let full_path = self.volume.clone().join(file); let full_path = self.volume.clone().join(file);
files_utils::has_not_file(full_path, file, self.test_id.clone()) files_utils::has_not_file(full_path, file, self.test_id.clone())
} }
pub fn has_not_dir(&mut self, dir: &str) -> bool {
let full_path = self.volume.clone().join(dir);
dbg!(full_path.clone());
files_utils::has_not_file(full_path, dir, self.test_id.clone())
}
} }