summaryrefslogtreecommitdiff
path: root/src/git_tools.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/git_tools.rs')
-rw-r--r--src/git_tools.rs43
1 files changed, 28 insertions, 15 deletions
diff --git a/src/git_tools.rs b/src/git_tools.rs
index 311aa93..878cf7b 100644
--- a/src/git_tools.rs
+++ b/src/git_tools.rs
@@ -2,6 +2,8 @@ use core::panic;
use git2::{Repository, RepositoryInitOptions};
+use crate::{cgit_helper::cgit_add_repo, read_config::Config};
+
pub enum RepoCreateError {
Ownership,
Path,
@@ -9,11 +11,12 @@ pub enum RepoCreateError {
Internal,
}
-fn fmt_repo_name_dotgit(raw_name: &str) -> Result<String, RepoCreateError> {
+fn fmt_repo_name_dotgit(raw_name: &str) -> Result<(String, String), RepoCreateError> {
if raw_name.ends_with(".git") {
- Ok(raw_name.to_string())
+ let no_suffix_name = &raw_name.to_string()[..raw_name.chars().count() - 4];
+ Ok((no_suffix_name.to_string(), raw_name.to_string()))
} else {
- Ok(raw_name.to_owned() + ".git")
+ Ok((raw_name.to_string(), raw_name.to_string() + ".git"))
}
}
fn check_owner_against_config(_owner: &str) -> bool {
@@ -23,25 +26,25 @@ fn check_owner_against_config(_owner: &str) -> bool {
* 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";
+pub fn make_repo(
+ raw_repo_name: &str,
+ owner_text: &str,
+ section: &str,
+ description: &str,
+ config: Config,
+) -> Result<Repository, RepoCreateError> {
if !(check_owner_against_config(owner_text)) {
return Err(RepoCreateError::Ownership);
}
- let repo_name: Result<String, RepoCreateError> = fmt_repo_name_dotgit(raw_repo_name);
- let name = repo_name.unwrap_or_else(|error| {
+ let repo_name: Result<(String, String), RepoCreateError> = fmt_repo_name_dotgit(raw_repo_name);
+ let (no_suffix, with_suffix) = repo_name.unwrap_or_else(|error| {
if let RepoCreateError::RepoNameParse = error {
panic!("RepoNameParseError");
} else {
panic!("Unknown error");
}
});
- let git_repo_path: String = git_base_path + "/" + &name;
+ let git_repo_path: String = config.git_host_dir_path + "/" + &with_suffix;
let mut options = RepositoryInitOptions::new();
options.no_reinit(true).bare(true);
@@ -55,7 +58,17 @@ pub fn make_repo(raw_repo_name: &str, owner_text: &str) -> Result<Repository, Re
}
};
/* TODO
- * We want to change the config file to append http.receivepack = true
- * */
+ * We want to change the config file to append http.receivepack = true
+ * */
+ cgit_add_repo(
+ config.cgitrepos_file_path,
+ crate::cgit_helper::CGitRepoInfo {
+ section: String::from(section),
+ url: no_suffix,
+ path: git_repo_path,
+ owner: owner_text.to_string(),
+ description: description.to_string(),
+ },
+ );
Ok(repo)
}