From 4504b98112317bc4ca07c192393e62753f92b235 Mon Sep 17 00:00:00 2001 From: grimhilt Date: Thu, 18 Apr 2024 15:19:35 +0200 Subject: [PATCH] fix(push): push deletion --- src/commands/push.rs | 3 ++- src/commands/push/deleted.rs | 2 +- src/commands/status.rs | 15 ++------------- src/store/object/object.rs | 31 +++++++++++++++++++------------ tests/add.rs | 11 +++++++++++ tests/utils/client.rs | 3 +-- 6 files changed, 36 insertions(+), 29 deletions(-) diff --git a/src/commands/push.rs b/src/commands/push.rs index 6eb2213..cee4cf3 100644 --- a/src/commands/push.rs +++ b/src/commands/push.rs @@ -71,9 +71,10 @@ pub fn push() { }, PushState::Done => remove_obj_from_index(obj.clone()), PushState::Conflict => { + eprintln!("conflict when pushing blob"); // download file } - PushState::Error => (), + PushState::Error => (eprintln!("error when pushing changes blob")), } } } diff --git a/src/commands/push/deleted.rs b/src/commands/push/deleted.rs index 615cadd..279b14e 100644 --- a/src/commands/push/deleted.rs +++ b/src/commands/push/deleted.rs @@ -44,7 +44,7 @@ impl PushChange for Deleted { // update tree // todo date - Blob::from_path(obj.path.clone()).rm()?; + Blob::from_path(obj.path.clone()).rm_node()?; // remove index index::rm_line(obj.path.to_str().unwrap())?; diff --git a/src/commands/status.rs b/src/commands/status.rs index d9cae49..c73c734 100644 --- a/src/commands/status.rs +++ b/src/commands/status.rs @@ -160,19 +160,8 @@ pub struct LocalObj { } pub fn get_all_staged() -> Vec { - let mut staged_objs = vec![]; - - if let Ok(entries) = index::read_line() { - for line in entries { - - let obj = Obj::from_path(line.unwrap()).get_local_obj(); - if obj.state != State::Default { - staged_objs.push(obj); - } - } - } - - staged_objs + let mut all_hashes = get_all_objs_hashes(); + get_staged(&mut all_hashes) } fn get_staged(hashes: &mut HashMap) -> Vec { diff --git a/src/store/object/object.rs b/src/store/object/object.rs index 40c5466..f4e56fb 100644 --- a/src/store/object/object.rs +++ b/src/store/object/object.rs @@ -26,7 +26,7 @@ pub trait ObjMethods { fn get_name(&self) -> String; fn get_hash_path(&self) -> String; fn get_local_obj(&self) -> LocalObj; - fn get_line(&self) -> String; + fn get_line(&self, obj_type: ObjType) -> String; fn add_ref_to_parent(&self) -> io::Result<()>; fn rm(&mut self) -> io::Result<()>; fn rm_node(&mut self) -> io::Result<()>; @@ -89,12 +89,17 @@ impl ObjMethods for Obj { } // build line for parent reference - fn get_line(&self) -> String { - format!("tree {} {}", self.get_hash_path(), self.get_name()) + fn get_line(&self, obj_type: ObjType) -> String { + let type_str = match obj_type { + ObjType::BLOB => "blob", + ObjType::TREE => "tree", + ObjType::DEFAULT => "default", + }; + format!("{} {} {}", type_str, self.get_hash_path(), self.get_name()) } fn add_ref_to_parent(&self) -> io::Result<()> { - let line = self.get_line(); + let line = self.get_line(self.obj_type); if self.get_relative_file_path().iter().count() == 1 { head::add_line(line)?; } else { @@ -104,11 +109,8 @@ impl ObjMethods for Obj { } fn rm_node(&mut self) -> io::Result<()> { - // remove self object and children object - self.rm_node_down(); - // remove parent reference to self - let line = self.get_line(); + let line = self.get_line(self.obj_type); if self.get_relative_file_path().iter().count() == 1 { head::rm_line(&line)?; } else { @@ -166,8 +168,8 @@ impl ObjMethods for Blob { self.obj.get_hash_path() } - fn get_line(&self) -> String { - self.obj.get_line() + fn get_line(&self, _: ObjType) -> String { + self.obj.get_line(ObjType::BLOB) } fn add_ref_to_parent(&self) -> io::Result<()> { @@ -175,6 +177,9 @@ impl ObjMethods for Blob { } fn rm_node(&mut self) -> io::Result<()> { + // remove self object and children object + self.rm_node_down(); + self.obj.rm_node() } @@ -231,8 +236,8 @@ impl ObjMethods for Tree { self.obj.get_hash_path() } - fn get_line(&self) -> String { - self.obj.get_line() + fn get_line(&self, _: ObjType) -> String { + self.obj.get_line(ObjType::TREE) } fn add_ref_to_parent(&self) -> io::Result<()> { @@ -240,6 +245,8 @@ impl ObjMethods for Tree { } fn rm_node(&mut self) -> io::Result<()> { + // remove self object and children object + self.rm_node_down(); self.obj.rm_node() } diff --git a/tests/add.rs b/tests/add.rs index 1184fcf..8801b42 100644 --- a/tests/add.rs +++ b/tests/add.rs @@ -94,4 +94,15 @@ mod add_tests { client.clean(); } + #[test] + fn add_no_change() { + assert!(false); + // add a file push it and add it again + // let id = get_random_test_id(); + // let mut client = ClientTest::new(id).init(); + // + // + // client.clean(); + } + } diff --git a/tests/utils/client.rs b/tests/utils/client.rs index fbb134c..6a817c5 100644 --- a/tests/utils/client.rs +++ b/tests/utils/client.rs @@ -100,8 +100,7 @@ impl ClientTest { let mut path = self.volume.clone(); path.push_str("/"); path.push_str(name); - - fs::remove_file(name)?; + fs::remove_file(path)?; Ok(()) }