diff options
-rw-r--r-- | README.md | 11 | ||||
-rw-r--r-- | src/main.rs | 14 | ||||
-rw-r--r-- | src/object.rs | 12 | ||||
-rw-r--r-- | src/repository.rs | 13 |
4 files changed, 50 insertions, 0 deletions
diff --git a/README.md b/README.md new file mode 100644 index 0000000..b9b7158 --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +### Grit +*NOT FINISHED YET* + +An attempt to implement a git client... in Rust, so that I can say "I use Rust btw". + +- Install rust and cargo +- `cargo run -- [ARGS]` to test in dev mode + +`ARGS` is supposedly partially compatible with git commands. + +For now the only sub-command that works properly is `init` diff --git a/src/main.rs b/src/main.rs index 66891c4..a82a6e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ mod object; use crate::repository::GitRepository; use clap::{Parser, Subcommand}; use std::path::PathBuf; +use crate::object::{GitObject, GitObjectType}; #[derive(Parser)] #[command(version, about, long_about = None)] @@ -69,6 +70,19 @@ fn main() { } GitRepository::create_new_repo(p); } + Command::CatFile { object_type, sha} => { + // todo detect root otomatis + // temporary + let repo = GitRepository::from_dir(PathBuf::from(".")); + let object_type = match &object_type[..] { + "blob" => GitObjectType::Blob, + "commit" => GitObjectType::Commit, + "tag" => GitObjectType::Tag, + "tree" => GitObjectType::Tree, + _ => GitObjectType::Undefined + }; + repo.cat_file(object_type, sha.as_str()); + } _ => {} } }
\ No newline at end of file diff --git a/src/object.rs b/src/object.rs index 12f83ae..8877641 100644 --- a/src/object.rs +++ b/src/object.rs @@ -1,3 +1,4 @@ +use std::fmt::{Display, Formatter}; use crate::utilities::{deserialize_kv_with_message, serialize_kv_with_message}; use crate::{create_path_or_die, die}; use derive_is_enum_variant::is_enum_variant; @@ -143,4 +144,15 @@ impl GitCommit { pub fn serialize(&self) -> Vec<u8> { serialize_kv_with_message(&self.header, self.message.as_str()) } +} + +impl Display for GitCommit { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + for (k, v) in self.header { + write!(f, "{} {}\n", k, v)?; + } + write!(f, "\n")?; + write!(f, "{}", self.message)?; + Ok(()) + } }
\ No newline at end of file diff --git a/src/repository.rs b/src/repository.rs index f5ecffb..97b7816 100644 --- a/src/repository.rs +++ b/src/repository.rs @@ -5,6 +5,7 @@ use ini::ini; use std::collections::HashMap; use std::io::Write; use std::path::PathBuf; +use crate::object::{GitCommit, GitObject, GitObjectType}; pub struct GitRepository { pub worktree: PathBuf, @@ -70,4 +71,16 @@ impl GitRepository { } println!("Git repository initialized in {}", repo.worktree.display()); } + + pub fn cat_file(&self, object_type: GitObjectType, sha: &str) { + match object_type { + GitObjectType::Commit => { + let path = self.gitdir.join(&sha[..2]).join(&sha[2..]); + let git_object = GitObject::from_file(path); + let git_commit = GitCommit::from_git_object(git_object); + println!("{}", git_commit); + } + _ => {} + } + } }
\ No newline at end of file |