git-next/crates/git/src/repo_details.rs

67 lines
2.2 KiB
Rust

use config::{
BranchName, ForgeAlias, ForgeConfig, ForgeDetails, GitDir, RepoAlias, RepoConfig, RepoPath,
ServerRepoConfig,
};
use git_next_config as config;
use super::{Generation, GitRemote};
/// The derived information about a repo, used to interact with it
#[derive(Clone, Debug, derive_more::Display, derive_with::With)]
#[display("gen-{}:{}:{}/{}", generation, forge.forge_type(), forge.forge_alias(), repo_alias )]
pub struct RepoDetails {
pub generation: Generation,
pub repo_alias: RepoAlias,
pub repo_path: RepoPath,
pub branch: BranchName,
pub forge: ForgeDetails,
pub repo_config: Option<RepoConfig>,
pub gitdir: GitDir,
}
impl RepoDetails {
pub fn new(
generation: Generation,
repo_alias: &RepoAlias,
server_repo_config: &ServerRepoConfig,
forge_alias: &ForgeAlias,
forge_config: &ForgeConfig,
gitdir: GitDir,
) -> Self {
Self {
generation,
repo_alias: repo_alias.clone(),
repo_path: server_repo_config.repo(),
repo_config: server_repo_config.repo_config(),
branch: server_repo_config.branch(),
gitdir,
forge: ForgeDetails::new(
forge_alias.clone(),
forge_config.forge_type(),
forge_config.hostname(),
forge_config.user(),
forge_config.token(),
),
}
}
pub fn origin(&self) -> secrecy::Secret<String> {
let repo_details = self;
let user = &repo_details.forge.user();
let hostname = &repo_details.forge.hostname();
let repo_path = &repo_details.repo_path;
let expose_secret = repo_details.forge.token();
use secrecy::ExposeSecret;
let token = expose_secret.expose_secret();
let origin = format!("https://{user}:{token}@{hostname}/{repo_path}.git");
origin.into()
}
pub fn git_remote(&self) -> GitRemote {
GitRemote::new(self.forge.hostname().clone(), self.repo_path.clone())
}
pub fn with_hostname(mut self, hostname: config::Hostname) -> Self {
let forge = self.forge;
self.forge = forge.with_hostname(hostname);
self
}
}