use std::collections::{BTreeMap, BTreeSet};
use clap::Args;
use oakum::changeset::PackageSpec;
use oakum::commits::{commit_type, message_intent, CommitIntent, MessageIntent};
use oakum::plan::{BumpLevel, Package, PackageId, Workspace};
use oakum::state::UnresolvedScope;
use super::add::{bump_file_body, knope_presence, write_bump_file_in};
use super::config::{load_config, require_config, unresolved_scope_line};
use super::deliver_block;
use super::dependency_updates::{self, CommitReach};
use super::git::{Git, Op};
use super::install_pin::{self, PinPolicy};
use super::repository;
use super::workspace::discover_validated;
use super::CliError;
#[derive(Debug, Args)]
pub(super) struct GenerateArgs {
#[arg(long, value_name = "REF", help = super::FROM_MERGE_BASE)]
from: Option<String>,
#[arg(long)]
dry_run: bool,
#[arg(long, value_name = "SLUG")]
name: Option<String>,
}
pub(super) fn run(args: &GenerateArgs) -> Result<(), Box<dyn std::error::Error>> {
let repo = repository::discover()?;
let config = load_config(&repo)?;
require_config(&config)?;
install_pin::gate(&repo, PinPolicy::Writer)?;
if !config.generate_allowed() {
return Err(Box::new(CliError::new(
"`oakum generate` needs both `change-files` and `conventional-commits` enabled in `.changeset/_config.toml` (ADR-0029)",
)));
}
let workspace = discover_validated(&repo, &config)?;
let git = Git::at_repository(&repo)?;
let from = resolve_from_ref(&git, args.from.as_deref())?;
let found = commit_intents(
&git,
&workspace,
&CommitRanges::from_base(&from, &workspace),
|package| config.version_managed(package),
)?;
for commit in &found.unresolved {
super::say_err(&unresolved_scope_line(commit));
}
for (hash, why) in &found.unread_paths {
let short = hash.get(..7).unwrap_or(hash);
super::say_err(&format!(
"commit {short}'s paths could not be read, so it is attributed by its scope alone: {why}"
));
}
if found.intents.is_empty() {
return nothing_attributed(&found);
}
let intents = found.intents;
let mut levels: BTreeMap<String, BumpLevel> = BTreeMap::new();
let mut notes: Vec<String> = Vec::new();
let mut coverage_notes: Vec<String> = Vec::new();
for (_, intent) in &intents {
let names: BTreeSet<&str> = intent
.packages()
.iter()
.map(|id| id.name.as_str())
.collect();
for name in names {
let level = levels.entry(String::from(name)).or_insert(BumpLevel::None);
*level = (*level).max(intent.level());
let line = format!("- {name}: {}", intent.summary());
if intent.level().is_release() {
notes.push(line);
} else {
coverage_notes.push(line);
}
}
}
let note = if notes.is_empty() {
coverage_notes
} else {
notes
}
.join("\n");
if args.dry_run {
let knope = knope_presence(&repo)?;
let entries: Vec<(String, BumpLevel)> = levels.into_iter().collect();
let body =
bump_file_body(&entries, ¬e, knope).map_err(|err| CliError::new(err.to_string()))?;
deliver_block(&body)
.map_err(|err| CliError::undelivered("bump file composed, but it", &err))?;
return Ok(());
}
let specs: Vec<PackageSpec> = levels
.into_iter()
.map(|(name, level)| PackageSpec::new(name, level))
.collect();
write_bump_file_in(
&repo,
&workspace,
&config,
&specs,
¬e,
args.name.as_deref(),
)?;
Ok(())
}
pub(super) type CommitTypes = BTreeMap<String, CommitRead>;
#[derive(Clone, Debug, Default)]
pub(super) struct CommitRead {
pub(super) kind: Option<String>,
pub(super) subject: String,
}
pub(super) struct CommitIntents {
pub(super) intents: Vec<(String, CommitIntent)>,
pub(super) merges: BTreeSet<String>,
pub(super) releasing: Vec<(String, BumpLevel, Result<BTreeSet<PackageId>, CliError>)>,
pub(super) types: CommitTypes,
pub(super) left_out: BTreeSet<PackageId>,
pub(super) unresolved: Vec<UnresolvedScope>,
pub(super) dependency_updates: BTreeSet<String>,
pub(super) unread_paths: Vec<(String, String)>,
}
pub(super) struct CommitRanges(BTreeMap<PackageId, Option<String>>);
impl CommitRanges {
pub(super) const fn new(bases: BTreeMap<PackageId, Option<String>>) -> Self {
Self(bases)
}
pub(super) const fn bases(&self) -> &BTreeMap<PackageId, Option<String>> {
&self.0
}
pub(super) fn from_base(base: &str, workspace: &Workspace) -> Self {
Self::new(
workspace
.packages()
.map(|package| (package.id().clone(), Some(String::from(base))))
.collect(),
)
}
}
struct ReadCommit<'a> {
commit: &'a GitCommit,
reading: &'a Reading,
in_range: BTreeSet<PackageId>,
message: String,
intent: MessageIntent,
}
fn read_messages<'a>(
commits: &'a [(GitCommit, Reading)],
cancelled: &BTreeMap<&str, BTreeSet<PackageId>>,
workspace: &Workspace,
) -> (Vec<ReadCommit<'a>>, Option<CliError>) {
let mut read = Vec::new();
for (commit, reading) in commits {
let in_range: BTreeSet<PackageId> = match cancelled.get(commit.hash.as_str()) {
Some(both) => reading.packages.difference(both).cloned().collect(),
None => reading.packages.clone(),
};
if in_range.is_empty() {
continue;
}
let message = commit.message();
match message_intent(&message, workspace) {
Ok(intent) => read.push(ReadCommit {
commit,
reading,
in_range,
message,
intent,
}),
Err(why) => return (read, Some(CliError::new(why))),
}
}
(read, None)
}
pub(super) fn commit_intents(
git: &Git,
workspace: &Workspace,
ranges: &CommitRanges,
keep: impl Fn(&Package) -> bool,
) -> Result<CommitIntents, Box<dyn std::error::Error>> {
let mut intents = Vec::new();
let mut left_out_released = BTreeSet::new();
let mut unresolved = Vec::new();
let mut merges = BTreeSet::new();
let mut releasing = Vec::new();
let mut dependency_updates = BTreeSet::new();
let mut unread_paths = Vec::new();
let mut types = BTreeMap::new();
let commits = commits_in(git, ranges)?;
let cancelled = reverted_pairs(&commits, |revert, target| undoes(git, revert, target))?;
let (read, refused) = read_messages(&commits, &cancelled, workspace);
let wanted: Vec<&str> = read
.iter()
.filter(|read| !read.commit.merge)
.map(|read| read.commit.hash.as_str())
.collect();
let mut paths = git.commit_paths(&wanted);
for ReadCommit {
commit,
reading,
in_range,
message,
intent,
} in read
{
if commit.merge {
merges.insert(commit.hash.clone());
}
types.insert(
commit.hash.clone(),
CommitRead {
kind: commit_type(&message),
subject: commit.subject.clone(),
},
);
let files = paths.remove(&commit.hash).unwrap_or(Ok(Vec::new()));
let (by_paths, unread) = read_paths(git, workspace, commit, files, &intent, &keep)?;
if by_paths.intent.is_empty() && by_paths.touched {
dependency_updates.insert(commit.hash.clone());
}
let (packages, level, summary, unresolved_scope) = attributed(intent, &by_paths);
let names_nothing = packages.is_empty() && !by_paths.touched;
let reached = oakum::plan::reach(workspace, &packages, &keep);
let kept: BTreeSet<PackageId> = reached
.managed
.into_keys()
.filter(|id| in_range.contains(id) && !by_paths.update_only.contains(id))
.collect();
let left_out: BTreeSet<PackageId> = reached
.stranded
.into_iter()
.filter(|id| in_range.contains(id))
.collect();
if let Some(err) = &unread {
unread_paths.push((commit.hash.clone(), err.detail()));
}
if level.is_release() {
let by_intent = oakum::plan::reach(workspace, &by_paths.intent, &keep).managed;
let touched = match unread {
Some(err) => Err(err),
None => Ok(by_intent.into_keys().collect()),
};
releasing.push((commit.hash.clone(), level, touched));
match unresolved_scope {
Some(scope) if names_nothing && reading.in_every_range => {
unresolved.push(UnresolvedScope {
commit: commit.hash.clone(),
scope,
});
}
_ => left_out_released.extend(left_out),
}
}
if let Some(intent) = CommitIntent::new(kept, level, summary) {
intents.push((commit.hash.clone(), intent));
}
}
if let Some(refused) = refused {
return Err(Box::new(refused));
}
Ok(CommitIntents {
intents,
merges,
releasing,
types,
left_out: left_out_released,
unresolved,
dependency_updates,
unread_paths,
})
}
fn nothing_attributed(found: &CommitIntents) -> Result<(), Box<dyn std::error::Error>> {
if !found.left_out.is_empty() {
let names: BTreeSet<&str> = found.left_out.iter().map(|id| id.name.as_str()).collect();
return Err(Box::new(CliError::new(format!(
"the commits touched only packages the config does not version: {}; adjust `include`/`exclude` or set `private-packages.version = true`",
names.into_iter().collect::<Vec<_>>().join(", ")
))));
}
let only_dependencies = !found.dependency_updates.is_empty()
&& found
.releasing
.iter()
.all(|(hash, ..)| found.dependency_updates.contains(hash));
if only_dependencies {
super::say_out(&format!(
"no bump file written: {} commit(s) moved only dependencies, which need no intent (ADR-0040)",
found.dependency_updates.len()
));
return Ok(());
}
Err(Box::new(CliError::new(
"no package bumps detected from commits (need a conventional scope matching a workspace package, or changed files under a package directory)",
)))
}
fn read_paths(
git: &Git,
workspace: &Workspace,
commit: &GitCommit,
files: Result<Vec<String>, CliError>,
intent: &MessageIntent,
keep: impl Fn(&Package) -> bool,
) -> Result<(CommitReach, Option<CliError>), CliError> {
if commit.merge {
return Ok((CommitReach::default(), None));
}
let reach = files.and_then(|files| {
dependency_updates::commit_reach(git, workspace, &commit.hash, &files, keep)
});
match (reach, intent) {
(Ok(reach), _) => Ok((reach, None)),
(Err(err), MessageIntent::Scoped { package, .. }) => Ok((
CommitReach {
intent: BTreeSet::from([package.clone()]),
update_only: BTreeSet::new(),
touched: true,
},
Some(err),
)),
(Err(err), MessageIntent::PathFallback { .. }) => Err(err),
}
}
fn attributed(
intent: MessageIntent,
by_paths: &CommitReach,
) -> (BTreeSet<PackageId>, BumpLevel, String, Option<String>) {
match intent {
MessageIntent::Scoped {
package,
level,
summary,
} => {
let only_updates = by_paths.intent.is_empty() && by_paths.touched;
let packages = if only_updates {
BTreeSet::new()
} else if by_paths.update_only.contains(&package) {
by_paths.intent.clone()
} else {
BTreeSet::from([package])
};
(packages, level, summary, None)
}
MessageIntent::PathFallback {
level,
summary,
unresolved_scope,
} => (by_paths.intent.clone(), level, summary, unresolved_scope),
}
}
fn reverted_pairs(
commits: &[(GitCommit, Reading)],
undoes: impl Fn(&str, &str) -> Result<bool, CliError>,
) -> Result<BTreeMap<&str, BTreeSet<PackageId>>, CliError> {
let mut cancelled: BTreeMap<&str, BTreeSet<PackageId>> = BTreeMap::new();
for (commit, reading) in commits.iter().rev() {
if cancelled.contains_key(commit.hash.as_str())
|| !(commit.subject.starts_with("Revert \"")
|| commit.subject.starts_with("Reapply \""))
{
continue;
}
let Some(target) = commit.body.lines().find_map(|line| {
line.trim()
.strip_prefix("This reverts commit ")
.and_then(|rest| rest.strip_suffix('.'))
}) else {
continue;
};
let reverted = commits.iter().find(|(other, _)| {
other.hash != commit.hash
&& other.hash == target
&& !cancelled.contains_key(other.hash.as_str())
});
let Some((reverted, its_reading)) = reverted else {
continue;
};
let both: BTreeSet<PackageId> = reading
.packages
.intersection(&its_reading.packages)
.cloned()
.collect();
if !both.is_empty() && undoes(&commit.hash, &reverted.hash)? {
cancelled.insert(&reverted.hash, both.clone());
cancelled.insert(&commit.hash, both);
}
}
Ok(cancelled)
}
fn undoes(git: &Git, revert: &str, target: &str) -> Result<bool, CliError> {
let patch = |hash, reverse| -> Result<Option<Vec<String>>, CliError> {
let text = git.text(Op::CommitPatch { hash, reverse })?;
if text.lines().any(|line| line.starts_with("Binary files ")) {
return Ok(None);
}
Ok(Some(
text.lines()
.filter(|line| !line.starts_with("index ") && !line.starts_with("@@"))
.map(String::from)
.collect(),
))
};
Ok(match (patch(revert, true)?, patch(target, false)?) {
(Some(revert), Some(target)) => revert == target,
_ => false,
})
}
#[derive(Debug)]
struct GitCommit {
hash: String,
merge: bool,
subject: String,
body: String,
}
impl GitCommit {
fn message(&self) -> String {
if self.body.is_empty() {
self.subject.clone()
} else {
format!("{}\n\n{}", self.subject, self.body)
}
}
}
pub(super) fn resolve_from_ref(
git: &Git,
explicit: Option<&str>,
) -> Result<String, Box<dyn std::error::Error>> {
if let Some(from) = explicit {
return Ok(String::from(from));
}
Ok(git.default_base(|| default_base(git))?)
}
fn default_base(git: &Git) -> Result<String, CliError> {
for candidate in ["origin/main", "main", "master"] {
if git.predicate(Op::RefExists {
reference: candidate,
})? {
return Ok(match merge_base(git, candidate)? {
Some(base) => base,
None => String::from(candidate),
});
}
}
Err(CliError::new(
"could not find a default base ref; pass `--from <ref>`",
))
}
fn merge_base(git: &Git, tip: &str) -> Result<Option<String>, CliError> {
git.optional_text(Op::MergeBase { tip })
}
#[derive(Debug, Default)]
struct Reading {
packages: BTreeSet<PackageId>,
in_every_range: bool,
}
fn commits_in(
git: &Git,
ranges: &CommitRanges,
) -> Result<Vec<(GitCommit, Reading)>, Box<dyn std::error::Error>> {
let mut by_base: BTreeMap<Option<&str>, BTreeSet<PackageId>> = BTreeMap::new();
for (id, base) in &ranges.0 {
by_base
.entry(base.as_deref())
.or_default()
.insert(id.clone());
}
if let [(base, packages)] = by_base.iter().collect::<Vec<_>>().as_slice() {
return Ok(list_commits(git, **base)?
.into_iter()
.map(|commit| {
let reading = Reading {
packages: (*packages).clone(),
in_every_range: true,
};
(commit, reading)
})
.collect());
}
let mut held: BTreeMap<String, (BTreeSet<PackageId>, usize)> = BTreeMap::new();
for (base, packages) in &by_base {
for commit in list_commits(git, *base)? {
let entry = held.entry(commit.hash).or_default();
entry.0.extend(packages.iter().cloned());
entry.1 += 1;
}
}
if held.is_empty() {
return Ok(Vec::new());
}
Ok(list_commits(git, None)?
.into_iter()
.filter_map(|commit| {
let (packages, count) = held.remove(&commit.hash)?;
let reading = Reading {
packages,
in_every_range: count == by_base.len(),
};
Some((commit, reading))
})
.collect())
}
fn list_commits(
git: &Git,
from: Option<&str>,
) -> Result<Vec<GitCommit>, Box<dyn std::error::Error>> {
let raw = git.commits(from)?;
let mut commits = Vec::new();
for chunk in raw.split('\0').collect::<Vec<_>>().chunks(4) {
if chunk.len() < 4 {
continue;
}
let hash = chunk[0].trim();
if hash.is_empty() {
continue;
}
commits.push(GitCommit {
hash: String::from(hash),
merge: chunk[1].split_whitespace().count() > 1,
subject: String::from(chunk[2].trim()),
body: String::from(chunk[3].trim()),
});
}
Ok(commits)
}
#[cfg(test)]
mod tests {
use std::collections::BTreeMap;
use oakum::plan::{Ecosystem, Package, PackageId, ResolvesDependenciesAt, Workspace};
use semver::Version;
use super::{commit_intents, merge_base, resolve_from_ref, CommitRanges, Git};
use crate::cli::git::Reply;
fn workspace(names: &[&str]) -> Workspace {
Workspace::new(names.iter().map(|name| {
Package::new(
PackageId::new(Ecosystem::Cargo, *name),
Version::new(0, 1, 0),
ResolvesDependenciesAt::Install,
true,
Vec::new(),
)
.with_manifest_dir(*name)
}))
.expect("workspace")
}
fn cargo(name: &str) -> PackageId {
PackageId::new(Ecosystem::Cargo, name)
}
fn log(commits: &[(&str, &str)]) -> Reply {
Reply::said(
commits
.iter()
.flat_map(|(hash, subject)| [hash, "\0p\0", subject, "\0\0\n"])
.collect::<String>(),
)
}
fn ranges(bases: &[(&str, Option<&str>)]) -> CommitRanges {
CommitRanges::new(
bases
.iter()
.map(|(name, base)| (cargo(name), base.map(String::from)))
.collect::<BTreeMap<_, _>>(),
)
}
#[test]
fn diverging_ranges_keep_history_order() {
let git = Git::answering([
(
"log",
log(&[
("b1", "fix(alpha): b1"),
("B", "fix(alpha): B"),
("h1", "fix(beta): h1"),
]),
),
(
"log",
log(&[
("a1", "fix(beta): a1"),
("A", "fix(beta): A"),
("h1", "fix(beta): h1"),
]),
),
(
"log",
log(&[
("r", "chore: root"),
("a1", "fix(beta): a1"),
("b1", "fix(alpha): b1"),
("A", "fix(beta): A"),
("B", "fix(alpha): B"),
("h1", "fix(beta): h1"),
]),
),
(
"diff-tree --stdin",
Reply::said("\u{1}a1\0\u{1}b1\0\u{1}A\0\u{1}B\0\u{1}h1\0"),
),
]);
let ws = workspace(&["alpha", "beta"]);
let found = commit_intents(
&git,
&ws,
&ranges(&[("alpha", Some("tagA")), ("beta", Some("tagB"))]),
|_| true,
)
.expect("intents");
let order: Vec<&str> = found
.intents
.iter()
.map(|(hash, _)| hash.as_str())
.collect();
assert_eq!(order, ["a1", "b1", "A", "B", "h1"]);
}
#[test]
fn a_commit_outside_a_package_range_is_not_its_intent() {
let git = Git::answering([
(
"log",
log(&[("b1", "fix(beta): b1"), ("h1", "fix(alpha): h1")]),
),
("log", log(&[("h1", "fix(alpha): h1")])),
(
"log",
log(&[("b1", "fix(beta): b1"), ("h1", "fix(alpha): h1")]),
),
("diff-tree --stdin", Reply::said("\u{1}b1\0\u{1}h1\0")),
]);
let ws = workspace(&["alpha", "beta"]);
let found = commit_intents(
&git,
&ws,
&ranges(&[("alpha", Some("a-old")), ("beta", Some("b-new"))]),
|_| true,
)
.expect("intents");
let named: Vec<(&str, Vec<&PackageId>)> = found
.intents
.iter()
.map(|(hash, intent)| (hash.as_str(), intent.packages().iter().collect()))
.collect();
assert_eq!(named, [("h1", vec![&cargo("alpha")])]);
}
#[test]
fn an_unresolved_scope_behind_a_base_is_not_reported() {
let git = Git::answering([
(
"log",
log(&[("x1", "fix(zzz): old"), ("x2", "fix(zzz): new")]),
),
("log", log(&[("x2", "fix(zzz): new")])),
(
"log",
log(&[("x1", "fix(zzz): old"), ("x2", "fix(zzz): new")]),
),
(
"diff-tree --stdin",
Reply::said("\u{1}x1\0\nREADME.md\0\u{1}x2\0\nREADME.md\0"),
),
]);
let ws = workspace(&["alpha", "beta"]);
let found = commit_intents(
&git,
&ws,
&ranges(&[("alpha", Some("a-old")), ("beta", Some("b-new"))]),
|_| true,
)
.expect("intents");
let reported: Vec<&str> = found.unresolved.iter().map(|u| u.commit.as_str()).collect();
assert_eq!(reported, ["x2"]);
}
#[test]
fn a_scope_whose_paths_name_an_unread_package_is_not_unresolved() {
let git = Git::answering([
("log", log(&[("x1", "fix(typo): p")])),
(
"diff-tree --stdin",
Reply::said("\u{1}x1\0\npending/src/lib.rs\0"),
),
]);
let ws = workspace(&["alpha", "pending"]);
let found = commit_intents(&git, &ws, &ranges(&[("alpha", Some("v1"))]), |_| true)
.expect("intents");
assert!(found.unresolved.is_empty(), "{:?}", found.unresolved);
assert_eq!(found.intents.len(), 0);
}
#[test]
fn an_earlier_unreadable_commit_is_reported_before_a_later_refused_scope() {
let package = |ecosystem| {
Package::new(
PackageId::new(ecosystem, "dup"),
Version::new(0, 1, 0),
ResolvesDependenciesAt::Install,
true,
Vec::new(),
)
.with_manifest_dir(match ecosystem {
Ecosystem::Cargo => "crates/dup",
Ecosystem::Npm => "packages/dup",
})
};
let ws = Workspace::new([package(Ecosystem::Cargo), package(Ecosystem::Npm)])
.expect("workspace");
let git = Git::answering([
(
"log",
log(&[("x1", "chore: tidy"), ("x2", "fix(dup): both")]),
),
(
"diff-tree --stdin",
Reply::said(b"\x01x1\0\nbad\xff\0".to_vec()),
),
("diff-tree", Reply::said(b"bad\xff\0".to_vec())),
]);
let err = commit_intents(&git, &ws, &ranges(&[("dup", Some("v1"))]), |_| true)
.err()
.expect("refused");
assert!(err.to_string().contains("not valid UTF-8"), "{err}");
}
#[test]
fn no_ranges_list_nothing() {
let git = Git::answering([]);
let found =
commit_intents(&git, &workspace(&["alpha"]), &ranges(&[]), |_| true).expect("intents");
assert_eq!(found.intents.len(), 0);
assert!(git.asked().is_empty(), "{:?}", git.asked());
}
#[test]
fn resolve_from_ref_keeps_an_explicit_ref() {
let git = Git::answering([]);
let from = resolve_from_ref(&git, Some("v1.0.0")).expect("explicit");
assert_eq!(from, "v1.0.0");
assert!(git.asked().is_empty(), "{:?}", git.asked());
}
#[test]
fn resolve_from_ref_uses_the_merge_base_when_one_exists() {
let git = Git::answering([
("rev-parse --verify", Reply::said("tipsha")),
("merge-base", Reply::said("abc123")),
]);
let from = resolve_from_ref(&git, None).expect("resolved");
assert_eq!(from, "abc123");
assert_eq!(
git.asked(),
vec![
String::from("rev-parse --verify"),
String::from("merge-base"),
]
);
}
#[test]
fn resolve_from_ref_falls_back_to_the_tip_when_histories_are_unrelated() {
let git = Git::answering([
("rev-parse --verify", Reply::said("tipsha")),
("merge-base", Reply::absent()),
]);
let from = resolve_from_ref(&git, None).expect("resolved");
assert_eq!(from, "origin/main");
}
#[test]
fn resolve_from_ref_propagates_a_diagnosed_merge_base_failure() {
let git = Git::answering([
("rev-parse --verify", Reply::said("tipsha")),
(
"merge-base",
Reply::failed(128, "fatal: Not a valid object name origin/main"),
),
]);
let err = resolve_from_ref(&git, None).expect_err("diagnosed failure");
assert!(
err.to_string().contains("merge-base") || err.to_string().contains("valid object"),
"{err}"
);
}
#[test]
fn merge_base_distinguishes_no_ancestor_from_a_failed_look() {
let unrelated = Git::answering([("merge-base", Reply::absent())]);
assert_eq!(
merge_base(&unrelated, "main").expect("looked"),
None,
"no common ancestor is Ok(None)"
);
let diagnosed = Git::answering([(
"merge-base",
Reply::failed(128, "fatal: Not a valid object name main"),
)]);
let err = merge_base(&diagnosed, "main").expect_err("failed look");
assert!(
err.to_string().contains("merge-base") || err.to_string().contains("valid object"),
"{err}"
);
}
}