summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock67
-rw-r--r--Cargo.toml1
-rw-r--r--src/object.rs52
-rw-r--r--src/utilities.rs44
4 files changed, 116 insertions, 48 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 8e0b946..c98c234 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -101,10 +101,10 @@ version = "4.5.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09176aae279615badda0765c0c0b3f6ed53f4709118af73cf4655d85d1530cd7"
dependencies = [
- "heck",
+ "heck 0.5.0",
"proc-macro2",
- "quote",
- "syn",
+ "quote 1.0.39",
+ "syn 2.0.100",
]
[[package]]
@@ -160,6 +160,17 @@ dependencies = [
]
[[package]]
+name = "derive_is_enum_variant"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d0ac8859845146979953797f03cc5b282fb4396891807cdb3d04929a88418197"
+dependencies = [
+ "heck 0.3.3",
+ "quote 0.3.15",
+ "syn 0.11.11",
+]
+
+[[package]]
name = "digest"
version = "0.10.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -195,6 +206,7 @@ version = "0.1.0"
dependencies = [
"clap",
"configparser 3.1.0",
+ "derive_is_enum_variant",
"flate2",
"ini",
"sha1",
@@ -202,6 +214,15 @@ dependencies = [
[[package]]
name = "heck"
+version = "0.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
+dependencies = [
+ "unicode-segmentation",
+]
+
+[[package]]
+name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
@@ -253,6 +274,12 @@ dependencies = [
[[package]]
name = "quote"
+version = "0.3.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a"
+
+[[package]]
+name = "quote"
version = "1.0.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1f1914ce909e1658d9907913b4b91947430c7d9be598b15a1912935b8c04801"
@@ -279,16 +306,36 @@ checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "syn"
+version = "0.11.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad"
+dependencies = [
+ "quote 0.3.15",
+ "synom",
+ "unicode-xid",
+]
+
+[[package]]
+name = "syn"
version = "2.0.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b09a44accad81e1ba1cd74a32461ba89dee89095ba17b32f5d03683b1b1fc2a0"
dependencies = [
"proc-macro2",
- "quote",
+ "quote 1.0.39",
"unicode-ident",
]
[[package]]
+name = "synom"
+version = "0.11.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6"
+dependencies = [
+ "unicode-xid",
+]
+
+[[package]]
name = "typenum"
version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -301,6 +348,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
[[package]]
+name = "unicode-segmentation"
+version = "1.12.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f6ccf251212114b54433ec949fd6a7841275f9ada20dddd2f29e9ceea4501493"
+
+[[package]]
+name = "unicode-xid"
+version = "0.0.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc"
+
+[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index f927c89..9399511 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -7,6 +7,7 @@ description = "Brainrot git"
[dependencies]
clap = { version = "4.5.32", features = ["derive"] }
configparser = "3.1.0"
+derive_is_enum_variant = "0.1.1"
flate2 = "1.1.0"
ini = "1.3.0"
sha1 = "0.10.6"
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)
+}