diff options
-rw-r--r-- | src/core.rs | 6 | ||||
-rw-r--r-- | src/main.rs | 18 | ||||
-rw-r--r-- | src/utilities.rs | 8 |
3 files changed, 24 insertions, 8 deletions
diff --git a/src/core.rs b/src/core.rs index 3cc1a67..b074188 100644 --- a/src/core.rs +++ b/src/core.rs @@ -44,16 +44,17 @@ impl GitRepository { pub fn create_new_repo(worktree: PathBuf) { let repo = GitRepository::new(worktree); - path_should_not_exist(&repo.worktree, "Already a git repository"); + path_should_not_exist(&repo.gitdir, "Already a git repository"); create_path_or_die!(dir: repo.gitdir.clone(), "Failed to initialize repository"); create_path_or_die!(dir: repo.gitdir.clone().join("objects"), "Failed to create objects directory"); + create_path_or_die!(dir: repo.gitdir.clone().join("refs"), "Failed to create refs directory"); create_path_or_die!(dir: repo.gitdir.clone().join("refs").join("heads"), "Failed to create git refs/head directory"); create_path_or_die!(dir: repo.gitdir.clone().join("refs").join("tags"), "Failed to create git refs/tags directory"); create_path_or_die!(file: repo.gitdir.clone().join("HEAD"), &"ref: refs/heads/master\n", "Failed to write HEAD file"); create_path_or_die!( file: repo.gitdir.clone().join("HEAD"), - "Unnamed repository; edit this file 'description' to name the repository.\n", + "ref: refs/heads/master\n", "Failed to write description file"); let mut config = Ini::new(); @@ -64,5 +65,6 @@ impl GitRepository { if let Err(_) = config.write(repo.gitdir.join("config")) { die!("Failed to write config file"); } + println!("Git repository initialized in {}", repo.worktree.display()); } }
\ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 1827e66..8958192 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,9 @@ mod core; mod utilities; +use std::path::PathBuf; use clap::{Parser, Subcommand}; +use crate::core::GitRepository; #[derive(Parser)] #[command(version, about, long_about = None)] @@ -12,13 +14,25 @@ struct Cli { #[derive(Subcommand)] enum Command { - Init, + Init { + path: Option<PathBuf>, + }, } fn main() { let cli = Cli::parse(); match &cli.command { - Command::Init => { todo!() } + Command::Init { path} => { + let mut p = PathBuf::new(); + if let Some(path) = path.clone() { + p = path.clone(); + } else { + if let Ok(path) = std::env::current_dir() { + p = path; + } + } + GitRepository::create_new_repo(p); + } } }
\ No newline at end of file diff --git a/src/utilities.rs b/src/utilities.rs index 1c2b26f..1357cdf 100644 --- a/src/utilities.rs +++ b/src/utilities.rs @@ -14,15 +14,15 @@ macro_rules! die { #[macro_export] macro_rules! create_path_or_die { (dir: $path:expr, $message:expr) => { - if let Err(_) = std::fs::create_dir($path) { - die!($message); + if let Err(e) = std::fs::create_dir($path) { + die!(format!("{}\nError: {}", $message, e.to_string())); } }; (file: $path:expr, $content:expr, $message:expr) => { if let Ok(mut file) = std::fs::File::create($path) { - if let Err(_) = file.write_all($content.as_ref()) { - die!($message); + if let Err(e) = file.write_all($content.as_ref()) { + die!(format!("{}\nError: {}", $message, e.to_string())); } } else { die!($message); |