205 lines
7 KiB
Rust
205 lines
7 KiB
Rust
use anyhow::Result;
|
|
use clap::Parser;
|
|
use conventional_commit::ConventionalCommit;
|
|
use git2::{string_array::StringArray, Oid, Repository};
|
|
use semver::Version;
|
|
use semver_bump_trait::SemverBump;
|
|
use slog::{info, warn};
|
|
use std::cmp::Ordering;
|
|
use std::collections::HashMap;
|
|
use std::str::FromStr;
|
|
|
|
/// Gets the current version based on commits.
|
|
#[derive(Debug, Parser)]
|
|
pub struct GetCommand {}
|
|
|
|
#[derive(Debug, Clone, PartialOrd, PartialEq)]
|
|
enum VersionBump {
|
|
None = 0,
|
|
Patch = 1,
|
|
Minor = 2,
|
|
Major = 3,
|
|
}
|
|
|
|
impl GetCommand {
|
|
pub async fn run(&self, log: slog::Logger) -> Result<()> {
|
|
// Figure out the path we are loading.
|
|
let current_dir =
|
|
"/home/dmoonfire/src/mfgames/mfgames-cil/src/MfGames.Nitride";
|
|
info!(log, "using current directory {:?}", current_dir);
|
|
|
|
// Load the repository so we can walk through it.
|
|
let repo = Repository::discover(current_dir)?;
|
|
|
|
// Load a map of all commits that are pointed to by a tag.
|
|
let tag_prefix = "MfGames.Nitride-*";
|
|
let tag_name_list = &repo.tag_names(Some(tag_prefix))?;
|
|
let tag_map: HashMap<Oid, Version> =
|
|
self.get_tag_map(&log, &repo, &tag_prefix, tag_name_list)?;
|
|
|
|
// Figure out the head.
|
|
let head = repo.head()?;
|
|
let head = head.resolve()?;
|
|
let commit = head.peel_to_commit()?;
|
|
|
|
// We have to walk back through the revisions until we find a proper
|
|
// commit. If we encounter a merge commit, then we split our search to
|
|
// look down both parents until we find the commit or we reach the end.
|
|
//
|
|
// While we're collecting it, we figure out what is the proper operation
|
|
// to perform once we do find it.
|
|
//
|
|
// Failing everything, we use our fallback.
|
|
let mut version = Version::parse("0.0.1").unwrap();
|
|
let mut check_list = vec![(commit.id(), VersionBump::None)];
|
|
|
|
while !&check_list.is_empty() {
|
|
info!(log, "checking {:?} entries", &check_list.len());
|
|
|
|
let old_list = check_list.clone();
|
|
check_list.clear();
|
|
|
|
for (oid, bump) in old_list {
|
|
// First check to see if we have a version for this commit.
|
|
info!(log, "checking oid {:?}, currently {:?}", oid, bump);
|
|
|
|
if let Some(tag_version) = &tag_map.get(&oid) {
|
|
// We have a tag, so use our gathered operation to figure
|
|
// out the new version since we don't have to continue this.
|
|
let mut bump_version: Version =
|
|
tag_version.to_owned().clone();
|
|
|
|
match &bump {
|
|
VersionBump::Major => {
|
|
bump_version.mut_increment_major()
|
|
}
|
|
VersionBump::Minor => {
|
|
bump_version.mut_increment_minor()
|
|
}
|
|
VersionBump::Patch => {
|
|
bump_version.mut_increment_patch()
|
|
}
|
|
_ => {}
|
|
}
|
|
|
|
info!(
|
|
log,
|
|
"found tag {} + {:?} -> {}",
|
|
&tag_version,
|
|
&bump,
|
|
&bump_version
|
|
);
|
|
|
|
if &bump_version.cmp(&version) == &Ordering::Greater {
|
|
version = bump_version;
|
|
}
|
|
|
|
// We can skip this one.
|
|
continue;
|
|
}
|
|
|
|
// Parse the message to see if we need to modify the bump.
|
|
let message = commit.message().unwrap_or("");
|
|
let conv = ConventionalCommit::from_str(message)?;
|
|
let commit_bump = self.get_version_bump(&conv);
|
|
let new_bump = match &bump > &commit_bump {
|
|
true => bump.clone(),
|
|
false => commit_bump.clone(),
|
|
};
|
|
|
|
let commit = &repo.find_commit(oid)?;
|
|
let parent_count = commit.parent_count();
|
|
|
|
info!(log, "something {:?}", commit.id());
|
|
info!(log, " message {:?}", message);
|
|
info!(log, " type {:?}", conv.type_());
|
|
info!(
|
|
log,
|
|
" bump {:?} + {:?} -> {:?}", &bump, &commit_bump, new_bump
|
|
);
|
|
info!(log, " parent_count {:?}", parent_count);
|
|
|
|
// Since we haven't found a tag, insert the entry into the new
|
|
// list.
|
|
for parent in commit.parent_ids() {
|
|
check_list.push((parent, new_bump.clone()));
|
|
}
|
|
}
|
|
}
|
|
|
|
// Report the final version.
|
|
info!(log, "final version {}", version);
|
|
|
|
// while &commit_id.is_some() == &true {
|
|
// // Process the commit.
|
|
// let commit = commit.unwrap();
|
|
|
|
// break;
|
|
// }
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn get_version_bump(&self, commit: &ConventionalCommit) -> VersionBump {
|
|
if commit.breaking_change().is_some() {
|
|
return VersionBump::Major;
|
|
}
|
|
|
|
return match commit.type_() {
|
|
"feat" => VersionBump::Minor,
|
|
"fix" => VersionBump::Patch,
|
|
_ => VersionBump::None,
|
|
};
|
|
}
|
|
|
|
fn get_tag_map<'a>(
|
|
&'a self,
|
|
log: &slog::Logger,
|
|
repo: &Repository,
|
|
tag_prefix: &str,
|
|
tag_name_list: &'a StringArray,
|
|
) -> Result<HashMap<Oid, Version>> {
|
|
let mut tag_map: HashMap<Oid, Version> = HashMap::new();
|
|
|
|
info!(log, "looking for {}", tag_prefix);
|
|
|
|
for name in tag_name_list.into_iter() {
|
|
// Get the object that we're pointing to.
|
|
let name = name.unwrap();
|
|
let obj = repo.revparse_single(&name)?;
|
|
|
|
if let Some(_tag) = obj.as_tag() {
|
|
warn!(log, "cannot handle a tag pointing to another tag");
|
|
} else if let Some(commit) = obj.as_commit() {
|
|
// Keep the OID since that is how we will look up the commits.
|
|
let oid = commit.id();
|
|
|
|
// Strip off the version and convert it into a semver.
|
|
let version = name.chars();
|
|
let version = version.skip(tag_prefix.len() - 1);
|
|
let version: String = version.collect();
|
|
let version = Version::parse(&version).unwrap();
|
|
|
|
if let Some(old_version) = tag_map.get(&oid) {
|
|
if version.cmp(old_version) == Ordering::Greater {
|
|
tag_map.insert(oid, version);
|
|
}
|
|
} else {
|
|
tag_map.insert(oid, version);
|
|
}
|
|
} else {
|
|
warn!(log, "cannot handle a tag pointing to something other than a commit");
|
|
}
|
|
}
|
|
|
|
let tag_map_len = &tag_map.len();
|
|
|
|
info!(
|
|
log,
|
|
"loaded in {:?} tags matching {:?}", tag_map_len, tag_prefix
|
|
);
|
|
|
|
Ok(tag_map)
|
|
}
|
|
}
|