refactor: minor kind refactor
All checks were successful
CI checks / Clippy (push) Successful in 26s
CI checks / Format (push) Successful in 30s

This commit is contained in:
Patrick MARIE 2025-02-04 23:51:22 +01:00
parent 8db6c791fc
commit e6ab0bebd6
Signed by: mycroft
GPG Key ID: BB519E5CD8E7BFA7
3 changed files with 22 additions and 10 deletions

View File

@ -2,7 +2,7 @@ use anyhow::{anyhow, Result};
#[derive(Debug)] #[derive(Debug)]
pub enum Kind { pub enum Kind {
Blob, // 100644 or 100755 Blob(bool), // 100644 or 100755
Commit, // 160000 Commit, // 160000
Tree, // 040000 Tree, // 040000
Symlink, // 120000 Symlink, // 120000
@ -11,7 +11,8 @@ pub enum Kind {
impl Kind { impl Kind {
pub fn from_mode(mode: &str) -> Result<Self> { pub fn from_mode(mode: &str) -> Result<Self> {
match mode { match mode {
"100644" | "100755" => Ok(Kind::Blob), "100644" => Ok(Kind::Blob(false)),
"100755" => Ok(Kind::Blob(true)),
"160000" => Ok(Kind::Commit), "160000" => Ok(Kind::Commit),
"120000" => Ok(Kind::Symlink), "120000" => Ok(Kind::Symlink),
"040000" | "40000" => Ok(Kind::Tree), "040000" | "40000" => Ok(Kind::Tree),
@ -20,9 +21,19 @@ impl Kind {
} }
} }
pub fn to_mode(&self) -> &str {
match self {
Kind::Blob(false) => "100644",
Kind::Blob(true) => "100755",
Kind::Commit => "160000",
Kind::Tree => "040000",
Kind::Symlink => "120000",
}
}
pub fn string(&self) -> &str { pub fn string(&self) -> &str {
match self { match self {
Kind::Blob => "blob", Kind::Blob(_) => "blob",
Kind::Commit => "commit", Kind::Commit => "commit",
Kind::Tree => "tree", Kind::Tree => "tree",
Kind::Symlink => "symlink", Kind::Symlink => "symlink",

View File

@ -53,7 +53,7 @@ pub fn read_object(path: &Path, object: &str) -> Result<Object<impl BufRead>> {
}; };
let object_type = match object_type { let object_type = match object_type {
"blob" => Kind::Blob, "blob" => Kind::Blob(true),
"commit" => Kind::Commit, "commit" => Kind::Commit,
"tree" => Kind::Tree, "tree" => Kind::Tree,
_ => anyhow::bail!("invalid object type found"), _ => anyhow::bail!("invalid object type found"),
@ -128,7 +128,7 @@ impl<R: BufRead> Object<R> {
let mut buf_hash: [u8; 20] = [0; 20]; let mut buf_hash: [u8; 20] = [0; 20];
let res = match self.kind { let res = match self.kind {
Kind::Blob | Kind::Commit => { Kind::Blob(_) | Kind::Commit => {
self.data.read_to_end(&mut buf)?; self.data.read_to_end(&mut buf)?;
String::from_utf8(buf)? String::from_utf8(buf)?
} }

View File

@ -1,5 +1,6 @@
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use sha1::{Digest, Sha1}; use sha1::{Digest, Sha1};
use std::os::unix::fs::MetadataExt;
use std::path::PathBuf; use std::path::PathBuf;
use crate::kind::Kind; use crate::kind::Kind;
@ -23,11 +24,11 @@ pub fn write_tree(_repo_path: &PathBuf, path: &PathBuf) -> Result<[u8; 20]> {
kind = Kind::Tree; kind = Kind::Tree;
} else { } else {
hash = hash_file(&file_path)?; hash = hash_file(&file_path)?;
kind = Kind::Blob; kind = Kind::Blob(file_path.metadata()?.mode() & 0o111 != 0);
} }
entries.push(TreeObject { entries.push(TreeObject {
mode: "100644".to_string(), mode: kind.to_mode().to_string(),
kind, kind,
name: file_name.into_string().unwrap(), name: file_name.into_string().unwrap(),
hash, hash,