diff options
-rw-r--r-- | src/git_tools.rs | 58 | ||||
-rw-r--r-- | src/main.rs | 27 |
2 files changed, 85 insertions, 0 deletions
diff --git a/src/git_tools.rs b/src/git_tools.rs new file mode 100644 index 0000000..3b697e0 --- /dev/null +++ b/src/git_tools.rs @@ -0,0 +1,58 @@ +use core::panic; + +use git2::{Repository, RepositoryInitOptions}; + +pub enum RepoCreateError { + OwnershipError, + PathError, + RepoNameParseError, + InternalError, +} + +fn fmt_repo_name_dotgit(raw_name: &str) -> Result<String, RepoCreateError> { + if raw_name.ends_with(".git") { + Ok(raw_name.to_string()) + } else { + Ok(raw_name.to_owned() + ".git") + } +} +fn check_owner_against_config(_owner: &str) -> bool { + true + /* TODO + * Check owner against configuration + * to see if authenticated for managing git repos + */ +} +pub fn make_repo(raw_repo_name: &str, owner_text: &str) -> Result<Repository, RepoCreateError> { + let path = match home::home_dir() { + Some(home_path) => home_path.display().to_string(), + + None => todo!(), + }; + let git_base_path = path + "/libgit_test_repos"; + if !(check_owner_against_config(owner_text)) { + return Err(RepoCreateError::OwnershipError); + } + let repo_name: Result<String, RepoCreateError> = fmt_repo_name_dotgit(raw_repo_name); + let name = repo_name.unwrap_or_else(|error| { + if let RepoCreateError::RepoNameParseError = error { + panic!("RepoNameParseError"); + } else { + panic!("Unknown error"); + } + }); + let git_repo_path: String = git_base_path + "/" + &name; + + let mut options = RepositoryInitOptions::new(); + options.no_reinit(true).bare(true); + let repo = match Repository::init_opts(&git_repo_path, &options) { + Ok(repo) => { + println!("Successfully init repository at {}", git_repo_path); + repo + } + Err(_e) => { + return Err(RepoCreateError::InternalError); + } + }; + Ok(repo) +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..9f8344b --- /dev/null +++ b/src/main.rs @@ -0,0 +1,27 @@ +use core::panic; + +use git2::BranchType; + +mod git_tools; + +fn main() { + println!("Hello, world!"); + let repository = match git_tools::make_repo("any_test.git", "z.liu@outlook.com.gr") { + Ok(repo) => repo, + Err(_e) => panic!("Something went wrong during repo init"), + }; + repository.branches(Some(BranchType::Local)).map_or_else(|_err| { + panic!("branch parse error") + }, |branches| { + branches.for_each(|branch_result| { + match branch_result { + Ok(branch) => { + println!("{:?}", branch.0.name().unwrap()); + }, + Err(_e) => { + panic!("error stuff"); + } + } + }) + }) +} |