tree save&load 未完成

This commit is contained in:
HouXiaoxuan
2023-12-20 00:01:42 +08:00
parent d61b2f57e2
commit c034120dfd

View File

@@ -1,31 +1,46 @@
use serde::{Deserialize, Serialize};
use crate::store;
use super::{index::Index, object::Hash};
/*Tree
* Tree是一个版本中所有文件的集合。从根目录还是每个目录是一个Tree每个文件是一个Blob。Tree之间互相嵌套表示文件的层级关系。
* 每一个Tree对象也是对应到git储存仓库的一个文件其内容是一个或多个TreeEntry。
*/
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct TreeEntry {
pub filemode: (String, String), // (type, mode), type: blob or tree; mode: 100644 or 04000
pub hash: Hash, // blob hash or tree hash
pub object_hash: Hash, // blob hash or tree hash
pub name: String, // file name
}
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Tree {
#[serde(skip)]
pub hash: Hash,
pub entries: Vec<TreeEntry>,
}
impl Tree {
pub fn new(index: &Index) -> Tree {
unimplemented!()
Tree {
hash: "".to_string(),
entries: Vec::new(),
}
}
pub fn load(hash: &String) -> Tree {
unimplemented!()
let s = store::Store::new();
let tree_data = s.load(hash);
let mut tree: Tree = serde_json::from_str(&tree_data).unwrap();
tree.hash = hash.clone();
tree
}
pub fn save(&self) {
unimplemented!()
pub fn save(&self) -> String {
let s = store::Store::new();
let tree_data = serde_json::to_string(&self).unwrap();
let hash = s.save(&tree_data);
hash
}
}