diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/core.rs | 33 | ||||
-rw-r--r-- | src/utilities.rs | 13 |
2 files changed, 22 insertions, 24 deletions
diff --git a/src/core.rs b/src/core.rs index aa1d04d..3cc1a67 100644 --- a/src/core.rs +++ b/src/core.rs @@ -1,4 +1,4 @@ -use crate::utilities::ConfigHashMap; +use crate::utilities::{path_should_exist, path_should_not_exist, ConfigHashMap}; use crate::{create_path_or_die, die}; use configparser::ini::Ini; use ini::ini; @@ -20,17 +20,11 @@ impl GitRepository { } pub fn from_dir(worktree: PathBuf) -> Self { - if !worktree.exists() { - die!("Worktree does not exist"); - } + path_should_exist(&worktree, "Worktree does not exist"); let gitdir = worktree.join(".git"); - if !gitdir.exists() { - die!("Not a valid git repository"); - } + path_should_exist(&gitdir, "Not a valid git repository"); let conf_file = gitdir.join("config"); - if !conf_file.exists() { - die!("Config file does not exist"); - } + path_should_exist(&conf_file, "Configuration file not found"); let conf = ini!(conf_file.to_str().unwrap()); let repo_format_version = conf["core"]["repositoryformatversion"].clone(); if let Some(version) = repo_format_version { @@ -50,21 +44,17 @@ impl GitRepository { pub fn create_new_repo(worktree: PathBuf) { let repo = GitRepository::new(worktree); - if repo.worktree.exists() { - if repo.gitdir.exists() { - die!("Repo already exists"); - } - } + path_should_not_exist(&repo.worktree, "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").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", + "Failed to write description file"); let mut config = Ini::new(); config.set("core", "repositoryformatversion", Some("0".to_owned())); @@ -74,10 +64,5 @@ impl GitRepository { if let Err(_) = config.write(repo.gitdir.join("config")) { die!("Failed to write config file"); } - - create_path_or_die!( - file: repo.gitdir.clone().join("HEAD"), - "Unnamed repository; edit this file 'description' to name the repository.\n", - "Failed to write description file"); } }
\ No newline at end of file diff --git a/src/utilities.rs b/src/utilities.rs index aa7a44e..1c2b26f 100644 --- a/src/utilities.rs +++ b/src/utilities.rs @@ -1,4 +1,5 @@ use std::collections::HashMap; +use std::path::PathBuf; pub type ConfigHashMap = HashMap<String, HashMap<String, Option<String>>>; @@ -28,3 +29,15 @@ macro_rules! create_path_or_die { } }; } + +pub fn path_should_exist(path: &PathBuf, message: &str) { + if !path.exists() { + die!(message); + } +} + +pub fn path_should_not_exist(path: &PathBuf, message: &str) { + if path.exists() { + die!(message); + } +} |