2024-07-25 09:02:43 +01:00
|
|
|
//
|
|
|
|
use crate::{
|
2024-07-26 06:49:09 +01:00
|
|
|
git::{
|
|
|
|
self,
|
|
|
|
repository::open::{oreal::RealOpenRepository, OpenRepositoryLike},
|
|
|
|
Generation, GitRemote,
|
|
|
|
},
|
2024-07-25 09:02:43 +01:00
|
|
|
pike, BranchName, ForgeAlias, ForgeConfig, ForgeDetails, GitDir, Hostname, RemoteUrl,
|
|
|
|
RepoAlias, RepoConfig, RepoPath, ServerRepoConfig, StoragePathType,
|
2024-05-11 19:46:20 +01:00
|
|
|
};
|
2024-07-06 19:55:39 +01:00
|
|
|
|
2024-07-25 09:02:43 +01:00
|
|
|
use std::sync::{Arc, RwLock};
|
2024-05-11 19:46:20 +01:00
|
|
|
|
2024-07-25 09:02:43 +01:00
|
|
|
use secrecy::Secret;
|
2024-05-11 19:46:20 +01:00
|
|
|
|
|
|
|
/// The derived information about a repo, used to interact with it
|
2024-07-06 14:25:43 +01:00
|
|
|
#[derive(Clone, Debug, derive_more::Display, derive_with::With)]
|
2024-06-09 10:21:09 +01:00
|
|
|
#[display("gen-{}:{}:{}/{}", generation, forge.forge_type(), forge.forge_alias(), repo_alias )]
|
2024-05-11 19:46:20 +01:00
|
|
|
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,
|
2024-05-29 19:22:05 +01:00
|
|
|
forge_alias: &ForgeAlias,
|
2024-05-11 19:46:20 +01:00
|
|
|
forge_config: &ForgeConfig,
|
|
|
|
gitdir: GitDir,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
generation,
|
|
|
|
repo_alias: repo_alias.clone(),
|
2024-05-15 07:55:05 +01:00
|
|
|
repo_path: server_repo_config.repo(),
|
2024-05-11 19:46:20 +01:00
|
|
|
repo_config: server_repo_config.repo_config(),
|
2024-05-15 07:55:05 +01:00
|
|
|
branch: server_repo_config.branch(),
|
2024-05-11 19:46:20 +01:00
|
|
|
gitdir,
|
2024-05-15 07:55:05 +01:00
|
|
|
forge: ForgeDetails::new(
|
2024-05-29 19:22:05 +01:00
|
|
|
forge_alias.clone(),
|
2024-05-15 07:55:05 +01:00
|
|
|
forge_config.forge_type(),
|
|
|
|
forge_config.hostname(),
|
|
|
|
forge_config.user(),
|
|
|
|
forge_config.token(),
|
|
|
|
),
|
2024-05-11 19:46:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn origin(&self) -> secrecy::Secret<String> {
|
|
|
|
let repo_details = self;
|
2024-05-15 07:55:05 +01:00
|
|
|
let user = &repo_details.forge.user();
|
|
|
|
let hostname = &repo_details.forge.hostname();
|
2024-05-11 19:46:20 +01:00
|
|
|
let repo_path = &repo_details.repo_path;
|
2024-05-15 07:55:05 +01:00
|
|
|
let expose_secret = repo_details.forge.token();
|
2024-05-11 19:46:20 +01:00
|
|
|
use secrecy::ExposeSecret;
|
|
|
|
let token = expose_secret.expose_secret();
|
|
|
|
let origin = format!("https://{user}:{token}@{hostname}/{repo_path}.git");
|
|
|
|
origin.into()
|
|
|
|
}
|
|
|
|
|
2024-07-06 19:55:39 +01:00
|
|
|
pub const fn gitdir(&self) -> &GitDir {
|
|
|
|
&self.gitdir
|
|
|
|
}
|
|
|
|
|
2024-05-11 19:46:20 +01:00
|
|
|
pub fn git_remote(&self) -> GitRemote {
|
2024-05-15 07:55:05 +01:00
|
|
|
GitRemote::new(self.forge.hostname().clone(), self.repo_path.clone())
|
2024-05-11 19:46:20 +01:00
|
|
|
}
|
2024-06-19 07:03:08 +01:00
|
|
|
|
2024-07-25 09:02:43 +01:00
|
|
|
pub fn with_hostname(mut self, hostname: Hostname) -> Self {
|
2024-06-19 07:03:08 +01:00
|
|
|
let forge = self.forge;
|
|
|
|
self.forge = forge.with_hostname(hostname);
|
|
|
|
self
|
|
|
|
}
|
2024-07-06 19:55:39 +01:00
|
|
|
|
|
|
|
// url is a secret as it contains auth token
|
|
|
|
pub fn url(&self) -> Secret<String> {
|
|
|
|
let user = self.forge.user();
|
|
|
|
use secrecy::ExposeSecret;
|
|
|
|
let token = self.forge.token().expose_secret();
|
2024-07-26 08:24:35 +01:00
|
|
|
let auth_delim = match token.is_empty() {
|
|
|
|
true => "",
|
|
|
|
false => ":",
|
|
|
|
};
|
2024-07-06 19:55:39 +01:00
|
|
|
let hostname = self.forge.hostname();
|
|
|
|
let repo_path = &self.repo_path;
|
2024-07-26 08:24:35 +01:00
|
|
|
format!("https://{user}{auth_delim}{token}@{hostname}/{repo_path}.git").into()
|
2024-07-06 19:55:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::result_large_err)]
|
2024-07-26 06:49:09 +01:00
|
|
|
pub fn open(&self) -> Result<impl OpenRepositoryLike, git::validation::remotes::Error> {
|
2024-07-06 19:55:39 +01:00
|
|
|
let gix_repo = pike! {
|
|
|
|
self
|
|
|
|
|> Self::gitdir
|
|
|
|
|> GitDir::pathbuf
|
|
|
|
|> gix::ThreadSafeRepository::open
|
|
|
|
}?;
|
|
|
|
let repo = pike! {
|
|
|
|
gix_repo
|
|
|
|
|> RwLock::new
|
|
|
|
|> Arc::new
|
|
|
|
|> RealOpenRepository::new
|
|
|
|
};
|
|
|
|
Ok(repo)
|
|
|
|
}
|
|
|
|
|
2024-07-25 09:02:43 +01:00
|
|
|
pub fn remote_url(&self) -> Option<RemoteUrl> {
|
2024-07-06 19:55:39 +01:00
|
|
|
use secrecy::ExposeSecret;
|
|
|
|
RemoteUrl::parse(self.url().expose_secret())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument]
|
2024-07-26 06:49:09 +01:00
|
|
|
pub fn assert_remote_url(&self, found: Option<RemoteUrl>) -> git::repository::Result<()> {
|
2024-07-06 19:55:39 +01:00
|
|
|
let Some(found) = found else {
|
|
|
|
tracing::debug!("No remote url found to assert");
|
|
|
|
return Ok(());
|
|
|
|
};
|
|
|
|
let Some(expected) = self.remote_url() else {
|
|
|
|
tracing::debug!("No remote url to assert against");
|
|
|
|
return Ok(());
|
|
|
|
};
|
2024-07-26 08:24:35 +01:00
|
|
|
if !found.matches(&expected) {
|
2024-07-06 19:55:39 +01:00
|
|
|
tracing::debug!(?found, ?expected, "urls differ");
|
|
|
|
match self.gitdir.storage_path_type() {
|
|
|
|
StoragePathType::External => {
|
|
|
|
tracing::debug!("Refusing to update an external repo - user must resolve this");
|
2024-07-26 06:49:09 +01:00
|
|
|
return Err(git::repository::Error::MismatchDefaultFetchRemote {
|
2024-07-06 19:55:39 +01:00
|
|
|
found: Box::new(found),
|
|
|
|
expected: Box::new(expected),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
StoragePathType::Internal => {
|
|
|
|
tracing::debug!(?expected, "Need to update config with new url");
|
|
|
|
self.write_remote_url(&expected)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tracing::instrument]
|
|
|
|
pub fn write_remote_url(&self, url: &RemoteUrl) -> Result<(), kxio::fs::Error> {
|
|
|
|
if self.gitdir.storage_path_type() != StoragePathType::Internal {
|
2024-07-12 08:12:45 +01:00
|
|
|
return Ok(());
|
2024-07-06 19:55:39 +01:00
|
|
|
}
|
|
|
|
let fs = self.gitdir.as_fs();
|
|
|
|
// load config file
|
|
|
|
let config_filename = &self.gitdir.join("config");
|
|
|
|
let config_file = fs.file_read_to_string(config_filename)?;
|
|
|
|
let mut config_lines = config_file
|
|
|
|
.lines()
|
|
|
|
.map(|l| l.to_owned())
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
tracing::debug!(?config_lines, "original file");
|
|
|
|
let url_line = format!(r#" url = "{}""#, url);
|
|
|
|
if config_lines
|
|
|
|
.iter()
|
|
|
|
.any(|line| *line == r#"[remote "origin"]"#)
|
|
|
|
{
|
|
|
|
tracing::debug!("has an 'origin' remote");
|
|
|
|
config_lines
|
|
|
|
.iter_mut()
|
|
|
|
.filter(|line| line.starts_with(r#" url = "#))
|
|
|
|
.for_each(|line| line.clone_from(&url_line));
|
|
|
|
} else {
|
|
|
|
tracing::debug!("has no 'origin' remote");
|
|
|
|
config_lines.push(r#"[remote "origin"]"#.to_owned());
|
|
|
|
config_lines.push(url_line);
|
|
|
|
}
|
|
|
|
tracing::debug!(?config_lines, "updated file");
|
|
|
|
// write config file back out
|
|
|
|
fs.file_write(config_filename, config_lines.join("\n").as_str())?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2024-05-11 19:46:20 +01:00
|
|
|
}
|