step sixteen: set parent to HEAD

This commit is contained in:
Gonçalo Valério 2020-11-01 23:01:24 +00:00
parent 909320d2fb
commit 6dbfc533a6
2 changed files with 10 additions and 0 deletions

View File

@ -64,6 +64,11 @@ pub fn read_tree(oid: String) {
pub fn commit(message: &str) -> String {
let mut commit = format!("tree {}\n", write_tree(".".to_owned()));
if let Ok(head) = data::get_head() {
commit += format!("parent {}\n", head).as_str();
}
commit += "\n";
commit += format!("{}\n", message).as_str();

View File

@ -45,3 +45,8 @@ pub fn get_object(hash: String, expected: String) -> String {
pub fn set_head(oid: String) {
fs::write(format!("{}/HEAD", RGIT_DIR), oid).expect("Failed to updated HEAD");
}
pub fn get_head() -> Result<String, Box<dyn std::error::Error + 'static>> {
let oid = fs::read_to_string(format!("{}/HEAD", RGIT_DIR))?;
return Ok(oid);
}