From babdda6a2d33dcebb1d5ea07683a692c303c0de1 Mon Sep 17 00:00:00 2001 From: NobodyForNothing <82763757+NobodyForNothing@users.noreply.github.com> Date: Mon, 1 Jul 2024 14:22:19 +0200 Subject: [PATCH] feat: git object detection --- rust/Cargo.lock | 119 ++++++++++++++++++++++++++++++++++++ rust/vcs/Cargo.toml | 2 + rust/vcs/src/git/objects.rs | 24 ++++++++ rust/vcs/src/git/repo.rs | 30 +++++++++ 4 files changed, 175 insertions(+) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 8a7e31d..5a2c578 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + [[package]] name = "anstream" version = "0.6.14" @@ -51,6 +57,21 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "clap" version = "4.5.8" @@ -97,10 +118,68 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" +[[package]] +name = "cpufeatures" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53fe5e26ff1b7aef8bca9c6080520cfb8d9333c7568e1829cef191a9723e5504" +dependencies = [ + "libc", +] + +[[package]] +name = "crc32fast" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + [[package]] name = "dir_size" version = "0.1.0" +[[package]] +name = "flate2" +version = "1.0.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f54427cfd1c7829e2a139fcefea601bf088ebca651d2bf53ebc600eac295dae" +dependencies = [ + "crc32fast", + "miniz_oxide", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + [[package]] name = "heck" version = "0.5.0" @@ -120,12 +199,27 @@ version = "1.70.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" +[[package]] +name = "libc" +version = "0.2.155" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" + [[package]] name = "log" version = "0.4.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" +[[package]] +name = "miniz_oxide" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8a240ddb74feaf34a79a7add65a741f3167852fba007066dcac1ca548d89c08" +dependencies = [ + "adler", +] + [[package]] name = "proc-macro2" version = "1.0.86" @@ -144,6 +238,17 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "sha1" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3bf829a2d51ab4a5ddf1352d8470c140cadc8301b2ae1789db023f01cedd6ba" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "strsim" version = "0.11.1" @@ -161,6 +266,12 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + [[package]] name = "unicode-ident" version = "1.0.12" @@ -178,10 +289,18 @@ name = "vcs" version = "0.1.0" dependencies = [ "clap", + "flate2", "iniconf", "log", + "sha1", ] +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + [[package]] name = "windows-sys" version = "0.52.0" diff --git a/rust/vcs/Cargo.toml b/rust/vcs/Cargo.toml index 5d39db2..0997a4b 100644 --- a/rust/vcs/Cargo.toml +++ b/rust/vcs/Cargo.toml @@ -7,3 +7,5 @@ edition = "2021" clap = { version = "4.5.8", features = ["derive"] } log = "0.4.22" iniconf = { path = "../iniconf", version = "1.0.0"} +flate2 = "1.0.30" +sha1 = "0.10.6" diff --git a/rust/vcs/src/git/objects.rs b/rust/vcs/src/git/objects.rs index e69de29..c570453 100644 --- a/rust/vcs/src/git/objects.rs +++ b/rust/vcs/src/git/objects.rs @@ -0,0 +1,24 @@ + +pub trait GitObjectT { + /* TODO + fn init(data: Option>) -> Box { + match data { + None => Self::new(), + Some(data) => { + let mut obj = Self::new(); + obj.deserialize(data); + obj + } + } + }*/ + fn new() -> Self; + fn serialize(&self) -> Vec; + fn deserialize(&mut self, data: Vec); +} + +pub enum GitObject { + Commit, + Tree, + Tag, + Blob, +} diff --git a/rust/vcs/src/git/repo.rs b/rust/vcs/src/git/repo.rs index c633a00..4692ada 100644 --- a/rust/vcs/src/git/repo.rs +++ b/rust/vcs/src/git/repo.rs @@ -1,7 +1,10 @@ use iniconf::{IniFile, IniFileOpenError}; use std::fs; +use std::io::Read; use std::path::{Path, PathBuf}; use log::warn; +use crate::git; +use crate::git::objects::GitObject; pub struct Repository { /// Where the files meant to be in version control live. @@ -126,6 +129,33 @@ impl Repository { None } } + + fn object_read(&self, sha: [u8; 20]) -> () { + let sha: String = sha.iter().map(|byte| format!("{:x}", byte)).collect(); + let path = self.repo_path(vec!["objects", &sha[0..2], &sha[2..sha.len()]], None, Some(true)); + if path.as_ref().is_some_and(|p| p.is_file()) { + if let Ok(data) = fs::read(path.unwrap()) { + let mut data = flate2::read::ZlibDecoder::new(&data[..]); + let mut data = data.bytes(); + let mut obj_type = String::new(); + while let Some(Ok(byte)) = data.next() { + if byte == 20 { + break; + } + obj_type.push(char::from(byte)); + } + let obj = match obj_type.as_str() { + "commit" => GitObject::Commit, + "tree" => GitObject::Tree, + "tag" => GitObject::Tag, + "blob" => GitObject::Blob, + _ => panic!("Unknown type {obj_type} for object {sha}"), + }; + } + + } + todo!() + } } #[derive(Debug)]