refactor: minor kind refactor
This commit is contained in:
parent
8db6c791fc
commit
e6ab0bebd6
23
src/kind.rs
23
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<Self> {
|
||||
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",
|
||||
|
@ -53,7 +53,7 @@ pub fn read_object(path: &Path, object: &str) -> Result<Object<impl BufRead>> {
|
||||
};
|
||||
|
||||
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<R: BufRead> Object<R> {
|
||||
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)?
|
||||
}
|
||||
|
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user