Skip to content

Commit

Permalink
feat: git object detection
Browse files Browse the repository at this point in the history
  • Loading branch information
derdilla committed Jul 1, 2024
1 parent bd2d7b1 commit babdda6
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 0 deletions.
119 changes: 119 additions & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions rust/vcs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
24 changes: 24 additions & 0 deletions rust/vcs/src/git/objects.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

pub trait GitObjectT {
/* TODO
fn init(data: Option<Vec<u8>>) -> Box<Self> {
match data {
None => Self::new(),
Some(data) => {
let mut obj = Self::new();
obj.deserialize(data);
obj
}
}
}*/
fn new() -> Self;
fn serialize(&self) -> Vec<u8>;
fn deserialize(&mut self, data: Vec<u8>);
}

pub enum GitObject {
Commit,
Tree,
Tag,
Blob,
}
30 changes: 30 additions & 0 deletions rust/vcs/src/git/repo.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit babdda6

Please sign in to comment.