WIP: Adding support for git subtree

This commit is contained in:
2024-02-11 08:31:43 +01:00
parent aabcb21f3b
commit f3eaaafd3c
8 changed files with 95 additions and 37 deletions

View File

@ -1,8 +1,8 @@
use std::error::Error;
use std::{error::Error, fmt::format};
use serde::{Deserialize, Serialize};
use crate::cli::{cli_exec, cli_exec_from_dir};
use crate::cli::{cli_exec, cli_exec_from_dir, is_path_relative};
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct GitOptions {
@ -46,6 +46,10 @@ pub struct Git {
impl Git {
pub fn new(url: String, repo_path: String) -> Self {
let repo_path = match is_path_relative(repo_path.clone()) {
true => format!("./{}", repo_path),
false => todo!(),
};
Self { url, repo_path }
}
@ -90,8 +94,9 @@ impl Git {
let _ = self.exec(cmd, git_opts.workdir.clone())?;
}
let cmd = format!(
"{} diff --staged --quiet || {} -C {} commit -m \"{}\"",
git_opts.bin, git_opts.bin, self.repo_path, opts.message
"{} -C {} diff --staged --quiet || {} -C {} commit -m \"{}\"",
git_opts.bin, self.repo_path,
git_opts.bin, self.repo_path, opts.message
);
match self.exec(cmd, git_opts.workdir.clone()) {
Ok(_) => Ok(()),

View File

@ -4,6 +4,7 @@ use crate::git::GitOptions;
use crate::{cli::cli_exec, helm::repository::Version};
use std::error::Error;
use std::fs::{self, rename};
use std::process::exit;
use base64::{engine::general_purpose, Engine};
use serde::Deserialize;
@ -19,6 +20,7 @@ pub struct GitRepo {
#[serde(alias = "ref")]
pub git_ref: String,
pub path: String,
pub subtree: bool,
#[serde(default = "default_git_bin")]
pub(crate) git_bin: String,
}
@ -40,7 +42,7 @@ impl RepositoryImpl for GitRepo {
git_ref: self.git_ref.clone(),
};
git_instance.checkout(git_opts, checkout_opts)?;
git_instance.checkout(git_opts.clone(), checkout_opts)?;
let old_dir_name = format!(
"{}/{}/{}/{}",
@ -56,13 +58,29 @@ impl RepositoryImpl for GitRepo {
match serde_yaml::from_str::<Version>(&helm_stdout) {
Ok(res) => {
new_dir_name = format!("{}/{}-{}", workdir_path, chart.name.clone(), res.version);
rename(old_dir_name, new_dir_name.clone())?;
match self.subtree {
true => {
let cmd = format!("git subtree split --prefix {}/{} -b helmule-subtree-split", self.path, chart.name.clone());
cli_exec_from_dir(cmd, format!("{}/{}", workdir_path.clone(), repo_name))?;
let checkout_opts = CheckoutOptions{
create: false,
git_ref: "helmule-subtree-split".to_string(),
};
git_instance.checkout(git_opts.clone(), checkout_opts)?;
let old_dir_name = format!("{}/{}", workdir_path.clone(), repo_name);
rename(old_dir_name, new_dir_name.clone())?;
},
false => {
rename(old_dir_name, new_dir_name.clone())?;
// Cleaning up
fs::remove_dir_all(format!("{}/{}", workdir_path, repo_name))?;
}
};
}
Err(err) => return Err(Box::from(err)),
};
// Cleaning up
fs::remove_dir_all(format!("{}/{}", workdir_path, repo_name))?;
// Get the version
let cmd = "helm show chart . | yq '.version'".to_string();