use std::path::Path;
use git2::{Error, Repository};
use super::credentials::{CredType, GitCredentials, GitHttpsCredentials};
pub struct GitRepository {
pub(crate) repository: Option<Repository>,
pub(crate) cred: GitCredentials,
pub(crate) skip_owner_validation: bool,
pub(crate) bypass_certificate_check: bool,
}
impl GitRepository {
pub fn open(path: &Path) -> Result<Self, Error> {
let repo = Repository::open(path)?;
Ok(GitRepository {
cred: GitCredentials::Default,
repository: Some(repo),
skip_owner_validation: false,
bypass_certificate_check: false,
})
}
pub fn new() -> Self {
GitRepository {
cred: GitCredentials::Default,
repository: None,
skip_owner_validation: false,
bypass_certificate_check: false,
}
}
pub fn get_skip_owner_validation(&self) -> bool {
self.skip_owner_validation
}
pub fn get_bypass_certificate_check(&self) -> bool {
self.bypass_certificate_check
}
pub fn skip_owner_validation(&mut self, skip: bool) {
self.skip_owner_validation = skip;
}
pub fn bypass_certificate_check(&mut self, bypass: bool) {
self.bypass_certificate_check = bypass;
}
pub fn get_cred_type(&self) -> Result<CredType, Error> {
match &self.cred {
GitCredentials::Https(git_https_credentials) => git_https_credentials.get_cred_type(),
GitCredentials::Default => Ok(CredType::Default),
}
}
pub fn set_user_pass(&mut self, user: impl Into<String>, pass: impl Into<String>) {
let http_cred = GitHttpsCredentials::new(Some(user.into()), Some(pass.into()));
self.cred = GitCredentials::Https(http_cred);
}
pub fn set_user(&mut self, user: impl Into<String>) {
let http_cred = GitHttpsCredentials::new(Some(user.into()), None);
self.cred = GitCredentials::Https(http_cred);
}
}