2023-07-29 19:23:05 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
pub enum VcsClient {
|
|
|
|
Noop {},
|
|
|
|
Git { source: PathBuf },
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VcsClient {
|
|
|
|
pub fn new_noop() -> VcsClient {
|
|
|
|
Self::Noop {}
|
|
|
|
}
|
2023-07-30 22:23:03 +02:00
|
|
|
|
2023-07-29 19:23:05 +02:00
|
|
|
pub fn new_git(path: &Path) -> anyhow::Result<VcsClient> {
|
|
|
|
if !path.to_path_buf().join(".git").exists() {
|
|
|
|
anyhow::bail!("git directory not found in: {}", path.display().to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(Self::Git {
|
|
|
|
source: path.to_path_buf(),
|
|
|
|
})
|
|
|
|
}
|
2023-07-30 22:23:03 +02:00
|
|
|
|
|
|
|
pub fn checkout_branch(&self) -> anyhow::Result<()> {
|
|
|
|
match self {
|
|
|
|
VcsClient::Noop {} => {}
|
|
|
|
VcsClient::Git { .. } => {
|
2023-07-30 22:47:00 +02:00
|
|
|
if let Err(_e) = self.exec_git(&["branch", "-D", "cuddle-please/release"]) {
|
2023-07-30 22:23:03 +02:00
|
|
|
tracing::debug!("failed to cleaned up local branch for force-push, this may be because it didn't exist before running this command");
|
|
|
|
}
|
|
|
|
self.exec_git(&["checkout", "-b", "cuddle-please/release"])?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn exec_git(&self, args: &[&str]) -> anyhow::Result<()> {
|
|
|
|
match self {
|
|
|
|
VcsClient::Noop {} => {}
|
|
|
|
VcsClient::Git { source } => {
|
|
|
|
let checkout_branch = std::process::Command::new("git")
|
|
|
|
.current_dir(source.as_path())
|
|
|
|
.args(args)
|
|
|
|
.output()?;
|
|
|
|
|
|
|
|
let stdout = std::str::from_utf8(&checkout_branch.stdout)?;
|
|
|
|
let stderr = std::str::from_utf8(&checkout_branch.stderr)?;
|
|
|
|
tracing::debug!(stdout = stdout, stderr = stderr, "git {}", args.join(" "));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn commit_and_push(&self, version: impl Into<String>, dry_run: bool) -> anyhow::Result<()> {
|
|
|
|
match self {
|
|
|
|
VcsClient::Noop {} => {}
|
|
|
|
VcsClient::Git { .. } => {
|
|
|
|
self.exec_git(&["add", "."])?;
|
|
|
|
self.exec_git(&[
|
|
|
|
"commit",
|
|
|
|
"-m",
|
|
|
|
&format!("chore(release): {}", version.into()),
|
|
|
|
])?;
|
|
|
|
|
|
|
|
tracing::trace!("git push -u -f origin cuddle-please/release");
|
|
|
|
if !dry_run {
|
|
|
|
self.exec_git(&["push", "-u", "-f", "origin", "cuddle-please/release"])?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-07-29 19:23:05 +02:00
|
|
|
}
|