Init commit
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
use clap::Parser;
|
||||
use hex::encode;
|
||||
|
||||
use crate::{
|
||||
git_fs::object::{Blob, GitObject},
|
||||
subcommands::Subcommand
|
||||
};
|
||||
|
||||
use super::CmdResult;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
pub struct HashObjectSubcommand {
|
||||
#[arg(short)]
|
||||
/// Save object in database
|
||||
pub write: bool,
|
||||
|
||||
pub path: String,
|
||||
}
|
||||
|
||||
impl Subcommand for HashObjectSubcommand {
|
||||
fn run (&self) -> CmdResult {
|
||||
let object = match Blob::create(self.path.clone()) {
|
||||
Ok(o) => o,
|
||||
_ => return Err("".to_owned())
|
||||
};
|
||||
|
||||
match object.hash(self.write) {
|
||||
Ok(hash) => Ok(encode(hash)),
|
||||
_ => return Err("".to_owned())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
use clap::Parser;
|
||||
use std::{
|
||||
fs,
|
||||
path::Path
|
||||
};
|
||||
|
||||
use crate::git_fs::head::Head;
|
||||
use crate::subcommands::Subcommand;
|
||||
use crate::GIT_DIR;
|
||||
|
||||
use super::CmdResult;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
pub struct InitSubcommand {
|
||||
directory: Option<String>,
|
||||
}
|
||||
|
||||
impl Subcommand for InitSubcommand {
|
||||
fn run(&self) -> CmdResult {
|
||||
let path = match &self.directory {
|
||||
None => Path::new("."),
|
||||
Some(path) => Path::new(path),
|
||||
}.join(GIT_DIR);
|
||||
|
||||
let new_repo = path.exists();
|
||||
|
||||
match fs::create_dir_all(&path) {
|
||||
Err(_) => return Err("Error while creating dir".to_owned()),
|
||||
Ok(()) => (),
|
||||
};
|
||||
|
||||
let folders = [
|
||||
"objects/info",
|
||||
"objects/pack",
|
||||
"refs/heads",
|
||||
"refs/tags",
|
||||
];
|
||||
|
||||
for folder in folders {
|
||||
match fs::create_dir_all(&path.join(folder)) {
|
||||
Err(_) => return Err("".to_owned()),
|
||||
Ok(()) => (),
|
||||
};
|
||||
}
|
||||
|
||||
let head = Head { ref_to: String::from("refs/heads/master") };
|
||||
match head.save() {
|
||||
Err(_) => return Err("".to_owned()),
|
||||
Ok(()) => (),
|
||||
};
|
||||
|
||||
let canonical_path = path.canonicalize();
|
||||
|
||||
if new_repo {
|
||||
Ok(format!("Reinitialized exisiting Git repo in {}", canonical_path.unwrap().display()))
|
||||
} else {
|
||||
Ok(format!("Initialized empty Git repo in {}", canonical_path.unwrap().display()))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
use crate::subcommands::{
|
||||
init::InitSubcommand,
|
||||
test::TestSubcommand,
|
||||
hash_object::HashObjectSubcommand,
|
||||
};
|
||||
|
||||
mod hash_object;
|
||||
mod init;
|
||||
mod test;
|
||||
|
||||
pub type CmdResult = Result<String, String>;
|
||||
|
||||
#[derive(clap::Parser, Debug)]
|
||||
pub enum SubcommandType {
|
||||
/// Init a Git repository
|
||||
Init(InitSubcommand),
|
||||
HashObject(HashObjectSubcommand),
|
||||
Test(TestSubcommand),
|
||||
}
|
||||
|
||||
pub trait Subcommand {
|
||||
fn run(&self) -> CmdResult;
|
||||
}
|
||||
|
||||
impl Subcommand for SubcommandType {
|
||||
fn run(&self) -> CmdResult {
|
||||
match self {
|
||||
Self::Init(cmd) => cmd.run(),
|
||||
Self::HashObject(cmd) => cmd.run(),
|
||||
Self::Test(cmd) => cmd.run(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
use clap::Parser;
|
||||
use std::fs;
|
||||
use crate::subcommands::Subcommand;
|
||||
|
||||
use super::CmdResult;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
pub struct TestSubcommand {
|
||||
path: String
|
||||
}
|
||||
|
||||
impl Subcommand for TestSubcommand {
|
||||
fn run(&self) -> CmdResult {
|
||||
let metadata = match fs::metadata(self.path.clone()) {
|
||||
Ok(metadata) => metadata,
|
||||
_ => return Err(String::from("")),
|
||||
};
|
||||
|
||||
let mtime = match metadata.modified() {
|
||||
Ok(mtime) => mtime,
|
||||
_ => return Err(String::from("")),
|
||||
};
|
||||
|
||||
// mtime
|
||||
println!("{mtime:?}");
|
||||
|
||||
Ok(String::from(""))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user