From 909320d2fbb1a287760998ec86fff399bd43f5ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gon=C3=A7alo=20Val=C3=A9rio?= Date: Sun, 1 Nov 2020 22:38:22 +0000 Subject: [PATCH] step fifteen: record hash of last commit to HEAD --- src/base.rs | 6 ++++-- src/data.rs | 4 ++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/base.rs b/src/base.rs index c5c00d2..f540be9 100644 --- a/src/base.rs +++ b/src/base.rs @@ -67,7 +67,9 @@ pub fn commit(message: &str) -> String { commit += "\n"; commit += format!("{}\n", message).as_str(); - return data::hash_object(&commit.into_bytes(), "commit".to_owned()); + let oid = data::hash_object(&commit.into_bytes(), "commit".to_owned()); + data::set_head(oid.clone()); + return oid; } fn is_ignored(path: &String) -> bool { @@ -112,7 +114,7 @@ fn get_tree(oid: String, base_path: String) -> HashMap { } fn empty_current_directory(dir: &str) -> io::Result<()> { - // Delete current directory, less the ignored directories + // Delete current directory, except the ignored directories and files for entry in fs::read_dir(dir)? { let entry = entry?; let path = entry.path(); diff --git a/src/data.rs b/src/data.rs index ea9d174..336e9a3 100644 --- a/src/data.rs +++ b/src/data.rs @@ -41,3 +41,7 @@ pub fn get_object(hash: String, expected: String) -> String { return data; } + +pub fn set_head(oid: String) { + fs::write(format!("{}/HEAD", RGIT_DIR), oid).expect("Failed to updated HEAD"); +}