-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement handle_cmd and git storage - filesystem
- Loading branch information
Showing
16 changed files
with
164 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use crate::repo::Repository; | ||
|
||
use super::GitSubCommand; | ||
|
||
pub fn handle_command(cmd: &GitSubCommand) -> anyhow::Result<()> { | ||
match cmd { | ||
GitSubCommand::Init(opts) => { | ||
println!("init repo optiond: {:?}", opts); | ||
let repo = Repository::new("."); | ||
repo.init()?; | ||
} | ||
GitSubCommand::Add(opts) => { | ||
todo!() | ||
} | ||
GitSubCommand::Commit(opts) => { | ||
todo!() | ||
} | ||
GitSubCommand::Log(opts) => { | ||
todo!() | ||
} | ||
GitSubCommand::Branch(opts) => { | ||
todo!() | ||
} | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
pub mod command; | ||
pub mod objects; | ||
pub mod plumbing; | ||
pub mod repo; | ||
pub mod storage; | ||
pub mod worktree; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
use anyhow::Result; | ||
use clap::Parser; | ||
use git_rs::command::SimpleGit; | ||
use git_rs::command::{handler::handle_command, SimpleGit}; | ||
|
||
fn main() { | ||
fn main() -> Result<()> { | ||
let cmd = SimpleGit::parse(); | ||
println!("{:?}", cmd); | ||
handle_command(&cmd.command)?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#[derive(Debug)] | ||
pub struct Blob {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#[derive(Debug)] | ||
pub struct Commit {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod blob; | ||
pub mod tree; | ||
pub mod commit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#[derive(Debug)] | ||
pub struct Tree {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub const HEAD: &'static str = "HEAD"; | ||
pub const MASTER: &'static str = "refs/heads/master"; | ||
pub const MAIN: &'static str = "refs/heads/main"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use crate::storage::{filesystem::FileSystem, Store}; | ||
|
||
pub struct Repository { | ||
storer: FileSystem, | ||
} | ||
|
||
impl Repository { | ||
pub fn new(path: &str) -> Self { | ||
Repository { | ||
storer: FileSystem::new(path), | ||
} | ||
} | ||
|
||
pub fn init(&self) -> anyhow::Result<()> { | ||
self.storer.init_dot_git()?; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use std::fs; | ||
use std::io::Write; | ||
use std::path::Path; | ||
|
||
use crate::plumbing; | ||
|
||
use super::Store; | ||
|
||
// The file operations git will perforam. | ||
pub struct FileSystem { | ||
pub root: String, | ||
} | ||
|
||
impl FileSystem { | ||
pub fn new(path: &str) -> Self { | ||
FileSystem { | ||
root: path.to_string(), | ||
} | ||
} | ||
} | ||
|
||
impl Store for FileSystem { | ||
fn init_dot_git(&self) -> anyhow::Result<()> { | ||
let full_path = format!("{}/{}", self.root, ".git"); | ||
if !Path::new(&full_path).exists() { | ||
fs::create_dir(full_path.clone())?; | ||
} | ||
|
||
// create the directories | ||
let dirs = ["objects", "refs", "hooks", "info"]; | ||
for dir in dirs { | ||
let sub_dir = format!("{}/{}", full_path, dir); | ||
if !Path::new(&sub_dir).exists() { | ||
fs::create_dir(sub_dir.clone())?; | ||
match dir { | ||
"objects" => { | ||
fs::create_dir(format!("{}/{}", sub_dir, "info"))?; | ||
fs::create_dir(format!("{}/{}", sub_dir, "pack"))?; | ||
} | ||
"refs" => { | ||
fs::create_dir(format!("{}/{}", sub_dir, "heads"))?; | ||
fs::create_dir(format!("{}/{}", sub_dir, "tags"))?; | ||
} | ||
"info" => { | ||
fs::create_dir(format!("{}/{}", sub_dir, "exclude"))?; | ||
} | ||
_ => {} | ||
} | ||
} | ||
} | ||
|
||
// create the files | ||
let files = [plumbing::HEAD, "config", "description"]; | ||
for file in files { | ||
let sub_file = format!("{}/{}", full_path, file); | ||
let _ = fs::File::create(sub_file)?; | ||
} | ||
|
||
let main_ref_path = format!("{}/{}", full_path, plumbing::HEAD); | ||
Self::set_head(&main_ref_path, "ref: refs/heads/master")?; | ||
Ok(()) | ||
} | ||
|
||
fn set_head(file: &str, ref_content: &str) -> anyhow::Result<()> { | ||
let mut ref_f = fs::File::open(file)?; | ||
let _ = ref_f.write_all(ref_content.as_bytes())?; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
pub mod filesystem; | ||
|
||
use anyhow::Result; | ||
pub trait Store { | ||
fn init_dot_git(&self) -> Result<()>; | ||
fn set_head(file: &str, ref_content: &str) -> Result<()>; | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod index; | ||
|
||
pub struct Worktree {} |