summaryrefslogtreecommitdiff
path: root/src/git_tools.rs
blob: bd05d07e9e2732dab50f18da08757fa3903bae40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use core::panic;

use git2::{Repository, RepositoryInitOptions};

pub enum RepoCreateError {
    Ownership,
    Path,
    RepoNameParse,
    Internal,
}

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::Ownership);
    }
    let repo_name: Result<String, RepoCreateError> = fmt_repo_name_dotgit(raw_repo_name);
    let name = 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 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::Internal);
        }
    };
    Ok(repo)
}