step twenty one: tag, generalize HEAD to refs

This commit is contained in:
Gonçalo Valério 2020-11-04 20:16:08 +00:00
parent 019729a3fe
commit 1b1ac3ec74
3 changed files with 9 additions and 9 deletions

View File

@ -71,7 +71,7 @@ pub fn read_tree(oid: String) {
pub fn commit(message: &str) -> String { pub fn commit(message: &str) -> String {
let mut commit = format!("tree {}\n", write_tree(".".to_owned())); let mut commit = format!("tree {}\n", write_tree(".".to_owned()));
if let Ok(head) = data::get_head() { if let Ok(head) = data::get_ref("HEAD".to_owned()) {
commit += format!("parent {}\n", head).as_str(); commit += format!("parent {}\n", head).as_str();
} }
@ -79,7 +79,7 @@ pub fn commit(message: &str) -> String {
commit += format!("{}\n", message).as_str(); commit += format!("{}\n", message).as_str();
let oid = 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()); data::update_ref("HEAD".to_owned(), oid.clone());
return oid; return oid;
} }
@ -112,7 +112,7 @@ pub fn get_commit(oid: String) -> Commit {
pub fn checkout(oid: String) { pub fn checkout(oid: String) {
let commit = get_commit(oid.clone()); let commit = get_commit(oid.clone());
read_tree(commit.tree); read_tree(commit.tree);
data::set_head(oid); data::update_ref("HEAD".to_owned(), oid);
} }
pub fn create_tag(name: String, oid: String) { pub fn create_tag(name: String, oid: String) {

View File

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

View File

@ -119,7 +119,7 @@ fn log_commits(matches: ArgMatches) {
let provided_oid = cmd_matches.value_of("oid").unwrap_or(""); let provided_oid = cmd_matches.value_of("oid").unwrap_or("");
let mut oid; let mut oid;
if provided_oid == "" { if provided_oid == "" {
oid = data::get_head().expect("Cannot read HEAD file"); oid = data::get_ref("HEAD".to_owned()).expect("Cannot read HEAD file");
} else { } else {
oid = provided_oid.to_owned(); oid = provided_oid.to_owned();
} }
@ -152,7 +152,7 @@ fn tag(matches: ArgMatches) {
let name = cmd_matches.value_of("name").unwrap().to_owned(); let name = cmd_matches.value_of("name").unwrap().to_owned();
let mut oid = cmd_matches.value_of("oid").unwrap_or("").to_owned(); let mut oid = cmd_matches.value_of("oid").unwrap_or("").to_owned();
if oid == "" { if oid == "" {
oid = data::get_head().expect("Cannot read HEAD"); oid = data::get_ref("HEAD".to_owned()).expect("Cannot read HEAD");
} }
base::create_tag(name, oid); base::create_tag(name, oid);
} }