use crate::error::Result;
use governor_core::{
domain::commit::{Commit, CommitType},
domain::version::{BumpType, SemanticVersion},
traits::source_control::SourceControl,
};
use governor_git::GitAdapter;
use std::path::PathBuf;
#[allow(dead_code)]
pub async fn determine_new_version(
workspace_path: &str,
forced_version: Option<&str>,
current: &SemanticVersion,
) -> Result<SemanticVersion> {
if let Some(ver_str) = forced_version {
return SemanticVersion::parse(ver_str).map_err(|e| {
crate::error::Error::Version(format!("Failed to parse target version: {e}"))
});
}
let config = governor_git::GitAdapterConfig {
repository_path: Some(PathBuf::from(workspace_path)),
..Default::default()
};
let git = GitAdapter::open(config)
.map_err(|e| crate::error::Error::Config(format!("Failed to open git repository: {e}")))?;
let commits = git
.get_commits_since(None)
.await
.map_err(|e| crate::error::Error::Git(format!("Failed to get commits: {e}")))?;
let bump_type = analyze_commits_for_bump(&commits);
Ok(bump_type.apply_to(current))
}
#[must_use]
pub fn analyze_commits_for_bump(commits: &[Commit]) -> BumpType {
let mut has_breaking = false;
let mut has_features = false;
let mut has_fixes = false;
for commit in commits {
match commit.commit_type {
Some(CommitType::Feat | CommitType::Fix | CommitType::Perf | CommitType::Refactor)
if commit.breaking =>
{
has_breaking = true;
}
Some(CommitType::Feat) => has_features = true,
Some(CommitType::Fix | CommitType::Perf) => has_fixes = true,
_ => {}
}
}
if has_breaking {
BumpType::Major
} else if has_features {
BumpType::Minor
} else if has_fixes {
BumpType::Patch
} else {
BumpType::None
}
}