Files
MIT/src/models/commit.rs
2023-12-21 01:39:45 +08:00

80 lines
2.2 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
use serde::{Deserialize, Serialize};
use crate::store;
use super::{index::Index, object::Hash, tree::Tree};
/*Commit
* git中版本控制的单位。
* 一份Commit中对应一份版Tree记录了该版本所包含的文件parent记录本次commit的来源形成了版本树
* 此外Commit中还包含了作者、提交者、提交信息等。
*/
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct Commit {
#[serde(skip)]
hash: Hash,
author: String, // unimplemented ignore
committer: String, // unimplemented ignore
message: String,
parent: Vec<Hash>, // parents commit hash
tree: String, // tree hash
}
impl Commit {
pub fn new(index: &Index, parent: Vec<Hash>, message: String) -> Commit {
let mut tree = Tree::new(index);
let tree_hash = tree.save();
Commit {
hash: "".to_string(),
author: "mit".to_string(),
committer: "mit-author".to_string(),
message,
parent,
tree: tree_hash,
}
}
pub fn load(hash: &String) -> Commit {
let s = store::Store::new();
let commit_data = s.load(hash);
let mut commit: Commit = serde_json::from_str(&commit_data).unwrap();
commit.hash = hash.clone();
commit
}
pub fn save(&mut self) -> String {
// unimplemented!()
let s = store::Store::new();
let commit_data = serde_json::to_string_pretty(&self).unwrap();
let hash = s.save(&commit_data);
self.hash = hash.clone();
hash
}
}
#[cfg(test)]
mod test {
use crate::utils::util;
#[test]
fn test_commit() {
util::setup_test_with_mit();
let index = super::Index::new();
let mut commit = super::Commit::new(
&index,
vec!["123".to_string(), "456".to_string()],
"test".to_string(),
);
assert!(commit.hash.len() == 0);
let hash = commit.save();
assert!(commit.hash == hash, "commit hash not equal");
let commit = super::Commit::load(&hash);
assert!(commit.hash == hash);
assert!(commit.hash.len() != 0);
assert!(commit.parent.len() == 2);
println!("{:?}", commit)
}
}