diff --git a/src/kind.rs b/src/kind.rs index 9e40ce2..b5c044a 100644 --- a/src/kind.rs +++ b/src/kind.rs @@ -2,16 +2,17 @@ use anyhow::{anyhow, Result}; #[derive(Debug)] pub enum Kind { - Blob, // 100644 or 100755 - Commit, // 160000 - Tree, // 040000 - Symlink, // 120000 + Blob(bool), // 100644 or 100755 + Commit, // 160000 + Tree, // 040000 + Symlink, // 120000 } impl Kind { pub fn from_mode(mode: &str) -> Result { match mode { - "100644" | "100755" => Ok(Kind::Blob), + "100644" => Ok(Kind::Blob(false)), + "100755" => Ok(Kind::Blob(true)), "160000" => Ok(Kind::Commit), "120000" => Ok(Kind::Symlink), "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 { match self { - Kind::Blob => "blob", + Kind::Blob(_) => "blob", Kind::Commit => "commit", Kind::Tree => "tree", Kind::Symlink => "symlink", diff --git a/src/object.rs b/src/object.rs index 24bd05c..eb8eb9f 100644 --- a/src/object.rs +++ b/src/object.rs @@ -53,7 +53,7 @@ pub fn read_object(path: &Path, object: &str) -> Result> { }; let object_type = match object_type { - "blob" => Kind::Blob, + "blob" => Kind::Blob(true), "commit" => Kind::Commit, "tree" => Kind::Tree, _ => anyhow::bail!("invalid object type found"), @@ -128,7 +128,7 @@ impl Object { let mut buf_hash: [u8; 20] = [0; 20]; let res = match self.kind { - Kind::Blob | Kind::Commit => { + Kind::Blob(_) | Kind::Commit => { self.data.read_to_end(&mut buf)?; String::from_utf8(buf)? } diff --git a/src/tree.rs b/src/tree.rs index 6d870a7..a9a498e 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -1,5 +1,6 @@ use anyhow::{Context, Result}; use sha1::{Digest, Sha1}; +use std::os::unix::fs::MetadataExt; use std::path::PathBuf; use crate::kind::Kind; @@ -23,11 +24,11 @@ pub fn write_tree(_repo_path: &PathBuf, path: &PathBuf) -> Result<[u8; 20]> { kind = Kind::Tree; } else { hash = hash_file(&file_path)?; - kind = Kind::Blob; + kind = Kind::Blob(file_path.metadata()?.mode() & 0o111 != 0); } entries.push(TreeObject { - mode: "100644".to_string(), + mode: kind.to_mode().to_string(), kind, name: file_name.into_string().unwrap(), hash,