From 85cf6bc2d8d32fb8a75723d4724ddb0f7631b002 Mon Sep 17 00:00:00 2001 From: Rosyid Haryadi Date: Thu, 13 Mar 2025 16:14:02 +0700 Subject: upd: init subcommand --- Cargo.lock | 9 ++++++++- Cargo.toml | 1 + src/core.rs | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++---------- src/main.rs | 1 + 4 files changed, 65 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c2a1f30..c28ac01 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -104,11 +104,18 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fe1d7dcda7d1da79e444bdfba1465f2f849a58b07774e1df473ee77030cb47a7" +[[package]] +name = "configparser" +version = "3.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e57e3272f0190c3f1584272d613719ba5fc7df7f4942fe542e63d949cf3a649b" + [[package]] name = "grit" version = "0.1.0" dependencies = [ "clap", + "configparser 3.1.0", "ini", ] @@ -124,7 +131,7 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0a9271a5dfd4228fa56a78d7508a35c321639cc71f783bb7a5723552add87bce" dependencies = [ - "configparser", + "configparser 1.0.0", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 33e4942..e206dfd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,5 @@ description = "Brainrot git" [dependencies] clap = { version = "4.5.32", features = ["derive"] } +configparser = "3.1.0" ini = "1.3.0" diff --git a/src/core.rs b/src/core.rs index be1edfb..4f9302e 100644 --- a/src/core.rs +++ b/src/core.rs @@ -1,10 +1,10 @@ +use crate::utilities::{die, ConfigHashMap}; +use ini::ini; use std::collections::HashMap; +use std::fs::{create_dir, File}; +use std::io::Write; use std::path::PathBuf; -use std::process; -use ini::ini; - -type ConfigHashMap = HashMap>>; - +use configparser::ini::Ini; pub struct GitRepository { pub worktree: PathBuf, @@ -12,11 +12,6 @@ pub struct GitRepository { conf: HashMap>> } -fn die(msg: &str) { - println!("{}", msg); - process::exit(1); -} - impl GitRepository { pub fn new(worktree: PathBuf) -> Self { let gitdir = worktree.join(".git"); @@ -52,4 +47,54 @@ impl GitRepository { conf } } + + pub fn create_new_repo(worktree: PathBuf) { + let repo = GitRepository::new(worktree); + if repo.worktree.exists() { + if repo.gitdir.exists() { + die("Repo already exists"); + } + } + + let create_gitdir = create_dir(repo.gitdir.clone()); + if let Err(_) = create_gitdir { + die("Failed to initialize repository"); + } + + if let Err(_) = create_dir(repo.gitdir.clone().join("objects")) { + die("Failed to create objects directory"); + } + + if let Err(_) = create_dir(repo.gitdir.clone().join("refs").join("heads")) { + die("Failed to create git refs/head directory"); + } + + if let Err(_) = create_dir(repo.gitdir.clone().join("refs").join("tags")) { + die("Failed to create git refs/tags directory"); + } + + if let Ok(mut head_file) = File::create(repo.gitdir.clone().join("HEAD")) { + if let Err(_) = head_file.write_all(&"ref: refs/heads/master\n".as_ref()) { + die("Failed to write HEAD file"); + } + } else { + die("Failed to create HEAD file"); + } + + let mut config = Ini::new(); + config.set("core", "repositoryformatversion", Some("0".to_owned())); + config.set("core", "filemode", Some("false".to_owned())); + config.set("core", "bare", Some("false".to_owned())); + + if let Err(_) = config.write(repo.gitdir.join("config")) { + die("Failed to write config file"); + } + + + if let Ok(mut description_file) = File::create(repo.gitdir.clone().join("config")) { + if let Err(_) = description_file.write("Unnamed repository; edit this file 'description' to name the repository.\n".as_ref()) { + die("Failed to write description file"); + } + } + } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index a533003..1827e66 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ mod core; +mod utilities; use clap::{Parser, Subcommand}; -- cgit v1.2.3-70-g09d2