diff options
author | Rosyid Haryadi <rosyid_haryadi@protonmail.com> | 2025-03-18 10:55:30 +0700 |
---|---|---|
committer | Rosyid Haryadi <rosyid_haryadi@protonmail.com> | 2025-03-18 10:55:30 +0700 |
commit | 66aa0f0d9d02800b4d5c12e9f566b8808b67c8ea (patch) | |
tree | 17b06b4b625d9a1273baa21e21f0bfe0ebbc272a /src | |
parent | ba1d9d094fa9c9930b3d3919b175f12e9eeeb3f3 (diff) |
refactor: deserialization out of commit obj because it will be used for tag as well
Diffstat (limited to 'src')
-rw-r--r-- | src/object.rs | 52 | ||||
-rw-r--r-- | src/utilities.rs | 44 |
2 files changed, 52 insertions, 44 deletions
diff --git a/src/object.rs b/src/object.rs index 11a7717..aa5f434 100644 --- a/src/object.rs +++ b/src/object.rs @@ -5,9 +5,12 @@ use flate2::read::{ZlibDecoder, ZlibEncoder}; use std::fs::File; use std::io::{BufReader, Read, Write}; use std::path::PathBuf; +use derive_is_enum_variant::is_enum_variant; use flate2::Compression; use sha1::{Digest, Sha1}; +use crate::utilities::deserialize_kv_with_message; +#[derive(is_enum_variant)] pub enum GitObjectType { Blob, Commit, @@ -129,50 +132,11 @@ impl GitCommit { } } - fn deserialize(data: &Vec<u8>) -> Self { - let string_rep = String::from_utf8(data.clone()); - if string_rep.is_err() { - die!("Failed to parse commit data"); - } - let string_rep = string_rep.unwrap(); - let mut is_message = false; - let mut header: HashMap<String, String> = HashMap::new(); - let mut message = String::new(); - let mut last_key: Option<String> = None; - for line in string_rep.lines() { - if is_message { - message.push_str(format!("\n{}", line).as_str()); - continue; - } - - if line == "" { - is_message = true; - continue; - } - - if line.starts_with(" ") && last_key.is_some() { - let key = last_key.clone().unwrap(); - match header.get(&key) { - Some(val) => { - let mut new_val = val.clone(); - new_val.push_str(format!("\n{}", line).as_str()); - header.insert(key, new_val); - } - None => { - header.insert(key, line.to_string()); - } - } - } else { - let mut line_iter = line.splitn(2, ' '); - let key = line_iter.next().unwrap(); - let val = line_iter.next().unwrap(); - header.insert(key.to_string(), val.to_string()); - last_key = Some(key.to_string()); - } - } - Self { - header, - message, + pub fn from_git_object(git_object: GitObject) -> Self { + if !git_object.object_type.is_commit() { + die!("Could not create commit object"); } + let (header, message) = deserialize_kv_with_message(&git_object.data); + Self { header, message } } }
\ No newline at end of file diff --git a/src/utilities.rs b/src/utilities.rs index 2c7a43f..11d0e23 100644 --- a/src/utilities.rs +++ b/src/utilities.rs @@ -49,3 +49,47 @@ pub fn path_should_not_exist(path: &PathBuf, message: &str) { die!(message); } } + +pub fn deserialize_kv_with_message(data: &Vec<u8>) -> (HashMap<String, String>, String) { + let string_rep = String::from_utf8(data.clone()); + if string_rep.is_err() { + die!("Failed to parse commit data"); + } + let string_rep = string_rep.unwrap(); + let mut is_message = false; + let mut header: HashMap<String, String> = HashMap::new(); + let mut message = String::new(); + let mut last_key: Option<String> = None; + for line in string_rep.lines() { + if is_message { + message.push_str(format!("\n{}", line).as_str()); + continue; + } + + if line == "" { + is_message = true; + continue; + } + + if line.starts_with(" ") && last_key.is_some() { + let key = last_key.clone().unwrap(); + match header.get(&key) { + Some(val) => { + let mut new_val = val.clone(); + new_val.push_str(format!("\n{}", line).as_str()); + header.insert(key, new_val); + } + None => { + header.insert(key, line.to_string()); + } + } + } else { + let mut line_iter = line.splitn(2, ' '); + let key = line_iter.next().unwrap(); + let val = line_iter.next().unwrap(); + header.insert(key.to_string(), val.to_string()); + last_key = Some(key.to_string()); + } + } + (header, message) +} |