use std::{fs, path::Path};
use semver::Version;
use crate::{Config, ReleaseContext, Tag, changelog, commits, errors::Result};
pub fn print_changelog(
ctx: &ReleaseContext<'_>,
config: &Config,
root: &Path,
output: Option<&Path>,
full: bool,
) -> Result<()> {
if full {
return print_full_changelog(ctx, config, root, output);
}
let current = ctx.manifest.read_version(root)?;
let since = match commits::latest_version_tag(ctx.backend, &config.release.tag_prefix)? {
Some(tag) => tag.name,
None => "root()".to_owned(),
};
let commits = ctx.backend.log_commits(&format!("{since}..@"))?;
let next = commits::apply_bump(¤t, commits::compute_bump(&commits));
let section = changelog::render_changelog_section(&commits, &next);
match output {
Some(path) => {
let existing = if path.exists() {
fs::read_to_string(path)?
} else {
String::new()
};
let updated = changelog::prepend_to_file(&existing, §ion);
fs::write(path, updated)?;
println!("✓ Written to {}", path.display());
}
None => print!("{section}"),
}
Ok(())
}
pub(super) fn print_full_changelog(
ctx: &ReleaseContext<'_>,
config: &Config,
root: &Path,
output: Option<&Path>,
) -> Result<()> {
let raw_tags = ctx.backend.list_tags()?;
let mut all_tags: Vec<Tag> = raw_tags
.into_iter()
.filter_map(|name| {
let stripped = name.strip_prefix(&config.release.tag_prefix)?;
let version = Version::parse(stripped).ok()?;
Some(Tag { name, version })
})
.collect();
all_tags.sort_by(|a, b| a.version.cmp(&b.version));
let mut sections = Vec::new();
let mut versions = Vec::new();
let mut prev = "root()".to_owned();
for tag in &all_tags {
let revset = format!("{prev}..{}", tag.name);
let tag_commits = ctx.backend.log_commits(&revset)?;
sections.push((tag.name.clone(), tag_commits));
versions.push(tag.version.clone());
prev.clone_from(&tag.name);
}
let current = ctx.manifest.read_version(root)?;
let since = all_tags
.last()
.map_or_else(|| "root()".to_owned(), |t| t.name.clone());
let tip_commits = ctx.backend.log_commits(&format!("{since}..@"))?;
let bump = commits::compute_bump(&tip_commits);
let next = commits::apply_bump(¤t, bump);
sections.push(("unreleased".to_owned(), tip_commits));
versions.push(next);
let result = changelog::render_full_changelog(§ions, &versions);
match output {
Some(path) => {
fs::write(path, &result)?;
println!("✓ Written to {}", path.display());
}
None => print!("{result}"),
}
Ok(())
}